QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#393720 | #2857. 树点涂色 | Ecec243 | 100 ✓ | 123ms | 27188kb | C++23 | 3.1kb | 2024-04-19 10:04:04 | 2024-04-19 10:04:04 |
Judging History
answer
#include<bits/stdc++.h>
#define ll long long
#define max(a,b) ((a)>(b)?(a):(b))
#define reg register
inline int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
return x*f;
}
const int MN=1e5+5;
int N,M,L[MN],R[MN],dep[MN],fdfn[MN];
class Seg
{
int lazy[MN<<2],t[MN<<2];
void C(int x,int y){t[x]+=y;lazy[x]+=y;}
void down(int x){if(lazy[x])C(x<<1,lazy[x]),C(x<<1|1,lazy[x]),lazy[x]=0;}
void Modi(int x,int l,int r,int a,int b,int ad)
{
if(l==a&&r==b){C(x,ad);return;}
reg int mid=(l+r)>>1;down(x);
if(b<=mid)Modi(x<<1,l,mid,a,b,ad);
else if(a>mid)Modi(x<<1|1,mid+1,r,a,b,ad);
else Modi(x<<1,l,mid,a,mid,ad),Modi(x<<1|1,mid+1,r,mid+1,b,ad);
t[x]=max(t[x<<1],t[x<<1|1]);
}
int Q1(int x,int l,int r,int a,int b)
{
if(l==a&&r==b)return t[x];
reg int mid=(l+r)>>1;down(x);
if(b<=mid)return Q1(x<<1,l,mid,a,b);
else if(a>mid)return Q1(x<<1|1,mid+1,r,a,b);
else return max(Q1(x<<1,l,mid,a,mid),Q1(x<<1|1,mid+1,r,mid+1,b));
}
public:
void Build(int x,int l,int r)
{
if(l==r) return (void)(t[x]=dep[fdfn[l]]);
reg int mid=(l+r)>>1;
Build(x<<1,l,mid);Build(x<<1|1,mid+1,r);
t[x]=max(t[x<<1],t[x<<1|1]);
}
void md(int x,int ad){if(x)Modi(1,1,N,L[x],R[x],ad);}
int q(int x){return Q1(1,1,N,L[x],L[x]);}
int _q(int x){return Q1(1,1,N,L[x],R[x]);}
}T;
class Link_Cut_Tree
{
int fa[MN],c[MN][2];
bool nrt(int x){return c[fa[x]][0]==x||c[fa[x]][1]==x;}
bool get(int x){return c[fa[x]][1]==x;}
void rtt(int x)
{
int y=fa[x],z=fa[y],l=get(x),r=l^1;if(nrt(y))c[z][get(y)]=x;fa[x]=z;
fa[c[x][r]]=y;c[y][l]=c[x][r];fa[y]=x;c[x][r]=y;
}
void Splay(int x)
{
for(;nrt(x);rtt(x))
if(nrt(fa[x])) rtt(get(fa[x])^get(x)?x:fa[x]);
}
int fir(int x){if(!x) return 0;while(c[x][0])x=c[x][0];return x;}
public:
void acs(int x){reg int i;for(i=0;x;x=fa[i=x])Splay(x),T.md(fir(c[x][1]),1),c[x][1]=i,T.md(fir(i),-1);}
void link(int x,int y){fa[x]=y;}
}lct;
class Tree
{
int fa[MN],mx[MN],siz[MN],top[MN],ind;
std::vector<int> A[MN];
void dfs1(int x,int f)
{
reg int i;siz[x]=1;fa[x]=f;lct.link(x,f);dep[x]=dep[f]+1;
for(i=A[x].size()-1;~i;--i)if(A[x][i]^f)
dfs1(A[x][i],x),siz[x]+=siz[A[x][i]],siz[A[x][i]]>siz[mx[x]]?mx[x]=A[x][i]:0;
}
void dfs2(int x,int tp)
{
L[x]=++ind;fdfn[ind]=x;top[x]=tp;if(mx[x])dfs2(mx[x],tp);reg int i;
for(i=A[x].size()-1;~i;--i)if(A[x][i]!=fa[x]&&A[x][i]!=mx[x])dfs2(A[x][i],A[x][i]);
R[x]=ind;
}
public:
void ins(int x,int y){A[x].push_back(y);A[y].push_back(x);}
void build(){dfs1(1,0);dfs2(1,1);}
int lca(int x,int y)
{
while(top[x]^top[y])
dep[top[x]]>dep[top[y]]?x=fa[top[x]]:y=fa[top[y]];
return dep[x]<dep[y]?x:y;
}
}tree;
int main()
{
N=read();M=read();
reg int i,opt,x,y;
for(i=1;i<N;++i) x=read(),tree.ins(x,read());
tree.build();T.Build(1,1,N);
while(M--)
{
opt=read(),x=read();
if(opt==1)lct.acs(x);
if(opt==2)y=read(),printf("%d\n",T.q(x)+T.q(y)+1-2*T.q(tree.lca(x,y)));
if(opt==3)printf("%d\n",T._q(x));
}
return 0;
}
詳細信息
Test #1:
score: 10
Accepted
time: 2ms
memory: 10080kb
input:
1000 1000 1 2 2 3 1 4 4 5 3 6 4 7 7 8 8 9 9 10 8 11 10 12 10 13 13 14 13 15 12 16 15 17 15 18 14 19 17 20 18 21 18 22 21 23 19 24 22 25 21 26 22 27 24 28 27 29 25 30 27 31 28 32 30 33 29 34 33 35 34 36 33 37 35 38 37 39 38 40 40 41 39 42 39 43 41 44 44 45 45 46 46 47 45 48 45 49 47 50 50 51 50 52 48...
output:
3 9 51 49 65 20 65 11 4 7 4 3 21 2 12 5 10 4 38 5 6 20 3 1 2 20 16 2 6 7 12 12 9 4 12 2 12 6 12 8 5 11 12 5 4 13 5 9 5 2 13 2 6 13 3 5 2 2 14 3 5 7 12 4 3 7 15 4 14 3 9 13 12 3 2 4 5 5 2 10 13 1 3 8 9 4 4 13 13 14 14 4 13 10 13 13 5 7 2 3 2 3 12 7 1 6 13 5 4 4 9 5 4 2 4 14 4 10 7 3 8 5 3 6 4 5 15 4 ...
result:
ok 647 lines
Test #2:
score: 10
Accepted
time: 121ms
memory: 27188kb
input:
100000 100000 1 2 2 3 3 4 4 5 5 6 5 7 7 8 7 9 8 10 9 11 11 12 12 13 13 14 13 15 14 16 15 17 17 18 18 19 19 20 20 21 21 22 21 23 22 24 23 25 25 26 26 27 26 28 28 29 29 30 29 31 30 32 32 33 33 34 34 35 34 36 35 37 36 38 37 39 38 40 40 41 40 42 42 43 42 44 44 45 44 46 45 47 47 48 47 49 48 50 49 51 50 5...
output:
39594 11295 11295 2631 4 2631 839 4 839 839 3 840 842 842 5 6 845 3 842 843 4 2 845 842 842 843 3 844 844 844 842 3 843 843 843 843 3 165 3 843 843 5 845 845 845 4 3 843 843 844 844 844 844 844 3 3 845 847 3 846 2 846 846 2 6 658 848 846 3 8 848 846 846 2 849 849 850 850 4 849 4 850 850 5 3 847 2 84...
result:
ok 50169 lines
Test #3:
score: 10
Accepted
time: 115ms
memory: 22780kb
input:
100000 100000 1 2 2 3 3 4 4 5 3 6 3 7 5 8 8 9 7 10 8 11 10 12 11 13 11 14 13 15 13 16 16 17 14 18 17 19 16 20 20 21 20 22 19 23 23 24 22 25 23 26 23 27 27 28 27 29 26 30 28 31 28 32 29 33 33 34 31 35 33 36 34 37 36 38 35 39 38 40 40 41 41 42 41 43 43 44 44 45 45 46 43 47 46 48 46 49 48 50 47 51 51 5...
output:
5 3 5 1452 8 2 1452 1453 1454 1453 2 8 5 436 436 437 9 438 3 12 3 6 439 439 438 6 439 440 442 17 9 7 8 10 10 4 2 2 437 3 7 2 438 438 2 3 2 4 438 4 8 439 3 3 3 3 438 4 4 6 439 439 6 14 440 9 441 439 439 4 440 2 6 440 440 5 2 441 4 12 443 6 442 4 442 363 5 7 438 7 439 33 439 439 439 439 5 3 3 7 440 4 ...
result:
ok 49945 lines
Test #4:
score: 10
Accepted
time: 103ms
memory: 27168kb
input:
100000 100000 1 2 2 3 3 4 3 5 5 6 5 7 7 8 7 9 8 10 9 11 11 12 12 13 13 14 14 15 14 16 15 17 16 18 17 19 19 20 20 21 21 22 22 23 22 24 23 25 25 26 25 27 26 28 27 29 29 30 29 31 31 32 32 33 33 34 33 35 35 36 36 37 36 38 38 39 38 40 39 41 40 42 42 43 43 44 44 45 45 46 46 47 47 48 48 49 49 50 49 51 51 5...
output:
12519 7543 1 2 3 3 3 2 1 3 3008 1 1 2 4 2626 3 1 1 1 2 3 1 8 3 3 3 2 4 6 4 3 2 2 605 1 1 3 1 2 1 2 4 4 3 1 2 1 1 5 2 1 882 3 4 3 4 1 4 1 2 4 6 2 1133 7 3 1 6 3 3 3 6 4 1 3 2 4 1 4 7 2 3 4 7 5 719 2 1 1 3 2 3 2 2 4 3 5 3 2 3 6 2 2 1 5 5 6 3 2 2 1 2 5 2 2 3 5 4 2 4 5 3 2 7 5 1 2 7 3 1 4 602 3 3 2 84 3...
result:
ok 49825 lines
Test #5:
score: 10
Accepted
time: 108ms
memory: 22160kb
input:
100000 100000 1 2 2 3 1 4 4 5 1 6 5 7 5 8 7 9 6 10 7 11 9 12 11 13 9 14 10 15 13 16 16 17 14 18 18 19 18 20 19 21 18 22 22 23 21 24 23 25 25 26 23 27 25 28 25 29 27 30 27 31 29 32 28 33 33 34 30 35 34 36 32 37 36 38 35 39 38 40 40 41 37 42 40 43 40 44 44 45 45 46 42 47 43 48 46 49 45 50 49 51 48 52 ...
output:
299 5 1 4 4 6 5 3 12 3 3 4 8 1289 10 2 3 3 1406 2 7 5 4 7 1 5 8 5 2 4 9 5 5 3 3 9 3 12 2 5 6 12 2 1 4 5 4 6 6 9 5 7 9 2 5 11 14 1 8 3 9 3 5 22 4 5 4 9 1 3 4 4 2 3 2 4 6 2 8 3 12 3 11 13 8 4 12 5 7 5 7 10 8 5 3 2 6 6 8 12 6 8 7 7 5 10 3 5 7 4 4 5 6 5 3 4 2 4 1 1 8 10 4 4 2 3 7 7 1 3 89 107 4 8 1 3 5 ...
result:
ok 50152 lines
Test #6:
score: 10
Accepted
time: 123ms
memory: 16396kb
input:
100000 100000 1 2 2 3 1 4 4 5 4 6 5 7 7 8 4 9 4 10 6 11 7 12 7 13 7 14 13 15 9 16 10 17 14 18 10 19 18 20 19 21 4 22 8 23 15 24 5 25 21 26 14 27 12 28 23 29 17 30 7 31 26 32 28 33 30 34 17 35 21 36 11 37 14 38 7 39 13 40 38 41 8 42 11 43 38 44 21 45 7 46 37 47 22 48 42 49 17 50 37 51 31 52 16 53 22 ...
output:
19 21 21 8 11 20 22 10 14 10 11 13 9 20 17 17 12 17 10 11 8 13 15 18 23 18 22 13 5 14 18 18 9 18 13 16 11 9 18 11 10 8 7 12 10 6 10 13 20 23 13 15 18 12 10 16 21 21 19 24 10 19 24 15 14 24 13 13 9 15 21 7 8 13 7 13 12 12 11 14 16 10 9 11 8 9 15 7 9 13 12 13 8 21 17 16 7 17 10 19 17 15 4 7 14 15 15 8...
result:
ok 66508 lines
Test #7:
score: 10
Accepted
time: 41ms
memory: 18584kb
input:
50000 50000 1 2 2 3 3 4 4 5 4 6 5 7 6 8 7 9 9 10 10 11 10 12 12 13 13 14 13 15 15 16 15 17 17 18 17 19 18 20 20 21 20 22 21 23 23 24 24 25 25 26 25 27 26 28 28 29 29 30 30 31 30 32 32 33 33 34 34 35 35 36 35 37 36 38 38 39 39 40 40 41 41 42 42 43 43 44 43 45 44 46 45 47 46 48 47 49 49 50 49 51 51 52...
output:
6 4 2 3 1379 2 945 3 3 3 2 2910 1 4 519 4 519 520 4 2 520 5 2 520 521 5 5 5 521 3 1 2 519 520 3 1 1 2 2 3 1 2 520 520 4 521 1 3 5 3 4 1 521 521 521 521 1 4 3 3 2 521 2 5 2 3 522 3 2 4 520 520 3 4 4 3 521 3 2 2 521 4 3 1 127 1 1 2 127 127 3 2 128 3 128 2 129 1 3 129 1 1 2 2 1 6 129 1 2 6 4 130 2 130 ...
result:
ok 33376 lines
Test #8:
score: 10
Accepted
time: 71ms
memory: 15968kb
input:
50000 100000 1 2 2 3 3 4 1 5 3 6 6 7 5 8 7 9 6 10 9 11 7 12 12 13 13 14 10 15 11 16 15 17 16 18 18 19 18 20 19 21 20 22 20 23 22 24 22 25 22 26 23 27 27 28 25 29 29 30 29 31 28 32 30 33 33 34 30 35 35 36 36 37 36 38 37 39 35 40 39 41 40 42 41 43 40 44 41 45 44 46 43 47 47 48 48 49 45 50 48 51 48 52 ...
output:
4342 5544 142 12 18 2320 2028 404 2 961 6 2 57 2 3 9 4 10 3 4 6 271 10 271 5 9 6 6 1 272 7 14 3 8 9 272 5 4 4 273 8 90 11 3 9 10 5 277 5 33 6 6 4 2 2 10 13 273 8 5 3 4 5 4 1 274 7 2 2 2 6 4 4 8 6 4 3 6 276 8 4 6 4 4 13 5 2 1 1 5 2 4 9 275 275 275 275 275 7 4 7 8 3 6 2 13 15 4 11 127 9 9 8 5 2 11 3 6...
result:
ok 66993 lines
Test #9:
score: 10
Accepted
time: 93ms
memory: 22692kb
input:
100000 100000 1 2 2 3 1 4 1 5 5 6 3 7 7 8 6 9 6 10 7 11 8 12 9 13 11 14 11 15 14 16 15 17 14 18 16 19 19 20 19 21 18 22 21 23 20 24 22 25 24 26 25 27 24 28 25 29 26 30 29 31 28 32 31 33 30 34 31 35 35 36 35 37 37 38 35 39 38 40 40 41 39 42 41 43 43 44 42 45 44 46 44 47 44 48 46 49 46 50 47 51 51 52 ...
output:
11194 19305 1 5586 17901 3 7069 6 2 9 227 5 6 498 502 12312 3 8976 5 6 12288 6 21 3 2 1676 1676 4 5 5 8 3 2 2 8 4 4 3 1680 3 2 1680 11 1681 7 3 127 4 5 4 8 5 3 2 6 6 3 4 3 3 396 396 4 4 397 4 9 9 2 8 3 7 2 7 3 3 14 5 4 6 8 9 401 6 401 5 8 9 396 2 397 1 1 1 6 4 5 5 4 8 5 3 399 3 7 399 6 11 11 4 8 7 4...
result:
ok 66877 lines
Test #10:
score: 10
Accepted
time: 95ms
memory: 24444kb
input:
100000 100000 1 2 2 3 1 4 2 5 4 6 6 7 5 8 6 9 8 10 10 11 10 12 11 13 12 14 13 15 13 16 14 17 15 18 16 19 18 20 20 21 19 22 20 23 23 24 24 25 23 26 24 27 26 28 28 29 29 30 30 31 29 32 31 33 32 34 32 35 33 36 34 37 37 38 38 39 37 40 40 41 41 42 41 43 42 44 42 45 45 46 46 47 45 48 47 49 49 50 50 51 51 ...
output:
37729 50002 4730 30097 2 4 11427 3 4 7190 4153 7 2 3 11 3 2 2 2107 2 1761 4 4 2 3 3 697 2109 6 509 2110 2110 2110 704 3 6 214 4 2111 2111 9 3 2110 2 2111 2112 2112 7 6 5 6 8 2112 3 1 4 9 6 4 2109 2109 2109 3 8 3 4 2110 6 527 5 1548 1548 5 1548 1549 1 2 1549 3 2 279 5 3 1 1331 5 4 4 5 4 9 7 4 699 699...
result:
ok 66671 lines