/ Randle /

记录详情

Runtime Error

/in/foo.cc:36:1: warning: multi-line comment [-Wcomment]
 // #define getchar()                                                         \
 ^
# 状态 耗时 内存占用
#1 Accepted 14ms 18.707 MiB
#2 Accepted 14ms 18.766 MiB
#3 Accepted 14ms 18.73 MiB
#4 Wrong Answer 14ms 18.734 MiB
#5 Accepted 14ms 18.746 MiB
#6 Accepted 14ms 18.77 MiB
#7 Accepted 35ms 19.289 MiB
#8 Accepted 319ms 23.941 MiB
#9 Accepted 19ms 19.461 MiB
#10 Accepted 61ms 24.191 MiB
#11 Accepted 103ms 29.062 MiB
#12 Accepted 132ms 32.613 MiB
#13 Accepted 49ms 29.848 MiB
#14 Runtime Error 39ms 32.195 MiB
#15 Runtime Error 50ms 34.152 MiB
#16 Runtime Error 62ms 36.016 MiB
#17 Wrong Answer 210ms 29.473 MiB
#18 Wrong Answer 172ms 28.367 MiB
#19 Wrong Answer 164ms 27.586 MiB
#20 Runtime Error 71ms 35.07 MiB

代码

#include <bits/stdc++.h>
// #pragma GCC optimize(2)
#define int long long
#define pii pair<int, int>
#define tpi tuple<int, int, int, int>
#define il inline
#define p_q priority_queue
#define u_m unordered_map
#define bt bitset
#define rg register
#define rep(i, x, y) for (int i = (x); i <= (y); i++)
#define per(i, x, y) for (int i = (x); i >= (y); i--)

using namespace std;

const int N1 = 300005;
const int N2 = 1000006;
const int mod = 998244353;

using i64 = long long;

void chmin(int& x, int c) {
	x = min(x, c);
}
void chmax(int& x, int c) {
	x = max(x, c);
}

namespace fast_IO {
#define IOSIZE 100000
	int precision = 3, POW[10] = {1,      10,      100,      1000,      10000,
	                              100000, 1000000, 10000000, 100000000, 1000000000
	                             };
	char ibuf[IOSIZE], obuf[IOSIZE], *p1 = ibuf, *p2 = ibuf, *p3 = obuf;
// #ifdef ONLINE_JUDGE
// #define getchar()                                                         \
//     ((p1 == p2) and                                                       \
//              (p2 = (p1 = ibuf) + fread(ibuf, 1, IOSIZE, stdin), p1 == p2) \
//          ? (EOF)                                                          \
// : (*p1++))
// #define putchar(x)
//     ((p3 == obuf + IOSIZE) && (fwrite(obuf, p3 - obuf, 1, stdout), p3 = obuf),
//      *p3++ = x)
// #define isdigit(ch) (ch > 47 && ch < 58)
// #define isspace(ch) (ch < 33)
// #endif
	template <typename T>
	inline T read() {
		T s = 0;
		int w = 1;
		char ch;
		while (ch = getchar(), !isdigit(ch) && (ch != EOF))
			if (ch == 45)
				w = -1;
		if (ch == EOF)
			return 0;
		while (isdigit(ch))
			s = s * 10 + ch - 48, ch = getchar();
		return s * w;
	}
	template <typename T>
	inline bool read(T& s) {
		s = 0;
		int w = 1;
		char ch;
		while (ch = getchar(), !isdigit(ch) && (ch != EOF))
			if (ch == 45)
				w = -1;
		if (ch == EOF)
			return 0;
		while (isdigit(ch))
			s = s * 10 + ch - 48, ch = getchar();
		return s *= w, 1;
	}
	inline bool read(char& s) {
		while (s = getchar(), isspace(s))
			;
		return 1;
	}
	inline bool read(char* s) {
		char ch;
		while (ch = getchar(), isspace(ch))
			;
		if (ch == EOF)
			return 0;
		while (!isspace(ch))
			*s++ = ch, ch = getchar();
		*s = '\000';
		return 1;
	}
	template <typename T>
	inline void print(T x) {
		if (x < 0)
			putchar(45), x = -x;
		if (x > 9)
			print(x / 10);
		putchar(x % 10 + 48);
	}
	inline void print(char x) {
		putchar(x);
	}
	inline void print(char* x) {
		while (*x)
			putchar(*x++);
	}
	inline void print(const char* x) {
		for (int i = 0; x[i]; i++)
			putchar(x[i]);
	}
	inline bool read(std::string& s) {
		s = "";
		char ch;
		while (ch = getchar(), isspace(ch))
			;
		if (ch == EOF)
			return 0;
		while (!isspace(ch))
			s += ch, ch = getchar();
		return 1;
	}
	inline void print(std::string x) {
		for (int i = 0, n = x.size(); i < n; i++)
			putchar(x[i]);
	}
	inline bool read(bool& b) {
		char ch;
		while (ch = getchar(), isspace(ch))
			;
		b = ch ^ 48;
		return 1;
	}
	inline void print(bool b) {
		putchar(b + 48);
	}
	inline bool read(double& x) {
		int a = 0, b = 0;
		char ch = getchar();
		bool f = 0;
		while (ch < 48 || ch > 57) {
			if (ch == 45)
				f = 1;
			ch = getchar();
		}
		while (47 < ch && ch < 58) {
			a = (a << 1) + (a << 3) + (ch ^ 48);
			ch = getchar();
		}
		if (ch != 46) {
			x = f ? -a : a;
			return 1;
		}
		ch = getchar();
		while (47 < ch && ch < 58) {
			b = (b << 1) + (b << 3) + (ch ^ 48), ch = getchar();
		}
		x = b;
		while (x > 1)        x /= 10;
		x /= 10;
		x = f ? -a - x : a + x;
		return 1;
	}
	inline void print(double x) {
		if (x < 0) {
			putchar(45), x = -x;
		}
		x = round((long double)x * POW[precision]) / POW[precision],
		print((long long)x), putchar(46), x -= (long long)(x);
		for (int i = 1; i <= precision; i++)
			x *= 10, putchar(x + 48), x -= (int)x;
	}
	template <typename T, typename... T1>
	inline int read(T& a, T1&... other) {
		return read(a) + read(other...);
	}
	template <typename T, typename... T1>
	inline void print(T a, T1... other) {
		print(a), print(other...);
	}
	struct Fast_IO {
		~Fast_IO() {
			fwrite(obuf, p3 - obuf, 1, stdout);
		}
	} io;
	template <typename T>
	Fast_IO& operator>>(Fast_IO& io, T& b) {
		return read(b), io;
	}
	template <typename T>
	Fast_IO& operator<<(Fast_IO& io, T b) {
		return print(b), io;
	}
#define cout io
#define cin io
}  // namespace fast_IO

