Submission #659557

# Submission time Handle Problem Language Result Execution time Memory
659557 2022-11-18T13:52:30 Z NekoRolly Traffickers (RMI18_traffickers) C++17
15 / 100
193 ms 70172 KB
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 3e4+4;
const int N1 = 5e4+4;

struct Bit{
	int n,t[N1];

	void up(int i,int val){
		for (; i<=n; i+=i&-i) t[i] += val;
	}

	int que(int l,int r){ int ans = 0;
		for (; r>0; r-=r&-r) ans += t[r];
		for (l--; l>0; l-=l&-l) ans -= t[l];
		return ans;
	}
} d[20][21];

struct Update{
	int t0,r,i,val;
};

struct Query{
	int T,i,id,val;
};

int n,n1,n2,k,q;
vector<int> adj[N];
int L[N],R[N],up[N][15];

vector<Update> Qs_U[N];
vector<Query> Qs_Q[N];
ll ans[N1];

void dfs1(int u,int p){ L[u] = n1++, up[u][0] = p;
	for (int i=1; i<15; i++) up[u][i] = up[up[u][i-1]][i-1];
	for (int v : adj[u]) if (v != p) dfs1(v, u);
	R[u] = n1;
}

bool isa(int a,int b){
	return L[a] <= L[b] && R[b] <= R[a];
}

int lca(int a,int b){
	if (isa(a, b)) return a;
	if (isa(b, a)) return b;
	for (int i=14; i>=0; i--)
		if (!isa(up[a][i], b)) a = up[a][i];
	return up[a][0];
}

void add(int a,int b,int id,int val){ vector<int> aux,v;
	int l = lca(a, b);
	for (; ; a=up[a][0]){ v.push_back(a);
		if (a == l) break;
	}
	for (; b!=l; b=up[b][0]) aux.push_back(b);
	reverse(aux.begin(), aux.end());
	for (int x : aux) v.push_back(x);
	for (int i=0; i<v.size(); i++)
		Qs_U[v[i]].push_back({i, int(v.size()), id, val});
}

void dfs2(int u,int p){ 
	for (auto [t0, r, i, val] : Qs_U[u]) d[t0][r].up(i, val);
	for (auto [T, i, id, val] : Qs_Q[u]) for (int t0=0; t0<=T; t0++) for (int r=1; r<=20; r++)
		ans[id] += 1ll*val*d[t0][r].que(1, i)*((T-t0)/r + 1);
	for (int v : adj[u]) if (v != p) dfs2(v, u);
	for (auto [t0, r, i, val] : Qs_U[u]) d[t0][r].up(i, -val);
}

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);

	cin >> n;

	for (int i=1,a,b; i<n; i++){ cin >> a >> b;
		adj[a].push_back(b), adj[b].push_back(a);
	}

	dfs1(1, 1);

	cin >> k;

	for (int a,b; k--; ){ cin >> a >> b; add(a, b, 1, 1);}

	cin >> q; q++;
	for (int t,a,b,t1,t2,i=2; i<=q; i++){ cin >> t >> a >> b;
		if (t == 3){ cin >> t1 >> t2; 
			int l = lca(a, b);
			if (t1){
				Qs_Q[a].push_back({t1-1, i, n2, -1});
				Qs_Q[b].push_back({t1-1, i, n2, -1});
				Qs_Q[l].push_back({t1-1, i, n2, 1});
				if (l != 1) Qs_Q[up[l][0]].push_back({t1-1, i, n2, 1});
			}
			Qs_Q[a].push_back({t2, i, n2, 1});
			Qs_Q[b].push_back({t2, i, n2, 1});
			Qs_Q[l].push_back({t2, i, n2, -1});
			if (l != 1) Qs_Q[up[l][0]].push_back({t2, i, n2, -1});
			n2++;
		}
		else add(a, b, i, t==1 ? 1 : -1);
	}

	for (int t0=0; t0<20; t0++) for (int r=1; r<=20; r++) d[t0][r].n = q;

	dfs2(1, -1);

	for (int i=0; i<n2; i++) cout << ans[i] << "\n";

	return 0;
}

Compilation message

traffickers.cpp: In function 'void add(int, int, int, int)':
traffickers.cpp:63:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   63 |  for (int i=0; i<v.size(); i++)
      |                ~^~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4180 KB Output is correct
2 Correct 5 ms 4904 KB Output is correct
3 Correct 6 ms 4900 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 29 ms 15276 KB Execution killed with signal 11
2 Runtime error 30 ms 15220 KB Execution killed with signal 11
3 Runtime error 30 ms 15812 KB Execution killed with signal 11
4 Runtime error 27 ms 15900 KB Execution killed with signal 11
5 Runtime error 25 ms 15520 KB Execution killed with signal 11
6 Runtime error 33 ms 15692 KB Execution killed with signal 11
7 Runtime error 33 ms 15512 KB Execution killed with signal 11
8 Runtime error 26 ms 15700 KB Execution killed with signal 11
9 Runtime error 26 ms 15712 KB Execution killed with signal 11
# Verdict Execution time Memory Grader output
1 Runtime error 169 ms 62640 KB Execution killed with signal 11
2 Runtime error 173 ms 63128 KB Execution killed with signal 11
3 Runtime error 169 ms 60608 KB Execution killed with signal 11
4 Runtime error 167 ms 62172 KB Execution killed with signal 11
5 Runtime error 180 ms 70172 KB Execution killed with signal 11
6 Runtime error 182 ms 61836 KB Execution killed with signal 11
7 Runtime error 185 ms 61236 KB Execution killed with signal 11
8 Runtime error 193 ms 60268 KB Execution killed with signal 11