Submission #381760

#TimeUsernameProblemLanguageResultExecution timeMemory
381760MohamedAhmed04Traffickers (RMI18_traffickers)C++14
100 / 100
3074 ms58068 KiB
#include <bits/stdc++.h> using namespace std ; const int MAX = 3e4 + 10 ; int arr[MAX] ; int n , m , q ; vector< vector<int> >adj(MAX) ; int tim = 0 ; int in[MAX] , out[MAX] , P[MAX][16] ; int dep[MAX] ; int bit[22][22][2*MAX] ; void Add(int a , int b , int x , int val) { for(int i = x ; i <= 2*n ; i += (i & (-i))) bit[a][b][i] += val ; } int Query(int a , int b , int x) { int cnt = 0 ; for(int i = x ; i ; i -= (i & (-i))) cnt += bit[a][b][i] ; return cnt ; } void dfs(int node , int par) { in[node] = ++tim ; P[node][0] = par ; for(int j = 1 ; j < 16 ; ++j) P[node][j] = P[P[node][j-1]][j-1] ; for(auto &child : adj[node]) { if(child == par) continue ; dep[child] = dep[node] + 1 ; dfs(child , node) ; } out[node] = ++tim ; } int LCA(int x , int y) { if(dep[x] < dep[y]) swap(x , y) ; for(int j = 15 ; j >= 0 ; --j) { if(dep[x] - (1 << j) >= dep[y]) x = P[x][j] ; } if(x == y) return x ; for(int j = 15 ; j >= 0 ; --j) { if(P[x][j] != P[y][j]) x = P[x][j] , y = P[y][j] ; } return P[x][0] ; } vector<int> getPath(int x , int y) { vector<int>v1 , v2 ; int lca = LCA(x , y) ; while(x != lca) v1.push_back(x) , x = P[x][0] ; v1.push_back(lca) ; while(y != lca) v2.push_back(y) , y = P[y][0] ; reverse(v2.begin() , v2.end()) ; for(auto &i : v2) v1.push_back(i) ; return v1 ; } void modify(int x , int y , int val) { vector<int>v = getPath(x , y) ; for(int i = 0 ; i < v.size() ; ++i) { Add(v.size() , i , in[v[i]] , val) ; Add(v.size() , i , out[v[i]] , -val) ; } } long long solve(int x , int y , int t) { if(t < 0) return 0ll ; int lca = LCA(x , y) ; long long cnt = 0 ; for(int i = 1 ; i <= 20 ; ++i) { for(int j = 0 ; j < min(i , t+1) ; ++j) { long long a = 1ll + (t - j) / i ; long long b = Query(i , j , in[x]) - Query(i , j , in[P[lca][0]]) ; b += Query(i , j , in[y]) - Query(i , j , in[lca]) ; cnt += a * b ; } } return cnt ; } int main() { ios_base::sync_with_stdio(0) ; cin.tie(0) ; cin>>n ; for(int i = 0 ; i < n-1 ; ++i) { int x , y ; cin>>x>>y ; adj[x].push_back(y) ; adj[y].push_back(x) ; } dfs(1 , -1) ; cin>>m ; for(int i = 0 ; i < m ; ++i) { int x , y ; cin>>x>>y ; modify(x , y , 1) ; } cin>>q ; while(q--) { int ty ; cin>>ty ; if(ty == 1) { int x , y ; cin>>x>>y ; modify(x , y , 1) ; } else if(ty == 2) { int x , y ; cin>>x>>y ; modify(x , y , -1) ; } else if(ty == 3) { int x , y , l , r ; cin>>x>>y>>l>>r ; cout<<solve(x , y , r) - solve(x , y , l-1)<<"\n" ; } } return 0 ; }

Compilation message (stderr)

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