bzoj3196 tyvj 17,302 Forced balance tree

Source: Internet
Author: User

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 Input9 6
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

The first question of tree number ... Wrote for a long time ah ...

In fact, it is not difficult ... But the variables are really much ...

Using the segment tree to maintain the interval, each interval will be thrown into a balance tree of all the number of intervals, query for each segment of the tree line in the balance tree to find

This is nlog^2n.

Find the interval K big time also to two points, so in fact the limit data is nlog^3n

On the Bzoj 9.8s ran over ... I'm drunk on this time card, too.

#include <cstdio> #include <iostream> #include <cstring> #include <cstdlib> #include < algorithm> #include <cmath> #include <queue> #include <deque> #include <set> #include <map > #include <ctime> #define LL long long#define INF 0x7ffffff#define pa pair<int,int> #define Pi 3.1415926535897932384626433832795028841971#define N 200010#define M 3000010using namespace Std;inline ll read () {ll x=0    , F=1;char Ch=getchar (); while (ch< ' 0 ' | |    Ch> ' 9 ') {if (ch== '-') F=-1;ch=getchar ();}    while (ch>= ' 0 ' &&ch<= ' 9 ') {x=x*10+ch-' 0 '; Ch=getchar ();} return x*f;} int n,m,treesize,wrk;int l[m],r[m],rnd[m],dat[m],son[m],rep[m];int root[n],a[n];inline void update (int k) {Son[k]=son [L[k]]+son[r[k]]+rep[k];} inline void right_rotate (int &k) {int t=l[k];l[k]=r[t];r[t]=k;son[t]=son[k];update (k); k=t;} inline void left_rotate (int &k) {int t=r[k];r[k]=l[t];l[t]=k;son[t]=son[k];update (k); k=t;} inline void Insert (int &k,int x) {if (!k) {k=++treesize;Dat[k]=x;rnd[k]=rand (); rep[k]=son[k]=1;return;} Son[k]++;if (X==dat[k]) {Rep[k]++;return;} else if (X<dat[k]) {insert (l[k],x); if (Rnd[l[k]]>rnd[k]) right_rotate (k);} else if (X>dat[k]) {insert (r[k],x); if (Rnd[r[k]]>rnd[k]) left_rotate (k);}}    inline void del (int &k,int x) {if (!k) return;        if (dat[k]==x) {if (rep[k]>1) {Rep[k]--;son[k]--;return;}        if (l[k]*r[k]==0) k=l[k]+r[k];        else if (Rnd[l[k]]>rnd[r[k]]) {right_rotate (k);d El (k,x);}    else {left_rotate (k);d El (k,x);}    }else if (X<dat[k]) {del (l[k],x); son[k]--;} else {del (r[k],x); son[k]--;}} inline void buildtree (int k,int l,int r,int x,int dat) {insert (Root[k],dat), if (l==r) Return;int mid= (l+r) >>1;if (x& Lt;=mid) Buildtree (k<<1,l,mid,x,dat); else Buildtree (K<<1|1,mid+1,r,x,dat);} inline void Get_rank (int k,int x) {if (!k) return;if (X==dat[k]) {Wrk+=son[l[k]];return;} if (X<dat[k]) Get_rank (l[k],x), if (X>dat[k]) {Wrk+=son[l[k]]+rep[k];get_rank (r[k],x);}} inline void Ask_rank (int k,intL,int r,int x,int y,int dat) {if (l==x&&r==y) {Get_rank (root[k],dat); return;} int mid= (L+R) >>1;if (y<=mid) Ask_rank (k<<1,l,mid,x,y,dat); else if (X>mid) Ask_rank (k<<1|1, Mid+1,r,x,y,dat); Else{ask_rank (K<<1,l,mid,x,mid,dat); Ask_rank (K<<1|1,mid+1,r,mid+1,y,dat);}} inline int ask_kth (int x,int y,int dat) {int l=0,r=inf,ans=0;while (l<=r) {int mid= (L+R) >>1;wrk=1;ask_rank (1, 1, N,X,Y,MID); if (Wrk<=dat) {l=mid+1;ans=mid;} else r=mid-1;} return ans;} inline void change (int k,int l,int r,int x,int dat,int todel) {del (Root[k],todel); insert (Root[k],dat); if (l==r) return; int mid= (L+R) >>1;if (x<=mid) Change (K<<1,l,mid,x,dat,todel); Todel);} inline void get_pred (int k,int x) {if (!k) return;if (X>dat[k]) {Wrk=max (wrk,dat[k]); get_pred (r[k],x);} else get_pred (l[k],x);} inline void pred (int k,int l,int r,int x,int y,int dat) {if (l==x&&r==y) {get_pred (root[k],dat); return;} int mid= (L+R) >>1;if (Y<=mid) pRed (K<<1,l,mid,x,y,dat), else if (x>mid) pred (K<<1|1,mid+1,r,x,y,dat); Else{pred (K<<1,l,mid,x, Mid,dat);p Red (K<<1|1,mid+1,r,mid+1,y,dat);}} inline void get_succ (int k,int x) {if (!k) return;if (X<dat[k]) {wrk=min (wrk,dat[k]); GET_SUCC (l[k],x);} else GET_SUCC (r[k],x);} inline void succ (int k,int l,int r,int x,int y,int dat) {if (l==x&&r==y) {get_succ (root[k],dat); return;} int mid= (L+R) >>1;if (y<=mid) succ (k<<1,l,mid,x,y,dat); else if (X>mid) succ (K<<1|1,mid+1,r,x, Y,dat); else{succ (K<<1,l,mid,x,mid,dat); succ (K<<1|1,mid+1,r,mid+1,y,dat);}} int main () {n=read (); M=read (); for (int i=1;i<=n;i++) A[i]=read (), for (int i=1;i<=n;i++) Buildtree (1,1,n,i,a[i]); for (int i=1;i<=m;i++) {int opr=read (); if (opr==1) {int x=read (), Y=read (), Z=read (); Wrk=1;ask_rank (1,1,n,x,y,z); printf ("%d\n", wrk);} else if (opr==2) {int x=read (), Y=read (), Z=read ();p rintf ("%d\n", ask_kth (x, Y, z));} else if (opr==3) {int x=read (), Y=read (); Change (1,1,n,x,y,a[x]); a[x]=y;} else if (opr==4) {int X=read (), Y=read (), Z=read (), wrk=0;pred (1,1,n,x,y,z);p rintf ("%d\n", wrk);} else if (opr==5) {int x=read (), Y=read (), Z=read (), WRK=INF;SUCC (1,1,n,x,y,z);p rintf ("%d\n", wrk);}} return 0;}

bzoj3196 tyvj 17,302 Forced 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.