Submission #1011075

#TimeUsernameProblemLanguageResultExecution timeMemory
1011075imarnJOI tour (JOI24_joitour)C++17
6 / 100
1114 ms293972 KiB
#include<bits/stdc++.h> #pragma GCC optimize("O3") #pragma GCC target("avx2") #define ll long long #define pii pair<int,int> #define pll pair<ll,ll> #define plx pair<ll,int> #define f first #define s second #define pb push_back #define all(x) x.begin(),x.end() #define vi vector<int> #define vl vector<ll> #define vvi vector<vi> using namespace std; const int mxn=2e5+5; vector<int>g[mxn],f; int sz[mxn]{0},lv[mxn]{0},pr[mxn]; bool vis[mxn]{0}; ll ans=0; int getsz(int u,int p){ sz[u]=1; for(auto v:g[u])if(v!=p&&!vis[v])sz[u]+=getsz(v,u); return sz[u]; } int getct(int u,int p,int r){ for(auto v:g[u])if(v!=p&&!vis[v]&&sz[v]*2>r)return getct(v,u,r); return u; } vector<ll>dp[6][mxn]; ll td[6][mxn]{0}; struct few{ ll fw[mxn]{0}; void add(int i,int amt){ for(;i<mxn;i+=i&-i)fw[i]+=amt; } ll qr(int i,ll res=0){ for(;i;i-=i&-i)res+=fw[i]; return res; } }fw[3][19],wf[3][19]; int tin[19][mxn],tout[19][mxn],cnt[19]{0},ID[19][mxn]; ll getP(int col,int lvl,int u){ return fw[col][lvl].qr(tin[lvl][u]); } ll getC(int col,int lvl,int u){ return wf[col][lvl].qr(tout[lvl][u])-wf[col][lvl].qr(tin[lvl][u]-1); } void gen1(int u,int p,int x,int id,int x0,int x1,int x2){ dp[f[u]][x][id]++;tin[lv[x]][u]=++cnt[lv[x]]; fw[f[u]][lv[x]].add(tin[lv[x]][u],1); if(f[u]==2)dp[3][x][id]+=x1; for(auto v:g[u])if(v!=p&&!vis[v])gen1(v,u,x,id,x0+(f[v]==0),x1+(f[v]==1),x2+(f[v]==2)); tout[lv[x]][u]=cnt[lv[x]];ID[lv[x]][u]=id; fw[f[u]][lv[x]].add(tout[lv[x]][u]+1,-1); } void gen2(int u,int p,int x,int id){ for(auto v:g[u])if(v!=p&&!vis[v])gen2(v,u,x,id); wf[f[u]][lv[x]].add(tin[lv[x]][u],1); if(f[u]==1)dp[4][x][id]+=getC(0,lv[x],u); } void play(int u,int p){int id=-1; u = getct(u,u,getsz(u,u));vis[u]=1; pr[u]=p;if(p!=-1)lv[u]=lv[p]+1; tin[lv[u]][u]=++cnt[lv[u]]; for(auto v:g[u]){ if(vis[v])continue; for(int i=0;i<6;i++)dp[i][u].pb(0);id++; gen1(v,u,u,id,(f[v]==0),(f[v]==1),(f[v]==2));gen2(v,u,u,id); for(int i=0;i<5;i++)td[i][u]+=dp[i][u][id];td[5][u]+=dp[0][u][id]*dp[2][u][id]; }tout[lv[u]][u]=cnt[lv[u]]; for(int j=0;j<=id;j++){ ans+=dp[0][u][j]*(td[3][u]-dp[3][u][j]); ans+=dp[4][u][j]*(td[2][u]-dp[2][u][j]); if(f[u]==1)ans+=dp[0][u][j]*(td[2][u]-dp[2][u][j]); }if(f[u]==0)ans+=td[3][u];if(f[u]==2)ans+=td[4][u]; for(auto v:g[u])if(!vis[v])play(v,u); } void init(int N, std::vector<int> F, std::vector<int> U, std::vector<int> V,int Q){ f=F;for(int i=0;i<N-1;i++)g[U[i]].pb(V[i]),g[V[i]].pb(U[i]); play(0,-1); } void up(int col,int u,int sig){ int x=u; if(col==0){ ans+=td[3][u]*sig;x=pr[u];int j=ID[lv[x]][u]; while(x!=-1){ ans-=dp[0][x][j]*(td[3][x]-dp[3][x][j]); ans-=dp[4][x][j]*(td[2][x]-dp[2][x][j]); ans-=dp[3][x][j]*(td[0][x]-dp[0][x][j]); ans-=dp[2][x][j]*(td[4][x]-dp[4][x][j]); if(f[x]==1)ans-=td[0][x]*td[2][x]-td[5][x]; if(f[x]==2)ans-=td[4][x];td[0][x]+=sig;dp[0][x][j]+=sig; td[5][x]-=td[0][x]*td[2][x]; td[4][x]-=dp[4][x][j]; dp[4][x][j]+=sig*getP(1,lv[x],u); td[4][x]+=dp[4][x][j]; td[5][x]+=td[0][x]*td[2][x]; if(f[x]==1)ans+=td[0][x]*td[2][x]-td[5][x]; if(f[x]==2)ans+=td[4][x]; ans+=dp[0][x][j]*(td[3][x]-dp[3][x][j]); ans+=dp[4][x][j]*(td[2][x]-dp[2][x][j]); ans+=dp[3][x][j]*(td[0][x]-dp[0][x][j]); ans+=dp[2][x][j]*(td[4][x]-dp[4][x][j]); fw[0][lv[x]].add(tin[lv[x]][u],sig); fw[0][lv[x]].add(tout[lv[x]][u]+1,-sig); wf[0][lv[x]].add(tin[lv[x]][u],sig); if(pr[x]==-1)break;x=pr[x];j=ID[lv[x]][u]; } } else if(col==1){ ans+=(td[0][u]*td[2][u]-td[5][u])*sig;x=pr[u];int j=ID[lv[x]][u]; while(x!=-1){ ans-=dp[0][x][j]*(td[3][x]-dp[3][x][j]); ans-=dp[4][x][j]*(td[2][x]-dp[2][x][j]); ans-=dp[3][x][j]*(td[0][x]-dp[0][x][j]); ans-=dp[2][x][j]*(td[4][x]-dp[4][x][j]); if(f[x]==0)ans-=td[3][x]; if(f[x]==2)ans-=td[4][x];td[1][x]+=sig;dp[1][x][j]+=sig; td[4][x]-=dp[4][x][j]; td[3][x]-=dp[3][x][j]; dp[4][x][j]+=sig*getC(0,lv[x],u); dp[3][x][j]+=sig*getC(2,lv[x],u); td[4][x]+=dp[4][x][j]; td[3][x]+=dp[3][x][j]; if(f[x]==0)ans+=td[3][x]; if(f[x]==2)ans+=td[4][x]; ans+=dp[0][x][j]*(td[3][x]-dp[3][x][j]); ans+=dp[4][x][j]*(td[2][x]-dp[2][x][j]); ans+=dp[3][x][j]*(td[0][x]-dp[0][x][j]); ans+=dp[2][x][j]*(td[4][x]-dp[4][x][j]); fw[1][lv[x]].add(tin[lv[x]][u],sig); fw[1][lv[x]].add(tout[lv[x]][u]+1,-sig); wf[1][lv[x]].add(tin[lv[x]][u],sig); if(pr[x]==-1)break;x=pr[x];j=ID[lv[x]][u]; } } else { ans+=td[3][u]*sig;x=pr[u];int j=ID[lv[x]][u]; while(x!=-1){ ans-=dp[0][x][j]*(td[3][x]-dp[3][x][j]); ans-=dp[4][x][j]*(td[2][x]-dp[2][x][j]); ans-=dp[3][x][j]*(td[0][x]-dp[0][x][j]); ans-=dp[2][x][j]*(td[4][x]-dp[4][x][j]); if(f[x]==1)ans-=td[0][x]*td[2][x]-td[5][x]; if(f[x]==0)ans-=td[3][x];td[2][x]+=sig;dp[2][x][j]+=sig; td[5][x]-=td[0][x]*td[2][x]; td[3][x]-=dp[3][x][j]; dp[3][x][j]+=sig*getP(1,lv[x],u); td[3][x]+=dp[3][x][j]; td[5][x]+=td[0][x]*td[2][x]; if(f[x]==1)ans+=td[0][x]*td[2][x]-td[5][x]; if(f[x]==0)ans+=td[3][x]; ans+=dp[0][x][j]*(td[3][x]-dp[3][x][j]); ans+=dp[4][x][j]*(td[2][x]-dp[2][x][j]); ans+=dp[3][x][j]*(td[0][x]-dp[0][x][j]); ans+=dp[2][x][j]*(td[4][x]-dp[4][x][j]); fw[2][lv[x]].add(tin[lv[x]][u],sig); fw[2][lv[x]].add(tout[lv[x]][u]+1,-sig); wf[2][lv[x]].add(tin[lv[x]][u],sig); if(pr[x]==-1)break;x=pr[x];j=ID[lv[x]][u]; } } } void change(int X, int Y){ up(f[X],X,-1);up(Y,X,1); f[X]=Y; } long long num_tours() { return ans; } /*int main(){ ios_base::sync_with_stdio(0);cin.tie(0); int n;cin>>n; vector<int>ff(n),u(n-1),v(n-1); for(int i=0;i<n;i++)cin>>ff[i]; for(int i=0;i<n-1;i++)cin>>u[i]>>v[i]; init(n,ff,u,v,0);cout<<num_tours()<<'\n'; int q;cin>>q; while(q--){ int x,y;cin>>x>>y;change(x,y); cout<<num_tours()<<'\n'; } }*/

