답안 #1038519

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1038519 2024-07-29T21:57:07 Z Vectors_Master Fire (JOI20_ho_t5) C++17
100 / 100
406 ms 185412 KB
#include<bits/stdc++.h>
#define all(x) x.begin(),x.end()
#define LL long long
#define LD long double
#define pb push_back
#define F first
#define S second
const double PI=3.1415926535897932384626433;
const int KL=1e6+10;
const LL MOD=1e9+7;
using namespace std;


int n,q,a[KL],prv[KL],nxt[KL];
vector<int> stop_increase[KL],start_decrease[KL],zero[KL];

// nxt[i] nxt element greater than a[i]
// prv[i] previois element greater or equal to a[i]
// start_decrease[i] time in which left bearer of the a[i] starts to go right.
// stop_increase[i] time in which right bearer of the a[i] stops to go right.

void calc_prev_nxt() {
    vector<int> vec;
    a[0]=1e9+1;
    vec.pb(0);
    for(int i=1;i<=n;i++) {
        while(a[vec.back()]<a[i])vec.pop_back();
        prv[i]=vec.back();
        vec.pb(i);
    }

    vec.clear();
    a[n+1]=1e9+1;
    vec.pb(n+1);
    for(int i=n;i>=1;i--) {
        while(a[vec.back()]<=a[i])vec.pop_back();
        nxt[i]=vec.back();
        vec.pb(i);
    }
}

int l[KL],r[KL],T[KL],cur[KL];
vector<int> adj[KL],stop[KL];
LL ans[KL];


LL cnst[KL],sub[KL],add[KL];

void update_cnst(int pos,LL new_val,int cur=1,int st=1,int en=n) {
    if(st==en){cnst[cur]=new_val;return ;}
    int mid=(st+en)/2;
    if(pos<=mid)update_cnst(pos,new_val,2*cur,st,mid);
    else update_cnst(pos,new_val,2*cur+1,mid+1,en);
    cnst[cur]=cnst[2*cur]+cnst[2*cur+1];
}
void update_cnst_add(int pos,LL new_val,int cur=1,int st=1,int en=n) {
    if(st==en){cnst[cur]+=new_val;return ;}
    int mid=(st+en)/2;
    if(pos<=mid)update_cnst_add(pos,new_val,2*cur,st,mid);
    else update_cnst_add(pos,new_val,2*cur+1,mid+1,en);
    cnst[cur]=cnst[2*cur]+cnst[2*cur+1];
}
void update_add(int pos,LL new_val,int cur=1,int st=1,int en=n) {
    if(st==en){add[cur]=new_val;return ;}
    int mid=(st+en)/2;
    if(pos<=mid)update_add(pos,new_val,2*cur,st,mid);
    else update_add(pos,new_val,2*cur+1,mid+1,en);
    add[cur]=add[2*cur]+add[2*cur+1];
}
void update_sub(int pos,LL new_val,int cur=1,int st=1,int en=n) {
    if(st==en){sub[cur]=new_val;return ;}
    int mid=(st+en)/2;
    if(pos<=mid)update_sub(pos,new_val,2*cur,st,mid);
    else update_sub(pos,new_val,2*cur+1,mid+1,en);
    sub[cur]=sub[2*cur]+sub[2*cur+1];
}

LL query(int l,int r,LL t,int cur=1,int st=1,int en=n) {
    if(l>en || r<st)return 0;
    if(l<=st && en<=r)return cnst[cur]+(t+1LL)*add[cur]+(t+1LL)*sub[cur];
    int mid=(st+en)/2;
    return query(l,r,t,2*cur,st,mid)+query(l,r,t,2*cur+1,mid+1,en);
}


pair<int,int> spt[KL][20];
int llen[KL];

pair<int,int> mrg(pair<int,int> A,pair<int,int> B) {
    if(A.F>=B.F)return A;
    return B;
}
void buildspt() {
    for(int i=1;i<=n;i++) {
        spt[i][0]={a[i],i};
        if(i>=2)llen[i]=llen[i/2]+1;
    }

    for(int j=1;(1<<j)<=n;j++) {
        for(int i=1;i+(1<<j)-1<=n;i++) {
            spt[i][j]=mrg(spt[i][j-1],spt[i+(1<<(j-1))][j-1]);
        }
    }
}
int sptquery(int l,int r) {
    int len=llen[r-l+1];
    return mrg(spt[l][len],spt[r-(1<<len)+1][len]).S;
}

