QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#718872#9608. 皮鞋的多项式zlt0 0ms0kbC++144.1kb2024-11-06 21:39:152024-11-06 21:39:19

Judging History

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

  • [2024-11-06 21:39:19]
  • 评测
  • 测评结果:0
  • 用时:0ms
  • 内存:0kb
  • [2024-11-06 21:39:15]
  • 提交

answer

#include <bits/stdc++.h>
#define pb emplace_back
#define fst first
#define scd second
#define mkp make_pair
#define uint unsigned
#define mems(a, x) memset((a), (x), sizeof(a))

using namespace std;
typedef long long ll;
typedef double db;
typedef unsigned long long ull;
typedef long double ldb;
typedef pair<int, int> pii;

const int maxn = 500100;
const int logn = 21;

int test, n, m, ans;
vector<int> G[maxn];
pii a[maxn];

int st[logn][maxn], dfn[maxn], tim, b[maxn], fa[maxn], rnk[maxn];

inline int get(int i, int j) {
	return dfn[i] < dfn[j] ? i : j;
}

inline int qlca(int x, int y) {
	if (x == y) {
		return x;
	}
	x = dfn[x];
	y = dfn[y];
	if (x > y) {
		swap(x, y);
	}
	++x;
	int k = __lg(y - x + 1);
	return get(st[k][x], st[k][y - (1 << k) + 1]);
}

void dfs(int u, int fa) {
	dfn[u] = ++tim;
	st[0][tim] = fa;
	::fa[u] = fa;
	for (int v : G[u]) {
		if (v == fa) {
			continue;
		}
		dfs(v, u);
	}
}

void dfs2(int u, int fa) {
	for (int v : G[u]) {
		if (v == fa) {
			continue;
		}
		dfs2(v, u);
		b[u] += b[v];
	}
}

int c[maxn];
vector<int> vc[maxn];

int sz[maxn], son[maxn], dep[maxn], top[maxn];

int dfs3(int u, int f, int d) {
	sz[u] = 1;
	dep[u] = d;
	son[u] = 0;
	int mx = -1;
	for (int v : G[u]) {
		if (v == f) {
			continue;
		}
		sz[u] += dfs3(v, u, d + 1);
		if (sz[v] > mx) {
			son[u] = v;
			mx = sz[v];
		}
	}
	return sz[u];
}

void dfs4(int u, int tp) {
	top[u] = tp;
	dfn[u] = ++tim;
	rnk[tim] = u;
	if (!son[u]) {
		return;
	}
	dfs4(son[u], tp);
	for (int v : G[u]) {
		if (!dfn[v]) {
			dfs4(v, v);
		}
	}
}

namespace SGT {
	pii a[maxn << 2];
	int tag[maxn << 2];
	
	inline pii get(pii a, pii b) {
		return (a.fst > b.fst || (a.fst == b.fst && a.scd < b.scd)) ? a : b;
	}
	
	inline void pushup(int x) {
		a[x] = get(a[x << 1], a[x << 1 | 1]);
	}
	
	inline void pushtag(int x, int y) {
		a[x].fst += y;
		tag[x] += y;
	}
	
	inline void pushdown(int x) {
		if (!tag[x]) {
			return;
		}
		pushtag(x << 1, tag[x]);
		pushtag(x << 1 | 1, tag[x]);
		tag[x] = 0;
	}
	
	void build(int rt, int l, int r) {
		tag[rt] = 0;
		if (l == r) {
			a[rt] = mkp(b[rnk[l]], l);
			return;
		}
		int mid = (l + r) >> 1;
		build(rt << 1, l, mid);
		build(rt << 1 | 1, mid + 1, r);
		pushup(rt);
	}
	
	void update(int rt, int l, int r, int ql, int qr, int x) {
		if (ql <= l && r <= qr) {
			pushtag(rt, x);
			return;
		}
		pushdown(rt);
		int mid = (l + r) >> 1;
		if (ql <= mid) {
			update(rt << 1, l, mid, ql, qr, x);
		}
		if (qr > mid) {
			update(rt << 1 | 1, mid + 1, r, ql, qr, x);
		}
		pushup(rt);
	}
}

inline void update(int x, int y, int z) {
	while (top[x] != top[y]) {
		if (dep[top[x]] < dep[top[y]]) {
			swap(x, y);
		}
		SGT::update(1, 1, n, dfn[top[x]], dfn[x], z);
		x = fa[top[x]];
	}
	if (dep[x] > dep[y]) {
		swap(x, y);
	}
	SGT::update(1, 1, n, dfn[x], dfn[y], z);
}

void solve() {
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; ++i) {
		vector<int>().swap(G[i]);
		vector<int>().swap(vc[i]);
		b[i] = 0;
	}
	for (int i = 1, u, v; i < n; ++i) {
		scanf("%d%d", &u, &v);
		G[u].pb(v);
		G[v].pb(u);
	}
	tim = 0;
	dfs(1, 0);
	for (int j = 1; (1 << j) <= n; ++j) {
		for (int i = 1; i + (1 << j) - 1 <= n; ++i) {
			st[j][i] = get(st[j - 1][i], st[j - 1][i + (1 << (j - 1))]);
		}
	}
	for (int i = 1; i <= m; ++i) {
		scanf("%d%d", &a[i].fst, &a[i].scd);
		++b[a[i].fst];
		++b[a[i].scd];
		int z = qlca(a[i].fst, a[i].scd);
		--b[z];
		if (z > 1) {
			--b[fa[z]];
		}
		vc[z].pb(i);
		c[i] = 0;
	}
	dfs2(1, -1);
	ans = *max_element(b + 1, b + n + 1);
	printf("%d\n", ans);
	dfs3(1, -1, 1);
	tim = 0;
	for (int i = 1; i <= n; ++i) {
		dfn[i] = 0;
	}
	dfs4(1, 1);
	SGT::build(1, 1, n);
	while (SGT::a[1].fst) {
		int u = rnk[SGT::a[1].scd];
		int i = vc[u].back();
		vc[u].pop_back();
		c[i] = SGT::a[1].fst;
		update(a[i].fst, a[i].scd, -1);
	}
	for (int i = 1; i <= m; ++i) {
		printf("%d%c", c[i], " \n"[i == m]);
	}
}

int main() {
	int T = 1;
	scanf("%d%d", &test, &T);
	while (T--) {
		solve();
	}
	return 0;
}

详细

Subtask #1:

score: 0
Runtime Error

Test #1:

score: 0
Runtime Error

input:

1977 200000
0 883734638
1 497045124 50605999
0 467033353
8 514303373 873913661 21585656 827572829 831599042 669912647 980444584 921677622 90967524
0 111009203
0 980468811
1 965285721 647475291
0 55665482
0 810210109
5 99482052 915734955 536563337 860629716 489661090 127640528
4 452261176 414532348 8...

output:


result:


Subtask #2:

score: 0
Runtime Error

Test #6:

score: 0
Runtime Error

input:

98154 200000
0 948053956
0 149079029
0 871940052
0 888807640
0 284687863
0 760258916
0 916765457
0 121680504
0 210430381
0 162441114
0 640680402
0 269559148
0 706423649
0 619089994
0 776236890
0 44769489
0 863235377
0 283984837
0 251593760
0 863377054
0 40488948
0 100272768
0 628132233
0 18841959
0 ...

output:


result:


Subtask #3:

score: 0
Runtime Error

Test #8:

score: 0
Runtime Error

input:

97330 200000
2 356080749 854511831 888131963
0 533633039
0 260190631
0 217335008
2 998111375 903316988 891866314
0 507509609
0 556810297
1 190927168 988903728
1 270553180 387224380
0 360295480
0 775464651
0 755424805
0 71030175
0 690904984
0 702271750
0 360541906
0 903384679
0 769283169
0 6990072
0 ...

output:


result:


Subtask #4:

score: 0
Runtime Error

Test #13:

score: 0
Runtime Error

input:

50000 50000
1 610345459 691411093
1 476654936 529767753
1 8856530 640833948
1 961473497 456987897
1 462733802 114971681
1 662244461 415955667
1 717992437 907944693
1 346097988 176526535
1 805826501 182409272
1 33105050 971783530
1 45972429 258997374
1 964103067 796756441
1 958668755 735146502
1 9543...

output:


result:


Subtask #5:

score: 0
Runtime Error

Test #21:

score: 0
Runtime Error

input:

19854 20000
1 150513542 240180212
0 987796281
0 90054116
1 191708494 438440429
0 192815969
0 867402303
1 531762469 210966860
2 95662022 345368425 199338548
0 269135053
0 816253511
0 66854944
0 319745952
0 202288549
0 492853777
0 410846691
0 824737426
0 821545014
0 72050044
0 534080091
1 542636124 52...

output:


result:


Subtask #6:

score: 0
Skipped

Dependency #1:

0%