namespace temp {
	int fac[N2] = {0};
	il void Cinit(int p) {
		fac[0] = 1;
		for (int i = 1; i < N2; i++) {
			fac[i] = fac[i - 1] * i % p;
		}
	}
	il int qpow(int a, int b, int p) {
		int ans = 1;
		while (b) {
			if (b & 1) {
				ans = ans * a % p;
			}
			a = a * a % p;
			b >>= 1;
		}
		return ans;
	}
	il int C(int n, int m, int p) {
		if (m > n || m < 0) {
			return 0;
		}
		return fac[n] * qpow(fac[m], p - 2, p) % p * qpow(fac[n - m], p - 2, p) % p;
	}
	il int Lucas(int n, int m, int p) {
		if (!m)
			return 1;
		return (C(n % p, m % p, p) * Lucas(n / p, m / p, p)) % p;
	}
	il int GCD(int n, int m, int p) {
		return __gcd(n, m) % p;
	}
	il int LCM(int n, int m, int p) {
		return n * m % p * qpow(GCD(n, m, p), p - 2, p) % p;
	}
}  // namespace temp

//-----------------------------------------------------------------------------------------------//

using namespace fast_IO;

#define debug 1
// #define multi_test_ 1
#define endl '\n'

int _test_ = 1;

namespace zqh {
	const int N = 200005;

	int n, q, fa[N];
	vector<int> g[N];

	struct node {
		int bmax;
		int b_max;
		int b__max;
		int lmax, l_max, l__max, lmax_;
		int fht, tch;
		node() {
			bmax = b__max = b_max = -1;
			fht = tch = lmax = l_max = l__max = lmax_ = 0;
		}
	} b[N];

	void dfs(int u) {
		if (!u) return;
		if (g[u].empty()) {
			b[u].bmax = b[u].fht;
			b[u].lmax = b[u].tch;
			return;
		}
		vector<int> q, p;
//		cout << "hmz AK IOI\n";
		q.push_back(b[u].fht);
		for (int x : g[u]) {
			dfs(x);
			q.push_back(b[x].bmax);
			q.push_back(b[x].b_max);
			q.push_back(b[x].b__max);
			p.push_back(b[x].lmax);
		}
		p.push_back(b[u].tch);
		sort(p.begin(), p.end(), greater<int>());
		sort(q.begin(), q.end(), greater<int>());
		b[u].lmax_ = p[1];
		b[u].lmax = p[0];
		b[u].bmax = q[0];
		b[u].b_max = q[1];
		b[u].b__max = q[2];
	}

	int get_l_max(int u) {
		if (b[u].l_max) return b[u].l_max;
		if (u == 1) {
			return b[u].l_max = 0;
		}
		int t = fa[u];
		int ans = b[t].tch;
		for (int x : g[t]) {
			if (x == u) continue;
			ans = max(ans, b[x].lmax);
			ans = max(ans, b[x].lmax_);
		}
		b[u].l_max = max(ans, get_l_max(t));
		return b[u].l_max;
	}

	int get_l__max(int u) {
		if (b[u].l__max) return b[u].l__max;
		if (u == 1) {
			return b[u].l__max = 0;
		}
		int t = fa[u];
		vector<int> q;
		q.push_back(b[t].tch);
		for (int x : g[t]) {
			if (x == u) continue;
			q.push_back(b[x].lmax);
			q.push_back(b[x].lmax_);
//			ans = max(ans, b[x].lmax);
		}
		q.push_back(get_l_max(t));
		q.push_back(get_l__max(t));
		sort(q.begin(), q.end(), greater<int>());
		b[u].l__max = q[1];
		return b[u].l__max;
	}

	void calc_l_max() {
		rep (i, 1, n) {
			if (g[i].empty())
				get_l_max(i);
		}
	}

	void calc_l__max() {
		rep (i, 1, n) {
			if (g[i].empty())
				get_l__max(i);
		}
	}

	int dfs3(int u, int max_) {
		int ans = 0;
		if (b[u].fht != max_) {
			ans = b[u].fht;
		}
		for (int x : g[u]) {
			ans = max(ans, dfs3(x, max_));
		}
		return ans;
	}

	void init() {
		cin >> n >> q;
		rep (i, 2, n) {
			cin >> fa[i];
			g[fa[i]].push_back(i);
		}
		rep (i, 1, n) {
			cin >> b[i].fht >> b[i].tch;
		}
	}

	void solve() {
		dfs(1);
		calc_l_max();
		calc_l__max();
//		rep (i, 1, n) {
//			cout << b[i].bmax << " " << b[i].b_max << " " << b[i].b__max << " " << b[i].l_max << " " << b[i].l__max << " " << b[i].lmax << endl;
//		}
		while (q--) {
			int s;
			cin >> s;
//			cout << "[" << b[s].bmax << " " << b[s].b_max << " " << b[s].b__max << "]\n";
			int l = b[s].l_max;
//			cout << l << endl;
			if (b[s].b_max == -1) {
				cout << "0\n";
				continue;
			}
			if (b[s].b__max == -1) {
				cout << min(l + b[s].b_max, b[s].bmax) << endl;
				continue;
			}
			if (l == 0 && b[s].bmax == b[s].b_max && b[s].b__max == b[s].bmax) {
				cout << dfs3(s, b[s].bmax) << endl;
				continue;
			}
			if (l + b[s].b_max > b[s].bmax) {
				cout << b[s].bmax << endl;
				continue;
			}
			if (l + b[s].b_max < b[s].bmax) {
				cout << l + b[s].b_max << endl;
				continue;
			}
//			cout << b[s].l__max << endl;
			if (b[s].b__max != b[s].b_max) {
				int ans = min(b[s].bmax, b[s].b__max + l);
				if (b[s].b_max + b[s].l__max > b[s].bmax) ans = max(b[s].bmax, ans);
				else if (b[s].b_max + b[s].l__max < b[s].bmax) ans = max(b[s].b_max + b[s].l__max, ans);
				cout << ans << endl;
			} else {
				cout << min(b[s].bmax, b[s].b_max + b[s].l__max) << endl;
			}
		}
	}

	void main() {
		init();
		solve();
	}
}  // namespace zqh

signed main() {
//     ios::sync_with_stdio(0);
//     cin.tie(0);
//     cout.tie(0);
#ifdef debug
//	freopen("soldier1.in", "r", stdin);
//	freopen("soldier.out", "w", stdout);
#endif
#ifdef multi_test_
	cin >> _test_;
#endif
	while (_test_--) {
		zqh::main();
	}
	return 0;
}
/*

*/

信息

递交者
类型
递交
题目
士兵训练(CQ直辖市noip模拟赛) T3
题目数据
下载
语言
C++
递交时间
2024-09-02 16:18:49
评测时间
2024-09-02 16:18:49
评测机
分数
60
总耗时
1580ms
峰值内存
36.016 MiB