QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#245233#7767. 数据结构Crysfly20 5592ms207900kbC++177.4kb2023-11-09 19:55:552023-11-09 19:55:55

Judging History

你现在查看的是最新测评结果

  • [2023-11-09 19:55:55]
  • 评测
  • 测评结果:20
  • 用时:5592ms
  • 内存:207900kb
  • [2023-11-09 19:55:55]
  • 提交

answer

// what is matter? never mind. 
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,sse4,popcnt,abm,mmx,avx,avx2") 
#include<bits/stdc++.h>
#define For(i,a,b) for(int i=(a);i<=(b);++i)
#define Rep(i,a,b) for(int i=(a);i>=(b);--i)
#define ll long long
//#define int long long
#define ull unsigned long long
using namespace std;
inline ll read()
{
    char c=getchar();ll x=0;bool f=0;
    for(;!isdigit(c);c=getchar())f^=!(c^45);
    for(;isdigit(c);c=getchar())x=(x<<1)+(x<<3)+(c^48);
    if(f)x=-x;return x;
}

#define fi first
#define se second
#define pb push_back
#define mkp make_pair
typedef pair<int,int>pii;
typedef vector<int>vi;
 
#define maxn 400005
#define inf 0x3f3f3f3f

mt19937_64 rng(114);

int n;

struct dat{
	ull c,s;
	dat(ull x=0,ull y=0){c=x,s=y;}
	void add(ull x){
		s+=c*x;
	}
};
dat operator +(dat a,dat b){
	return dat(a.c+b.c,a.s+b.s);
}
dat operator -(dat a,dat b){
	return dat(a.c-b.c,a.s-b.s);
}
void operator +=(dat &a,dat b){
	a.c+=b.c,a.s+=b.s;
}
void operator -=(dat &a,dat b){
	a.c-=b.c,a.s-=b.s;
}

struct node{
	dat s0,s[4];
	node(){
		s0=0;
		For(i,0,3)s[i]=0;
	}
};
node operator +(node a,node b){
	a.s0+=b.s0;
	For(i,0,3)a.s[i]+=b.s[i];
	return a;
}
//void operator +=(node&a,node b){
//	a=a+b;
//}

struct oper{
	ull t0,t[4];
	oper(){
		t0=0;
		For(i,0,3)t[i]=0;
	}
	bool emp(){
		For(i,0,3)if(t[i])return 0;
		if(t0)return 0;
		return 1;
	}
};
void operator +=(oper &a,oper b){
	For(i,0,3)a.t[i]+=b.t[i];
}
void operator +=(node &a,oper b){
	a.s0.add(b.t0);
	For(i,0,3){
		a.s[i].add(b.t0);
		if(b.t[i]) a.s0.s+=a.s[i].c*b.t[i],a.s[i].add(b.t[i]);
	}
}

oper mov(oper a){
	For(i,0,2)a.t[i]=a.t[i+1];
	a.t[3]=0;
	return a;
}
node movup(node a){
	Rep(i,3,1)a.s[i]=a.s[i-1]; a.s[0]=0;
	return a;
}

