Submission #332169

#TimeUsernameProblemLanguageResultExecution timeMemory
332169Vladth11Traffickers (RMI18_traffickers)C++14
15 / 100
878 ms59500 KiB
#include <bits/stdc++.h> #define debug(x) cerr << #x << " " << x << "\n" #define debug_with_space(x) cerr << #x << " " << x << " " using namespace std; typedef long long ll; typedef pair <ll, ll> pii; typedef pair <ll, pii> piii; const ll NMAX = 30001; const ll INF = (1 << 30); const ll MOD = 1000000007; const ll BLOCK = 101; const ll nr_of_bits = 14; const ll delta = 0.0000001; int n; vector <int> v[NMAX]; int dp[NMAX][nr_of_bits + 1]; int pz[NMAX]; int sf[NMAX]; int stamp; int fnw[NMAX][21][21]; void Precalc(){ for(int j = 1; j <= nr_of_bits; j++){ for(int i = 1; i <= n; i++){ dp[i][j] = dp[dp[i][j - 1]][j - 1]; } } } void DFS(int node, int p){ dp[node][0] = p; pz[node] = ++stamp; for(auto x : v[node]){ if(x == p) continue; DFS(x, node); } sf[node] = stamp; } bool isParent(int a, int b){ return (pz[a] <= pz[b] && sf[b] <= sf[a]); } int LCA(int a, int b){ int r = a, pas = nr_of_bits; while(pas >= 0){ if(dp[r][pas] != 0 && !isParent(dp[r][pas], b)) r = dp[r][pas]; pas--; } if(!isParent(r, b)) r = dp[r][0]; return r; } int query(int node, int t){ int sum = 0; for(int i = 1; i <= 20; i++){ int rest = t % i + 1; int cnt = 0; for(int j = pz[node]; j > 0; j -= j&(-j)){ for(int k = rest; k > 0; k -= k&(-k)){ cnt += fnw[j][i][k]; } } sum += cnt * (t / i); int cnt2 = 0; for(int j = pz[node]; j > 0; j -= j&(-j)){ for(int k = i; k > 0; k -= k&(-k)){ cnt2 += fnw[j][i][k]; } } cnt2 -= cnt; sum += (t / i - 1) * cnt2; } return sum; } void baga(int nod, int rest, int imp, int val){ for(int i = nod; i <= n; i += i&(-i)){ for(int j = rest; j <= imp; j += j&(-j)) fnw[i][imp][j] += val; } } void adauga(int u, int v, int c){ vector <int> up; while(!isParent(u, v)){ up.push_back(u); u = dp[u][0]; } vector <int> idk; while(v != u){ idk.push_back(v); v = dp[v][0]; } idk.push_back(v); reverse(idk.begin(), idk.end()); for(auto x : idk) up.push_back(x); idk.clear(); for(int i = 0; i < up.size(); i++){ baga(pz[up[i]], i + 1, up.size(), c); baga(sf[up[i]] + 1, i + 1, up.size(), -c); } } int sol(int u, int v, int t){ int lca = LCA(u, v); return query(u, t) + query(v, t) - query(lca, t) - query(dp[lca][0], t); } int main() { int i; cin >> n; for(i = 1; i <= n - 1; i++){ int a, b; cin >> a >> b; v[a].push_back(b); v[b].push_back(a); } DFS(1, 0); Precalc(); int k; cin >> k; while(k--){ int a, b; cin >> a >> b; adauga(a, b, 1); } int q; cin >> q; while(q--){ int t; cin >> t; if(t == 1){ int a, b; cin >> a >> b; adauga(a, b, 1); }else if(t == 2){ int a, b; cin >> a >> b; adauga(a, b, -1); }else{ int a, b, t1, t2; cin >> a >> b >> t1 >> t2; cout << sol(a, b, t2) - sol(a, b, t1 - 1) << "\n"; } } return 0; }

Compilation message (stderr)

traffickers.cpp: In function 'void adauga(int, int, int)':
traffickers.cpp:107:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  107 |     for(int i = 0; i < up.size(); i++){
      |                    ~~^~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...