"Bzoj 3196" tyvj 17,302 Force balance Tree

Source: Internet
Author: User

3196:TYVJ 17,302 Force balance Tree Time limit: ten Sec Memory limit: MB
Submit: 842 Solved: 350
[Submit] [Status] Description

You need to write a data structure (which can refer to the title of the topic) to maintain an ordered sequence, which requires the following actions:
1. Query k ranking within the range
2. The value of the rank k in the query interval
3. Modify values on a single value
4. Query k within the range of the precursor (the precursor is defined as less than X, and the largest number)
5. Query k in the interval of the successor (subsequent definition is greater than X, and the smallest number)

Input

The first row of two numbers n,m indicates an ordered sequence of N and M operations
The second line has n number, indicating ordered sequence
There are m lines below, opt denotes operation label
If Opt=1 is Operation 1, then there are three numbers l,r,k indicating the rank of the query k in the interval [l,r]
If Opt=2 is Operation 2, then there are three numbers l,r,k representing the number of k in the query interval [l,r]
If Opt=3 is Operation 3, then there are two numbers pos,k to change the number of POS positions to K
If Opt=4 is Operation 4, then there are three numbers l,r,k representing the precursor of k in the query interval [l,r]
If the opt=5 is Operation 5, then there are three l,r,k to indicate the successor of K in the query interval [l,r].

Output

For the operation 1,2,4,5 each output row, indicating the query result

Sample Input8 W
4 2 2 1 9 4 0 1 1
2 1 4 3
3 4 10
2 1 4 3
1 2 5 9
4 3 9 5
5 2 8 5Sample Output2
4
3
4
9HINT

Data range for 1.N and M: n,m<=50000


2. Data range for each number in the sequence: [0,1e8]


3. Although the original question is not, the fact that the 5 operation K may be negative


Tree set tree first question. The position segment tree sets the weight value balance tree (treap).


A segment tree is built with the location keyword, and a treap is built on each node of the segment tree with the weight of the keyword.


of five operations Implementation Method :

1. Query k ranking within the range (GetRank)

First find the position of the interval in the segment tree (GetRank), and then find the number of treap on each of the smaller intervals (askrank), the output number +1 can be


2. The value of the rank k in the query interval (getnum)

First the two-point answer (Getnum), and then use the GetRank in Operation 1 to find the current two points out of the number in the interval ranking, if ranked <=k, then in the right half to continue to find, otherwise in the left part


3. Change the value in a location (Modify)

This operation is equivalent to deleting a number and then inserting a number. However, these two operations are performed on each interval in the segment tree that contains this number.


4. Query precursor (GETPRE)

Find the position of the interval in the segment tree, find the maximum number less than k in each interval, and finally take the maximum value


5. Query successor (GETNEXT)

Find the position of the interval in the segment tree, find the smallest number less than k in each interval, and finally take the minimum value


Attention:

1. The process of repeating numeric values is to maintain a weight on treap, indicating that there are several of the same weights as the current node.


2 the query operation should be given to all exits, otherwise it will die loop.


#include <iostream> #include <algorithm> #include <cstring> #include <cstdio> #include < Cstdlib> #define N 3000005using namespace std;struct treap{int fix,weight,size,l,r,data;} A[n];int ans,num,root[n],tot=0,x[50005],n,m;void push_up (int x) {a[x].size=a[a[x].r].size+a[a[x].l].size+a[x]. Weight;} void Zag (int &x) {int o=a[x].l;a[x].l=a[o].r;a[o].r=x;a[o].size=a[x].size; PUSH_UP (x); x=o;} void Zig (int &x) {int o=a[x].r;a[x].r=a[o].l;a[o].l=x;a[o].size=a[x].size; PUSH_UP (x); x=o;} void New_node (int &x,int data) {X=++tot;a[x].weight=a[x].size=1;a[x].data=data;a[x].fix=rand (); a[x].l=a[x].r=0 ;} void Insert (int &x,int data) {if (!x) {new_node (x,data); return;} A[x].size++;if (a[x].data==data) {A[x].weight++;return;} if (data<a[x].data) {Insert (A[x].l,data), if (A[a[x].l].fix<a[x].fix) zag (x);} Else{insert (A[x].r,data); if (A[a[x].r].fix<a[x].fix) zig (x);}} void delet (int &x,int data) {if (a[x].data==data) {if (a[x].weight>1)//Note {a[x].weight--;a[x].size--; return;}    if (a[x].l*a[x].r==0) X=a[x].l+a[x].r;else if (a[a[x].l].fix<a[a[x].r].fix) {zag (x);D Elet (x,data); Why not delet directly here (a[x].r,data)? Because A[x].size will go wrong after deleting this node}else{zig (x);D Elet (x,data); return;} A[x].size--;if (A[x].data>data) delet (a[x].l,data); else Delet (a[x].r,data);}    void Build (int x,int l,int r,int now,int data) {Insert (root[x],data); if (l==r) Return;int m= (l+r) >>1;if (now<=m) build (x<<1,l,m,now,data); else build (X<<1|1,m+1,r, Now,data);} void Askrank (int x,int data) {if (!x) return;if (a[x].data==data) {Ans+=a[a[x].l].size;return;} if (a[x].data>data) {Askrank (a[x].l,data); return;} Ans=ans+a[a[x].l].size+a[x].weight; Askrank (a[x].r,data);} void GetRank (int x,int lx,int rx,int l,int r,int data) {if (lx>=l&&rx<=r) {Askrank (root[x],data); return;} if (LX==RX) Return;int m= (LX+RX) >>1;if (l<=m) GetRank (X<<1,lx,m,l,r,data), if (r>m) Getrank (x< <1|1,m+1,rx,l,r,data);} void Getnum (int l,int r,int k) {int lx=0,rx=100000000;while (lx&LT;=RX) {int m= (LX+RX) >>1;ans=1; GetRank (1,1,n,l,r,m); if (ans<=k) {lx=m+1;num=m;} else rx=m-1;}} void Modify (int x,int l,int r,int p,int data,int c) {delet (root[x],data); Insert (root[x],c); if (l==r) Return;int m= (l+r) & Gt;>1;if (p<=m) Modify (x<<1,l,m,p,data,c); else Modify (x<<1|1,m+1,r,p,data,c);}   void Askpre (int x,int data) {if (!x) return;if (a[x].data>=data) askpre (a[x].l,data); Askpre (A[x].r,data) is not required because the precursor is the smallest of the number else{if (Ans<a[x].data) ans=a[x].data;     Askpre (A[x].r,data); }}void getpre (int x,int lx,int rx,int l,int r,int data) {if (lx>=l&&rx<=r) {askpre (root[x],data); return;} if (LX==RX) Return;int m= (LX+RX) >>1;if (l<=m) getpre (X<<1,lx,m,l,r,data), if (r>m) Getpre (x<<1 |1,m+1,rx,l,r,data);} void Asknext (int x,int data) {if (!x) return;if (a[x].data>data) {if (A[x].data<ans) Ans=a[x].data; Asknext (a[x].l,data);} else Asknext (a[x].r,data);} void Getnext (int x,int lx,int rx,int l,int r,int data) {if (lx>=l&&rx<=R) {Asknext (root[x],data); return;} if (LX==RX) Return;int m= (LX+RX) >>1;if (l<=m) Getnext (X<<1,lx,m,l,r,data), if (r>m) Getnext (x< <1|1,m+1,rx,l,r,data);} void Print (int x) {printf ("%d\n", x);} int main () {scanf ("%d%d", &n,&m), for (Int. i=1;i<=n;i++) scanf ("%d", &x[i]); for (int i=1;i<=n;i++) Build (1,1,n,i,x[i]), while (m--) {int q,l,r,k,p;scanf ("%d", &q), and switch (q) {case 1:scanf ("%d%d%d", &l,&r, &k); Ans=1; GetRank (1,1,N,L,R,K);   Print (ANS); Remember to write Breakcase 2:scanf ("%d%d%d", &l,&r,&k); Getnum (L,R,K); Print (num); Break;case 3:scanf ("%d%d", &l,&k);p =x[l];x[l]=k; Modify (1,1,n,l,p,k); Break;case 4:scanf ("%d%d%d", &l,&r,&k); ans=0; Getpre (1,1,N,L,R,K); Print (ans); Break;case 5:scanf ("%d%d%d", &l,&r,&k); ans=100000000; Getnext (1,1,N,L,R,K); Print (ans); break;}} return 0;}



Sentiment:

The problem with the balance tree is particularly easy to write because the operation and node maintenance values are named similar (ZIG,ZAG), often the difference between tle and AC. So be careful with your questions!



"Bzoj 3196" tyvj 17,302 Force balance Tree

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.