Submission #924266

# Submission time Handle Problem Language Result Execution time Memory
924266 2024-02-08T18:24:22 Z 42kangaroo Inside information (BOI21_servers) C++17
100 / 100
941 ms 77840 KB
//
// Created by 42kangaroo on 07/02/2024.
//
#include "bits/stdc++.h"

using namespace std;

template<typename T>
using g_t = vector<vector<T>>;

struct Que {
	char c;
	int f, s;
	string an;
};

struct BinL {
	bool asc, desc;
	int p, upVal, downVal;
};

struct Ed {
	int a, b, w;
};

struct FenT {
	vector<int> a, cooC;

	void inc(int k, int v) {
		int p = std::lower_bound(cooC.begin(), cooC.end(), k) - cooC.begin();
		for (int i = p + 1; i < a.size(); i += i & -i) {
			a[i] += v;
		}
	}

	int get(int k) {
		int res = 0;
		if (k < 0) return 0;
		int p = std::lower_bound(cooC.begin(), cooC.end(), k) - cooC.begin();
		for (int i = p + 1; i > 0; i -= i & -i) {
			res += a[i];
		}
		return res;
	}

};

array<int, 120005> logT;
g_t<BinL> binL;
g_t<Ed> g;
vector<int> de;
vector<int> pCD;
vector<int> dCD;
vector<FenT> fCD;

void dfs(int n, int p, int d, vector<pair<int, int>> &pa) {
	pa[n].first = p;
	de[n] = d;
	for (auto &e: g[n]) {
		if (e.b == p) {
			pa[n].second = e.w;
			continue;
		}
		dfs(e.b, n, d + 1, pa);
	}
}

BinL combine(const BinL &l, const BinL &r) {
	BinL res{};
	res.p = r.p;
	res.upVal = r.upVal;
	res.downVal = l.downVal;
	res.asc = l.asc && r.asc && l.upVal > r.downVal;
	res.desc = l.desc && r.desc && l.upVal < r.downVal;
	return res;
}

BinL reverse(const BinL& in) {
	BinL res{};
	res.p = -1;
	res.upVal = in.downVal;
	res.downVal = in.upVal;
	res.asc = in.desc;
	res.desc = in.asc;
	return res;
}

void binLPrep(vector<pair<int, int>> &p) {
	binL = g_t<BinL>(logT[p.size()] + 1, vector<BinL>(p.size()));
	for (int i = 0; i < p.size(); ++i) {
		binL[0][i] = {true, true, p[i].first, p[i].second, p[i].second};
	}
	for (int i = 1; i < binL.size(); ++i) {
		for (int j = 0; j < p.size(); ++j) {
			binL[i][j] = combine(binL[i - 1][j], binL[i - 1][binL[i - 1][j].p]);
		}
	}
}

pair<bool, BinL> connected(int i, int st, int en) {
	if (st == en) return {true, {true, true, -1, -1, -1}};
	bool sw = false;
	if (de[st] < de[en]) {
		sw = true;
		swap(st, en);
	}
	int k = de[st] - de[en];
	BinL biSt{true, true, -1, -1, -1};
	BinL biEn{true, true, -1, -1, -1};
	for (int j = 0; k; k >>= 1, ++j) {
		if (k & 1) {
			if (biSt.p == -1) {
				biSt = binL[j][st];
			} else {
				biSt = combine(biSt, binL[j][st]);
			}
			st = biSt.p;
		}
	}
	if (st == en) {
		if (sw) {
			return {biSt.asc && biSt.downVal <= i, reverse(biSt)};
		} else {
			return {biSt.desc && biSt.upVal <= i, biSt};
		}
	}

	for (int j = binL.size() - 1; j >= 0; j--) {
		if (binL[j][st].p != binL[j][en].p) {
			if (biSt.p == -1) {
				biSt = binL[j][st];
			} else {
				biSt = combine(biSt, binL[j][st]);
			}
			st = biSt.p;
			if (biEn.p == -1) {
				biEn = binL[j][en];
			} else {
				biEn = combine(biEn, binL[j][en]);
			}
			en = biEn.p;
		}
	}
	if (biSt.p == -1) {
		biSt = binL[0][st];
	} else {
		biSt = combine(biSt, binL[0][st]);
	}
	if (biEn.p == -1) {
		biEn = binL[0][en];
	} else {
		biEn = combine(biEn, binL[0][en]);
	}
	if (sw) {
		swap(biEn, biSt);
	}
	auto reVbiEn = reverse(biEn);
	return {biSt.desc && biEn.asc && biSt.upVal < biEn.upVal && biEn.downVal <= i, combine(biSt, reVbiEn)};
}

