167212025-05-10 19:42:07algoproTornyokcpp17Időlimit túllépés 42/100490ms27444 KiB
// UUID: 7f9b6e75-ad0c-4c78-bac4-27ea2f7318b6
#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];
int cnt=0;

void do_prop(int ind, int L, int R){
    cnt++;
    if (cnt>maxn){
        cout<<"ERROR";
        exit(0);
    }
    int cur=prop[ind];
    tree[ind]+=cur;
    prop[ind]=0;

    if (L==R) return;

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

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);
    tree[ind] = max(tree[2*ind], tree[2*ind+1]);
}

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;
            cnt=0;
            add(1, ind, nxt[ind]-1, 1, 1, n);
            cur++;
        }
        cnt=0;
        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
base42/100
1Elfogadva0/01ms316 KiB
2Időlimit túllépés0/0485ms23860 KiB
3Elfogadva2/21ms316 KiB
4Elfogadva2/21ms316 KiB
5Elfogadva6/61ms316 KiB
6Elfogadva6/61ms500 KiB
7Elfogadva4/432ms2168 KiB
8Elfogadva4/459ms4068 KiB
9Elfogadva8/8233ms15420 KiB
10Időlimit túllépés0/8435ms25908 KiB
11Időlimit túllépés0/5486ms24112 KiB
12Időlimit túllépés0/5483ms23348 KiB
13Elfogadva5/5158ms7476 KiB
14Elfogadva5/5298ms15164 KiB
15Időlimit túllépés0/5456ms26676 KiB
16Időlimit túllépés0/5481ms25436 KiB
17Időlimit túllépés0/5474ms23896 KiB
18Időlimit túllépés0/5488ms23856 KiB
19Időlimit túllépés0/5479ms23528 KiB
20Időlimit túllépés0/5490ms25648 KiB
21Időlimit túllépés0/5481ms25652 KiB
22Időlimit túllépés0/5483ms27444 KiB