Advertisement
Guest User

Awesome Shawarma

a guest
Jan 18th, 2020
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.31 KB | None | 0 0
  1. #pragma GCC optimize ("Ofast,unroll-loops")
  2. #pragma GCC target ("sse,sse2,sse3,ssse3,sse4")
  3. #pragma GCC target ("popcnt,abm,mmx,avx,tune=native")
  4.  
  5. #include <bits/stdc++.h>
  6. using namespace std;
  7. #define sz(x) int(x.size())
  8. #define pb push_back
  9. #define FER(i,a,b) for(int i = ll(a); i < ll(b); ++i)
  10. #define IFR(i,a,b) for(int i = ll(a); i >= ll(b); --i)
  11. #define all(v) (v).begin(),(v).end()
  12. #define mp make_pair
  13. #define ff first
  14. #define ss second
  15. #define tm1 first
  16. #define tm2 second.first
  17. #define tm3 second.second
  18. #define fill(x,v) memset(x,v,sizeof(x))
  19. #define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
  20. #define sqr(x) (x)*(x)
  21. #define bas 987625403
  22. #define N 100010
  23. typedef long double ld;
  24. typedef long long ll;
  25. typedef pair<ll,ll> ii;
  26. typedef pair<ll,ii> tri;
  27. typedef vector<ll> vi;
  28. typedef vector<ii> vii;
  29. #define trace(...) fff(#__VA_ARGS__,__VA_ARGS__)
  30.  
  31. const int oo = 1e9;
  32. template<typename t> void fff(const char *x, t&& val1){
  33.    cout << x << " : " << val1 << endl;
  34. }
  35.  
  36. template<typename t1, typename... t2> void fff(const char *x, t1&& val1, t2&&... val2){
  37.    const char *xd = strchr(x+1,',');
  38.    cout.write(x,xd-x)<< " : " << val1 << " | ";
  39.    fff(xd+1,val2...);
  40. }
  41.  
  42.  
  43.  
  44. const int MAXCD = 1e5 + 5;
  45. struct CentroidD{
  46.     int n;
  47.     set< int > graph[MAXCD];
  48.     vector<int> cdGraph[MAXCD];
  49.     int sub[MAXCD];
  50.     int cpar[MAXCD];
  51.     int root;
  52.    
  53.     void add_edge(int a, int b){
  54.         graph[a].insert(b);
  55.         graph[b].insert(a);
  56.     }
  57.  
  58.     void dfs(int cur, int parent){
  59.         sub[cur] = 1;
  60.         for(int to: graph[cur]){
  61.             if(to != parent && cpar[to] == -1){
  62.                 dfs(to, cur);
  63.                 sub[cur] += sub[to];
  64.             }
  65.         }
  66.     }
  67.  
  68.     void decompose(int cur, int parent, int sb, int prevc){
  69.         for(int to: graph[cur]){
  70.             if(to != parent && cpar[to] == -1 && (2 * sub[to] > sb)){
  71.                 decompose(to, cur, sb, prevc);
  72.                 return;
  73.             }
  74.         }
  75.         cpar[cur] = prevc;
  76.         for(int to: graph[cur]){
  77.             if(cpar[to] == -1){
  78.                 dfs(to, - 1);
  79.                 decompose(to, cur, sub[to], cur);
  80.             }
  81.         }
  82.     }
  83.  
  84.     void init(int start){
  85.         for(int i = 0; i < n; ++i) cpar[i] = -1, sub[i] = 0;
  86.         dfs(start, - 1);
  87.         decompose(start, -1, sub[start], -2);
  88.         for(int i = 0; i < n; i ++) if(cpar[i] == -2){
  89.             root = i; break;
  90.         }
  91.         for(int i = 0; i < n; i ++){
  92.             if(cpar[i] == -2) continue;
  93.             cdGraph[i].pb(cpar[i]); cdGraph[cpar[i]].pb(i);
  94.         }
  95.     }
  96. }cd;
  97.  
  98. struct FenwickTree{
  99.     vector<int> ft;
  100.     int LSO(int b){ return (b & (-b) ); }
  101.     FenwickTree(){}
  102.     int rsq(int b){
  103.         int sum = 0; for(;b ; b-= LSO(b)) sum+= ft[b];
  104.         return sum; }
  105.     int rsq(int a, int b){
  106.         return rsq(b) - (a == 1? 0 : rsq(a-1)); }
  107.     //adjusts value of the k-th element by v
  108.     void adjust(int k, int v){
  109.         for(; k < (int)ft.size(); k += LSO(k)) ft[k] += v;
  110.     }
  111. }fenw;
  112.  
  113. queue<int> toAdd, aux, total;
  114. int A, B;
  115.  
  116. ll query(int d){
  117.     int l = A - d + 1, r = B - d + 1;
  118.     if(r <= 0) return 0;
  119.     l = max(l, 1);
  120.     return fenw.rsq(l, r);
  121. }
  122.  
  123. ll add(){
  124.     ll res = 0;
  125.     while(!toAdd.empty()){
  126.         aux.push(toAdd.front());
  127.         res += query(toAdd.front());
  128.         toAdd.pop();
  129.     }
  130.     while(!aux.empty()){
  131.         fenw.adjust(aux.front() + 1, 1);
  132.         total.push(aux.front() + 1);
  133.         aux.pop();
  134.     }
  135.     return res;
  136. }
  137.  
  138. void subtree(int u, int par, int dist){
  139.     toAdd.push(dist);
  140.     for(int v: cd.graph[u]){
  141.         if(v == par) continue;
  142.         subtree(v, u, dist + 1);
  143.     }
  144. }
  145.  
  146. ll compute(ll centroid){
  147.     ll ans = 0;
  148.     fenw.adjust(1, 1); //for centroid root
  149.     for(int v: cd.graph[centroid]){
  150.         subtree(v, centroid, 1);
  151.         ll ret = add();
  152.         ans += ret;
  153.     }
  154.     fenw.adjust(1, -1);
  155.     while(!total.empty()){
  156.         fenw.adjust(total.front(), -1);
  157.         total.pop();
  158.     }
  159.     return ans;
  160. }
  161.  
  162. int main(){
  163.     fastio;
  164.     freopen("awesome.in", "r", stdin);
  165.     int t; cin >> t;
  166.     while(t --){
  167.         int n, l, r; cin >> n >> l >> r;
  168.         cd.n = n;
  169.         FER(i, 0, n) {
  170.             cd.graph[i].clear();
  171.             cd.cdGraph[i].clear();
  172.         }
  173.         A = n - r - 1;
  174.         B = n - l - 1;
  175.         fenw.ft.assign(n + 1, 0);
  176.         FER(i, 0, n - 1){
  177.             int a, b; cin >> a >> b;
  178.             a --; b --;
  179.             cd.add_edge(a, b);
  180.         }
  181.         cd.init(0);
  182.         queue<int> q; q.push(cd.root);
  183.         ll ans = 0;
  184.         while(!q.empty()){
  185.             int u = q.front(); q.pop();
  186.             ans += compute(u);
  187.             for(int v: cd.cdGraph[u]){
  188.                 if(v == cd.cpar[u]) continue;
  189.                 q.push(v);
  190.             }
  191.             for(int v: cd.graph[u]){
  192.                 cd.graph[v].erase(u);  
  193.             }
  194.             cd.graph[u].clear();
  195.         }
  196.         //if(r == n - 1) ans += n;
  197.         cout << ans << endl;
  198.     }
  199.     return 0;
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement