其他分享
首页 > 其他分享> > HDU - 6406 选苹果

HDU - 6406 选苹果

作者:互联网

There is an apple tree in front of Taotao’s house. When autumn comes, n apples on the tree ripen, and Taotao will go to pick these apples.
When Taotao picks apples, Taotao scans these apples from the first one to the last one. If the current apple is the first apple, or it is strictly higher than the previously picked one, then Taotao will pick this apple; otherwise, he will not pick.
Given the heights of these apples h1,h2,⋯,hn, you are required to answer some independent queries. Each query is two integers p,q, which asks the number of apples Taotao would pick, if the height of the p-th apple were q (instead of hp). Can you answer all these queries?

Input
The first line of input is a single line of integer T (1≤T≤10), the number of test cases.
Each test case begins with a line of two integers n,m (1≤n,m≤105), denoting the number of apples and the number of queries. It is then followed by a single line of n integers h1,h2,⋯,hn (1≤hi≤109), denoting the heights of the apples. The next m lines give the queries. Each of these m lines contains two integers p (1≤p≤n) and q (1≤q≤109), as described in the problem statement.

Output
For each query, display the answer in a single line.

Sample Input
1
5 3
1 2 3 4 4
1 5
5 5
2 3

Sample Output
1
5
3

Hint
For the first query, the heights of the apples were 5, 2, 3, 4, 4, so Taotao would only pick the first apple.
For the second query, the heights of the apples were 1, 2, 3, 4, 5, so Taotao would pick all these five apples.
For the third query, the heights of the apples were 1, 3, 3, 4, 4, so Taotao would pick the first, the second and the fourth apples.

#include<bits/stdc++.h>
using namespace std;
#define maxn 100010
struct Tree{
	int l,r,len;
	int mv,mx;
}tree[maxn<<2];
int a[maxn];
int query(int mx,int k){
	int ans=0;
	if(tree[k].l==tree[k].r && tree[k].mx>mx) return 1;
	if(tree[k].mx<=mx) return 0;
	else if(tree[k<<1].mx<=mx) ans+=query(mx,k<<1|1); //直接在右子树中查找 
	else ans+=(tree[k].mv-tree[k<<1].mv+query(mx,k<<1)); //右子树的价值+左子树的价值 
	return ans;
}
void pushup(int k){
	tree[k].mx=max(tree[k<<1].mx,tree[k<<1|1].mx);
	tree[k].mv=tree[k<<1].mv+query(tree[k<<1].mx,k<<1|1);
}
void build(int l,int r,int k){
	tree[k].l=l; tree[k].r=r; tree[k].len=r-l+1;
	if(l==r){
		tree[k].mv=1;
		tree[k].mx=a[l];
		return;
	}
	int mid=(l+r)>>1;
	build(l,mid,k<<1);
	build(mid+1,r,k<<1|1);
	pushup(k);
}
void update(int x,int y,int k){
	if(tree[k].l==tree[k].r){
		tree[k].mx=y;
		return;
	}
	int mid=(tree[k].l+tree[k].r)>>1;
	if(x<=mid) update(x,y,k<<1);
	else update(x,y,k<<1|1);
	pushup(k);
}
int main(){
	int t; scanf("%d",&t);
	while(t--){
		int n,m; scanf("%d%d",&n,&m);
		for(int i=1;i<=n;i++) scanf("%d",&a[i]);
		build(1,n,1);
		for(int i=1;i<=m;i++){
			int x,y; scanf("%d%d",&x,&y);
			int t=a[x];
			update(x,y,1);
			printf("%d\n",tree[1].mv);
			update(x,t,1);
		}
	}
	return 0;
}

标签:HDU,6406,apple,these,苹果,Taotao,pick,first,apples
来源: https://blog.csdn.net/zt2650693774/article/details/99704372