167192025-05-10 19:32:22algoproTornyokcpp17Időlimit túllépés 16/100503ms33988 KiB
// UUID: 24a11ac0-dce1-4eaa-9916-0e972df7aa6c
#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 (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();
    }
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
base16/100
1Elfogadva0/01ms316 KiB
2Időlimit túllépés0/0483ms30512 KiB
3Elfogadva2/21ms316 KiB
4Elfogadva2/223ms316 KiB
5Elfogadva6/637ms316 KiB
6Elfogadva6/626ms316 KiB
7Időlimit túllépés0/4500ms2104 KiB
8Időlimit túllépés0/4500ms4064 KiB
9Időlimit túllépés0/8500ms15676 KiB
10Időlimit túllépés0/8486ms26236 KiB
11Időlimit túllépés0/5486ms29776 KiB
12Időlimit túllépés0/5503ms32512 KiB
13Időlimit túllépés0/5500ms7384 KiB
14Időlimit túllépés0/5488ms14908 KiB
15Időlimit túllépés0/5481ms26428 KiB
16Időlimit túllépés0/5501ms28148 KiB
17Időlimit túllépés0/5501ms30500 KiB
18Időlimit túllépés0/5490ms33332 KiB
19Időlimit túllépés0/5485ms33332 KiB
20Időlimit túllépés0/5503ms33884 KiB
21Időlimit túllépés0/5503ms33988 KiB
22Időlimit túllépés0/5472ms33588 KiB