int dfsSi(int n, int p, vector<bool> &vis, vector<int> &si) {
	if (vis[n]) return 0;
	si[n] = 1;
	for (auto &e: g[n]) {
		if (e.b == p) continue;
		si[n] += dfsSi(e.b, n, vis, si);
	}
	return si[n];
}

int dfsCentro(int n, int p, int to, vector<bool> &vis, vector<int> &si) {
	for (auto &e: g[n]) {
		if (vis[e.b] || e.b == p) continue;
		if (2 * si[e.b] >= to) return dfsCentro(e.b, n, to, vis, si);
	}
	return n;
}

void CenTroDecomp(int n, int p, int d, vector<bool> &vis, vector<int> &si) {
	dfsSi(n, n, vis, si);
	int c = dfsCentro(n, n, si[n], vis, si);
	if (p == -1) p = c;
	pCD[c] = p;
	vis[c] = true;
	dCD[c] = d;
	for (auto &e: g[c]) {
		if (!vis[e.b]) {
			fCD[c].cooC.push_back(e.w);
			CenTroDecomp(e.b, c, d + 1, vis, si);
		}
	}
	std::sort(fCD[c].cooC.begin(), fCD[c].cooC.end());
	fCD[c].a.resize(fCD[c].cooC.size() + 2, 0);
}

void updCD(Ed e) {
	Ed oE = e;
	if (dCD[e.a] < dCD[e.b]) swap(e.a, e.b);
	while (dCD[e.a] != dCD[e.b]) {
		e.a = pCD[e.a];
	}
	while (e.a != e.b) {
		e.a = pCD[e.a];
		e.b = pCD[e.b];
	}
	for (;;) {
		auto [con, bi] = connected(e.w - 1, e.a, oE.a);
		if (con) {
			auto re = connected(e.w, e.a, oE.b);
			con = re.first;
			bi = re.second;
		} else {
			auto re = connected(e.w, e.a, oE.a);
			con = re.first;
			bi = re.second;
		}
		if (con) fCD[e.a].inc(bi.downVal, 1);
		if (pCD[e.a] == e.a) break;
		e.a = pCD[e.a];
	}
}

int numWith(int c, int i) {
	int res = 0;
	int oC = c;
	for (;;) {
		auto [con, bi] = connected(i, oC, c);
		if (con) {
			res += fCD[c].get(1e6) - fCD[c].get(bi.upVal) + 1;
		}
		if (pCD[c] == c) break;
		c = pCD[c];
	}
	return res;
}

signed main() {
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	int n, k;
	cin >> n >> k;
	logT[1] = 0;
	for (int i = 2; i < logT.size(); ++i) {
		logT[i] = logT[i / 2] + 1;
	}
	g = g_t<Ed>(n);
	vector<Que> que(k + n - 1);
	for (int i = 0; i < k + n - 1; ++i) {
		char c;
		cin >> c;
		if (c == 'S') {
			int a, b;
			cin >> a >> b;
			--a;
			--b;
			g[a].push_back({a, b, i});
			g[b].push_back({b, a, i});
			que[i] = {c, a, b, ""};
		}
		if (c == 'Q') {
			int a, b;
			cin >> a >> b;
			--a;
			--b;
			que[i] = {c, a, b, ""};
		}
		if (c == 'C') {
			int a;
			cin >> a;
			--a;
			que[i] = {c, a, -1, ""};
		}
	}
	vector<pair<int, int>> p(n, {0, 0});
	de = vector<int>(n);
	dfs(0, 0, 0, p);
	binLPrep(p);
	vector<bool> vis(n, false);
	vector<int> si(n, 0);
	pCD.resize(n);
	dCD.resize(n);
	fCD.resize(n);
	CenTroDecomp(0, -1, 0, vis, si);
	for (int i = 0; i < que.size(); ++i) {
		if (que[i].c == 'S') {
			updCD({que[i].f, que[i].s, i});
		}
		if (que[i].c == 'Q') {
			que[i].an = (connected(i, que[i].s, que[i].f).first ? "yes" : "no");
		}
		if (que[i].c == 'C') {
			que[i].an = to_string(numWith(que[i].f, i));
		}
	}
	for (auto &qu: que) {
		if (!qu.an.empty()) cout << qu.an << "\n";
	}
}

Compilation message