Compilation message (stderr)

joitour.cpp: In function 'void play(int, int)':
joitour.cpp:68:9: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
   68 |         for(int i=0;i<6;i++)dp[i][u].pb(0);id++;
      |         ^~~
joitour.cpp:68:44: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
   68 |         for(int i=0;i<6;i++)dp[i][u].pb(0);id++;
      |                                            ^~
joitour.cpp:70:9: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
   70 |         for(int i=0;i<5;i++)td[i][u]+=dp[i][u][id];td[5][u]+=dp[0][u][id]*dp[2][u][id];
      |         ^~~
joitour.cpp:70:52: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
   70 |         for(int i=0;i<5;i++)td[i][u]+=dp[i][u][id];td[5][u]+=dp[0][u][id]*dp[2][u][id];
      |                                                    ^~
joitour.cpp: In function 'void up(int, int, int)':
joitour.cpp:93:13: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
   93 |             if(f[x]==2)ans-=td[4][x];td[0][x]+=sig;dp[0][x][j]+=sig;
      |             ^~
joitour.cpp:93:38: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
   93 |             if(f[x]==2)ans-=td[4][x];td[0][x]+=sig;dp[0][x][j]+=sig;
      |                                      ^~
joitour.cpp:108:13: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
  108 |             if(pr[x]==-1)break;x=pr[x];j=ID[lv[x]][u];
      |             ^~
