QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#204412#7563. Fun on Treeucup-team918#TL 3ms22200kbC++172.8kb2023-10-07 11:14:192023-10-07 11:14:19

Judging History

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

  • [2023-10-07 11:14:19]
  • 评测
  • 测评结果:TL
  • 用时:3ms
  • 内存:22200kb
  • [2023-10-07 11:14:19]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int B = 450;
int n, q, a[200010], fa[200010], par[200010];
vector<pair<int, int>> e[200010];
int p[400010], cnt, eur[200010], nfd[200010], dfn[200010], tot, out[200010];
void dfs(int now) {
	p[++cnt] = now, eur[now] = cnt;
	nfd[dfn[now] = ++tot] = now;
	for (auto [v, w] : e[now])
		dfs(v), p[++cnt] = {now};
	out[now] = tot;
}
pair<int, int> qq[200010], ch[200010], ans[200010];
struct node {
	int mx, mi, tag;
} t[200010 * 4];
#define lson (now << 1)
#define rson (now << 1 | 1)
#define mid ((l + r) >> 1)
void build(int now, int l = 1, int r = n) {
	if (l == r) {
		t[now].mi = nfd[l], t[now].mx = -a[nfd[l]];
		return;
	}
	build(lson, l, mid), build(rson, mid + 1, r);
	if (t[lson].mx > t[rson].mx || (t[lson].mx == t[rson].mx && t[lson].mi <= t[rson].mi))
		t[now].mx = t[lson].mx, t[now].mi = t[lson].mi;
	else t[now].mx = t[rson].mx, t[now].mi = t[rson].mi;
}
void add(int now, int ql, int qr, int x, int l = 1, int r = n) {
	if (ql <= l && qr >= r) {
		t[now].mx += x, t[now].tag += x;
		return;
	}
	if (ql <= mid) add(lson, ql, qr, x, l, mid);
	if (qr > mid) add(rson, ql, qr, x, mid + 1, r);
	if (t[lson].mx > t[rson].mx || (t[lson].mx == t[rson].mx && t[lson].mi <= t[rson].mi))
		t[now].mx = t[lson].mx, t[now].mi = t[lson].mi;
	else t[now].mx = t[rson].mx, t[now].mi = t[rson].mi;
	t[now].mx += t[now].tag;
}
void dfs2(int now) {
	for (auto [v, w] : e[now])
		add(1, dfn[v], out[v], w), dfs2(v);
}
signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	cin >> n >> q;
	for (int i = 1; i <= n; i++)
		cin >> a[i];
	for (int i = 2; i <= n; i++) {
		cin >> fa[i] >> par[i];
		e[fa[i]].push_back({i, par[i]});
	}
	dfs(1);
	for (int i = 1; i <= q; i++) {
		cin >> qq[i].second >> ch[i].first >> ch[i].second;
		qq[i].first = i, qq[i].second = eur[qq[i].second];
	}
	sort(qq + 1, qq + q + 1, [&](auto x, auto y) {
		if (x.first / B != y.first / B) return x.first < y.first;
		if (x.first / B % 2 == 0) return x.second < y.second;
		return x.second > y.second;
	});
	build(1), dfs2(1);
	int nowx = 1, nowq = 0, res = 0;
	auto mov = [&](int now, int nxt) {
		if (fa[nxt] == now)
			res += par[nxt], add(1, dfn[nxt], out[nxt], -par[nxt] * 2);
		else res -= par[now], add(1, dfn[now], out[now], par[now] * 2);
	};
	auto turn = [&](int i, int flag) {
		add(1, dfn[ch[i].first], out[ch[i].first], ch[i].second * flag);
	};
	for (int i = 1; i <= q; i++) {
		int toq = qq[i].first, tox = qq[i].second;
		while (nowx < tox) mov(p[nowx], p[nowx + 1]), nowx++;
		while (nowx > tox) mov(p[nowx], p[nowx - 1]), nowx--;
		while (nowq < toq) turn(++nowq, -1);
		while (nowq > toq) turn(nowq--, 1);
		ans[toq] = {t[1].mi, t[1].mx + res};
	}
	for (int i = 1; i <= q; i++)
		cout << ans[i].first << " " << ans[i].second << endl;
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

6 6
1 1 4 5 1 4
1 5
2 0
3 2
4 1
5 6
3 2 -100000
1 2 100000
1 1 0
2 2 66
3 1 5
4 4 -3

output:

6 100005
6 10
6 10
1 4
1 -1
1 1

result:

ok 6 lines

Test #2:

score: 0
Accepted
time: 0ms
memory: 22200kb

input:

5 6
-10 0 2 -4 8
1 7
1 1
2 2
2 -2
1 1 100
2 1 -100
1 1 0
4 3 10
2 5 3
5 2 2

output:

4 -87
1 17
4 13
1 19
1 17
1 15

result:

ok 6 lines

Test #3:

score: 0
Accepted
time: 0ms
memory: 22152kb

input:

6 3
0 0 0 0 0 0
1 10
1 10
1 -100
4 10
4 11
1 1 0
4 1 0
1 4 1000

output:

2 10
6 11
2 10

result:

ok 3 lines

Test #4:

score: 0
Accepted
time: 3ms
memory: 16000kb

input:

2 0
1 1
1 3

output:


result:

ok 0 lines

Test #5:

score: -100
Time Limit Exceeded

input:

200000 200000
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 ...

output:


result: