QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#187111#3847. AirlinerealIyxiang#WA 48ms37012kbC++142.2kb2023-09-24 14:41:402023-09-24 14:41:40

Judging History

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

  • [2023-09-24 14:41:40]
  • 评测
  • 测评结果:WA
  • 用时:48ms
  • 内存:37012kb
  • [2023-09-24 14:41:40]
  • 提交

answer

#include <bits/stdc++.h>

#define rep(i, x, y) for(int i = (x); i <= (y); i++)
#define per(i, x, y) for(int i = (x); i >= (y); i--)
#define eb emplace_back

using namespace std;

using vec = vector < int >;
using ll = long long;

template < typename T > void chkmin(T &x, const T &y) { x = x < y ? x : y; }

const int N = 1e6 + 10;
const int M = 1e7 + 10;

int n, q;

#define in read()

int read() { int x; scanf("%d", &x); return x; }

vec G[N];
int siz[N], dep[N], fa[N];

void dfs(int x, int p) {
	fa[x] = p; dep[x] = dep[p] + 1; siz[x] = 1;
	for(auto y : G[x]) if(y ^ p) dfs(y, x), siz[x] += siz[y];
}

int id[M], tot, tsz[M];
int ssz[M];

int lca(int x, int y) {
	if(dep[x] < dep[y]) swap(x, y);
	tot = 0;
	vec pot, tpot;
	while(dep[x] > dep[y]) {
		pot.eb(x), x = fa[x];
	}
	while(x != y) pot.eb(x), tpot.eb(y), x = fa[x], y = fa[y];
	pot.eb(x); reverse(pot.begin(), pot.end()), reverse(tpot.begin(), tpot.end());
	tot = pot.size() + tpot.size();
	rep(i, 1, tot) {
		if(pot.size()) id[i] = pot.back(), pot.pop_back();
		else if(tpot.size()) id[i] = tpot.back(), tpot.pop_back();
	}
	rep(i, 1, tot) tsz[i] = siz[id[i]];
	tsz[0] = tsz[tot + 1] = 0;
	bool isl = 1;
	rep(i, 1, tot) {
		if(isl && i > 1) tsz[i] -= siz[id[i - 1]];
		if(!isl && i < tot) tsz[i] -= siz[id[i + 1]];
		if(id[i] == x) tsz[i] -= siz[id[i + 1]], isl = 0, tsz[i] += n - siz[x];
	}
	rep(i, 1, tot) ssz[i] = ssz[i - 1] + tsz[i];
	return x;
}

int main() {
#ifdef YJR_2333_TEST
	freopen("1.in", "r", stdin);
#endif
	n = in, q = in; rep(i, 1, n - 1) { int u = in, v = in; G[u].eb(v), G[v].eb(u); }
	dfs(1, 0);
	rep(i, 1, q) {
		int x = in, y = in;
		int L = lca(x, y); // unused
		ll ans = 0;
		//cerr << L << endl;
		//rep(x, 1, tot) cerr << id[x] << " "; cerr << endl;
		//rep(x, 1, tot) cerr << tsz[x] << " "; cerr << endl;
		rep(x, 1, tot) {
			int l = 1, r = (2 * x - tot - 1) / 2;
			//cerr << x << " " << l << " " << r << endl;
			if(l <= r) ans += (ssz[r] - ssz[l - 1]) * 1ll * tsz[x];
			l = (2 * x + tot + 2) >> 1, r =  tot;
			//cerr << x << " " << l << " " << r << endl;
			if(l <= r) ans += (ssz[r] - ssz[l - 1]) * 1ll * tsz[x];
		} ans /= 2; printf("%lld\n", ans);
	}
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 48ms
memory: 37012kb

input:

1000 100000
552 9
456 720
540 790
628 695
848 478
66 268
798 360
773 277
116 471
874 792
912 784
502 831
359 965
925 434
677 629
271 670
76 755
92 200
814 422
922 266
617 44
480 331
18 662
153 753
669 491
368 187
99 867
476 808
774 509
98 147
724 478
447 182
923 469
881 665
674 589
770 613
436 310
8...

output:

30821
44549
41828
20298
11139
6764
6347
36792
8954
19293
26479
10438
18244
3469
18826
8752
29782
27867
1906
33626
16175
19920
44549
6419
6610
12303
7208
7257
24259
32981
9282
5610
17197
9803
25439
24236
18641
3161
15528
24407
30680
21727
24466
5524
7167
18980
19054
4129
19928
6541
17791
4726
30440
4...

result:

wrong answer 1st lines differ - expected: '8905', found: '30821'