Submission #943549

# Submission time Handle Problem Language Result Execution time Memory
943549 2024-03-11T15:05:25 Z NK_ Traffickers (RMI18_traffickers) C++17
0 / 100
131 ms 225504 KB
// Success consists of going from failure to failure without loss of enthusiasm
#include <bits/stdc++.h>

using namespace std;

#define nl '\n'
#define pb push_back
#define sz(x) int(x.size())

template<class T> using V = vector<T>;
using vi = V<int>;

const int nax = 6e4+5;
const int T = 21;
const int LG = 15;
int A[T][T][nax];

void upd(int i, int j, int p, int x) { for(++p; p <= nax; p += p & -p) A[i][j][p - 1] += x; }
int qry(int i, int j, int r) { int s = 0; for(; r; r -= r & -r) s += A[i][j][r - 1]; return s; }
int qry(int i, int j, int l, int r) { return qry(i, j, r + 1) - qry(i, j, l); }


int main() {
	cin.tie(0)->sync_with_stdio(0);
	
	memset(A, 0, sizeof A);

	int N; cin >> N;

	V<vi> adj(N); for(int i = 0; i < N - 1; i++) {
		int u, v; cin >> u >> v; --u, --v;
		adj[u].pb(v);
		adj[v].pb(u);
	}

	vi st(N), en(N), dep(N); V<vi> up(N, vi(LG)); int tim = 0;
	function<void(int, int)> gen = [&](int u, int p) {
		// cout << u << " <-> " << p << endl;
		st[u] = tim++; dep[u] = (u == p ? 0 : dep[p] + 1); 
		up[u][0] = p; for(int i = 1; i < LG; i++) up[u][i] = up[up[u][i-1]][i-1]; 

		for(auto& v : adj[u]) if (v != p) {
			// cout << v << " " << u << endl;
			gen(v, u);
		}

		en[u] = tim++;
	};

	gen(0, 0);

	auto lca = [&](int a, int b) {
		if (dep[a] > dep[b]) swap(a, b);
		for(int i = LG - 1; i >= 0; i--) {
			if (!(st[up[a][i]] <= st[b] && en[b] <= en[up[a][i]])) a = up[a][i];
		}
		return up[a][0];
	};	

	auto traf = [&](int a, int b, int d = +1) {
		// cout << a << " " << b << " =====> ";
		int l = lca(a, b);
		// cout << l << endl;

		vi path;
		while(a != l) {
			path.pb(a);
			a = up[a][0];
		}
		
		path.pb(l);

		vi dpath;
		while(b != l) {
			dpath.pb(b);
			b = up[b][0];
		}
		reverse(begin(dpath), end(dpath));

		path.insert(prev(end(path)), begin(dpath), end(dpath));

		for(int r = 0; r < sz(path); r++) {
			upd(r, sz(path), st[path[r]], d);
			upd(r, sz(path), en[path[r]], -d);
		}

		// for(auto& x : path) cout << x << " ";
		// cout << endl;
	};

	int K; cin >> K;
	for(int i = 0; i < K; i++) {
		int a, b; cin >> a >> b; --a, --b;
		traf(a, b);
	}

	int Q; cin >> Q;
	for(int i = 0; i < Q; i++) {
		int t; cin >> t;
		if (t == 1) {
			int a, b; cin >> a >> b; --a, --b;
			traf(a, b);
		} else if (t == 2) {
			int a, b; cin >> a >> b; --a, --b;
			traf(a, b, -1);
		} else if (t == 3) {
			int a, b, l, r; cin >> a >> b >> l >> r; --a, --b;
			int p = lca(a, b);

			int ans = 0;
			for(int len = 1; len <= 20; len++) {
				for(int rem = 0; rem < len; rem++) {
					int L = (l - rem + len - 1) / len, R = (r - rem) / len;
					int trafs = qry(rem, len, st[p], st[a]) + qry(rem, len, st[p], st[b]) - qry(rem, len, st[p], st[p]);
					int times = R - L + 1;
					ans += times * trafs;
					// cout << rem << " " << len << endl;
					// cout << a << " " << b << " | " << l << " " << r << " => " << trafs << " || " << L << " " << R << " => " << times << endl;
				}
			}

			cout << ans << nl;
		}
	}


	exit(0-0);
}
# Verdict Execution time Memory Grader output
1 Runtime error 116 ms 210512 KB Execution killed with signal 11
2 Runtime error 95 ms 211024 KB Execution killed with signal 11
3 Runtime error 95 ms 210908 KB Execution killed with signal 11
# Verdict Execution time Memory Grader output
1 Runtime error 103 ms 214872 KB Execution killed with signal 11
2 Runtime error 97 ms 214100 KB Execution killed with signal 11
3 Runtime error 102 ms 215616 KB Execution killed with signal 11
4 Runtime error 113 ms 215020 KB Execution killed with signal 11
5 Runtime error 100 ms 214604 KB Execution killed with signal 11
6 Runtime error 98 ms 214908 KB Execution killed with signal 11
7 Runtime error 101 ms 214864 KB Execution killed with signal 11
8 Runtime error 104 ms 215128 KB Execution killed with signal 11
9 Runtime error 107 ms 215724 KB Execution killed with signal 11
# Verdict Execution time Memory Grader output
1 Runtime error 128 ms 223156 KB Execution killed with signal 11
2 Runtime error 116 ms 225344 KB Execution killed with signal 11
3 Runtime error 113 ms 223948 KB Execution killed with signal 11
4 Runtime error 112 ms 221772 KB Execution killed with signal 11
5 Runtime error 111 ms 221264 KB Execution killed with signal 11
6 Runtime error 111 ms 224964 KB Execution killed with signal 11
7 Runtime error 131 ms 225504 KB Execution killed with signal 11
8 Runtime error 110 ms 224568 KB Execution killed with signal 11