結果

問題 No.2311 [Cherry 5th Tune] Cherry Month
ユーザー square1001square1001
提出日時 2023-03-25 12:45:22
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 339 ms / 4,600 ms
コード長 7,901 bytes
コンパイル時間 3,102 ms
コンパイル使用メモリ 129,460 KB
実行使用メモリ 73,840 KB
最終ジャッジ日時 2023-08-22 02:03:37
合計ジャッジ時間 21,379 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 274 ms
51,184 KB
testcase_03 AC 124 ms
27,352 KB
testcase_04 AC 118 ms
22,364 KB
testcase_05 AC 117 ms
22,068 KB
testcase_06 AC 149 ms
26,412 KB
testcase_07 AC 128 ms
23,892 KB
testcase_08 AC 221 ms
47,380 KB
testcase_09 AC 193 ms
38,028 KB
testcase_10 AC 175 ms
34,400 KB
testcase_11 AC 194 ms
40,796 KB
testcase_12 AC 243 ms
45,232 KB
testcase_13 AC 232 ms
46,460 KB
testcase_14 AC 231 ms
47,060 KB
testcase_15 AC 244 ms
47,880 KB
testcase_16 AC 145 ms
27,256 KB
testcase_17 AC 251 ms
47,152 KB
testcase_18 AC 225 ms
43,792 KB
testcase_19 AC 244 ms
48,436 KB
testcase_20 AC 238 ms
47,136 KB
testcase_21 AC 201 ms
35,132 KB
testcase_22 AC 197 ms
35,040 KB
testcase_23 AC 236 ms
49,856 KB
testcase_24 AC 222 ms
48,120 KB
testcase_25 AC 217 ms
40,736 KB
testcase_26 AC 267 ms
52,360 KB
testcase_27 AC 297 ms
73,788 KB
testcase_28 AC 304 ms
73,840 KB
testcase_29 AC 301 ms
73,724 KB
testcase_30 AC 146 ms
29,732 KB
testcase_31 AC 145 ms
29,700 KB
testcase_32 AC 149 ms
29,716 KB
testcase_33 AC 244 ms
56,356 KB
testcase_34 AC 234 ms
56,268 KB
testcase_35 AC 237 ms
56,356 KB
testcase_36 AC 321 ms
59,008 KB
testcase_37 AC 314 ms
59,272 KB
testcase_38 AC 316 ms
58,724 KB
testcase_39 AC 331 ms
58,680 KB
testcase_40 AC 304 ms
58,384 KB
testcase_41 AC 339 ms
58,740 KB
testcase_42 AC 312 ms
58,040 KB
testcase_43 AC 338 ms
59,220 KB
testcase_44 AC 313 ms
60,084 KB
testcase_45 AC 313 ms
60,176 KB
testcase_46 AC 218 ms
51,348 KB
testcase_47 AC 316 ms
60,288 KB
testcase_48 AC 276 ms
58,840 KB
testcase_49 AC 302 ms
58,616 KB
testcase_50 AC 280 ms
58,628 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

class query {
public:
	int tp, t, x, y;
	query() : tp(-1), t(-1), x(-1), y(-1) {}
	query(int tp_, int t_, int x_, int y_) : tp(tp_), t(t_), x(x_), y(y_) {}
	bool operator<(const query& q) const {
		return t < q.t;
	}
};

