chang2394

Untitled

Sep 26th, 2014
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.80 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 SQ = 500;
  69.  
  70. int inp[NN], seg[NN];
  71. int st[SQ+5], en[SQ+5];
  72. int t[SQ+5][50005];
  73. pii q[10005];
  74.  
  75. void update(int node,int i,int v){
  76.     for(; i < 50005; i += i&-i)
  77.         t[node][i] += v;
  78. }
  79.  
  80. int query(int node,int i){
  81.     int ans = 0;
  82.     for(; i > 0; i -= i&-i)
  83.         ans += t[node][i];
  84.     return ans;
  85. }
  86.  
  87. LL get(int i,int j,int x,int y,int n){
  88.     if (i > j) return 0LL;
  89.     if (x > y) swap(x,y);
  90.  
  91.     LL ans = 0;
  92.     int ss = seg[i];
  93.     if (i != st[ss] and ss != seg[j]){
  94.         while(i < en[ss] and i <= j){
  95.             if (inp[i] > x and inp[i] < y)
  96.                 ++ans;
  97.             ++i;
  98.         }
  99.         ++ss;
  100.     }
  101.  
  102.     if (i > j) return ans;
  103.     while(ss != seg[j]){
  104.         ans += (LL)query(ss,y-1);
  105.         ans -= (LL)query(ss,x);
  106.         ++ss;
  107.         i = st[ss];
  108.     }
  109.  
  110.     if (i != st[ss] or j != en[ss]-1){
  111.         while(i <= j){
  112.             if (inp[i] > x and inp[i] < y)
  113.                 ++ans;
  114.             ++i;
  115.         }
  116.     } else {
  117.         ans += (LL)query(ss,y-1);
  118.         ans -= (LL)query(ss,x);
  119.     }
  120.     return ans;
  121. }
  122.  
  123. void solve(){
  124.     int n,m,mx = 0;
  125.     fi(&n);
  126.     for(int i = 0; i < n; ++i)
  127.         fi(&inp[i]), mx = max(mx,inp[i]);
  128.  
  129.     fi(&m);
  130.     for(int i = 0; i < m; ++i)
  131.         fi(&q[i].fr),fi(&q[i].se), --q[i].fr;
  132.  
  133.     LL inv_cnt = 0;
  134.     int nodes = 0;
  135.  
  136.     for(int j = 0; j < n; ++nodes){
  137.         st[nodes] = j;
  138.         while(j < st[nodes]+SQ and j < n)
  139.             seg[j++] = nodes;
  140.         en[nodes] = j;
  141.     }
  142.  
  143.     for(int i = 0; i < n; ++i){
  144.         inv_cnt += (LL)query(nodes,mx) - (LL)query(nodes,inp[i]);
  145.         update(nodes,inp[i],1);
  146.     }
  147.  
  148.     for(int i = 0; i < nodes; ++i){
  149.         for(int k = st[i]; k < en[i]; ++k)
  150.             update(i,inp[k],1);
  151.     }
  152.  
  153.     for(int i = 0; i < m; ++i){
  154.         int x = q[i].fr,y = q[i].se;
  155.         int p = inp[x];
  156.         if (y > p){
  157.             inv_cnt -= get(0,x-1,p,y+1,n);
  158.             inv_cnt += get(x+1,n-1,p-1,y,n);
  159.         }
  160.         if (y < p){
  161.             inv_cnt += get(0,x-1,y,p+1,n);
  162.             inv_cnt -= get(x+1,n-1,y-1,p,n);
  163.         }
  164.  
  165.         if (y != p){
  166.             update(seg[x],y,1);
  167.             update(seg[x],p,-1);
  168.             inp[x] = y;
  169.         }
  170.         printf("%lld\n",inv_cnt);
  171.     }
  172. }
  173.  
  174. int main()
  175. {
  176.     solve();
  177.     return 0;
  178. }
Advertisement
Add Comment
Please, Sign In to add comment