답안 #878543

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
878543 2023-11-24T16:48:52 Z winter0101 Fire (JOI20_ho_t5) C++14
100 / 100
401 ms 77588 KB
#include<bits/stdc++.h>
using namespace std;
#define all(fl) fl.begin(),fl.end()
#define pb push_back
#define fi first
#define se second
#define for1(i,j,k) for(int i=j;i<=k;i++)
#define for2(i,j,k) for(int i=j;i>=k;i--)
#define for3(i,j,k,l) for(int i=j;i<=k;i+=l)
#define lb lower_bound
#define ub upper_bound
#define sz(a) (int)a.size()
#define pii pair<int,int>
#define pli pair<long long,int>
#define gcd __gcd
#define lcm(x,y) x*y/__gcd(x,y)
#define lastbit(i) __builtin_ctz(i)
/*
    dung duoc cay voi i->l[i] la so gan i nhat > i
    tu do ta luu canh voi chi so i-l[i] a[l[i]]-a[i]
    voi 1 dinh neu con du thoi gian di qua canh thi thoi gian se - (i-l[i]) va value duoc them bang a[l[i]]-a[i]
    nhan xet euler tour thi in[i]=i
    1 truy van l r ta co the tach rieng thanh 1 r va 1 l
    vua dfs vua giai cac truy van bang bit
*/
const int maxn=2e5+9;
int a[maxn];
vector<pii>b[maxn];
struct BIT{
vector<long long>bit;
int n;
void resz(int _n){
n=_n;
bit.resize(n+1);
}
void add(int pos,long long val){
for(;pos<=n;pos+=(pos-(pos&(pos-1))))bit[pos]+=val;
}
long long get(int pos){
long long ans=0;
for(;pos>=1;pos-=(pos-(pos&(pos-1))))ans+=bit[pos];
return ans;
}
long long get(int l,int r){
if (l>r)return 0;
return get(r)-get(l-1);
}
};
int in[maxn],out[maxn],tme=0,st[maxn][19];
vector<pii>add[maxn],del[maxn],add2[maxn],del2[maxn];
long long ans[maxn];
BIT cnt,bit;
BIT t1,t2,t3,t4;
int n,q;
int par[maxn];
void predfs(int u){
in[u]=++tme;
for (auto v:add[u]){
int tmp=u;
for2(j,18,0){
if (!st[tmp][j]||!par[st[tmp][j]])continue;
int pr=par[st[tmp][j]];
if (v.fi+pr>=u){
tmp=st[tmp][j];
}
}
if (st[tmp][0]&&par[st[tmp][0]]&&v.fi+par[st[tmp][0]]<u){
tmp=st[tmp][0];
add2[tmp].pb(v);
//cout<<tmp<<'\n';
}
}
for (auto v:del[u]){
int tmp=u;
for2(j,18,0){
if (!st[tmp][j]||!par[st[tmp][j]])continue;
int pr=par[st[tmp][j]];
if (v.fi+pr>=u){
tmp=st[tmp][j];
}
}
if (st[tmp][0]&&par[st[tmp][0]]&&v.fi+par[st[tmp][0]]<u){
tmp=st[tmp][0];
del2[tmp].pb(v);
}
}
for (auto v:b[u]){
st[v.fi][0]=u;
for1(i,1,18)st[v.fi][i]=st[st[v.fi][i-1]][i-1];
predfs(v.fi);
}
out[u]=tme;
}
void solve(int u){
if (par[u]){
int diff=par[u];
long long w=a[par[u]]-a[u];
t1.add(diff,-w*(u-1));
t2.add(diff,w);
diff=u-par[u];
t3.add(diff,-1ll*w*(diff-1));
t4.add(diff,w);
}
for (auto v:add2[u]){
ans[v.se]+=1ll*v.fi*t4.get(v.fi)+t3.get(v.fi);
}
for (auto v:del2[u]){
ans[v.se]-=1ll*v.fi*t4.get(v.fi)+t3.get(v.fi);
}
for (auto v:add[u]){
ans[v.se]+=1ll*v.fi*cnt.get(v.fi)+bit.get(v.fi);
ans[v.se]+=1ll*u*t2.get(max(u-v.fi,1),n)+t1.get(max(u-v.fi,1),n);
}
for (auto v:del[u]){
ans[v.se]-=1ll*v.fi*cnt.get(v.fi)+bit.get(v.fi);
ans[v.se]-=1ll*u*t2.get(max(u-v.fi,1),n)+t1.get(max(u-v.fi,1),n);
}
for (auto v:b[u]){
solve(v.fi);
}
if (par[u]){
    int diff=par[u];
    long long w=a[par[u]]-a[u];
    t1.add(diff,w*(u-1));
    t2.add(diff,-w);
    diff=u-par[u];
    t3.add(diff,1ll*w*(diff-1));
    t4.add(diff,-w);
}
if (par[u]){
int sz=out[u]-in[u]+1;
int time=u-par[u];
bit.add(time+sz,1ll*(a[par[u]]-a[u])*sz);
long long value=(a[par[u]]-a[u]);
value*=(time-1);
bit.add(time,-value);
bit.add(time+sz,value);
cnt.add(time,(a[par[u]]-a[u]));
cnt.add(time+sz,-(a[par[u]]-a[u]));
}
}
long long pf[maxn];
signed main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    //freopen("temp.INP","r",stdin);
    //freopen("temp.OUT","w",stdout);
    cin>>n>>q;
    cnt.resz(n);
    t3.resz(n);
    t4.resz(n);
    bit.resz(n);
    t1.resz(n);
    t2.resz(n);
    for1(i,1,n)cin>>a[i];
    for1(i,1,n)pf[i]=pf[i-1]+a[i];
    a[0]=1e9+7;
    stack<int>t;
    t.push(0);
    vector<int>root;
    for1(i,1,n){
    while (!t.empty()&&a[t.top()]<=a[i])t.pop();
    int p=t.top();
    if (p)b[p].pb({i,i-p});
    else root.pb(i);
    par[i]=t.top();
    t.push(i);
    }
    for1(i,1,q){
    int t,l,r;
    cin>>t>>l>>r;
    add[r].pb({t,i});
    del[l-1].pb({t,i});
    ans[i]+=pf[r]-pf[l-1];
    }
    for (auto v:root)predfs(v);
    for (auto v:root)solve(v);
    for1(i,1,q)cout<<ans[i]<<'\n';
}
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 29528 KB Output is correct
2 Correct 8 ms 29736 KB Output is correct
3 Correct 6 ms 29788 KB Output is correct
4 Correct 6 ms 29768 KB Output is correct
5 Correct 8 ms 29788 KB Output is correct
6 Correct 6 ms 29788 KB Output is correct
7 Correct 8 ms 29784 KB Output is correct
8 Correct 6 ms 29784 KB Output is correct
9 Correct 7 ms 29788 KB Output is correct
10 Correct 7 ms 29788 KB Output is correct
11 Correct 6 ms 29720 KB Output is correct
12 Correct 6 ms 29788 KB Output is correct
13 Correct 7 ms 29736 KB Output is correct
14 Correct 7 ms 29788 KB Output is correct
15 Correct 6 ms 29788 KB Output is correct
16 Correct 6 ms 29788 KB Output is correct
17 Correct 6 ms 29788 KB Output is correct
18 Correct 7 ms 29764 KB Output is correct
19 Correct 6 ms 29788 KB Output is correct
20 Correct 6 ms 29788 KB Output is correct
21 Correct 8 ms 29788 KB Output is correct
22 Correct 6 ms 29788 KB Output is correct
23 Correct 6 ms 29784 KB Output is correct
24 Correct 7 ms 29788 KB Output is correct
25 Correct 7 ms 29788 KB Output is correct
26 Correct 6 ms 29788 KB Output is correct
27 Correct 7 ms 29788 KB Output is correct
28 Correct 7 ms 29740 KB Output is correct
29 Correct 6 ms 29788 KB Output is correct
30 Correct 7 ms 29724 KB Output is correct
31 Correct 6 ms 29788 KB Output is correct
32 Correct 6 ms 29704 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 29528 KB Output is correct
2 Correct 330 ms 74136 KB Output is correct
3 Correct 339 ms 74024 KB Output is correct
4 Correct 362 ms 75168 KB Output is correct
5 Correct 351 ms 75432 KB Output is correct
6 Correct 365 ms 76484 KB Output is correct
7 Correct 344 ms 74512 KB Output is correct
8 Correct 336 ms 75932 KB Output is correct
9 Correct 392 ms 75512 KB Output is correct
10 Correct 340 ms 74064 KB Output is correct
11 Correct 320 ms 74580 KB Output is correct
12 Correct 336 ms 73764 KB Output is correct
13 Correct 316 ms 76268 KB Output is correct
14 Correct 319 ms 75104 KB Output is correct
15 Correct 318 ms 76412 KB Output is correct
16 Correct 332 ms 77104 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 29528 KB Output is correct
2 Correct 376 ms 75072 KB Output is correct
3 Correct 357 ms 74548 KB Output is correct
4 Correct 360 ms 75716 KB Output is correct
5 Correct 370 ms 74032 KB Output is correct
6 Correct 353 ms 74400 KB Output is correct
7 Correct 346 ms 74412 KB Output is correct
8 Correct 355 ms 74972 KB Output is correct
9 Correct 337 ms 73940 KB Output is correct
10 Correct 366 ms 73952 KB Output is correct
11 Correct 349 ms 75212 KB Output is correct
12 Correct 322 ms 74800 KB Output is correct
13 Correct 384 ms 75144 KB Output is correct
14 Correct 351 ms 73812 KB Output is correct
15 Correct 350 ms 74700 KB Output is correct
16 Correct 353 ms 74620 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 288 ms 68964 KB Output is correct
2 Correct 259 ms 69192 KB Output is correct
3 Correct 358 ms 69724 KB Output is correct
4 Correct 278 ms 69012 KB Output is correct
5 Correct 303 ms 69132 KB Output is correct
6 Correct 274 ms 69316 KB Output is correct
7 Correct 265 ms 69580 KB Output is correct
8 Correct 293 ms 69580 KB Output is correct
9 Correct 274 ms 69244 KB Output is correct
10 Correct 343 ms 69340 KB Output is correct
11 Correct 267 ms 69580 KB Output is correct
12 Correct 268 ms 69632 KB Output is correct
13 Correct 260 ms 69288 KB Output is correct
14 Correct 298 ms 69640 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 29528 KB Output is correct
2 Correct 8 ms 29736 KB Output is correct
3 Correct 6 ms 29788 KB Output is correct
4 Correct 6 ms 29768 KB Output is correct
5 Correct 8 ms 29788 KB Output is correct
6 Correct 6 ms 29788 KB Output is correct
7 Correct 8 ms 29784 KB Output is correct
8 Correct 6 ms 29784 KB Output is correct
9 Correct 7 ms 29788 KB Output is correct
10 Correct 7 ms 29788 KB Output is correct
11 Correct 6 ms 29720 KB Output is correct
12 Correct 6 ms 29788 KB Output is correct
13 Correct 7 ms 29736 KB Output is correct
14 Correct 7 ms 29788 KB Output is correct
15 Correct 6 ms 29788 KB Output is correct
16 Correct 6 ms 29788 KB Output is correct
17 Correct 6 ms 29788 KB Output is correct
18 Correct 7 ms 29764 KB Output is correct
19 Correct 6 ms 29788 KB Output is correct
20 Correct 6 ms 29788 KB Output is correct
21 Correct 8 ms 29788 KB Output is correct
22 Correct 6 ms 29788 KB Output is correct
23 Correct 6 ms 29784 KB Output is correct
24 Correct 7 ms 29788 KB Output is correct
25 Correct 7 ms 29788 KB Output is correct
26 Correct 6 ms 29788 KB Output is correct
27 Correct 7 ms 29788 KB Output is correct
28 Correct 7 ms 29740 KB Output is correct
29 Correct 6 ms 29788 KB Output is correct
30 Correct 7 ms 29724 KB Output is correct
31 Correct 6 ms 29788 KB Output is correct
32 Correct 6 ms 29704 KB Output is correct
33 Correct 330 ms 74136 KB Output is correct
34 Correct 339 ms 74024 KB Output is correct
35 Correct 362 ms 75168 KB Output is correct
36 Correct 351 ms 75432 KB Output is correct
37 Correct 365 ms 76484 KB Output is correct
38 Correct 344 ms 74512 KB Output is correct
39 Correct 336 ms 75932 KB Output is correct
40 Correct 392 ms 75512 KB Output is correct
41 Correct 340 ms 74064 KB Output is correct
42 Correct 320 ms 74580 KB Output is correct
43 Correct 336 ms 73764 KB Output is correct
44 Correct 316 ms 76268 KB Output is correct
45 Correct 319 ms 75104 KB Output is correct
46 Correct 318 ms 76412 KB Output is correct
47 Correct 332 ms 77104 KB Output is correct
48 Correct 376 ms 75072 KB Output is correct
49 Correct 357 ms 74548 KB Output is correct
50 Correct 360 ms 75716 KB Output is correct
51 Correct 370 ms 74032 KB Output is correct
52 Correct 353 ms 74400 KB Output is correct
53 Correct 346 ms 74412 KB Output is correct
54 Correct 355 ms 74972 KB Output is correct
55 Correct 337 ms 73940 KB Output is correct
56 Correct 366 ms 73952 KB Output is correct
57 Correct 349 ms 75212 KB Output is correct
58 Correct 322 ms 74800 KB Output is correct
59 Correct 384 ms 75144 KB Output is correct
60 Correct 351 ms 73812 KB Output is correct
61 Correct 350 ms 74700 KB Output is correct
62 Correct 353 ms 74620 KB Output is correct
63 Correct 288 ms 68964 KB Output is correct
64 Correct 259 ms 69192 KB Output is correct
65 Correct 358 ms 69724 KB Output is correct
66 Correct 278 ms 69012 KB Output is correct
67 Correct 303 ms 69132 KB Output is correct
68 Correct 274 ms 69316 KB Output is correct
69 Correct 265 ms 69580 KB Output is correct
70 Correct 293 ms 69580 KB Output is correct
71 Correct 274 ms 69244 KB Output is correct
72 Correct 343 ms 69340 KB Output is correct
73 Correct 267 ms 69580 KB Output is correct
74 Correct 268 ms 69632 KB Output is correct
75 Correct 260 ms 69288 KB Output is correct
76 Correct 298 ms 69640 KB Output is correct
77 Correct 347 ms 74924 KB Output is correct
78 Correct 398 ms 75652 KB Output is correct
79 Correct 339 ms 75320 KB Output is correct
80 Correct 387 ms 74724 KB Output is correct
81 Correct 381 ms 75236 KB Output is correct
82 Correct 369 ms 75760 KB Output is correct
83 Correct 401 ms 75440 KB Output is correct
84 Correct 356 ms 74528 KB Output is correct
85 Correct 397 ms 75316 KB Output is correct
86 Correct 381 ms 75672 KB Output is correct
87 Correct 323 ms 74524 KB Output is correct
88 Correct 368 ms 74584 KB Output is correct
89 Correct 311 ms 73808 KB Output is correct
90 Correct 334 ms 74340 KB Output is correct
91 Correct 327 ms 73852 KB Output is correct
92 Correct 365 ms 73492 KB Output is correct
93 Correct 348 ms 74068 KB Output is correct
94 Correct 325 ms 74576 KB Output is correct
95 Correct 381 ms 74620 KB Output is correct
96 Correct 328 ms 74068 KB Output is correct
97 Correct 331 ms 74320 KB Output is correct
98 Correct 339 ms 76500 KB Output is correct
99 Correct 356 ms 77476 KB Output is correct
100 Correct 397 ms 77484 KB Output is correct
101 Correct 349 ms 76752 KB Output is correct
102 Correct 356 ms 77588 KB Output is correct