namespace T{
	int rt[maxn],ls[maxn],rs[maxn];
	node v[maxn],s[maxn];
	oper tag[maxn],tag2[maxn];
	unsigned rnd[maxn];
	void up(int x){
		s[x]=s[ls[x]]+v[x]+s[rs[x]];
	}
	void pt(int x,oper y){
		if(!x)return;
		tag[x]+=y;
		tag2[x]+=y;
		v[x]+=y;
		s[x]+=y;
	}
	void down(int x){
		if(!x || tag[x].emp())return;
		pt(ls[x],tag[x]),pt(rs[x],tag[x]),tag[x]=oper();
	}
	void split(int p,int k,int&x,int&y){
		if(!p)return x=y=0,void();
		down(p);
		if(p<=k)x=p,split(rs[p],k,rs[p],y);
		else y=p,split(ls[p],k,x,ls[p]); up(p); 
	}
	void split(int p,int k,int&x,int&y,int&z){
		int t;
		split(p,k-1,x,t);
		split(t,k,y,z);
	}
	int merge(int x,int y){
		if(!x||!y)return x|y;
		down(x),down(y);
		if(rnd[x]<rnd[y])return rs[x]=merge(rs[x],y),up(x),x;
		return ls[y]=merge(x,ls[y]),up(y),y;
	}
	void ins(int&p,int x){
		if(!p||rnd[x]<rnd[p]){
			split(p,x,ls[x],rs[x]);
			p=x,up(p);return;
		}
		down(p);
		if(x<=p)ins(ls[p],x);
		else ins(rs[p],x);
		up(p);
	}
	void del(int&p,int x){
		down(p);
		if(p==x){
			p=merge(ls[p],rs[p]);
			return;
		}
		if(x<p)del(ls[p],x);
		else del(rs[p],x);
		up(p);
	}
};

#define ls(x) ch[x][0]
#define rs(x) ch[x][1]
int fa[maxn],ch[maxn][2];
oper tag[maxn];
node v[maxn],s[maxn];
bool rev[maxn];

bool chk(int x){return rs(fa[x])==x;}
bool nrt(int x){return ls(fa[x])==x||rs(fa[x])==x;}
void up(int x){
	s[x]=s[ls(x)]+v[x]+s[rs(x)]+movup(T::s[T::rt[x]]);
}
void pr(int x){
	rev[x]^=1;
	swap(ls(x),rs(x));
}
void update(int u,ull x){
//	if(x)cerr<<"update "<<u<<" "<<x<<"\n";
	oper t;
	t.t[0]=x;
	v[u]+=t;
}

void pt(int x,oper y){
	if(!x || y.emp())return;
	assert(x);
	tag[x]+=y;
	
//	cout<<"pushtag "<<x<<"\n";
//	For(i,0,3)cout<<y.t[i]<<" "; cout<<"\n";
	update(x,y.t0+y.t[0]);
	
	s[x]+=y;
	T::pt(T::rt[x],mov(y));
}

void down(int x){
	if(rev[x]){
		if(ls(x))pr(ls(x));
		if(rs(x))pr(rs(x));
		rev[x]=0;
	}
	if(!tag[x].emp()){
		if(ls(x))pt(ls(x),tag[x]);
		if(rs(x))pt(rs(x),tag[x]);
		tag[x]=oper();
	}
}

void downall(int p){
	if(nrt(p))downall(fa[p]);
	down(p);
}
void rot(int x){
	int y=fa[x],z=fa[y],k=chk(x),s=ch[x][!k];
	fa[x]=z; if(nrt(y)) ch[z][chk(y)]=x;
	fa[y]=x; ch[x][!k]=y;
	if(s)fa[s]=y; ch[y][k]=s; up(y),up(x);
}
void splay(int x){
	if(!x)return;
	downall(x);
	while(nrt(x)){
		int y=fa[x];
		if(nrt(y))rot(chk(x)==chk(y)?y:x);
		rot(x);
	}
}
int findL(int x){
	down(x);
	while(ls(x))down(x=ls(x));
	return splay(x),x;
}

int st[maxn],tp;

