2018 Multi-University Training Contest 8 J - Taotao Picks Apples 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.

给定一个序列 ,现在有q 个操作,将 P位置上的数修改为x;
问每次修改后从首位置开始的最长递增子序列的长度;

思路:比较好的一道题;用单调栈维护一下; 说明一下各个变量:s1维护的是所选的序列的数字在原序列的下标,而 s2 是其值,stk 就是一个单调栈;另外注意的一点是,二分一定要会写,处理好边界,

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<string>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
using namespace std;
#define maxn 200005
#define inf 0x3f3f3f3f
#define INF 0x7fffffff
typedef long long  ll;
typedef unsigned long long ull;
#define ms(x) memset(x,0,sizeof(x))
const long long int mod = 1e9 + 7;


inline int read()
{
	int x = 0, k = 1; char c = getchar();
	while (c < '0' || c > '9') { if (c == '-')k = -1; c = getchar(); }
	while (c >= '0' && c <= '9')x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
	return x * k;
}

int T, n, q;
int s1[maxn], s2[maxn], sq;
int ans[maxn];
int val[maxn];
int stk[maxn], bag;
int a[maxn];
struct node {
	int pos, num, id;
}query[maxn];

bool cmp(node a, node b) {
	return a.pos > b.pos;
}

int fin(int *gg, int l, int r, int x) {
	if (gg[r] <= x)return r;
	while (l < r) {
		int mid = (l + r + 1) >> 1;
		if (gg[mid] <= x)l = mid;
		else r = mid - 1;
	}
	return l;
}

int main()
{
	ios::sync_with_stdio(false);
	int T; cin >> T;
	while (T--) {
		cin >> n >> q;
		ms(ans); ms(s1); ms(s2); ms(val); ms(stk); ms(a);
		bag = n; sq = 0;
		int last = -1;
		for (int i = 1; i <= n; i++) {
			cin >> a[i];
			if (last < a[i]) {
				s1[++sq] = i; s2[sq] = a[i]; last = a[i];
			}
			val[i] = sq;
		}
		for (int i = 1; i <= q; i++) {
			cin >> query[i].pos >> query[i].num; query[i].id = i;
		}
		s2[0] = int(-1e9); s1[0] = 0; s1[sq + 1] = s2[sq + 1] = int(1e9);

		sort(query + 1, query + 1 + q, cmp);

		for (int i = n, Q = 1; i >= 1;) {
			if (query[Q].pos == i) {
				int &res = ans[query[Q].id];
				res = 0;
				int left;
				if (s1[sq] < query[Q].pos) {
					left= sq;
				}
				else left = fin(s1, 0, sq, i);

				if (s1[left] == i)left--;
				res = left;
				if (s2[left] < query[Q].num)res++;
				else {
					query[Q].num = s2[left];
				}
				if (stk[bag] > query[Q].num) {
					res += n - bag + 1;
				}
				else {
					if (stk[n] > query[Q].num) {
						int pps = fin(stk, bag, n, query[Q].num);
						res += n - pps;
					}
				
				}
				Q++;
			}
			else {
				if (stk[bag] > a[i]) {
					stk[--bag] = a[i--]; continue;
				}
				int pos = fin(stk, bag, n, a[i]);
				stk[pos] = a[i--]; bag = pos;// 更新栈
			}
		}

		for (int i = 1; i <= q; i++) {
			cout << ans[i] << endl;
		}
	}
	
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值