bingxuan9112

CDQ

Jun 30th, 2019
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.59 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. typedef long ll;
  5. const ll N = 300500;
  6.  
  7. ll n,v[N],ans[N],u[N];
  8. ll b[N],sz;
  9. void init(){memset(b,0,sizeof(ll)*(sz+1));}
  10. void add(int p,ll d){for(;p<=sz;p+=p&-p) b[p]+=d;}
  11. ll qry(int p){ll res=0;for(;p;p-=p&-p) res+=b[p]; return res;}
  12. struct Query{
  13.     int type, x, y, ans;
  14. } Q[N];
  15. inline bool LESS(int a,int b){return Q[a].x!=Q[b].x ? Q[a].x<Q[b].x : a<b;}
  16. inline void merges(int l,int m,int r){
  17.     vector<int> tmp;
  18.     int i = l, j = m;
  19.     while(i<m || j<r){
  20.         if((j==r) || (i<m && LESS(v[i],v[j]))) {
  21.             auto &q = Q[v[i]];
  22.             if(q.type == 1) add(q.y,1);
  23.             tmp.push_back(v[i++]);
  24.         }else{
  25.             auto &q = Q[v[j]];
  26.             if(q.type != 1) q.ans += qry(q.y);
  27.             tmp.push_back(v[j++]);
  28.         }
  29.     }
  30.     for(int i = l; i < m; i++) if(Q[v[i]].type == 1) add(Q[v[i]].y,-1);
  31.     for(int i = l; i < r; i++) v[i] = tmp[i-l];
  32. }
  33. void CDQ(int l,int r){
  34.     if(l+1 == r) return;
  35.     int mid = l+(r-l>>1);
  36.     CDQ(l,mid),CDQ(mid,r);
  37.     merges(l,mid,r);
  38. }
  39. signed main(){
  40.     ios_base::sync_with_stdio(0),cin.tie(0);
  41.     cin >> n;
  42.     for(int i = 0; i < n; i++) cin >> Q[i].type >> Q[i].x >> Q[i].y;
  43.     for(int i = 0; i < n; i++) u[sz++] = Q[i].y;
  44.     sort(u,u+sz), sz=unique(u,u+sz)-u;
  45.     for(int i = 0; i < n; i++) Q[i].y = lower_bound(u,u+sz,Q[i].y)-u+1;
  46.     init();
  47.     iota(v,v+n,0);
  48.     CDQ(0,n);
  49.     for(int i = 0; i < n; i++) Q[i].x = -Q[i].x, Q[i].y = sz+1-Q[i].y;
  50.     iota(v,v+n,0);
  51.     CDQ(0,n);
  52.     for(int i = 0; i < n; i++) if(Q[i].type != 1) cout << Q[i].ans << '\n';
  53. }
Advertisement
Add Comment
Please, Sign In to add comment