void ins(int x,int y){
//	cout<<"ins "<<x<<" "<<y<<"\n";
	
	y=findL(y);
	int a,b;
//	T::split(T::rt[x],y,a,b);
//	T::v[y]=T::s[y]=[y]; // wrong.

	node tmp;
	tmp.s0=s[y].s0;
	int z=y;
	For(i,0,3){
		tmp.s[i]=tmp.s[i]+v[z].s[0];
		For(j,0,2-i) tmp.s[i+j+1]=tmp.s[i+j+1]+T::s[T::rt[z]].s[j];
		if(i==3 || !rs(z))break;
		z=findL(rs(z)),splay(z);
	}
	
//	cout<<"tmp:\n";
//	For(i,0,3)cout<<tmp.s[i].s<<" ";puts("");
	
	splay(y);
	T::v[y]=T::s[y]=tmp;
	T::tag[y]=T::tag2[y]=oper();
	
	T::ins(T::rt[x],y);
//	T::rt[x]=T::merge(T::merge(a,y),b);
}
void del(int x,int y){
//	cout<<"del "<<x<<" "<<y<<"\n";
	y=findL(y),splay(y);
	
	T::del(T::rt[x],y);
	oper tmp=T::tag2[y];
//	For(i,0,3) cout<<tmp.t[i]<<" "; cout<<"val_y\n";
	
	oper tt; tt.t0=tmp.t0;
	pt(y,tt); tt.t0=0;
	
	int z=y;
	For(i,0,3){
		For(j,0,3-i) tt.t[j]=tmp.t[i+j];
		T::pt(T::rt[z],mov(tt));
		//v[z]
		
	//	oper tt0; tt0.t[0]=tmp.t[i];
		update(z,tmp.t[i]);
		
		up(z);
		if(i==3 || !rs(z))break;
		z=findL(rs(z)),splay(z);
	}
	
	splay(y);
}
void make(int x,int y){
//	cout<<"mak "<<x<<" "<<y<<"\n";
    if(rs(x)){
    	int tmp=rs(x); rs(x)=0;
    	ins(x,tmp);
    }
    splay(y);
  //  cout<<"mk "<<x<<" "<<y<<"\n";
    if(y){
    	del(x,y);
    	splay(y);
    }
    rs(x)=y;
    up(x);
}
void acc(int x){
//	cout<<"acc "<<x<<"\n";
    tp=0;
    for(int y=0;x;x=fa[y=x]) splay(x),st[++tp]=x;
 //   For(i,1,tp) cout<<st[i]<<" "; cout<<"\n";
    Rep(i,tp,1){
        splay(st[i]);
        make(st[i],st[i-1]);
    }
 //   cout<<"done\n";
}
void mkrt(int x){acc(x),splay(x),pr(x);}
void split(int x,int y){
	mkrt(x),acc(y),splay(y);
}

void dfs(int u){
	if(!u)return;
	down(u);
	if(ls(u))dfs(ls(u));
	cout<<u<<" ";
	if(rs(u))dfs(rs(u));
}

vi e[maxn];
void dfs(int u,int pa){
	for(int v:e[u])
		if(v!=pa){
			fa[v]=u;
			dfs(v,u),ins(u,v);
		}
	up(u);
}
int m;
signed main()
{
//	freopen("3.in","r",stdin);
//	freopen("my.out","w",stdout);
	n=read(),m=read();
	For(i,2,n){
		int u=read(),v=read();
		e[u].pb(v),e[v].pb(u);
	}
	For(i,1,n)T::rnd[i]=rng(),v[i].s0.c=v[i].s[0].c=1;
	dfs(1,0);
	
//	For(i,1,n)cout<<fa[i]<<" ";cout<<" fa\n";
	For(_,1,m){
		int op=read(),u,v,k,x;
		if(op==1){
			u=read(),v=read(),k=read(),x=read();
			oper t;
			For(i,0,k) t.t[i]=x;
			split(u,v);
	//		dfs(v);cout<<" ---\n";
		//	cout<<"pushtag "<<(t.emp())<<"\n";
			pt(v,t);
		}
		if(op==2){
			u=read(),x=read();
			mkrt(1);
			acc(u),splay(u);
			oper t; t.t0=x;
			update(u,x);
			T::pt(T::rt[u],t);
			up(u);
		}
		if(op==3){
			u=read(),v=read(),k=read();
			split(u,v);
			ull res=0;
			For(i,0,k)res+=s[v].s[i].s;
			cout<<res<<"\n";
		}
		if(op==4){
			u=read();
			mkrt(1);
			acc(u),splay(u);
			ull res=0;
			res+=::v[u].s[0].s;
			res+=T::s[T::rt[u]].s0.s;
			cout<<res<<"\n";
		}
		
//		For(i,1,n){
//			cout<<"ASK "<<i<<"\n";
//			split(i,i);
//			cout<<"i: "<<i<<" "<<::v[i].s0.s<<" "<<::v[i].s0.c<<" "<<s[i].s0.s<<" "<<s[i].s0.c<<"\n";
//		}
//		cout<<"\n";
	}
	return 0;
}
/*
8 6
1 2
1 3
2 4
2 5
2 8
4 6
4 7

1 6 8 0 1
1 6 8 1 1
*/

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 0
Wrong Answer
time: 81ms
memory: 193616kb

