QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#404441 | #4891. 树上的孤独 | linweitong | 0 | 251ms | 179496kb | C++14 | 5.3kb | 2024-05-03 22:31:23 | 2024-05-03 22:31:24 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int N=400005,Q=1000005;
int n,m,q,mx;
struct SGT{
int rt[N],tr[N<<5],ls[N<<5],rs[N<<5],tot;
}T1,T2;
int st[N],top,tmp[N];
void upd1(int &x,int l,int r,int C,int D){
x=++T1.tot;
if (l==r){
T1.tr[x]=D;
return;
}
int mid=(l+r)>>1;
if (C<=mid)upd1(T1.ls[x],l,mid,C,D);
else upd1(T1.rs[x],mid+1,r,C,D);
}
void upd2(int &x,int l,int r,int D){
x=++T2.tot;
T2.tr[x]=1;
if (l==r)return;
int mid=(l+r)>>1;
if (D<=mid)upd2(T2.ls[x],l,mid,D);
else upd2(T2.rs[x],mid+1,r,D);
}
int merge1(int x,int y,int l,int r){
if ((!x)||(!y))return x+y;
int z=++T1.tot;
if (l==r){
T1.tr[z]=min(T1.tr[x],T1.tr[y]);
int o=max(T1.tr[x],T1.tr[y]);
st[++top]=o;
++tmp[o];
}
else{
int mid=(l+r)>>1;
T1.ls[z]=merge1(T1.ls[x],T1.ls[y],l,mid);
T1.rs[z]=merge1(T1.rs[x],T1.rs[y],mid+1,r);
}
return z;
}
int merge2(int x,int y,int l,int r){
if ((!x)||(!y))return x+y;
int z=++T2.tot;
if (l==r){
T2.tr[z]=T2.tr[x]+T2.tr[y];
return z;
}
int mid=(l+r)>>1;
T2.ls[z]=merge2(T2.ls[x],T2.ls[y],l,mid);
T2.rs[z]=merge2(T2.rs[x],T2.rs[y],mid+1,r);
T2.tr[z]=T2.tr[T2.ls[z]]+T2.tr[T2.rs[z]];
return z;
}
int qry2(int x,int l,int r,int D){
if (!x)return 0;
if (l>D)return 0;
if (r<=D)return T2.tr[x];
int mid=(l+r)>>1;
return qry2(T2.ls[x],l,mid,D)+qry2(T2.rs[x],mid+1,r,D);
}
int qry1(int x,int l,int r,int C){
if (!x)return 0;
if (l==r)return T1.tr[x];
int mid=(l+r)>>1;
if (C<=mid)return qry1(T1.ls[x],l,mid,C);
return qry1(T1.rs[x],mid+1,r,C);
}
void print2(int x,int l,int r){
if (!x)return;
if (l==r)return cout<<l<<" ",void();
int mid=(l+r)>>1;
print2(T2.ls[x],l,mid);
print2(T2.rs[x],mid+1,r);
}
void print(int x,int l,int r){
if (!x)return;
if (l==r)return cout<<l<<" ",void();
int mid=(l+r)>>1;
print(T1.ls[x],l,mid);
print(T1.rs[x],mid+1,r);
}
int chk(int x,int C,int lim){
int o=qry1(T1.rt[x],1,m,C);
if (o==0||o>lim)return 1;
return 0;
}
int wk(int x,int l,int r,int D){
if (!x)return 0;
int y=++T2.tot;
T2.tr[y]=T2.tr[x]-1;
int mid=(l+r)>>1;
if (D<=mid){
T2.rs[y]=T2.rs[x];
T2.ls[y]=wk(T2.ls[x],l,mid,D);
}
else{
T2.ls[y]=T2.ls[x];
T2.rs[y]=wk(T2.rs[x],mid+1,r,D);
}
return y;
}
const int inf=1e9;
int bz[N],ti,tem[N],cnt;
int buc[N];
vector<pair<int,int>>anss[N],ask[N];
struct Graph{
vector<int>E[N];
void add(int x,int y){E[x].emplace_back(y),E[y].emplace_back(x);}
int col[N],dep[N],fa[N],sz[N],rnk[N],son[N],dfn[N],L[N],R[N],tottt;
void dfs(int x,int fat){
dep[x]=dep[fat]+1;
fa[x]=fat;sz[x]=1;
for (int y:E[x])if (y!=fat)dfs(y,x),sz[x]+=sz[y],son[x]=(sz[y]>sz[son[x]]?y:son[x]);
}
void dfs1(int x){
dfn[x]=++tottt;rnk[tottt]=x;
L[x]=R[x]=dfn[x];
if (son[x])dfs1(son[x]);
for (int y:E[x])if (y!=son[x]&&y!=fa[x])dfs1(y);
for (int y:E[x])if (y!=fa[x])R[x]=max(R[x],R[y]);
}
void dfs2(int x,int fat){
upd1(T1.rt[x],1,m,col[x],dep[x]);
upd2(T2.rt[x],1,mx,dep[x]);
for (int y:E[x])if (y!=fat){
dfs2(y,x);
T1.rt[x]=merge1(T1.rt[x],T1.rt[y],1,mx);
T2.rt[x]=merge2(T2.rt[x],T2.rt[y],1,mx);
while (top){
T2.rt[x]=wk(T2.rt[x],1,mx,st[top]);
--tmp[st[top]],--top;
}
}
}
void dfs3(int x,int D){
if (dep[x]>D)return;
if (bz[col[x]]!=ti)tem[++cnt]=col[x];
bz[col[x]]=ti;
for (int y:E[x])if (y!=fa[x])dfs3(y,D);
}
void dfs5(int x){
if (bz[col[x]]!=ti)tem[++cnt]=col[x];
bz[col[x]]=ti;
for (int y:E[x])if (y!=fa[x])dfs5(y);
}
void dfs4(int x,int tp){
for (int y:E[x])if (y!=fa[x]&&y!=son[x]){
dfs4(y,0);
}
if (son[x])dfs4(son[x],1);
for (int y:E[x])if (y!=fa[x]&&y!=son[x]){
for (int i=L[y];i<=R[y];++i){
buc[col[i]]=min(buc[col[i]],dep[rnk[i]]);
}
}
buc[col[x]]=min(buc[col[x]],dep[x]);
for (auto o:ask[x]){
anss[o.first].emplace_back(make_pair(o.second,buc[o.second]));
}
if (!tp){
for (int i=L[x];i<=R[x];++i){
buc[col[i]]=inf;
}
}
}
}G1,G2;
struct Query{
int tp,x,y,u,v;
}que[Q];
int coll[N];
int main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin>>n>>m>>q;
for (int i=1;i<n;++i){
int x,y;
cin>>x>>y;
G1.add(x,y);
}
for (int i=1;i<m;++i){
int x,y;
cin>>x>>y;
G2.add(x,y);
}
for (int i=1;i<=n;++i)cin>>G1.col[i];
for (int i=1;i<=m;++i)cin>>G2.col[i];
for (int i=1;i<=q;++i){
cin>>que[i].tp;
if (que[i].tp==1)cin>>que[i].x>>que[i].y>>que[i].u>>que[i].v;
else cin>>que[i].x>>que[i].y;
}
mx=max(n,m);
G1.dfs(1,0);
G2.dfs(1,0);
G2.dfs2(1,0);
for (int i=1;i<=n;++i)coll[i]=G1.col[i];
for (int i=1;i<=q;++i){
++ti,cnt=0;
if (que[i].tp==1){
G1.dfs5(que[i].x);
for (int j=1;j<=cnt;++j)ask[que[i].y].emplace_back(make_pair(i,tem[j]));
}
else{
G1.col[que[i].x]=que[i].y;
}
}
for (int i=1;i<=m;++i)buc[i]=inf;
G2.dfs4(1,0);
for (int i=1;i<=n;++i)G1.col[i]=coll[i];
int lst=0;
for (int i=1;i<=q;++i){
++ti,cnt=0;
if (que[i].tp==1){
que[i].u^=lst;que[i].v^=lst;
G1.dfs3(que[i].x,G1.dep[que[i].x]+que[i].u);
int now=qry2(T2.rt[que[i].y],1,mx,G2.dep[que[i].y]+que[i].v);
for (auto o:anss[i]){
if (bz[o.first]==ti){
now+=(o.second>G2.dep[que[i].y]+que[i].v);
}
}
// for (int j=1;j<=cnt;++j)now+=chk(que[i].y,tem[j],G2.dep[que[i].y]+que[i].v);
cout<<now<<"\n";
lst=now;
}
else{
G1.col[que[i].x]=que[i].y;
}
}
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 0
Wrong Answer
time: 4ms
memory: 43272kb
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:
103 84 1 40 1 37 187 6 69 3 6 200 64 75 137 24 54 4 70 25 11 6 12 57 53 100 26 200 24 118 84 20 76 36 197 76 15 48 11 19 57 44 24 2 108 157 14 3 15 93 36 98 15 39 35 23 106 142 163 8 136 15 100 135 119 55 22 54 1 12 40 33 47 13 33 151 103 127 140 200 9 75 45 109 72 9 15 25 170 37 61 46 107 4 53 58 4...
result:
wrong answer 1st numbers differ - expected: '105', found: '103'
Subtask #2:
score: 0
Runtime Error
Test #3:
score: 0
Runtime Error
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:
result:
Subtask #3:
score: 0
Wrong Answer
Test #7:
score: 0
Wrong Answer
time: 251ms
memory: 179496kb
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:
2565 612 8336 8983 3548 9180 634 281 2464 7307 2669 1165 1383 1255 8240 4407 108 2679 627 4855 3157 9182 1 8985 2091 9950 8009 9944 1 5037 71 2021 1881 1 1276 2179 335 1075 9386 3460 7723 1957 2741 1240 2320 708 1 545 1 429 699 2331 446 3 9 1423 2311 678 2436 1 2319 696 9999 1 177 1366 8932 6492 766...
result:
wrong answer 1st numbers differ - expected: '2568', found: '2565'
Subtask #4:
score: 0
Runtime Error
Test #13:
score: 0
Runtime Error
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:
result:
Subtask #5:
score: 0
Runtime Error
Test #15:
score: 0
Runtime Error
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...