chang2394

Untitled

Sep 26th, 2014
346
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.94 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <vector>
  4. #include <limits>
  5. #include <map>
  6. using namespace std;
  7.  
  8. // uzumaki naruto
  9. #define TRACE
  10.  
  11. #ifdef TRACE
  12. #define dbgarr(a,n)   cerr << "["; for(int i = 0; i < n; ++i) cerr << a[i] << " ";cerr << "\b]\n";
  13. #define dbg(args...)  {debug,args; cerr<<endl;}
  14. #define pause()       cin.get();cin.get();
  15.  
  16. #else
  17. #define dbgarr(a,n)
  18. #define dbg(args...)
  19. #define pause()
  20. #endif
  21.  
  22. struct debugger {
  23.     template<typename T> debugger& operator , (const T& v) {
  24.         cerr<<v<<" "; return *this;
  25.     }
  26. } debug;
  27.  
  28. template <typename T1, typename T2>
  29. inline ostream& operator << (ostream& os, const pair<T1, T2>& p) {
  30.     return os << "(" << p.first << ", " << p.second << ")";
  31. }
  32.  
  33. template<typename T>
  34. inline ostream &operator << (ostream & os,const vector<T>& v) {
  35.     bool first = true; os << "[";
  36.     for (typename vector<T>::const_iterator ii = v.begin(); ii != v.end(); ++ii) {
  37.         if(!first) os << ", ";
  38.         os << *ii; first = false;
  39.     }
  40.     return os << "]";
  41. }
  42.  
  43. template <typename T>
  44. inline void fi(T *a)
  45. {
  46.     register char c=0;
  47.     while (c<33) c=getchar_unlocked();
  48.     *a=0;
  49.     int tmp = 0;
  50.     while (c>33)
  51.     {
  52.         if ( c == 45 ) tmp = 1;
  53.         else *a=*a*10+c-'0';
  54.         c=getchar_unlocked();
  55.     }
  56.     if ( tmp == 1 ) *a = 0-(*a);
  57. }
  58.  
  59. #define fr first
  60. #define se second
  61. #define pb push_back
  62.  
  63. typedef long long LL;
  64. typedef pair<int,int> pii;
  65. typedef vector<int> vi;
  66.  
  67. const int NN = 250005;
  68. const int MM = 50005;
  69. const int SQ = 500;
  70.  
  71. // sqrt decomposition + 2D binary indexed tree
  72. int inp[NN], seg[NN];
  73. int st[SQ+5], en[SQ+5];
  74. int t[SQ+5][MM], nodes = 1;
  75. pii q[10005];
  76.  
  77. void update(int x,int y,int v){
  78.     for(int i = x; i <= nodes; i += i&-i){
  79.         for(int k = y; k < MM; k += k&-k)
  80.             t[i][k] += v;
  81.     }
  82. }
  83.  
  84. int query(int x,int y){
  85.     int ans = 0;
  86.     for(int i = x; i > 0; i -= i&-i){
  87.         for(int k = y; k > 0; k -= k&-k)
  88.             ans += t[i][k];
  89.     }
  90.     return ans;
  91. }
  92.  
  93. int query(int a,int b,int c,int d){
  94.     int ans = query(c,d);
  95.     ans -= query(a-1,d);
  96.     ans -= query(c,b-1);
  97.     ans += query(a-1,b-1);
  98.     return ans;
  99. }
  100.  
  101. int get(int i,int j,int x,int y,int n){
  102.     if (i > j) return 0LL;
  103.     if (x > y) swap(x,y);
  104.  
  105.     int ans = 0;
  106.     int ss = seg[i];
  107.     while(i < en[ss] and i <= j){
  108.         if (inp[i] > x and inp[i] < y)
  109.             ++ans;
  110.         ++i;
  111.     }
  112.  
  113.     if (i > j) return ans;
  114.     int r1 = ss+1, r2 = seg[j]-1;
  115.     if (r1 <= r2)
  116.         ans += query(r1,x+1,r2,y-1);
  117.     ss = seg[j];
  118.     i = st[ss];
  119.     while(i <= j){
  120.         if (inp[i] > x and inp[i] < y)
  121.             ++ans;
  122.         ++i;
  123.     }
  124.     return ans;
  125. }
  126.  
  127. void solve(){
  128.     int n,m,mx = 0;
  129.     fi(&n);
  130.     for(int i = 0; i < n; ++i)
  131.         fi(&inp[i]), mx = max(mx,inp[i]);
  132.  
  133.     fi(&m);
  134.     for(int i = 0; i < m; ++i){
  135.         fi(&q[i].fr),fi(&q[i].se), --q[i].fr;
  136.         mx = max(mx,q[i].se);
  137.     }
  138.  
  139.     LL inv_cnt = 0;
  140.     for(int j = 0; j < n; ++nodes){
  141.         st[nodes] = j;
  142.         while(j < st[nodes]+SQ and j < n)
  143.             seg[j++] = nodes;
  144.         en[nodes] = j;
  145.     }
  146.  
  147.     for(int i = 0; i < n; ++i){
  148.         inv_cnt += (LL)query(nodes,inp[i]+1,nodes,mx);
  149.         update(nodes,inp[i],1);
  150.     }
  151.  
  152.     for(int i = 0; i < nodes; ++i){
  153.         for(int k = st[i]; k < en[i]; ++k)
  154.             update(i,inp[k],1);
  155.     }
  156.  
  157.     for(int i = 0; i < m; ++i){
  158.         int x = q[i].fr,y = q[i].se;
  159.         int p = inp[x];
  160.         if (y > p){
  161.             inv_cnt -= (LL)get(0,x-1,p,y+1,n);
  162.             inv_cnt += (LL)get(x+1,n-1,p-1,y,n);
  163.         }
  164.         if (y < p){
  165.             inv_cnt += (LL)get(0,x-1,y,p+1,n);
  166.             inv_cnt -= (LL)get(x+1,n-1,y-1,p,n);
  167.         }
  168.  
  169.         if (y != p){
  170.             update(seg[x],y,1);
  171.             update(seg[x],p,-1);
  172.             inp[x] = y;
  173.         }
  174.         printf("%lld\n",inv_cnt);
  175.     }
  176. }
  177.  
  178. int main()
  179. {
  180.     solve();
  181.     return 0;
  182. }
Advertisement
Add Comment
Please, Sign In to add comment