167202025-05-10 19:33:09algoproTornyokcpp17Time limit exceeded 16/100503ms33840 KiB
// UUID: 822faa19-1b46-4c7c-af88-02df6312b54a
#include <bits/stdc++.h>
using namespace std;
using ll=long long;

#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define vi vector<int>
#define pii pair<int,int>

const int maxn=1e6+1;

int tree[4*maxn+10];
int prop[4*maxn+10];

void do_prop(int ind, int L, int R){
    int cur=prop[ind];
    tree[ind]+=cur;
    prop[ind]=0;

    if (L==R) return;

    prop[2*ind]+=cur;
    prop[2*ind+1]+=cur;

    int M=(L+R)/2;
    do_prop(2*ind, L, M);
    do_prop(2*ind+1, M+1, R);
    tree[ind] = max(tree[2*ind], tree[2*ind+1]);
}

void add(int ind, int l, int r, int x, int L, int R){
    if (R<l || r<L) return;
    do_prop(ind, L, R);

    if (l<=L && R<=r){
        prop[ind]+=x;
        do_prop(ind, L, R);
        return;
    }

    int M = (L+R)/2;
    add(2*ind, l, r, x, L, M);
    add(2*ind+1, l, r, x, M+1, R);
}

int get(int ind, int l, int r, int L, int R){
    if (R<l || r<L) return -1;
    do_prop(ind, L, R);

    if (l<=L && R<=r){
        return tree[ind];
    }

    int M=(L+R)/2;
    int res = -1;
    res = max(res, get(2*ind, l, r, L, M));
    res = max(res, get(2*ind+1, l, r, M+1, R));
    return res;
}

void solve(){
    int n, k;
    cin>>n>>k;
    vector<int> h(n+2);
    vector<pair<int,int>> hsrt(n+1);
    vector<int> nxt(n+1);
    stack<int> mon;
    
    for (int i=1;i<=n;i++) {
        cin>>h[i];
        hsrt[i]={h[i], i};
    }
    sort(all(hsrt));
    h[n+1]=INT_MAX;
    mon.push(n+1);
    for (int i=n;i>0;i--){
        while (h[mon.top()]<h[i]) mon.pop();
        nxt[i]=mon.top();
        mon.push(i);
    }
    vector<pair<int,int>> q(k);
    vector<int> ans(k);
    for (int i=0;i<k;i++){
        cin>>q[i].first;
        q[i].second=i;
    }
    sort(all(q));

    int cur=1;
    for (auto [a, i] : q){
        while (cur<=n && hsrt[cur].first<a){
            int ind = hsrt[cur].second;
            add(1, ind, nxt[ind]-1, 1, 1, n);
            cur++;
        }
        ans[i]=get(1, 1, n, 1, n);
    }
    for (int i=0;i<k;i++) cout<<ans[i]<<" ";

}

int main() {
    ios_base::sync_with_stdio(0);cin.tie(0);
    int t=1;
    //cin>>t;
    while (t--){
        solve();
    }
}
SubtaskSumTestVerdictTimeMemory
base16/100
1Accepted0/01ms316 KiB
2Time limit exceeded0/0474ms30496 KiB
3Accepted2/21ms508 KiB
4Accepted2/223ms316 KiB
5Accepted6/637ms472 KiB
6Accepted6/626ms316 KiB
7Time limit exceeded0/4500ms2100 KiB
8Time limit exceeded0/4483ms3892 KiB
9Time limit exceeded0/8500ms15684 KiB
10Time limit exceeded0/8486ms26232 KiB
11Time limit exceeded0/5488ms29988 KiB
12Time limit exceeded0/5503ms32308 KiB
13Time limit exceeded0/5500ms7312 KiB
14Time limit exceeded0/5485ms15156 KiB
15Time limit exceeded0/5479ms26568 KiB
16Time limit exceeded0/5501ms28148 KiB
17Time limit exceeded0/5501ms30496 KiB
18Time limit exceeded0/5493ms33328 KiB
19Time limit exceeded0/5486ms33256 KiB
20Time limit exceeded0/5503ms33764 KiB
21Time limit exceeded0/5501ms33840 KiB
22Time limit exceeded0/5485ms33604 KiB