input:

5000 5000
1 2
2 3
3 4
4 5
5 6
5 7
6 8
7 9
9 10
8 11
11 12
12 13
12 14
14 15
15 16
16 17
16 18
18 19
18 20
20 21
20 22
22 23
23 24
23 25
23 26
26 27
27 28
28 29
27 30
30 31
29 32
32 33
33 34
32 35
35 36
36 37
35 38
36 39
38 40
38 41
41 42
42 43
43 44
42 45
44 46
45 47
47 48
48 49
48 50
49 51
51 52
52...

output:

37227703492
2136305359188
2794367845468
1309925069860
1698169768858
2678958746332
4387826291396
1174586339295
2368683539614
512050492
1492396645711
0
3922681030
3226253922015
6279785772902
6024820982796
0
2233898093865
1533705520
2564984755435
3410630538
0
4571688388
5449408857555
8908521308880
0
19...

result:

wrong answer 7th numbers differ - expected: '6690595071246', found: '4387826291396'

Subtask #2:

score: 0
Wrong Answer

Test #3:

score: 0
Wrong Answer
time: 5333ms
memory: 202088kb

input:

200000 200000
1 2
1 3
1 4
3 5
1 6
1 7
7 8
8 9
2 10
1 11
5 12
1 13
7 14
10 15
2 16
7 17
11 18
5 19
5 20
1 21
16 22
1 23
3 24
20 25
14 26
2 27
6 28
15 29
10 30
15 31
5 32
13 33
12 34
31 35
31 36
36 37
36 38
1 39
28 40
5 41
12 42
41 43
20 44
30 45
22 46
11 47
47 48
45 49
14 50
41 51
3 52
30 53
29 54
6 ...

output:

0
0
0
0
0
0
0
0
7615073807
4176911055
0
4745654848
6222845818
0
0
9739142819
0
1424960716
5224818790
9459319003
13717923473
8673060864
0
11610197664
0
0
9587792729
0
0
0
2747489046
12425650803
0
0
11191496476
0
37597503993
0
0
15164651949
14868775382
15559673116
0
16391028892
0
15726757336
0
2421390...

result:

wrong answer 528th numbers differ - expected: '378636173609', found: '378121561754'

Subtask #3:

score: 0
Wrong Answer

Test #5:

score: 0
Wrong Answer
time: 4098ms
memory: 207900kb

input:

200000 200000
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
18 19
19 20
20 21
21 22
22 23
23 24
24 25
25 26
26 27
27 28
28 29
29 30
30 31
31 32
32 33
33 34
34 35
35 36
36 37
37 38
38 39
39 40
40 41
41 42
42 43
43 44
44 45
45 46
46 47
47 48
48 49
49 50
50 51
51 ...

output:

0
104135885566111
19445338831737
42251662678080
25202765567748
141195328320761
37492387868963
89938304435638
150861625339113
154613654909321
83937873387556
139899625476097
134696834609678
171989209011243
26465104186057
206640522768
156700335889538
299157388937945
330484282339348
51319600690663
12310...

result:

wrong answer 2nd numbers differ - expected: '134315201201061', found: '104135885566111'

Subtask #4:

score: 10
Accepted

Test #7:

score: 10
Accepted
time: 4265ms
memory: 202468kb

input:

