Advertisement
Guest User

Untitled

a guest
Feb 5th, 2016
371
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.99 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const long long mod = 1e9 + 7;
  4. const double eps = 1e-18;
  5. const double PI = atan(1.0);
  6. #define readFile freopen("input","r",stdin)
  7. #define writeFile freopen("output","w",stdout)
  8. #define fastIO ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
  9. typedef pair<long long,long long> ii;
  10. typedef unsigned long long ULL;
  11. const int N = 50005;
  12. const int inf = 50002;
  13.  
  14. struct xorShift{
  15.     ULL a,b,c,d;
  16.     xorShift(){
  17.         a = 1; b = 2; c = 3; d = 4;
  18.     }
  19.     ULL next(){
  20.         ULL t = a^(a<<11);
  21.         a = b; b = c; c = d;
  22.         return d = d^(d>>19)^t^(t>>8);
  23.     }
  24. };
  25. xorShift gen = xorShift();
  26. struct treap{
  27.     ii val;
  28.     ULL p;
  29.     long long sz;
  30.     treap *l,*r;
  31.     treap(){
  32.         l = r = NULL;
  33.     }
  34.     treap(ii val){
  35.         this->val = val;
  36.         p = gen.next();
  37.         l = r = NULL;
  38.         sz = val.second;
  39.     }
  40. };
  41.  
  42. #define pnode treap*
  43.  
  44. pnode tree[N*4];
  45. ii arr[N];
  46. int nex[N],n,m,a,b;
  47. long long arr1[N];
  48. map<long long,set<int> > guide;
  49. char c;
  50. long long sz(pnode node){
  51.     return node?node->sz:0;
  52. }
  53. void usz(pnode node){
  54.     if (node)
  55.         node->sz = sz(node->l)+sz(node->r)+node->val.second;
  56. }
  57.  
  58. void split(pnode node,pnode &l,pnode &r, int key){
  59.     if (!node) l = r = NULL;
  60.     else if (node->val.first<=key) split(node->r,node->r,r,key),l = node;
  61.     else split(node->l,l,node->l,key),r = node;
  62.     usz(node);
  63. }
  64.  
  65. void merge(pnode &node,pnode l,pnode r){
  66.     if (!l||!r) node = l?l:r;
  67.     else if (l->p>r->p) merge(l->r,l->r,r), node = l;
  68.     else merge(r->l,l,r->l), node = r;
  69.     usz(node);
  70. }
  71.  
  72. void tinsert(pnode &node,pnode ins){
  73.     if (!node) node = ins;
  74.     else if (node->p<ins->p){
  75.         split(node,ins->l,ins->r,ins->val.first);
  76.         node = ins;
  77.     }
  78.     else tinsert(node->val.first<=ins->val.first?node->r:node->l,ins);
  79.     usz(node);
  80. }
  81. void terase(pnode &node,ii val){
  82.     if (!node) return;
  83.     if (node->val == val){
  84.         pnode temp = node;
  85.         merge(node,node->l,node->r);
  86.         free(temp);
  87.     }
  88.     else if (node->val.first<val.first) terase(node->r,val);
  89.     else terase(node->l,val);
  90.     usz(node);
  91. }
  92. long long tquery(pnode node,int val){
  93.     if (!node) return 0;
  94.     if (node->val.first==val) return sz(node->r);
  95.     if (node->val.first>val) return sz(node->r) + node->val.second+ tquery(node->l,val);
  96.     return tquery(node->r,val);
  97. }
  98.  
  99. void insert(int node,int l,int r,int idx,ii val){
  100.     pnode temp = new treap(val);
  101.     tinsert(tree[node],temp);
  102.     if (l==r) return;
  103.     int mid = (l+r)>>1;
  104.     if (idx<=mid) insert(node<<1,l,mid,idx,val);
  105.     else insert(node<<1|1,mid+1,r,idx,val);
  106. }
  107.  
  108. void update(int node,int l,int r,int idx,ii old,ii val){
  109.     pnode temp = new treap(val);
  110.     terase(tree[node],old);
  111.     tinsert(tree[node],temp);
  112.     if (l==r) return;
  113.     int mid = (l+r)>>1;
  114.     if (idx<=mid) update(node<<1,l,mid,idx,old,val);
  115.     else update(node<<1|1,mid+1,r,idx,old,val);
  116. }
  117. long long query(int node,int l,int r,int ll,int rr){
  118.     if (l>rr || r<ll) return 0;
  119.     if (l>=ll && r<= rr) return tquery(tree[node],rr);
  120.     int mid = (l+r)>>1;
  121.     return query(node<<1,l,mid,ll,rr)+query(node<<1|1,mid+1,r,ll,rr);
  122. }
  123. #define sit set<int>::iterator
  124. #define mp(a,b) make_pair((a),(b))
  125. void process(int a,long long b){
  126.     sit it = guide[arr1[a]].lower_bound(a);
  127.     if (it!= guide[arr1[1]].begin()) it--;
  128.     if (it!=guide[arr1[a]].end() && (*it)<a){
  129.         int idx = (*it);
  130.         update(1,1,n,idx,mp(a,arr1[a]),mp(nex[a],arr1[a])),nex[idx] = nex[a];
  131.     }
  132.     it = guide[b].lower_bound(a);
  133.     if (it!= guide[b].begin()) it--;
  134.     if (it!=guide[b].end() && (*it)<a){
  135.         int idx = (*it);
  136.         update(1,1,n,idx,mp(nex[idx],b),mp(a,b));
  137.         update(1,1,n,a,mp(nex[a],arr1[a]),mp(nex[idx],b));      
  138.         nex[a] = nex[idx];
  139.         nex[idx] = a;
  140.     }
  141.     else{
  142.         it = guide[b].upper_bound(a);
  143.         if (it!=guide[b].end()){
  144.             int idx = (*it);
  145.             update(1,1,n,a,mp(nex[a],arr1[a]),mp(idx,b)),nex[a] = idx;
  146.         }
  147.         else update(1,1,n,a,mp(nex[a],arr1[a]),mp(inf,b)),nex[a] = inf;
  148.     }
  149.     guide[arr1[a]].erase(a);
  150.     guide[b].insert(a);
  151.     arr1[a] = b;
  152. }
  153.  
  154.  
  155.  
  156. int main(){
  157. #ifndef ONLINE_JUDGE
  158.     readFile;
  159. #endif
  160.     fastIO;
  161.     memset(tree,NULL,sizeof(tree));
  162.     cin>>n;
  163.     for(int i=1;i<=n;i++) cin>>arr[i].first,arr[i].second = i,arr1[i] = arr[i].first;
  164.     sort(arr+1,arr+n+1);
  165.     nex[arr[1].second] = inf;
  166.     guide[arr[1].first].insert(arr[1].second);
  167.     for(int i=2;i<=n;i++){
  168.         nex[arr[i].second] = inf;
  169.         if (arr[i].first == arr[i-1].first){
  170.             nex[arr[i-1].second] = arr[i].second;
  171.         }
  172.         guide[arr[i].first].insert(arr[i].second);
  173.     }
  174.     for(int i=1;i<=n;i++){
  175.         insert(1,1,n,i,make_pair(nex[i],arr1[i]));
  176.     }
  177.     cin>>m;
  178.     while (m--){
  179.         cin>>c>>a>>b;
  180.         if (c=='Q') {
  181.             if (a>b) swap(a,b);
  182.             cout<<query(1,1,n,a,b)<<"\n";
  183.         }
  184.         else process(a,b);
  185.     }
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement