제출 #484155

#제출 시각아이디문제언어결과실행 시간메모리
484155CyberSleeperPilot (NOI19_pilot)C++14
100 / 100
571 ms71596 KiB


#include <bits/stdc++.h>
#define fastio      ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define debug(x)    cout << "Line " << __LINE__ << ", " << #x << " is " << x << endl
#define fi          first
#define se          second
#define mp          make_pair
#define pb          push_back
#define ll          long long
#define pll         pair<ll, ll>
#define ull         unsigned long long
#define ld          long double
#define pii         pair<ll, ll>
#define nl          '\n'
#define tb          '\t'
#define sp          ' '
using namespace std;

const ll MX=1000007, MOD=1e9+7, BLOCK=330, INF=1e9+7;
const ld ERR=1e-7, pi=3.14159265358979323846;

ll N, Q, H[MX], ptr=0;
ll tot=0, ans[MX];
pii A[MX], Y[MX];
ll par[MX], sz[MX];

ll calc(ll x){
    return x*(1ll+x)/2ll;
}
ll f(ll u){
    if(par[u]!=u)
        par[u]=f(par[u]);
    return par[u];
}
void join(ll u, ll v){
    u=f(u);
    v=f(v);
    if(u==v)
        return;
    if(sz[u]>sz[v])
        swap(u, v);
    tot-=calc(sz[u])+calc(sz[v]);
    par[u]=v;
    sz[v]+=sz[u];
    tot+=calc(sz[v]);
}

int main(){
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> N >> Q;
    for(ll i=0; i<N; i++){
        par[i]=i;
        sz[i]=1;
        cin >> H[i];
        A[i]={H[i], i};
    }
    for(ll i=0; i<Q; i++){
        cin >> Y[i].fi;
        Y[i].se=i;
    }
    sort(Y, Y+Q);
    sort(A, A+N);
    for(ll i=0; ptr<Q && i<N; i++){
        ll h=A[i].fi, idx=A[i].se;
        while(ptr<Q && Y[ptr].fi<h)
            ans[Y[ptr++].se]=tot;
        if(ptr>=Q)
            break;
        tot++;
        if(idx && H[idx-1]<=h)
            join(idx-1, idx);
        if(idx+1<N && H[idx+1]<=h)
            join(idx+1, idx);
    }
    while(ptr<Q)
        ans[Y[ptr++].se]=tot;
    for(ll i=0; i<Q; i++){
        cout << ans[i] << '\n';
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...