200000 200000
1 2
2 3
3 4
1 5
3 6
5 7
5 8
7 9
2 10
7 11
11 12
10 13
6 14
3 15
14 16
4 17
11 18
3 19
14 20
4 21
4 22
12 23
18 24
5 25
5 26
14 27
13 28
24 29
11 30
26 31
29 32
28 33
31 34
23 35
33 36
6 37
11 38
22 39
13 40
35 41
37 42
21 43
12 44
4 45
16 46
12 47
21 48
1 49
26 50
45 51
41 52
46 53
7 5...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

ok 99786 numbers

Test #8:

score: 0
Accepted
time: 3578ms
memory: 203780kb

input:

200000 200000
1 2
2 3
1 4
1 5
2 6
3 7
6 8
8 9
8 10
9 11
8 12
12 13
13 14
11 15
13 16
13 17
16 18
17 19
18 20
19 21
19 22
21 23
21 24
21 25
24 26
23 27
26 28
27 29
26 30
30 31
28 32
29 33
32 34
32 35
33 36
36 37
35 38
38 39
38 40
40 41
39 42
42 43
43 44
41 45
45 46
43 47
45 48
46 49
49 50
50 51
51 52...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

ok 100404 numbers

Test #9:

score: 0
Accepted
time: 5592ms
memory: 202388kb

input:

200000 200000
166945 2
60190 3
101888 4
154621 5
188595 6
175999 7
140051 8
54071 9
167394 10
54228 11
48270 12
14564 13
25727 14
138072 15
77670 16
77795 17
155644 18
171648 19
94412 20
65323 21
130225 22
6359 23
17410 24
8580 25
142556 26
152863 27
166869 28
115234 29
87099 30
160349 31
98200 32
1...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

ok 99768 numbers

Subtask #5:

score: 10
Accepted

Dependency #4:

100%
Accepted

Test #10:

score: 10
Accepted
time: 4294ms
memory: 202532kb

input:

200000 200000
1 2
1 3
2 4
1 5
2 6
2 7
2 8
5 9
3 10
10 11
5 12
4 13
5 14
9 15
11 16
14 17
12 18
13 19
2 20
16 21
3 22
16 23
2 24
7 25
8 26
20 27
21 28
11 29
12 30
4 31
2 32
21 33
14 34
29 35
16 36
21 37
28 38
22 39
27 40
12 41
36 42
32 43
30 44
3 45
43 46
4 47
14 48
44 49
9 50
37 51
20 52
11 53
31 54...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

ok 100258 numbers

Test #11:

score: 0
Accepted
time: 5492ms
memory: 201772kb

input:

200000 200000
184821 2
53793 3
183415 4
113765 5
178864 6
46342 7
933 8
197825 9
177971 10
143394 11
99313 12
188890 13
25495 14
60986 15
162307 16
135027 17
145920 18
109359 19
5215 20
75134 21
53020 22
160666 23
30142 24
23800 25
38903 26
121838 27
164296 28
86957 29
89705 30
108331 31
147730 32
2...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

ok 100336 numbers

Subtask #6:

score: 0
Wrong Answer

Dependency #4:

100%
Accepted

Dependency #5:

100%
Accepted

Test #12:

score: 0
Wrong Answer
time: 4002ms
memory: 202136kb

input:

200000 200000
1 2
1 3
2 4
2 5
5 6
3 7
2 8
3 9
4 10
7 11
9 12
7 13
2 14
12 15
6 16
5 17
14 18
3 19
14 20
13 21
8 22
7 23
12 24
5 25
3 26
18 27
9 28
8 29
6 30
22 31
5 32
6 33
28 34
19 35
24 36
24 37
35 38
7 39
32 40
20 41
19 42
14 43
1 44
5 45
30 46
9 47
30 48
5 49
44 50
7 51
13 52
11 53
19 54
31 55
4...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

wrong answer 128th numbers differ - expected: '540038770', found: '0'

Subtask #7:

score: 0
Skipped

Dependency #2:

0%

Subtask #8:

score: 0
Skipped

Dependency #1:

0%