QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#300104#3275. 小明的树mazihang202230 1750ms150344kbC++144.0kb2024-01-07 17:56:282024-01-07 17:56:29

Judging History

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

  • [2024-01-07 17:56:29]
  • 评测
  • 测评结果:30
  • 用时:1750ms
  • 内存:150344kb
  • [2024-01-07 17:56:28]
  • 提交

answer

#include<bits/stdc++.h>

#define ll long long

#define int long long

#define fir first

#define sec second

#define pii pair<int,int>

#define isz(v) (int)(v.size())

using namespace std;



const int maxn=500005;

const int inf=0x3f3f3f3f3f3f3f3f;



namespace Solve {

	struct Val {

		int x,y,ans;

		Val operator + (const Val &rhs) const {

			return {min(x,rhs.x),min(y,rhs.y),ans+rhs.ans};

		}

	};

	struct Tag {

		int x,y,t,d;

		Tag operator + (const Tag &rhs) const {

			return {x+rhs.x,y+rhs.y,t+rhs.t,d+rhs.d-t*rhs.x};

		}

	};

	struct ST {

		struct Node {

			int l,r;

		} a[maxn*4];

		Val val[maxn*4];

		Tag tag[maxn*4];

		void pushup(int x) {

			val[x]=val[x*2]+val[x*2+1];

		}

		void spread(int x,Tag t) {

			val[x].x+=t.x;

			val[x].y+=t.y;

			val[x].ans+=val[x].x*t.t+t.d;

			tag[x]=tag[x]+t;

		}

		void pushdown(int x) {

			int p=val[x*2].y,q=val[x*2+1].y;

			if(p<=q) {

				spread(x*2,tag[x]);

			} else {

				Tag t=tag[x];

				t.t=t.d=0;

				spread(x*2,t);

			}

			if(p>=q) {

				spread(x*2+1,tag[x]);

			} else {

				Tag t=tag[x];

				t.t=t.d=0;

				spread(x*2+1,t);

			}

			tag[x]={};

		}

		void build(int l,int r,int k=1) {

			a[k]={l,r};

			tag[k]={};

			if(l==r) {

				val[k]={0,1,0};

				return ;

			}

			int mid=(l+r)>>1;

			build(l,mid,k*2);

			build(mid+1,r,k*2+1);

			pushup(k);

		}

		void update(int l,int r,Tag t,int k=1) {

			if(a[k].r<l||a[k].l>r) {

				return ;

			}

			if(l<=a[k].l&&a[k].r<=r) {

				spread(k,t);

				return ;

			}

			pushdown(k);

			update(l,r,t,k*2);

			update(l,r,t,k*2+1);

			pushup(k);

		}

		void dfs(int k=1) {

			if(a[k].l==a[k].r) {

				cout<<val[k].ans<<"\n";

				return ;

			}

			pushdown(k);

			dfs(k*2);

			dfs(k*2+1);

		}

		void print(int k=1) {

			cerr<<val[k].x<<" "<<val[k].y<<" "<<val[k].ans<<"\n";

			if(a[k].l==a[k].r) {

				return ;

			}

			pushdown(k);

			print(k*2);

			print(k*2+1);

		}

	} seg;

	int a[maxn];

	int p[maxn];

	vector<pii> v1[maxn];

	vector<pii> v2[maxn];

	void clear() {

		

	}

	void add(int x,int y,int l,int r) {

		if(p[x]>p[y]) {

			swap(x,y);

		}

		// cerr<<x<<" "<<y<<": "<<l<<" "<<r<<"\n";

		v1[p[x]].push_back({l,r});

		v2[p[y]].push_back({l,r});

	}

	void main(int tid) {

		int n,m;

		cin>>n>>m;

		map<pii,int> t;

		for(int i=1;i<=n-1;i++) {

			int x,y;

			cin>>x>>y;

			if(x>y) {

				swap(x,y);

			}

			t[{x,y}]=0;

		}

		for(int i=1;i<=n-1;i++) {

			cin>>a[i];

			p[a[i]]=i;

		}

		p[1]=n;

		for(int i=1;i<=m;i++) {

			int x,y;

			cin>>x>>y;

			if(x>y) {

				swap(x,y);

			}

			add(x,y,t[{x,y}],i-1);

			t[{x,y}]=-1;

			cin>>x>>y;

			if(x>y) {

				swap(x,y);

			}

			t[{x,y}]=i;

		}

		for(auto p:t) {

			if(p.sec==-1) {

				continue;

			}

			pii r=p.fir;

			add(r.fir,r.sec,p.sec,m);

		}if(m>8000)return ;

		seg.build(0,m);

		for(int i=1;i<=n-1;i++) {

			seg.update(0,m,{1,-1});

			for(pii p:v1[i]) {

				// cerr<<"?\n";

				seg.update(p.fir,p.sec,{0,1});

			}

			for(pii p:v2[i]) {

				seg.update(p.fir,p.sec,{-1,0});

			}

			if(seg.val[1].y==1) {

				seg.update(0,m,{0,0,1});

			}

			// seg.print();

			// cerr<<"-----\n";



			// cerr<<seg.val[1].x<<" "<<seg.val[1].y<<" "<<seg.val[1].ans<<"\n";

		}

		seg.dfs();

	}

	void init() {

		

	}

}



signed main() {

#ifndef ONLINE_JUDGE

	freopen("data.in","r",stdin);

	freopen("mine.out","w",stdout);

#endif

	ios::sync_with_stdio(false);

	cin.tie(0),cout.tie(0);

	int T=1;

//	cin>>T;

	Solve::init();

	for(int t=1;t<=T;t++) {

		Solve::main(t);

		Solve::clear();

	}

#ifndef ONLINE_JUDGE

	cerr<<"Time: "<<(1.0*clock()/CLOCKS_PER_SEC)*1000<<"ms\n";

#endif

}


详细

Subtask #1:

score: 10
Accepted

Test #1:

score: 10
Accepted
time: 519ms
memory: 98328kb

input:

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

output:

237

result:

ok 1 number(s): "237"

Subtask #2:

score: 20
Accepted

Test #2:

score: 20
Accepted
time: 23ms
memory: 38416kb

input:

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

output:

16
16
16
16
16
16
16
16
16
16
16
16
16
16
16
16
16
16
16
16
16
16
16
16
16
16
16
16
16
16
16
16
16
16
16
16
16
16
16
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
...

result:

ok 8001 numbers

Test #3:

score: 0
Accepted
time: 27ms
memory: 41228kb

input:

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

output:

12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
...

result:

ok 8001 numbers

Subtask #3:

score: 0
Wrong Answer

Dependency #1:

100%
Accepted

Dependency #2:

100%
Accepted

Test #4:

score: 0
Wrong Answer
time: 1750ms
memory: 150344kb

input:

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

output:


result:

wrong answer Answer contains longer sequence [length = 500001], but output contains 0 elements