servers.cpp: In member function 'void FenT::inc(int, int)':
servers.cpp:31:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   31 |   for (int i = p + 1; i < a.size(); i += i & -i) {
      |                       ~~^~~~~~~~~~
servers.cpp: In function 'void binLPrep(std::vector<std::pair<int, int> >&)':
servers.cpp:90:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   90 |  for (int i = 0; i < p.size(); ++i) {
      |                  ~~^~~~~~~~~~
servers.cpp:93:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<BinL, std::allocator<BinL> >, std::allocator<std::vector<BinL, std::allocator<BinL> > > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   93 |  for (int i = 1; i < binL.size(); ++i) {
      |                  ~~^~~~~~~~~~~~~
servers.cpp:94:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   94 |   for (int j = 0; j < p.size(); ++j) {
      |                   ~~^~~~~~~~~~
servers.cpp: In function 'int main()':
servers.cpp:243:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::array<int, 120005>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  243 |  for (int i = 2; i < logT.size(); ++i) {
      |                  ~~^~~~~~~~~~~~~
servers.cpp:284:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Que>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  284 |  for (int i = 0; i < que.size(); ++i) {
      |                  ~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 23 ms 6748 KB Output is correct
2 Correct 39 ms 8540 KB Output is correct
3 Correct 30 ms 8540 KB Output is correct
4 Correct 46 ms 8528 KB Output is correct
5 Correct 38 ms 8768 KB Output is correct
6 Correct 29 ms 8540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 6748 KB Output is correct
2 Correct 39 ms 8540 KB Output is correct
3 Correct 30 ms 8540 KB Output is correct
4 Correct 46 ms 8528 KB Output is correct
5 Correct 38 ms 8768 KB Output is correct
6 Correct 29 ms 8540 KB Output is correct
7 Correct 22 ms 6748 KB Output is correct
8 Correct 46 ms 9556 KB Output is correct
9 Correct 31 ms 9748 KB Output is correct
10 Correct 75 ms 9704 KB Output is correct
11 Correct 68 ms 9812 KB Output is correct
12 Correct 28 ms 9700 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 6748 KB Output is correct
2 Correct 161 ms 65600 KB Output is correct
3 Correct 177 ms 65904 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 6748 KB Output is correct
2 Correct 161 ms 65600 KB Output is correct
3 Correct 177 ms 65904 KB Output is correct
4 Correct 20 ms 6748 KB Output is correct
5 Correct 159 ms 68276 KB Output is correct
6 Correct 102 ms 67508 KB Output is correct
7 Correct 116 ms 67820 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 6748 KB Output is correct
2 Correct 597 ms 74696 KB Output is correct
3 Correct 561 ms 74748 KB Output is correct
4 Correct 377 ms 74748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 6748 KB Output is correct
2 Correct 597 ms 74696 KB Output is correct
3 Correct 561 ms 74748 KB Output is correct
4 Correct 377 ms 74748 KB Output is correct
5 Correct 21 ms 6748 KB Output is correct
6 Correct 727 ms 77628 KB Output is correct
7 Correct 549 ms 77676 KB Output is correct
8 Correct 836 ms 77052 KB Output is correct
9 Correct 822 ms 77064 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 6740 KB Output is correct
2 Correct 217 ms 66944 KB Output is correct
3 Correct 257 ms 67124 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 6740 KB Output is correct
2 Correct 217 ms 66944 KB Output is correct
3 Correct 257 ms 67124 KB Output is correct
4 Correct 22 ms 6740 KB Output is correct
5 Correct 258 ms 69884 KB Output is correct
6 Correct 301 ms 70144 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 6748 KB Output is correct
2 Correct 584 ms 74752 KB Output is correct
3 Correct 567 ms 74664 KB Output is correct
4 Correct 379 ms 74736 KB Output is correct
5 Correct 21 ms 6748 KB Output is correct
6 Correct 215 ms 67068 KB Output is correct
7 Correct 263 ms 67084 KB Output is correct
8 Correct 449 ms 66816 KB Output is correct
9 Correct 437 ms 67068 KB Output is correct
10 Correct 630 ms 70428 KB Output is correct
11 Correct 669 ms 70044 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 6748 KB Output is correct
2 Correct 584 ms 74752 KB Output is correct
3 Correct 567 ms 74664 KB Output is correct
4 Correct 379 ms 74736 KB Output is correct
5 Correct 21 ms 6748 KB Output is correct
6 Correct 215 ms 67068 KB Output is correct
7 Correct 263 ms 67084 KB Output is correct
8 Correct 449 ms 66816 KB Output is correct
9 Correct 437 ms 67068 KB Output is correct
10 Correct 630 ms 70428 KB Output is correct
11 Correct 669 ms 70044 KB Output is correct
12 Correct 21 ms 6744 KB Output is correct
13 Correct 744 ms 77712 KB Output is correct
14 Correct 529 ms 77840 KB Output is correct
15 Correct 797 ms 77248 KB Output is correct
16 Correct 822 ms 77296 KB Output is correct
17 Correct 24 ms 7760 KB Output is correct
18 Correct 256 ms 69888 KB Output is correct
19 Correct 287 ms 69888 KB Output is correct
20 Correct 565 ms 69884 KB Output is correct
21 Correct 540 ms 70012 KB Output is correct
22 Correct 941 ms 72192 KB Output is correct
23 Correct 931 ms 73984 KB Output is correct
24 Correct 710 ms 73984 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 6748 KB Output is correct
2 Correct 37 ms 8484 KB Output is correct
3 Correct 29 ms 8440 KB Output is correct
4 Correct 46 ms 8532 KB Output is correct
5 Correct 39 ms 8752 KB Output is correct
6 Correct 28 ms 8528 KB Output is correct
7 Correct 21 ms 6744 KB Output is correct
8 Correct 164 ms 65716 KB Output is correct
9 Correct 165 ms 65684 KB Output is correct
10 Correct 21 ms 6748 KB Output is correct
11 Correct 561 ms 74656 KB Output is correct
12 Correct 565 ms 74748 KB Output is correct
13 Correct 379 ms 74752 KB Output is correct
14 Correct 22 ms 6748 KB Output is correct
15 Correct 211 ms 67056 KB Output is correct
16 Correct 252 ms 67072 KB Output is correct
17 Correct 454 ms 66912 KB Output is correct
18 Correct 448 ms 67072 KB Output is correct
19 Correct 590 ms 70564 KB Output is correct
20 Correct 665 ms 69796 KB Output is correct
21 Correct 174 ms 65648 KB Output is correct
22 Correct 174 ms 65584 KB Output is correct
23 Correct 261 ms 66556 KB Output is correct
24 Correct 266 ms 66524 KB Output is correct
25 Correct 519 ms 70120 KB Output is correct
26 Correct 286 ms 66376 KB Output is correct
27 Correct 253 ms 66308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 6748 KB Output is correct
2 Correct 37 ms 8484 KB Output is correct
3 Correct 29 ms 8440 KB Output is correct
4 Correct 46 ms 8532 KB Output is correct
5 Correct 39 ms 8752 KB Output is correct
6 Correct 28 ms 8528 KB Output is correct
7 Correct 21 ms 6744 KB Output is correct
8 Correct 164 ms 65716 KB Output is correct
9 Correct 165 ms 65684 KB Output is correct
10 Correct 21 ms 6748 KB Output is correct
11 Correct 561 ms 74656 KB Output is correct
12 Correct 565 ms 74748 KB Output is correct
13 Correct 379 ms 74752 KB Output is correct
14 Correct 22 ms 6748 KB Output is correct
15 Correct 211 ms 67056 KB Output is correct
16 Correct 252 ms 67072 KB Output is correct
17 Correct 454 ms 66912 KB Output is correct
18 Correct 448 ms 67072 KB Output is correct
19 Correct 590 ms 70564 KB Output is correct
20 Correct 665 ms 69796 KB Output is correct
21 Correct 174 ms 65648 KB Output is correct
22 Correct 174 ms 65584 KB Output is correct
23 Correct 261 ms 66556 KB Output is correct
24 Correct 266 ms 66524 KB Output is correct
25 Correct 519 ms 70120 KB Output is correct
26 Correct 286 ms 66376 KB Output is correct
27 Correct 253 ms 66308 KB Output is correct
28 Correct 23 ms 6736 KB Output is correct
29 Correct 45 ms 9556 KB Output is correct
30 Correct 31 ms 9564 KB Output is correct
31 Correct 74 ms 9556 KB Output is correct
32 Correct 67 ms 9884 KB Output is correct
33 Correct 28 ms 9560 KB Output is correct
34 Correct 21 ms 8016 KB Output is correct
35 Correct 152 ms 68276 KB Output is correct
36 Correct 107 ms 67508 KB Output is correct
37 Correct 105 ms 67776 KB Output is correct
38 Correct 21 ms 7700 KB Output is correct
39 Correct 708 ms 77760 KB Output is correct
40 Correct 541 ms 77676 KB Output is correct
41 Correct 776 ms 77052 KB Output is correct
42 Correct 783 ms 77060 KB Output is correct
43 Correct 27 ms 8020 KB Output is correct
44 Correct 252 ms 69908 KB Output is correct
45 Correct 283 ms 69888 KB Output is correct
46 Correct 532 ms 70012 KB Output is correct
47 Correct 589 ms 69916 KB Output is correct
48 Correct 936 ms 72288 KB Output is correct
49 Correct 849 ms 73868 KB Output is correct
50 Correct 698 ms 73984 KB Output is correct
51 Correct 139 ms 68720 KB Output is correct
52 Correct 114 ms 68472 KB Output is correct
53 Correct 121 ms 68116 KB Output is correct
54 Correct 110 ms 68820 KB Output is correct
55 Correct 113 ms 68464 KB Output is correct
56 Correct 259 ms 69476 KB Output is correct
57 Correct 498 ms 72904 KB Output is correct
58 Correct 419 ms 69120 KB Output is correct
59 Correct 277 ms 69628 KB Output is correct