QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#139942 | #4891. 树上的孤独 | zhouhuanyi | 0 | 1384ms | 504268kb | C++14 | 5.7kb | 2023-08-14 20:00:57 | 2023-08-14 20:01:01 |
Judging History
answer
#include<iostream>
#include<cstdio>
#include<vector>
#include<set>
#include<algorithm>
#define N 300000
using namespace std;
int read()
{
char c=0;
int sum=0;
while (c<'0'||c>'9') c=getchar();
while ('0'<=c&&c<='9') sum=sum*10+c-'0',c=getchar();
return sum;
}
struct reads
{
int num,data;
bool operator < (const reads &t)const
{
return data<t.data;
}
};
reads tong[N+1],dque[N+1];
struct rds
{
int num,data,data2;
bool operator < (const rds &t)const
{
return data2!=t.data2?data2<t.data2:data<t.data;
}
};
rds tongs[N+1];
int n,m,q,ans,lg[N+1],rt[N+1],rt2[N+1],st[N+1],lengs,top,dfn[N+1],sz[N+1],len,v1[N+1],v2[N+1],fa[N+1],fa2[N+1][21],depth[N+1],depth2[N+1],L[N+1],R[N+1][20];
bool used[N+1],used2[N+1],vis[N+1];
vector<int>E[N+1];
vector<int>E2[N+1];
set<reads>s[N+1];
void add(int x,int y)
{
E[x].push_back(y),E[y].push_back(x);
return;
}
void add2(int x,int y)
{
E2[x].push_back(y),E2[y].push_back(x);
return;
}
void dfs(int x)
{
used[x]=1;
for (int i=0;i<E[x].size();++i)
if (!used[E[x][i]])
fa[E[x][i]]=x,depth[E[x][i]]=depth[x]+1,dfs(E[x][i]);
return;
}
void dfs2(int x)
{
dfn[x]=++len,used2[x]=sz[x]=1;
for (int i=0;i<E2[x].size();++i)
if (!used2[E2[x][i]])
fa2[E2[x][i]][0]=x,depth2[E2[x][i]]=depth2[x]+1,dfs2(E2[x][i]),sz[x]+=sz[E2[x][i]];
return;
}
bool cmp(int x,int y)
{
return depth[x]<depth[y];
}
int lca(int x,int y)
{
if (depth2[x]<depth2[y]) swap(x,y);
for (int i=lg[m];i>=0;--i)
if (depth2[fa2[x][i]]>=depth2[y])
x=fa2[x][i];
if (x==y) return x;
for (int i=lg[m];i>=0;--i)
if (fa2[x][i]!=fa2[y][i])
x=fa2[x][i],y=fa2[y][i];
return fa2[x][0];
}
void dfs3(int x,int d)
{
if (depth[x]<=d) dque[++top]=(reads){x,v1[x]};
for (int i=0;i<E[x].size();++i)
if (fa[E[x][i]]==x)
dfs3(E[x][i],d);
return;
}
struct seg
{
struct node
{
int ls,rs,data;
};
node tree[60*N+1];
int length;
void push_up(int k)
{
tree[k].data=tree[tree[k].ls].data+tree[tree[k].rs].data;
return;
}
int add(int k,int L,int R,int x,int d)
{
int nw=++length;
tree[nw]=tree[k];
if (L==R)
{
tree[nw].data+=d;
return nw;
}
int mid=(L+R)>>1;
if (x<=mid) tree[nw].ls=add(tree[nw].ls,L,mid,x,d);
else tree[nw].rs=add(tree[nw].rs,mid+1,R,x,d);
push_up(nw);
return nw;
}
int query(int k,int L,int R,int l,int r)
{
if (L==l&&R==r) return tree[k].data;
int mid=(L+R)>>1;
if (r<=mid) return query(tree[k].ls,L,mid,l,r);
else if (l>=mid+1) return query(tree[k].rs,mid+1,R,l,r);
else return query(tree[k].ls,L,mid,l,mid)+query(tree[k].rs,mid+1,R,mid+1,r);
}
};
seg T;
struct seg2
{
struct node
{
int sn[11],lazy;
};
node tree[60*N+1];
int length;
int add(int k,int L,int R,int l,int r,int d)
{
int nw=++length,sl,sr;
tree[nw]=tree[k];
if (L==l&&R==r)
{
tree[nw].lazy+=d;
return nw;
}
for (int i=1;i<=10;++i)
{
sl=L+((R-L+1)*(i-1))/10,sr=L-1+((R-L+1)*i)/10;
if (max(sl,l)<=min(sr,r))
{
if (!tree[nw].sn[i]) tree[nw].sn[i]=++length;
sl=L+((R-L+1)*(i-1))/10,sr=L-1+((R-L+1)*i)/10,tree[nw].sn[i]=add(tree[nw].sn[i],sl,sr,max(sl,l),min(sr,r),d);
}
}
return nw;
}
int query(int k,int L,int R,int x)
{
if (!x||!k) return 0;
if (L==R) return tree[k].lazy;
int sk=((x-L+1)*10)/(R-L+1);
return tree[k].lazy+query(tree[k].sn[k],L+((R-L+1)*(sk-1))/10,L-1+((R-L+1)*sk)/10,x);
}
int get_query(int k,int l,int r)
{
return query(k,1,m,r)-query(k,1,m,l-1);
}
};
seg2 T2;
int main()
{
int op,x,y,d,d1,d2,ps,l,r,pv,nt;
for (int i=2;i<=N;++i) lg[i]=lg[i>>1]+1;
n=read(),m=read(),q=read();
for (int i=1;i<=n-1;++i) x=read(),y=read(),add(x,y);
for (int i=1;i<=m-1;++i) x=read(),y=read(),add2(x,y);
for (int i=1;i<=n;++i) v1[i]=read();
for (int i=1;i<=m;++i) v2[i]=read(),st[++lengs]=v2[i];
sort(st+1,st+lengs+1),lengs=unique(st+1,st+lengs+1)-st-1,depth[1]=depth2[1]=1,dfs(1),dfs2(1);
for (int i=1;i<=m;++i) tong[i]=(reads){i,depth2[i]};
sort(tong+1,tong+m+1);
for (int i=1;i<=lg[m];++i)
for (int j=1;j<=m;++j)
fa2[j][i]=fa2[fa2[j][i-1]][i-1];
for (int i=1;i<=m;++i)
{
int d=lower_bound(st+1,st+lengs+1,v2[tong[i].num])-st;
auto it=s[d].lower_bound((reads){tong[i].num,dfn[tong[i].num]});
rt[i]=T.add(rt[i-1],1,m,dfn[tong[i].num],1);
if (it!=s[d].end()) nt=(*it).num;
else nt=-1;
if (it!=s[d].begin()) it--,pv=(*it).num;
else pv=-1;
if (pv!=-1) rt[i]=T.add(rt[i],1,m,dfn[lca(pv,tong[i].num)],-1);
if (nt!=-1) rt[i]=T.add(rt[i],1,m,dfn[lca(nt,tong[i].num)],-1);
if (pv!=-1&&nt!=-1) rt[i]=T.add(rt[i],1,m,dfn[lca(pv,nt)],1);
s[d].insert((reads){tong[i].num,dfn[tong[i].num]});
}
for (int i=1;i<=m;++i) tongs[i]=(rds){i,depth2[i],lower_bound(st+1,st+lengs+1,v2[i])-st};
sort(tongs+1,tongs+m+1);
for (int i=1;i<=m;++i) rt2[i]=T2.add(rt2[i-1],1,m,dfn[tongs[i].num],m,1);
for (int i=1;i<=n;++i)
{
d=lower_bound(st+1,st+lengs+1,v1[i])-st;
if (st[d]==v1[i])
{
vis[i]=1,L[i]=lower_bound(tongs+1,tongs+m+1,(rds){0,0,d})-tongs;
for (int j=1;j<=m;++j) R[i][j]=lower_bound(tongs+1,tongs+m+1,(rds){0,j+1,d})-tongs-1;
}
}
while (q--)
{
op=read();
if (op==1)
{
x=read(),y=read(),d1=read()^ans,d2=read()^ans,ps=lower_bound(tong+1,tong+m+1,(reads){0,depth2[y]+d2+1})-tong-1,ans=T.query(rt[ps],1,m,dfn[y],dfn[y]+sz[y]-1),top=0,dfs3(x,depth[x]+d1),sort(dque+1,dque+top+1);
for (int i=1;i<=top;++i)
if (i==1||dque[i].data!=dque[i-1].data)
{
if (vis[dque[i].num]) l=L[dque[i].num],r=R[dque[i].num][min(depth2[y]+d2,m)],ans+=(T2.get_query(rt2[r],dfn[y],dfn[y]+sz[y]-1)-T2.get_query(rt2[l-1],dfn[y],dfn[y]+sz[y]-1)==0);
else ans++;
}
printf("%d\n",ans);
}
else x=read(),d=read(),v1[x]=d;
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 0
Wrong Answer
time: 14ms
memory: 52712kb
input:
20 2000 2000 8 15 8 13 14 8 14 7 12 14 12 1 9 13 9 4 5 9 5 10 2 5 2 19 6 19 6 16 11 7 11 20 18 2 18 17 3 6 1395 1783 1395 1735 1735 457 457 739 739 438 438 101 101 441 441 1879 1879 1238 1238 501 501 1732 1732 1910 1910 2000 2000 834 834 917 917 111 111 780 780 1966 1966 1604 1604 623 623 1748 1748 ...
output:
106 91 2 44 19 56 210 25 70 20 22 211 74 94 138 34 72 71 56 44 21 10 25 84 64 101 27 219 42 77 104 21 93 36 216 70 29 50 11 20 55 62 24 5 110 158 23 21 15 111 43 116 26 83 164 93 30 148 134 9 137 33 114 150 130 72 35 69 18 42 57 50 59 14 37 149 118 149 157 204 22 89 80 41 73 26 29 40 164 55 63 53 10...
result:
wrong answer 1st numbers differ - expected: '105', found: '106'
Subtask #2:
score: 0
Wrong Answer
Test #3:
score: 0
Wrong Answer
time: 1384ms
memory: 492144kb
input:
20 200000 500000 8 18 8 4 2 4 2 14 13 4 13 16 6 4 6 3 1 4 1 17 15 6 15 19 7 17 7 11 5 14 5 10 20 7 12 15 9 8 165302 77239 165302 198189 165302 180850 165302 192738 165302 173589 165302 194087 165302 191990 165302 167370 165302 101092 165302 92553 165302 163654 165302 122381 165302 152105 165302 1919...
output:
2 17598 3899 3423 16 518 5138 11356 21 7340 21 18347 15774 6643 514 1184 6111 1889 6247 662 16738 13243 1597 5446 6079 2388 6266 2 21 282 3517 1807 1236 1038 17821 4658 5539 4633 4547 21 244 2704 2 4610 8578 3970 21 13498 5651 2054 3490 4868 5829 802 2072 1482 13232 2595 938 16770 2 17624 354 5 21 7...
result:
wrong answer 2nd numbers differ - expected: '17595', found: '17598'
Subtask #3:
score: 0
Wrong Answer
Test #7:
score: 0
Wrong Answer
time: 410ms
memory: 277852kb
input:
20 100000 100000 16 12 16 20 6 12 6 18 2 16 2 8 5 20 5 13 3 6 3 11 19 11 19 17 9 12 9 15 4 15 4 7 10 5 14 15 14 1 85812 94626 85812 91172 91172 93788 93788 96567 96567 75524 75524 23275 23275 98340 98340 81608 81608 91480 91480 75108 75108 56605 56605 93317 93317 41617 41617 77160 77160 727 727 7559...
output:
2568 612 8349 9003 3568 9200 673 301 2479 7310 2688 1047 1403 1270 8227 4412 109 2699 641 4484 3171 9202 21 8997 2092 9951 8021 9945 21 5043 89 2041 1881 19 1295 2185 336 1106 9387 3480 7726 1957 2742 1262 2335 717 21 545 21 448 699 2337 499 18 38 1424 2325 693 2465 21 2320 716 10019 8 196 1366 8952...
result:
wrong answer 3rd numbers differ - expected: '8337', found: '8349'
Subtask #4:
score: 0
Wrong Answer
Test #13:
score: 0
Wrong Answer
time: 921ms
memory: 504268kb
input:
1 200000 500000 189127 137023 189127 199761 199761 160701 160701 130639 130639 190908 190908 176819 176819 193363 193363 188021 188021 182446 182446 186028 186028 198828 198828 190792 190792 155378 155378 189428 189428 177276 177276 146159 146159 175923 175923 188073 188073 182206 182206 131072 1310...
output:
3782 1773 771 7329 18161 19395 1952 2 5499 2 2859 3369 7393 5131 5707 2 6002 19867 2 5124 2 12550 1497 4837 7771 16334 18176 5926 17984 19713 3821 17710 17093 4226 3822 576 5638 3660 4988 15688 2 18775 29 5068 16606 2276 16602 4545 598 845 19977 7055 882 164 2744 1683 6747 5091 1632 5137 2931 2778 1...
result:
wrong answer 4th numbers differ - expected: '7328', found: '7329'
Subtask #5:
score: 0
Time Limit Exceeded
Test #15:
score: 0
Time Limit Exceeded
input:
20 200000 1000000 13 10 13 5 19 5 19 20 15 10 15 6 12 6 12 3 8 10 8 2 1 20 1 11 14 10 14 16 18 3 18 7 4 3 9 10 9 17 194055 154514 194055 148156 148156 115271 115271 198116 198116 179433 179433 171975 171975 196600 196600 167194 167194 185078 185078 191409 191409 163496 163496 178243 178243 154093 15...
output:
459 5821 10129 7656 4885 19488 3579 16031 2835 7538 1131 9931 18500 498 11890 2117 9859 7579 7286 111 1854 7399 3350 6104 19508 16470 6208 1725 5926 5597 3823 1919 718 3322 2717 19897 5601 633 753 1301 16010 2974 1674 14673 5224 2622 118 1896 3268 1932 14619 4578 7219 5910 11045 4392 7462 9760 19652...