QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#217503 | #7448. rfplca | memset0 | TL | 0ms | 0kb | C++14 | 2.6kb | 2023-10-16 22:04:46 | 2023-10-16 22:04:47 |
answer
#include<bits/stdc++.h>
#ifndef memset0
#define endl '\n'
#endif
#define all(x) begin(x),end(x)
using namespace std;
const int N=4e5+9,S=1,B=N/S+9;
int n,m,f[N],g[N],h[N],bln[N];
struct Block{
int l,r,lazy;
bool force=true;
void rebuild(){
for(int i=l;i<=r;i++)f[i]=max(1,f[i]-lazy);
lazy=0;
for(int i=l;i<=r;i++)
if(f[i]>=l){
g[i]=g[f[i]];
h[i]=h[f[i]]+1;
}else{
g[i]=f[i];
h[i]=1;
}
if(force){
force=false;
for(int i=l;i<=r;i++)if(f[i]>=l){force=true; break;}
}
// cerr<<"FORCE :: "<<force<<endl;
// for(int i=l;i<=r;i++)cerr<<f[i]<<" \n"[i==r];
}
void jump(int d){
if(force){
for(int i=l;i<=r;i++)f[i]=max(1,f[i]-d);
rebuild();
}else{
lazy+=d;
}
}
}b[B];
int getf(int x){return b[bln[x]].force?f[x]:f[x]-b[bln[x]].lazy;}
int getg(int x){return b[bln[x]].force?g[x]:f[x]-b[bln[x]].lazy;}
int geth(int x){return b[bln[x]].force?h[x]:1;}
int depth(int u){
int dep=0;
cerr<<"DEPTH "<<u;
while(u!=1){
dep+=geth(u);
u=getg(u);
}
cerr<<" = "<<dep<<endl;
return dep;
}
int lca(int u,int v){
if(u==v)return u;
int pu=u,pv=v;
while(u!=v){
if(u>v)swap(u,v),swap(pu,pv);
pv=v,v=getg(v);
}
u=pu,v=pv;
while(u!=v){
if(u>v)swap(u,v);
v=getf(v);
}
return u;
}
int solve(vector<int> &b){
sort(all(b));
b.erase(unique(all(b)),b.end());
if(b.size()==1)return 1;
vector<int> c(b.size()-1);
for(int i=0;i<c.size();i++)c[i]=lca(b[i],b[i+1]);
b.insert(b.end(),all(c));
sort(all(b));
b.erase(unique(all(b)),b.end());
for(int x:b)cerr<<x<<",";cerr<<endl;
int ans=1;
for(int i=0;i+1<b.size();i++){
int t=lca(b[i],b[i+1]);
ans+=depth(b[i+1])-depth(t);
}
return ans;
}
int main(){
#ifdef memset0
freopen("H2.in","r",stdin);
#endif
cin.tie(0)->sync_with_stdio(0);
cin>>n>>m;
for(int i=2;i<=n;i++)cin>>f[i];
for(int i=2;i<=n;i++)bln[i]=(i-1)/S+1;
for(int i=2;i<=n;i++)b[bln[i]].r=i;
for(int i=n;i>=2;i--)b[bln[i]].l=i;
for(int i=1;i<=bln[n];i++)b[i].rebuild();
for(int op,l,r,d,u,v,lastans=0,i=1;i<=m;i++){
cin>>op;
if(op==1){
cin>>l>>r>>d;
l^=lastans,r^=lastans,d^=lastans;
// cerr<<"! "<<"MODIFY "<<l<<" "<<r<<" "<<d<<endl;
if(bln[l]==bln[r]){
for(int i=l;i<=r;i++)f[i]=max(1,f[i]-d);
b[bln[l]].rebuild();
}else{
for(int i=l;i<=b[bln[l]].r;i++)f[i]=max(1,f[i]-d);
b[bln[l]].rebuild();
for(int i=b[bln[r]].l;i<=r;i++)f[i]=max(1,f[i]-d);
b[bln[r]].rebuild();
for(int i=bln[l]+1;i<bln[r];i++)b[i].jump(d);
}
}else{
cin>>u>>v;
u^=lastans,v^=lastans;
cout<<(lastans=lca(u,v))<<endl;
}
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Time Limit Exceeded
input:
400000 400000 1 2 1 4 1 1 5 4 6 5 9 1 1 13 3 8 12 16 3 11 8 8 2 18 21 15 7 23 11 6 4 26 17 5 12 6 5 28 32 26 21 5 19 1 25 25 11 26 47 11 31 25 18 7 25 36 40 23 54 31 14 62 61 33 57 40 11 16 24 51 69 25 55 51 55 65 34 19 18 21 20 62 64 83 22 48 67 47 21 27 30 63 10 14 70 63 18 17 74 98 40 89 10 7 30 ...