[NOIP simulation test]: The big news (Chairman of the tree)

Title Description

The elderly in the Internet, most like to see young men make big news.
On a news page, a start has $ n $ large news article, every news has a positive integer value of $ naive $, there is order $ m $ events occur.
A total of $ 3 $ kinds of events:
$ 1 $ the beginning of the big news is deleted.
$ 2 $ a $ naive $ value of $ x $ of the big news is added to the top.
3. elders asking $ $ $ $ l from to $ naive $ value of $ r $ big news in $ naive $ value of $ k $ small news.
As all the reporters in the fastest man, you are responsible to answer questions of the elderly (certainly not for $ -1s $ ah).


Input Format

The first two acts number $ n, m $, meaning as the title.
The next line number $ n $, representing the beginning of $ n $ naive $ value $ strips big news.
Next $ m $ lines of operation, the input format is as follows:
read $ 1 $, on behalf of the first event.
Read $ 2, x $, represents the second event.
Read $ 3, l, r, k $, on behalf of a third event.


Output Format

For each event a third output line integer representing the answer


Sample

Sample input:

6 8
2 7 4 3 5 9
3 2 5 3
1
2 4
3 1 4 2
2 6
3 1 7 5
1
3 3 6 4

Sample output:

5
4
6
9


Data range and tips

Sample explained:

The initial sequence $ 2,7,4,3,5,9 $.
7,4,3,5 asking $ $ $ $ 3 the first small value, the answer is $ 5 $.
After the sequence of operations becomes 7,4,3,5,9 $ $.
After the sequence of operations becomes 4,7,4,3,5,9 $ $.
Asking $ 4,7,4,3 $ first $ 2 $ small value, the answer is $ 4 $.
After the sequence of operations becomes 6,4,7,4,3,5,9 $ $.
$ 6,4,7,4,3,5,9 $ interrogation sequence of the first $ 5 $ small value, the answer is $ 6 $.
After the sequence of operations becomes 4,7,4,3,5,9 $ $.
Interrogation sequence of the small value of $ 4 $ $ $ 4,3,5,9, the answer is $ 9 $.

data range:

For $ 30 \% $ data satisfies $ 1 \ leqslant n, m \ leqslant 2,000 $.
For $ 50 \% $ data satisfies $ 1 \ leqslant n, m \ leqslant 30,000 $.
For $ 70 \% $ data satisfies $ 1 \ leqslant n, m \ leqslant 80,000 $.
For $ 100 \% $ data satisfies $ 1 \ leqslant n, m \ leqslant 2 \ times 10 ^ 5 $.
To ensure that data is valid, when there is no news deletion does not exist.
Guaranteed $ 1 \ leqslant naive $ value $ \ leqslant 10 ^ 9 $, guaranteed $ 1 \ leqslant l \ leqslant r \ leqslant $ current number of news, $ k \ leqslant r-l + 1 $.


answer

Observation data and operating range, found to be the Chairman of the board tree problem (but does not play ......).

The first study will not look at it the Chairman of the tree.

Positive Solutions for the direct use of a tree difference, less a $ \ log $.

Time complexity: $ (n + m) \ log ^ 2n $.

Expectations score: $ 100 $ points.

Actual score: $ 100 $ points.


Code time

#include<bits/stdc++.h>
using namespace std;
int n,m;
int a[200001],root[500000],tot;
int trsum[10000001],lson[10000001],rson[10000001];
void insert(int &x,int pre,int l,int r,int w)
{
	x=++tot;
	trsum[x]=trsum[pre]+1;
	lson[x]=lson[pre];
	rson[x]=rson[pre];
	if(l==r)return;
	int mid=(l+r)>>1;
	if(w<=mid)insert(lson[x],lson[pre],l,mid,w);
	else insert(rson[x],rson[pre],mid+1,r,w);
}
int ask(int x,int pre,int l,int r,int k)
{
	if(l==r)return l;
	int mid=(l+r)>>1;
	if(trsum[lson[x]]-trsum[lson[pre]]>=k)return ask(lson[x],lson[pre],l,mid,k);
	else return ask(rson[x],rson[pre],mid+1,r,k-trsum[lson[x]]+trsum[lson[pre]]);
}
int main()
{
	scanf("%d%d",&n,&m);
	for(int i=n;i;i--)
		scanf("%d",&a[i]);
	for(int i=1;i<=n;i++)
		insert(root[i],root[i-1],1,1<<30,a[i]);
	while(m--)
	{
		int opt;
		scanf("%d",&opt);
		switch(opt)
		{
			case 1:n--;break;
			case 2:
				int x;
				scanf("%d",&x);
				n++;
				insert(root[n],root[n-1],1,1<<30,x);
				break;
			case 3:
				int l,r,k;
				scanf("%d%d%d",&l,&r,&k);
				printf("%d\n",ask(root[n-l+1],root[n-r],1,1<<30,k));
				break;
		}
	}
	return 0;
}

rp++

Guess you like

Origin www.cnblogs.com/wzc521/p/11447424.html