vector<long long> solve(int N, int M, int Q, const vector<long long>& TA, const vector<int>& TP, const vector<int>& X, const vector<int>& Y, const vector<int>& S, const vector<int>& I) {
	// step #1. preparation
	vector<query> qs(M + Q);
	for (int i = 0; i < M; i++) {
		qs[i] = query(TP[i], i * 2 + 1, X[i], Y[i]);
	}
	for (int i = 0; i < Q; i++) {
		qs[M + i] = query(5, S[i] * 2, I[i], i);
	}
	sort(qs.begin(), qs.end());

	// step #2. renumber vertices
	vector<vector<int> > group(N);
	vector<int> root(N), id(N), sz(N);
	for (int i = 0; i < N; i++) {
		group[i] = { i };
		root[i] = i;
		id[i] = 0;
		sz[i] = 1;
	}
	for (query q : qs) {
		if (q.tp == 1) {
			int rx = root[q.x];
			int ry = root[q.y];
			if (rx != ry) {
				if (group[rx].size() < group[ry].size()) {
					swap(rx, ry);
				}
				group[rx].insert(group[rx].end(), group[ry].begin(), group[ry].end());
				for (int j : group[ry]) {
					id[j] += sz[rx];
				}
				sz[rx] += sz[ry];
				for (int j : group[ry]) {
					root[j] = rx;
				}
				group[ry].clear();
			}
		}
	}
	int offset = 0;
	for (int i = 0; i < N; i++) {
		for (int j : group[i]) {
			id[j] += offset;
		}
		offset += group[i].size();
	}
	for (query &q : qs) {
		if (q.tp == 1) {
			q.x = id[q.x];
			q.y = id[q.y];
		}
		else {
			q.x = id[q.x];
		}
	}
	vector<long long> A(N);
	for (int i = 0; i < N; i++) {
		A[id[i]] = TA[i];
	}

	// step #3. find left/right of update queries
	vector<int> idl(N), idr(N);
	for (int i = 0; i < N; i++) {
		group[i] = { i };
		root[i] = i;
		idl[i] = i;
		idr[i] = i + 1;
	}
	vector<int> l(M + Q, -1), r(M + Q, -1);
	for (int i = 0; i < M + Q; i++) {
		query q = qs[i];
		if (q.tp == 1) {
			int rx = root[q.x];
			int ry = root[q.y];
			if (rx != ry) {
				if (group[rx].size() < group[ry].size()) {
					swap(rx, ry);
				}
				group[rx].insert(group[rx].end(), group[ry].begin(), group[ry].end());
				idl[rx] = min(idl[rx], idl[ry]);
				idr[rx] = max(idr[rx], idr[ry]);
				for (int j : group[ry]) {
					root[j] = rx;
				}
			}
		}
		if (q.tp == 4) {
			l[i] = idl[root[q.x]];
			r[i] = idr[root[q.x]];
		}
	}

	// step #4. find all high-degree vertices
	vector<int> deg(N, 0);
	for (query q : qs) {
		if (q.tp == 1) {
			deg[q.x] += 1;
			deg[q.y] += 1;
		}
	}
	int Z = 0;
	vector<int> vert_id(N, -1);
	for (int i = 0; i < N; i++) {
		if (1LL * deg[i] * deg[i] >= 16 * (M + Q)) {
			vert_id[i] = Z;
			Z += 1;
		}
	}
	vector<vector<int> > flag(N, vector<int>(Z, -1));

	// step #5. binary indexed tree
	vector<long long> bit(N + 1);
	auto add = [&](int pos, int val) {
		for (int i = pos + 1; i <= N; i += i & (-i)) {
			bit[i] += val;
		}
	};
	auto getsum = [&](int pos) {
		long long answer = 0;
		for (int i = pos; i >= 1; i -= i & (-i)) {
			answer += bit[i];
		}
		return answer;
	};

	// step #6. process queries
	vector<vector<int> > g(N);
	vector<long long> val(N, 0), answer(Q, -1);
	vector<vector<long long> > bigval(M + Q + 1);
	bigval[0] = vector<long long>(Z, 0);
	for (int i = 0; i < M + Q; i++) {
		bigval[i + 1] = bigval[i];
		query q = qs[i];
		if (q.tp == 1) {
			if (vert_id[q.x] == -1) {
				g[q.x].push_back(q.y);
			}
			else if (flag[q.y][vert_id[q.x]] == -1) {
				flag[q.y][vert_id[q.x]] = i;
			}
			if (vert_id[q.y] == -1) {
				g[q.y].push_back(q.x);
			}
			else if (flag[q.x][vert_id[q.y]] == -1) {
				flag[q.x][vert_id[q.y]] = i;
			}
		}
		if (q.tp == 2) {
			val[q.x] += q.y;
		}
		if (q.tp == 3) {
			if (vert_id[q.x] == -1) {
				for (int j : g[q.x]) {
					val[j] += q.y;
				}
			}
			else {
				bigval[i + 1][vert_id[q.x]] += q.y;
			}
			val[q.x] += q.y;
		}
		if (q.tp == 4) {
			add(l[i], q.y);
			add(r[i], -q.y);
		}
		if (q.tp == 5) {
			long long subval = val[q.x];
			for (int j = 0; j < Z; j++) {
				if (flag[q.x][j] != -1) {
					subval += bigval[i + 1][j] - bigval[flag[q.x][j]][j];
				}
			}
			subval += getsum(q.x + 1);
			answer[q.y] = max(A[q.x] - subval, 0LL);
		}
	}

	return answer;
}

vector<long long> solve_easy(int N, int M, int Q, const vector<long long>& TA, const vector<int>& TP, const vector<int>& X, const vector<int>& Y, const vector<int>& S, const vector<int>& I) {
	// step #1. preparation
	vector<query> qs(M + Q);
	for (int i = 0; i < M; i++) {
		qs[i] = query(TP[i], i * 2 + 1, X[i], Y[i]);
	}
	for (int i = 0; i < Q; i++) {
		qs[M + i] = query(5, S[i] * 2, I[i], i);
	}
	sort(qs.begin(), qs.end());

	// step #2. renumber vertices
	vector<long long> A = TA;
	vector<long long> answer(Q, -1);
	vector<vector<int> > g(N);
	for (query q : qs) {
		if (q.tp == 1) {
			g[q.x].push_back(q.y);
			g[q.y].push_back(q.x);
		}
		if (q.tp == 2) {
			A[q.x] = max(A[q.x] - q.y, 0LL);
		}
		if (q.tp == 3) {
			A[q.x] = max(A[q.x] - q.y, 0LL);
			for (int i : g[q.x]) {
				A[i] = max(A[i] - q.y, 0LL);
			}
		}
		if (q.tp == 4) {
			vector<bool> vis(N, false);
			auto dfs = [&](auto& self, int pos) -> void {
				vis[pos] = true;
				A[pos] = max(A[pos] - q.y, 0LL);
				for (int i : g[pos]) {
					if (!vis[i]) {
						self(self, i);
					}
				}
			};
			dfs(dfs, q.x);
		}
		if (q.tp == 5) {
			answer[q.y] = A[q.x];
		}
	}

	return answer;
}

#include <set>
#include <random>
#include <string>

mt19937_64 mt(1);

int rand_int(int l, int r) {
	return l + int(mt() % (r - l));
}

template<class T> string to_string(const vector<T>& arr) {
	string res = "[";
	for (int i = 0; i < int(arr.size()); i++) {
		if (i != 0) {
			res += ", ";
		}
		res += std::to_string(arr[i]);
	}
	res += "]";
	return res;
}

bool checker(int N) {
	const int samples = 10000;
	for (int id = 1; id <= samples; id++) {
		vector<long long> A(N);
		for (int i = 0; i < N; i++) {
			A[i] = 99;
		}
		int M = 2 * N;
		set<pair<int, int> > s;
		vector<int> TP(M), X(M), Y(M);
		for (int i = 0; i < M; i++) {
			TP[i] = rand_int(s.size() != N * (N - 1) / 2 ? 1 : 2, 5);
			X[i] = rand_int(0, N);
			if (TP[i] == 1) {
				do {
					X[i] = rand_int(0, N);
					Y[i] = rand_int(0, N);
				} while (X[i] == Y[i] || s.find(make_pair(min(X[i], Y[i]), max(X[i], Y[i]))) != s.end());
				s.insert(make_pair(min(X[i], Y[i]), max(X[i], Y[i])));
			}
			else {
				X[i] = rand_int(0, N);
				Y[i] = rand_int(1, 10);
			}
		}
		int Q = 1;
		vector<int> S(Q), I(Q);
		for (int i = 0; i < Q; i++) {
			S[i] = rand_int(0, M + 1);
			I[i] = rand_int(0, N);
		}
		vector<long long> res1 = solve(N, M, Q, A, TP, X, Y, S, I);
		vector<long long> res2 = solve_easy(N, M, Q, A, TP, X, Y, S, I);
		if (res1 != res2) {
			cout << "N = " << N << ", M = " << M << ", Q = " << Q << ", Case #" << id << ":" << endl;
			cout << "A = " << to_string(A) << endl;
			cout << "TP= " << to_string(TP) << endl;
			cout << "X = " << to_string(X) << endl;
			cout << "Y = " << to_string(Y) << endl;
			cout << "S = " << to_string(S) << endl;
			cout << "I = " << to_string(I) << endl;
			cout << "Returns: " << to_string(res1) << endl;
			cout << "Answer: " << to_string(res2) << endl;
			return false;
		}
	}
	return true;
}

int main() {
	/*
	for (int N = 2; N <= 10; N++) {
		if (!checker(N)) {
			break;
		}
		cout << "N = " << N << " Complete!" << endl;
	}
	*/
	cin.tie(0);
	ios::sync_with_stdio(false);
	int N;
	cin >> N;
	vector<long long> A(N);
	for (int i = 0; i < N; i++) {
		cin >> A[i];
	}
	int M;
	cin >> M;
	vector<int> TP(M), X(M), Y(M);
	for (int i = 0; i < M; i++) {
		cin >> TP[i] >> X[i] >> Y[i];
		X[i] -= 1;
		Y[i] -= (TP[i] == 1 ? 1 : 0);
	}
	int Q;
	cin >> Q;
	vector<int> S(Q), I(Q);
	for (int i = 0; i < Q; i++) {
		cin >> S[i] >> I[i];
		I[i] -= 1;
	}
	vector<long long> answer = solve(N, M, Q, A, TP, X, Y, S, I);
	for (int i = 0; i < Q; i++) {
		cout << answer[i] << '\n';
	}

	return 0;
}
0