QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#326928#7608. CliquesiliaaaaaWA 98ms27208kbC++203.2kb2024-02-14 14:18:422024-02-14 14:18:43

Judging History

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

  • [2024-02-14 14:18:43]
  • 评测
  • 测评结果:WA
  • 用时:98ms
  • 内存:27208kb
  • [2024-02-14 14:18:42]
  • 提交

answer

#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
#define F first
#define S second
#define All(x) (x).begin(), (x).end()
#define len(x) (int) (x).size()
#define pb push_back

const int N = 2e5 + 7, P = 1e9 + 7;
int n, st[N], sz[N], h[N], hld[N], big[N], cnt_lca[N], spar[N];
int sum[N << 2], lazy[N << 2], Cnt[N << 2];
vector<int> adj[N];

int power(int a, int b) {
	if (!b)
		return 1;
	int tmp = power(a, b / 2);
	return b % 2? 1LL * tmp * tmp % P * a % P: 1LL * tmp * tmp % P;
}

void add(int l, int r, int x, int id = 1, int st = 0, int en = n) {
	if (r <= st || l >= en)
		return;
	if (st >= l && en <= r) {
		Cnt[id] += x;
		return;
	}
	int mid = (st + en) >> 1;
	add(l, r, x, id << 1, st, mid);
	add(l, r, x, id << 1 | 1, mid, en);
}

int get_cnt(int p, int id = 1, int st = 0, int en = n) {
	if (en - st == 1)
		return Cnt[id];
	int mid = (st + en) >> 1;
	return Cnt[id] + (p < mid? get_cnt(p, id << 1, st, mid): get_cnt(p, id << 1 | 1, mid, en));
}

void Apply(int id, int x) {
	sum[id] = 1LL * sum[id] * x % P;
	lazy[id] = 1LL * lazy[id] * x % P;
}

void shift(int id) {
	Apply(id << 1, lazy[id]);
	Apply(id << 1 | 1, lazy[id]);
	lazy[id] = 1;
}

void mul(int l, int r, int x, int id = 1, int st = 0, int en = n) {
	if (r <= st || l >= en)
		return;
	if (st >= l && en <= r) {
		Apply(id, x);
		return;
	}
	shift(id);
	int mid = (st + en) >> 1;
	mul(l, r, x, id << 1, st, mid), mul(l, r, x, id << 1 | 1, mid, en);
	sum[id] = (sum[id << 1] + sum[id << 1 | 1]);
}

void Set(int p, int x, int id = 1, int st = 0, int en = n) {
	if (en - st == 1) {
		sum[id] = x;
		return;
	}
	int mid = (st + en) >> 1;
	shift(id);
	p < mid? Set(p, x, id << 1, st, mid): Set(p, x, id << 1 | 1, mid, en);
	sum[id] = (sum[id << 1] + sum[id << 1 | 1]) % P;
}

void dfs_sz(int u) {
	sz[u] = 1, big[u] = n;
	for (int v: adj[u])
		if (v != spar[u]) {
			spar[v] = u;
			h[v] = 1 + h[u];
			dfs_sz(v);
			if (sz[v] > sz[big[u]])
				big[u] = v;
			sz[u] += sz[v];
		}
	if (big[u] == n)
		big[u] = -1;
}

void dfs(int u) {
	static int t = 0;
	st[u] = t++;
	if (~big[u]) {
		hld[big[u]] = hld[u];
		dfs(big[u]);
	}
	for (int v: adj[u])
		if (v != spar[u] && v != big[u]) {
			hld[v] = v;
			dfs(v);
		}
}

void upd(int u, int v, int x) {
	int W = (x == 1? 2: power(2, P - 2));
	while (hld[u] != hld[v]) {
		if (h[hld[u]] < h[hld[v]])
			swap(u, v);
		add(st[hld[u]], st[u] + 1, x);
		mul(st[hld[u]], st[u] + 1, W);
		u = spar[hld[u]];
	}
	if (h[u] < h[v])
		swap(u, v);
	add(st[v], st[u] + 1, x), mul(st[v], st[u] + 1, W);
	add(st[v], st[v] + 1, -x);
	cnt_lca[v] += x;
	int X = cnt_lca[v], Y = get_cnt(st[v]);
	Set(st[v], 1LL * (power(2, X) - 1 + P) % P * power(2, Y) % P);
}

int32_t main() {
	ios:: sync_with_stdio(0), cin.tie(0), cout.tie(0);
	cin >> n;
	for (int i = 1; i < n; i++) {
		int u, v;
		cin >> u >> v;
		u--, v--;
		adj[u].pb(v);
		adj[v].pb(u);
	}
	dfs_sz(0);
	dfs(0);
	int q;
	cin >> q;
	while (q--) {
		char c;
		int u, v;
		cin >> c >> u >> v;
		u--, v--;
		if (c == '+')
			upd(u, v, 1);
		else
			upd(u, v, -1);
		cout << sum[1] << '\n';
	}
	return 0;
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 7756kb

input:

5
1 2
5 1
2 3
4 2
6
+ 4 5
+ 2 2
+ 1 3
- 2 2
+ 2 3
+ 4 4

output:

1
3
7
3
7
9

result:

ok 6 lines

Test #2:

score: 0
Accepted
time: 1ms
memory: 7740kb

input:

20
8 7
19 10
12 14
3 16
17 13
7 10
5 6
1 9
15 12
5 2
16 13
3 11
20 14
18 6
1 14
16 20
11 10
3 4
20 6
30
+ 10 20
+ 14 20
+ 12 17
- 14 20
- 12 17
+ 4 6
+ 8 20
+ 3 6
- 10 20
+ 2 17
+ 1 16
+ 6 10
+ 9 10
+ 5 15
+ 7 8
- 7 8
+ 2 5
+ 3 18
+ 1 20
+ 8 16
- 3 18
- 5 15
+ 4 20
+ 14 16
- 4 6
+ 8 19
+ 4 7
- 1 16
...

output:

1
3
7
3
1
3
7
15
7
15
31
63
127
255
257
255
259
515
1027
1283
643
385
769
1537
769
785
865
481
737
369

result:

ok 30 lines

Test #3:

score: -100
Wrong Answer
time: 98ms
memory: 27208kb

input:

131072
3641 72511
10338 18718
8949 15478
79108 62887
42154 28540
65359 102645
93744 48493
3277 103211
43542 61315
93315 118634
24021 57937
31034 436
30411 6208
1388 25159
93424 128520
119820 87281
5860 97559
59648 8225
57244 58766
119685 13716
130165 60958
79806 116338
97486 80167
101963 95499
51263...

output:

1
2
4
8
13
27
43
67
119
215
359
423
847
1167
1935
2447
2975
4511
5023
8639
6911
13759
12991
15103
23295
43775
47999
91647
85375
167295
169343
301695
600703
666239
1332351
2664575
2678911
2687103
5374207
5898495
11731455
11731967
22283263
22807551
21955327
43910399
43911423
44207359
44207360
44469504...

result:

wrong answer 83rd lines differ - expected: '331341256', found: '36373988'