Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2017
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 KB | None | 0 0
  1. #include "bits/stdc++.h"
  2. #include <x86intrin.h>
  3. #include<ext/pb_ds/assoc_container.hpp>
  4. #define MAXN 100009
  5. #define INF 1000000007
  6. #define mp(x,y) make_pair(x,y)
  7. #define all(v) v.begin(),v.end()
  8. #define pb(x) push_back(x)
  9. #define wr cout<<"----------------"<<endl;
  10. #define ppb() pop_back()
  11. #define tr(ii,c) for(__typeof((c).begin()) ii=(c).begin();ii!=(c).end();ii++)
  12. #define ff first
  13. #define ss second
  14. #define my_little_dodge 46
  15. #define debug(x) cerr<< #x <<" = "<< x<<endl;
  16. using namespace __gnu_pbds;
  17. using namespace std;
  18.  
  19. typedef long long ll;
  20. typedef pair<int,int> PII;
  21. template<class T>bool umin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
  22. template<class T>bool umax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
  23. typedef tree<pair<ll,int>, null_type, less<pair<ll,int> >, rb_tree_tag, tree_order_statistics_node_update> tes;
  24. const int offset=100001;
  25. vector<int>adj[MAXN];
  26. int med[MAXN],men[MAXN],sub[MAXN],vis[MAXN];
  27. int sz[MAXN*2],multi;
  28. tes s[MAXN*2];
  29. ll ans=0;
  30. void pre(int nd,int pr){
  31. sub[nd]=1;
  32. tr(it,adj[nd])
  33. if(*it!=pr and !vis[*it]){
  34. pre(*it,nd);
  35. sub[nd]+=sub[*it];
  36. }
  37. }
  38. int dfs1(int nd,int pr,int sz){
  39. tr(it,adj[nd])
  40. if(*it!=pr and !vis[*it] and sub[*it]>sz)
  41. return dfs1(*it,nd,sz);
  42. return nd;
  43. }
  44. int tap(int x,ll y){
  45. int res=0;
  46. for(;x<=200001;x+=x&(-x)){
  47. if(!sz[x])
  48. continue;
  49. int val=sz[x]-s[x].order_of_key(mp(y,0));
  50. res+=val;
  51. }
  52. return res;
  53. }
  54. void get(int nd,int pr,int X,ll Y){
  55. X+=med[nd];Y+=men[nd];
  56. ans+=(X>=0 and Y>=0);
  57. ans+=tap(-X+offset,-Y);
  58. tr(it,adj[nd])
  59. if(*it!=pr and !vis[*it])
  60. get(*it,nd,X,Y);
  61. }
  62. stack<pair<int,pair<ll,int> > >st;
  63. void update(int x,ll y,int turn,int id){
  64. for(;x;x-=x&(-x)){
  65. if(turn)
  66. s[x].insert(mp(y,id)),sz[x]++;
  67. else
  68. s[x].erase(mp(y,id)),sz[x]--;
  69. }
  70. }
  71. void upd(int nd,int pr,int X,ll Y){
  72. X+=med[nd];Y+=men[nd];
  73. ++multi;
  74. update(X+offset,Y,1,multi);
  75. st.push(mp(X+offset,mp(Y,multi)));
  76. tr(it,adj[nd])
  77. if(*it!=pr and !vis[*it])
  78. upd(*it,nd,X,Y);
  79. }
  80. void solve(int nd){
  81. ans+=(med[nd]>=0 and men[nd]>=0);
  82. for(int i=0;i<int(adj[nd].size());i++){
  83. int to=adj[nd][i];
  84. if(vis[to])
  85. continue;
  86. get(to,-1,med[nd],men[nd]);
  87. upd(to,-1,0,0);
  88. }
  89. while(!st.empty()){
  90. int xx=st.top().ff;
  91. ll yy=st.top().ss.ff;
  92. int mm=st.top().ss.ss;
  93. st.pop();
  94. update(xx,yy,0,mm);
  95. }multi=0;
  96. }
  97. void dfs(int nd){
  98. pre(nd,-1);
  99. int centr=dfs1(nd,-1,sub[nd]>>1);
  100. vis[centr]=1;solve(centr);
  101. tr(it,adj[centr])
  102. if(!vis[*it])
  103. dfs(*it);
  104. }
  105. int main(){
  106.  
  107. srand(__rdtsc());
  108. //~ freopen("file.in", "r", stdin);
  109. int t=2;
  110. //~ scanf("%d",&t);
  111. while(t--){
  112. int n=100000,X=rand()%1000000000,Y=rand()%1000000000;
  113. //~ scanf("%d%d%d",&n,&X,&Y);
  114. for(int i=1;i<=n;i++){
  115. int x=rand()%1000000000;
  116. //~ scanf("%d",&x);
  117. med[i]=(x<=X)*2-1;
  118. men[i]=x-Y;
  119. }
  120. for(int i=2;i<=n;i++){
  121. int u=rand()%(i-1)+1,v=i;
  122. //~ scanf("%d%d",&u,&v);
  123. adj[u].pb(v);
  124. adj[v].pb(u);
  125. }dfs(1);
  126. printf("%lld\n",ans);
  127. for(int i=1;i<=n;i++)
  128. vis[i]=0,adj[i].clear();
  129. ans=0;
  130. }
  131. return 0;
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement