Advertisement
omar-alhelo

Untitled

Jun 4th, 2019
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.18 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #define fff ios_base::sync_with_stdio(0);cin.tie(0)
  3. #define _for(i,_) for(int i=0 ; i<_ ; i++)
  4. #define _forr(i,_) for(int i=1 ; i<=_ ; i++)
  5. #define _rof(i,_) for(int i=_-1 ; i>=0 ; i--)
  6. #define _roff(i,_) for(int i=_ ; i>=1 ; i--)
  7. #define NL cout << endl
  8. #define SP cout << " "
  9. #define pb push_back
  10. #define pp pop_back
  11. #define inf INT_MAX
  12. #define BIG INT_MAX
  13. #define INF 1e18
  14. #define ss second
  15. #define ff first
  16. using namespace std;
  17. typedef long long ll;
  18. typedef long double ld;
  19. typedef pair<ll,ll> pl;
  20. typedef vector<ll> vl;
  21. typedef vector<ll> vl;
  22. typedef pair<ll,ll> pii;
  23. typedef vector<int> vi;
  24. const ld PI = 3.14159265359;
  25. const int mod = 1000000007;
  26. const int MAX = 300009;
  27.  
  28. ll a[MAX], tree[MAX*5], lazy[MAX*5], n;
  29. ll b[MAX] ,t, c[MAX];
  30. map<ll,ll> ans;
  31. pl p[MAX];
  32.  
  33. void build_tree(int node, int l, int r) {
  34.     if(l > r)
  35.         return;
  36.     if(l == r)
  37.     {
  38.         tree[node] = 1;
  39.     return;
  40.   }
  41.   build_tree(node*2, l, (l+r)/2);
  42.   build_tree(node*2+1, 1+(l+r)/2, r);
  43.   tree[node] = tree[node*2] + tree[node*2+1];
  44. }
  45.  
  46. void update_tree(int node, int l, int r, int i, int j, int value)
  47. {
  48.  
  49.   if(l > r || l > j || r < i) return;
  50.   if(l >= i && r <= j)
  51.   {
  52.     tree[node] = value*(r-l+1);
  53.     lazy[node] = value;
  54.     // if(l != r)
  55.     // {
  56.     //   lazy[node*2] += value;
  57.     //   lazy[node*2+1] += value;
  58.     // }
  59.     return;
  60.   }
  61.  
  62.   if(lazy[node] != -1)
  63.   {
  64.     // tree[node] = lazy[node]*(j-i+1);
  65.     if(l != r)
  66.     {
  67.       tree[node*2] = value*((r-l+2)/2);
  68.       lazy[node*2] = value;
  69.  
  70.       lazy[node*2+1] = value;
  71.       tree[node*2+1] = value*((r-l+1)/2);
  72.     }
  73.     lazy[node] = -1;
  74.   }
  75.  
  76.   update_tree(node*2, l, (l+r)/2, i, j, value);
  77.   update_tree(1+node*2, 1+(l+r)/2, r, i, j, value);
  78.   tree[node] = tree[node*2] + tree[node*2+1];
  79. }
  80.  
  81. ll query_tree(int node, int l, int r, int i, int j) {
  82.  
  83.   if(l > r || l > j || r < i)
  84.       return 0;
  85.  
  86.   if(l >= i && r <= j) return tree[node];
  87.  
  88.   if(lazy[node] != -1)
  89.   {
  90.     // tree[node] = lazy[node]*(j-i+1);
  91.     if(l != r)
  92.     {
  93.       tree[node*2] = lazy[node]*((r-l+1)/2 + (r-l+1)%2);
  94.       lazy[node*2] = lazy[node];
  95.  
  96.       lazy[node*2+1] = lazy[node];
  97.       tree[node*2+1] = lazy[node]*((r-l+1)/2);
  98.  
  99.       // lazy[node*2] = lazy[node];
  100.       // lazy[node*2+1] = lazy[node];
  101.     }
  102.     lazy[node] = -1;
  103.   }
  104.  
  105.   ll q1 = query_tree(node*2, l, (l+r)/2, i, j);
  106.   ll q2 = query_tree(1+node*2, 1+(l+r)/2, r, i, j);
  107.   ll res = q1 + q2;
  108.   return res;
  109. }
  110.  
  111. int main()
  112. {
  113.     fff;
  114.     memset(lazy, -1, sizeof lazy);
  115.     cin >> n;
  116.     _for(i,n)
  117.     {
  118.         cin >> p[i].ff >> p[i].ss;
  119.         b[i] = p[i].ff;
  120.         c[i] = p[i].ff;
  121.     }
  122.     // build_tree(1, 0, n-1);
  123.     sort(p, p+n);
  124.     sort(b, b+n);
  125.     _for(i,n)
  126.     {
  127.         ll u = upper_bound(b, b+n, b[i] + p[i].ss - 1) - b;
  128.         p[i].ss = u - i;
  129.     }
  130.     for(int i=n ; i--;  )
  131.     {
  132.  
  133.         ll sum = query_tree(1, 0, n-1, i, i + p[i].ss - 1);
  134.         sum++;
  135.         update_tree(1, 0, n-1, i, i + p[i].ss - 1, 0);
  136.         update_tree(1, 0, n-1, i, i, sum);
  137.         ans[p[i].ff] = sum;
  138.         // cout << p[i].ff <<  " " << sum << endl;
  139.     }
  140.     _for(i,n)
  141.         cout << ans[c[i]] << " ";
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement