提交记录 9054


用户 题目 状态 得分 用时 内存 语言 代码长度
eecs noip18f. 【NOIP2018】保卫王国 Accepted 100 352.531 ms 24160 KB C++ 2.18 KB
提交时间 评测时间
2019-04-09 17:31:43 2020-08-01 01:30:07
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const int maxn = 100010;
int n, m, fa[maxn], ch[maxn][2];
ll p[maxn], dp[maxn][2];
vector<int> G[maxn];
struct mat { ll a, b, c, d; mat() { a = b = c = d = 1e18; } } f[maxn], g[maxn];

bool get(int v) { return v == ch[fa[v]][1]; }
bool is_root(int v) { return ch[fa[v]][0] ^ v && ch[fa[v]][1] ^ v; }

mat operator * (mat &A, mat &B) {
	mat C;
	C.a = min(A.a + B.a, A.b + B.c), C.b = min(A.a + B.b, A.b + B.d);
	C.c = min(A.c + B.a, A.d + B.c), C.d = min(A.c + B.b, A.d + B.d);
	return C;
}

void dfs(int v) {
	dp[v][1] = p[v];
	for (int i = 0, u; i < G[v].size(); i++) {
		if ((u = G[v][i]) ^ fa[v]) {
			fa[u] = v, dfs(u);
			dp[v][0] += dp[u][1], dp[v][1] += min(dp[u][0], dp[u][1]);
		}
	}
	g[v].b = dp[v][0], g[v].c = dp[v][1];
}

void maintain(int v) {
	g[v].d = g[v].c, f[v] = g[v];
	if (ch[v][0]) f[v] = f[ch[v][0]] * f[v];
	if (ch[v][1]) f[v] = f[v] * f[ch[v][1]];
}

void rotate(int v) {
	int old = fa[v], oldf = fa[old], chk = get(v);
	if (!is_root(old)) ch[oldf][get(old)] = v;
	ch[old][chk] = ch[v][chk ^ 1], fa[ch[old][chk]] = old;
	ch[v][chk ^ 1] = old, fa[old] = v, fa[v] = oldf;
	maintain(old), maintain(v);
}

void splay(int v) {
	for (int f = fa[v]; !is_root(v); rotate(v), f = fa[v]) {
		if (!is_root(f)) rotate(get(v) ^ get(f) ? v : f);
	}
}

void access(int v) {
	for (int u = 0, t; v; v = fa[u = v]) {
		splay(v);
		if (t = ch[v][1]) g[v].b += f[t].d, g[v].c += min(f[t].b, f[t].d);
		if (ch[v][1] = u) g[v].b -= f[u].d, g[v].c -= min(f[u].b, f[u].d);
		maintain(v);
	}
}

void upd(int v, ll w) {
	access(v), splay(v), g[v].c += w - p[v];
	p[v] = w, maintain(v);
}

int main() {
	scanf("%d %d %*s", &n, &m);
	for (int i = 1; i <= n; i++) {
		scanf("%lld", &p[i]);
	}
	for (int i = 1, u, v; i < n; i++) {
		scanf("%d %d", &u, &v);
		G[u].push_back(v), G[v].push_back(u);
	}
	dfs(1);
	for (int i = 1, a, b, x, y; i <= m; i++) {
		scanf("%d %d %d %d", &a, &x, &b, &y);
		ll olda = p[a], oldb = p[b], ans = 0;
		if (x) upd(a, 0), ans += olda; else upd(a, 2e15);
		if (y) upd(b, 0), ans += oldb; else upd(b, 2e15);
		splay(1), ans += min(f[1].b, f[1].d);
		printf("%lld\n", ans > 1e15 ? -1 : ans);
		upd(a, olda), upd(b, oldb);
	}
	return 0;
}

CompilationN/AN/ACompile OKScore: N/A

Testcase #11.097 ms8 MB + 460 KBAcceptedScore: 4

Testcase #21.082 ms8 MB + 460 KBAcceptedScore: 4

Testcase #31.087 ms8 MB + 460 KBAcceptedScore: 4

Testcase #41.074 ms8 MB + 460 KBAcceptedScore: 4

Testcase #51.243 ms8 MB + 476 KBAcceptedScore: 4

Testcase #61.229 ms8 MB + 476 KBAcceptedScore: 4

Testcase #71.242 ms8 MB + 468 KBAcceptedScore: 4

Testcase #85.53 ms8 MB + 764 KBAcceptedScore: 4

Testcase #95.493 ms8 MB + 764 KBAcceptedScore: 4

Testcase #105.248 ms8 MB + 676 KBAcceptedScore: 4

Testcase #115.259 ms8 MB + 676 KBAcceptedScore: 4

Testcase #12218.752 ms23 MB + 608 KBAcceptedScore: 4

Testcase #13218.587 ms23 MB + 608 KBAcceptedScore: 4

Testcase #14300.257 ms23 MB + 412 KBAcceptedScore: 4

Testcase #15300.201 ms23 MB + 416 KBAcceptedScore: 4

Testcase #16300.459 ms23 MB + 416 KBAcceptedScore: 4

Testcase #17352.531 ms23 MB + 608 KBAcceptedScore: 4

Testcase #18112.757 ms16 MB + 72 KBAcceptedScore: 4

Testcase #19112.723 ms16 MB + 72 KBAcceptedScore: 4

Testcase #20197.154 ms19 MB + 452 KBAcceptedScore: 4

Testcase #21197.361 ms19 MB + 452 KBAcceptedScore: 4

Testcase #22236.404 ms19 MB + 256 KBAcceptedScore: 4

Testcase #23321.531 ms19 MB + 448 KBAcceptedScore: 4

Testcase #24321.694 ms19 MB + 452 KBAcceptedScore: 4

Testcase #25322.007 ms19 MB + 464 KBAcceptedScore: 4


Judge Duck Online | 评测鸭在线
Server Time: 2024-12-05 10:40:38 | Loaded in 2 ms | Server Status
个人娱乐项目,仅供学习交流使用 | 捐赠