joitour.cpp:108:32: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
  108 |             if(pr[x]==-1)break;x=pr[x];j=ID[lv[x]][u];
      |                                ^
joitour.cpp:119:13: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
  119 |             if(f[x]==2)ans-=td[4][x];td[1][x]+=sig;dp[1][x][j]+=sig;
      |             ^~
joitour.cpp:119:38: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
  119 |             if(f[x]==2)ans-=td[4][x];td[1][x]+=sig;dp[1][x][j]+=sig;
      |                                      ^~
joitour.cpp:135:13: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
  135 |             if(pr[x]==-1)break;x=pr[x];j=ID[lv[x]][u];
      |             ^~
joitour.cpp:135:32: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
  135 |             if(pr[x]==-1)break;x=pr[x];j=ID[lv[x]][u];
      |                                ^
joitour.cpp:146:13: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
  146 |             if(f[x]==0)ans-=td[3][x];td[2][x]+=sig;dp[2][x][j]+=sig;
      |             ^~
joitour.cpp:146:38: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
  146 |             if(f[x]==0)ans-=td[3][x];td[2][x]+=sig;dp[2][x][j]+=sig;
      |                                      ^~
joitour.cpp:161:13: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
  161 |             if(pr[x]==-1)break;x=pr[x];j=ID[lv[x]][u];
      |             ^~
joitour.cpp:161:32: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
  161 |             if(pr[x]==-1)break;x=pr[x];j=ID[lv[x]][u];
      |                                ^
#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...