LL query_prefix(int pos,LL t) {
    if(pos==n)return query(1,n,t);
    if(pos==0)return 0;
    int l=max(pos-(int) t,1);
    int element=sptquery(l,pos);
    int exp=min(nxt[element]-1,element+(int)t);
    assert(exp>=pos);
    assert(element<=pos);
    // cout<<element<<" "<<a[element]<<" "<<exp<<" "<<pos<<" "<<query(1,element,t)<<" "<<query(1,element,t)-(LL)(exp-pos)*(LL)a[element]<<"\n";
    return query(1,element,t)-(LL)(exp-pos)*(LL)a[element];

}
void solveTS(){
    cin>>n>>q;
    for(int i=1;i<=n;i++) cin>>a[i];

    buildspt();
    calc_prev_nxt();

    for(int i=1;i<=n;i++) {
        int posInc=nxt[i]-i;
        stop_increase[posInc].pb(i);
        int posDec=n+1;
        if(prv[i]!=0) {
            posDec=i-prv[i];
            start_decrease[posDec].pb(i);
        }
        if(posDec<posInc) {
            int to_be_deleted=posInc+posDec;
            zero[to_be_deleted].pb(i);
        }
        else {
            int to_be_deleted=posDec+posInc;
            zero[to_be_deleted].pb(i);
        }
    }

    for(int qr=1;qr<=q;qr++) {
        cin>>T[qr]>>l[qr]>>r[qr];
        adj[T[qr]].pb(qr);
    }

    for(int i=1;i<=n;i++)update_add(i,a[i]);

    for(int t=0;t<=n;t++) {
        for(auto i:stop_increase[t]) {
            update_cnst_add(i,(LL)t*(LL)a[i]);
            update_add(i,0);
        }
        for(auto i:start_decrease[t]) {
            update_cnst_add(i,(LL)(t)*(LL)a[i]);
            update_sub(i,-a[i]);
        }
        for(auto i:zero[t]) {
            update_cnst(i,0);
            update_sub(i,0);
        }

        for(auto qr:adj[t]) {
            // cout<<l[qr]<<" "<<r[qr]<<" "<<t<<"\n";

            ans[qr]=query_prefix(r[qr],t)-query_prefix(l[qr]-1,t);
        }
    }
    for(int qr=1;qr<=q;qr++) cout<<ans[qr]<<"\n";
}
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
#ifndef ONLINE_JUDGE
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
#endif
    int TS=1;
    // cin>>TS;
    while(TS--){
        solveTS();
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 37 ms 117840 KB Output is correct
2 Correct 39 ms 117848 KB Output is correct
3 Correct 42 ms 117848 KB Output is correct
4 Correct 39 ms 117844 KB Output is correct
5 Correct 40 ms 117848 KB Output is correct
6 Correct 38 ms 117844 KB Output is correct
7 Correct 39 ms 117856 KB Output is correct
8 Correct 40 ms 117980 KB Output is correct
9 Correct 41 ms 117944 KB Output is correct
10 Correct 41 ms 117844 KB Output is correct
11 Correct 39 ms 117840 KB Output is correct
12 Correct 42 ms 117840 KB Output is correct
13 Correct 41 ms 117844 KB Output is correct
14 Correct 42 ms 117844 KB Output is correct
15 Correct 42 ms 117852 KB Output is correct
16 Correct 42 ms 117840 KB Output is correct
17 Correct 42 ms 117844 KB Output is correct
18 Correct 41 ms 117852 KB Output is correct
19 Correct 55 ms 118028 KB Output is correct
20 Correct 56 ms 117844 KB Output is correct
21 Correct 42 ms 117924 KB Output is correct
22 Correct 42 ms 117844 KB Output is correct
23 Correct 42 ms 117848 KB Output is correct
24 Correct 50 ms 117840 KB Output is correct
25 Correct 44 ms 117852 KB Output is correct
26 Correct 43 ms 117796 KB Output is correct
27 Correct 41 ms 117840 KB Output is correct
28 Correct 44 ms 117936 KB Output is correct
29 Correct 41 ms 118020 KB Output is correct
30 Correct 40 ms 117844 KB Output is correct
31 Correct 42 ms 117852 KB Output is correct
32 Correct 39 ms 117960 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 37 ms 117840 KB Output is correct
2 Correct 313 ms 179840 KB Output is correct
3 Correct 303 ms 179284 KB Output is correct
4 Correct 312 ms 179532 KB Output is correct
5 Correct 323 ms 180928 KB Output is correct
6 Correct 302 ms 180060 KB Output is correct
7 Correct 314 ms 179892 KB Output is correct
8 Correct 320 ms 181008 KB Output is correct
9 Correct 313 ms 180480 KB Output is correct
10 Correct 302 ms 179008 KB Output is correct
11 Correct 356 ms 180824 KB Output is correct
12 Correct 297 ms 179060 KB Output is correct
13 Correct 311 ms 180628 KB Output is correct
14 Correct 296 ms 180408 KB Output is correct
15 Correct 351 ms 180540 KB Output is correct
16 Correct 313 ms 179712 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 37 ms 117840 KB Output is correct
2 Correct 347 ms 181508 KB Output is correct
3 Correct 333 ms 180760 KB Output is correct
4 Correct 360 ms 182604 KB Output is correct
5 Correct 311 ms 181056 KB Output is correct
6 Correct 321 ms 181676 KB Output is correct
7 Correct 346 ms 181820 KB Output is correct
8 Correct 349 ms 181464 KB Output is correct
9 Correct 323 ms 181072 KB Output is correct
10 Correct 320 ms 180332 KB Output is correct
11 Correct 319 ms 182612 KB Output is correct
12 Correct 332 ms 182096 KB Output is correct
13 Correct 318 ms 182352 KB Output is correct
14 Correct 331 ms 181056 KB Output is correct
15 Correct 318 ms 182592 KB Output is correct
16 Correct 346 ms 182080 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 371 ms 183904 KB Output is correct
2 Correct 391 ms 184184 KB Output is correct
3 Correct 371 ms 185412 KB Output is correct
4 Correct 390 ms 183632 KB Output is correct
5 Correct 393 ms 183888 KB Output is correct
6 Correct 382 ms 184260 KB Output is correct
7 Correct 387 ms 185152 KB Output is correct
8 Correct 406 ms 184640 KB Output is correct
9 Correct 395 ms 183884 KB Output is correct
10 Correct 353 ms 184644 KB Output is correct
11 Correct 370 ms 184396 KB Output is correct
12 Correct 361 ms 184396 KB Output is correct
13 Correct 389 ms 184388 KB Output is correct
14 Correct 380 ms 184308 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 37 ms 117840 KB Output is correct
2 Correct 39 ms 117848 KB Output is correct
3 Correct 42 ms 117848 KB Output is correct
4 Correct 39 ms 117844 KB Output is correct
5 Correct 40 ms 117848 KB Output is correct
6 Correct 38 ms 117844 KB Output is correct
7 Correct 39 ms 117856 KB Output is correct
8 Correct 40 ms 117980 KB Output is correct
9 Correct 41 ms 117944 KB Output is correct
10 Correct 41 ms 117844 KB Output is correct
11 Correct 39 ms 117840 KB Output is correct
12 Correct 42 ms 117840 KB Output is correct
13 Correct 41 ms 117844 KB Output is correct
14 Correct 42 ms 117844 KB Output is correct
15 Correct 42 ms 117852 KB Output is correct
16 Correct 42 ms 117840 KB Output is correct
17 Correct 42 ms 117844 KB Output is correct
18 Correct 41 ms 117852 KB Output is correct
19 Correct 55 ms 118028 KB Output is correct
20 Correct 56 ms 117844 KB Output is correct
21 Correct 42 ms 117924 KB Output is correct
22 Correct 42 ms 117844 KB Output is correct
23 Correct 42 ms 117848 KB Output is correct
24 Correct 50 ms 117840 KB Output is correct
25 Correct 44 ms 117852 KB Output is correct
26 Correct 43 ms 117796 KB Output is correct
27 Correct 41 ms 117840 KB Output is correct
28 Correct 44 ms 117936 KB Output is correct
29 Correct 41 ms 118020 KB Output is correct
30 Correct 40 ms 117844 KB Output is correct
31 Correct 42 ms 117852 KB Output is correct
32 Correct 39 ms 117960 KB Output is correct
33 Correct 313 ms 179840 KB Output is correct
34 Correct 303 ms 179284 KB Output is correct
35 Correct 312 ms 179532 KB Output is correct
36 Correct 323 ms 180928 KB Output is correct
37 Correct 302 ms 180060 KB Output is correct
38 Correct 314 ms 179892 KB Output is correct
39 Correct 320 ms 181008 KB Output is correct
40 Correct 313 ms 180480 KB Output is correct
41 Correct 302 ms 179008 KB Output is correct
42 Correct 356 ms 180824 KB Output is correct
43 Correct 297 ms 179060 KB Output is correct
44 Correct 311 ms 180628 KB Output is correct
45 Correct 296 ms 180408 KB Output is correct
46 Correct 351 ms 180540 KB Output is correct
47 Correct 313 ms 179712 KB Output is correct
48 Correct 347 ms 181508 KB Output is correct
49 Correct 333 ms 180760 KB Output is correct
50 Correct 360 ms 182604 KB Output is correct
51 Correct 311 ms 181056 KB Output is correct
52 Correct 321 ms 181676 KB Output is correct
53 Correct 346 ms 181820 KB Output is correct
54 Correct 349 ms 181464 KB Output is correct
55 Correct 323 ms 181072 KB Output is correct
56 Correct 320 ms 180332 KB Output is correct
57 Correct 319 ms 182612 KB Output is correct
58 Correct 332 ms 182096 KB Output is correct
59 Correct 318 ms 182352 KB Output is correct
60 Correct 331 ms 181056 KB Output is correct
61 Correct 318 ms 182592 KB Output is correct
62 Correct 346 ms 182080 KB Output is correct
63 Correct 371 ms 183904 KB Output is correct
64 Correct 391 ms 184184 KB Output is correct
65 Correct 371 ms 185412 KB Output is correct
66 Correct 390 ms 183632 KB Output is correct
67 Correct 393 ms 183888 KB Output is correct
68 Correct 382 ms 184260 KB Output is correct
69 Correct 387 ms 185152 KB Output is correct
70 Correct 406 ms 184640 KB Output is correct
71 Correct 395 ms 183884 KB Output is correct
72 Correct 353 ms 184644 KB Output is correct
73 Correct 370 ms 184396 KB Output is correct
74 Correct 361 ms 184396 KB Output is correct
75 Correct 389 ms 184388 KB Output is correct
76 Correct 380 ms 184308 KB Output is correct
77 Correct 353 ms 182332 KB Output is correct
78 Correct 338 ms 183180 KB Output is correct
79 Correct 354 ms 183096 KB Output is correct
80 Correct 345 ms 182340 KB Output is correct
81 Correct 321 ms 182168 KB Output is correct
82 Correct 377 ms 182880 KB Output is correct
83 Correct 342 ms 182452 KB Output is correct
84 Correct 348 ms 181932 KB Output is correct
85 Correct 352 ms 183232 KB Output is correct
86 Correct 346 ms 182316 KB Output is correct
87 Correct 306 ms 181160 KB Output is correct
88 Correct 314 ms 181132 KB Output is correct
89 Correct 324 ms 179576 KB Output is correct
90 Correct 310 ms 180932 KB Output is correct
91 Correct 309 ms 179912 KB Output is correct
92 Correct 346 ms 179516 KB Output is correct
93 Correct 298 ms 180096 KB Output is correct
94 Correct 320 ms 181636 KB Output is correct
95 Correct 295 ms 181300 KB Output is correct
96 Correct 314 ms 180288 KB Output is correct
97 Correct 303 ms 180412 KB Output is correct
98 Correct 325 ms 179516 KB Output is correct
99 Correct 321 ms 180080 KB Output is correct
100 Correct 319 ms 180332 KB Output is correct
101 Correct 348 ms 179904 KB Output is correct
102 Correct 375 ms 180636 KB Output is correct