HDU 5193 Go to Moviesⅱ block list of nested tree arrays (reverse pairs with insertions and deletions)

Source: Internet
Author: User
Tags arrays split


HDU 5193
Test instructions: gives a sequence of n number a,m operations.
Operation 1:[x,y] Inserts y into the first x person.
Operation 2:[x], delete the person X (x+1,.. n one cell).
N,m,a[i]<=2e4. Q. What is the inverse pair (i,j) of sequence A after each operation? (I<j && a[i]>a[j]).


If the current reverse is res, then insert a number y to know that [x+1,n] has more than y smaller, [1..x-1] directly have more than Y larger.
Insert, how does the delete operation handle subscript?


At this point a thing called a block list is used, and each element in the list is an array,
An array size of up to 2SQRT (n) if it exceeds 2SQRT (n), the split operation is used.
If the number of adjacent table elements <=sqrt (n) merges the two tables. Both the insert and delete operations of the Block list are sqrt (n) drops.


Now for each block in the linked list, set a tree-like array.
After x, the number of the list is larger than x, and the number of queries to its bit is greater than X for each walk.

X before how many of the smaller than X-linked list go forward. The number of small to x in each block is queried.

Then for the elements within the block, sqrt (n) brute force query can be.


#include <bits/stdc++.h> using namespace std;
typedef pair<int,int> II;
const int n=2e4+5,m=320; int lowbit (int x) {return x&-x;} void Add (int c[],int x,int val) {for (int i=x;i<n;i+=lowbit (i)) c[i]+=v
Al
    } int sum (int c[],int l,int r) {int sum1=0,sum2=0;
        while (l>0) {sum1+=c[l];
    L-=lowbit (l);
        } while (r>0) {sum2+=c[r];
    R-=lowbit (R);
} return SUM2-SUM1;
    } struct data{int s,a[n*2];
    Data *next;
    int c[n];
        Data () {memset (c,0,sizeof (c));
    Next=null;
}
};
Data *root;
        void Insert (int x,int pos) {if (root==null) {root=new data;
        root->s=1;
        root->a[1]=x;
    Add (root->c,x,1);//return;
    } data *k=root;
        while (pos> k->s && k->next!=null) {pos-=k->s;
    k=k->next;
    } memmove (k->a+pos+1,k->a+pos,sizeof (int) * (k->s-pos+1));
  k->s++;  k->a[pos]=x;

    Add (k->c,x,1);
        Split if (k->s==2*m) {data *t=new data;
        t->next=k->next;
        k->next=t;
        memcpy (t->a+1,k->a+m+1,sizeof (int) *m);
            for (int i=1;i<=m;i++) {Add (k->c,t->a[i],-1);
        Add (t->c,t->a[i],1);
    } t->s=k->s=m;
    }} int find (int pos) {data *k=root;
        while (Pos>k->s && k->next!=null) {pos-=k->s;
    k=k->next;
} return k->a[pos];
    } int work (int pos) {int res=0;
    Data *k=root;
    int X=find (POS);
        while (Pos>k->s && k->next!=null) {pos-=k->s;
    Res+=sum (k->c,x,n);//large than x k=k->next;
    } for (int i=1;i<pos;i++) if (k->a[i]>x) res++;
    for (int i=pos+1;i<=k->s;i++) if (k->a[i]<x) res++; while (k->next!=null) {K=k->next;
    Res+=sum (k->c,0,x-1);
} return res;
    } void Destroy (data *k) {if (K->next!=null) destroy (K->next);
Delete k;
    } void del (int pos) {data *k=root;
        while (pos>k->s&&k->next!=null) {pos-=k->s;
    k=k->next;
    } add (K->c,k->a[pos],-1);
    Memmove (k->a+pos,k->a+pos+1,sizeof (int) * (k->s-pos));
k->s--;
    } int main () {int n,p;
        while (~SCANF ("%d%d", &n,&p)) {root=null;
        int ans=0,x;
            for (int i=1;i<=n;i++) {scanf ("%d", &x);
            Insert (x,i);
        Ans+=work (i);
            } while (p--) {int q,x,y;
            scanf ("%d", &q);
                if (q==0) {scanf ("%d%d", &x,&y);
                x + +;
                Insert (Y,X);
            Ans+=work (x);
         } else {scanf ("%d", &x);       Ans-=work (x);
            Del (x);
        } printf ("%d\n", ans);
    } destroy (root);
} return 0;
 }


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.