计量大学 H Happy Time is Always Short

H Happy Time is Always Short

The happy time is always short. The party comes to an end, and the security guard GYQGYQGYQ can finally rest for a while. He was so miserable in the beginning that he didn't have the energy to complete the final task seriously.

Everyone at the party has a strength value expressed as nbinb_inbi. They will leave in a specific order. GYQGYQGYQ's original task is to count the maximum strength of the remaining people in all the different intervals, but he does not have the energy to calculate it carefully, so he can only submit a report to the senior management ZXZXZX to represent the departure of a particular group of people. As for the maximum value, ZXZXZX can only calculate by himself in order to provide different levels of service, which is going to drive him crazy. Please Help him!

Given a report submitted by GYQGYQGYQ, there are two integers NNN and MMM, representing that there were NNN people at the beginning and MMM leaving records. After that, given an nbnbnb array of NNN numbers, representing each person's strength value. Then followed MMM lines, each line has two numbers lll and rrr, representing all the people whose number iii is in this range (llliiirrr) had left. Of course, each person will leave only once and will not return, but due to GYQGYQGYQ's carelessness, the record may be overlap. You need to tell ZXZXZX the maximum strength of all the remaining people after each recording. If nobody is here, the answer is 000.

Input Specification:

There are multiple test cases. The first line of input contains an integer TTT (111TTT101010), indicating the number of test cases. For each test case:

The first line contains two integers NNN, MMM (111NNN, MMM10510^5105), indicating the number of plates.

The second line contains NNN integers, nb1nb_1nb1 , nb2nb_2nb2 ,……, nbnnb_nnbn (111nbinb_inbi10910^9109), indicating the strength value of person numbered iii.

Then, in the following next MMM lines, each line contains two integers ljl_jlj, rjr_jrj (111ljl_jljrjr_jrjNNN), indicating the jjj-th range.

Note: This problem has huge input data; it's recommended to use scanf instead of cin to read data in order to avoid time limit exceed.

Output Specification:

For each test case, print MMM lines, each line contains an integer, indicating the maximum strength value of people who still stay here.

Sample Input:

1
5 3
2 1 5 3 5
3 4
2 5
2 3

  
  

Sample Output:

5
2
2

  
  

题目大意

欢乐的时光总是短暂的。大家在一起的聚会也总有结束的时刻吧。
现在会场里有 N N N个人,然后每次都需要置定一个区间 [ L , R ] [L,R] [L,R],令这些人离开,每个人都有一个权重,需要在人们每次离开之后都输出当前存在的所有人权重最大的数值。

简析

线段树维护区间最大值乱搞。模板题。

参考代码

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 10;

int a[N];
int n;

struct Node
{
	int l, r;
	//区间最大值
	int mx;
	int lazy;
}tr[N * 4];

void pushup(int u)
{
	tr[u].mx = max(tr[u << 1].mx, tr[u << 1 | 1].mx);
}

void pushdown(int u)
{
	if (tr[u].lazy)
	{
		tr[u].lazy = 0;
		tr[u << 1].mx = 0;
		tr[u << 1 | 1].mx = 0;
		tr[u << 1].lazy = tr[u << 1 | 1].lazy = 1;
	}
}

void build(int u, int l, int r)
{
	tr[u] = { l, r, 0, 0 };
	if (l == r)
	{
		tr[u].mx = a[l];
		return;
	}
	int mid = l + r >> 1;
	build(u << 1, l, mid), build(u << 1 | 1, mid + 1, r);
	pushup(u);
}

int query(int u, int l, int r)
{
	if (l <= tr[u].l && r >= tr[u].r)
		return tr[u].mx;
	pushdown(u);
	int mid = tr[u].l + tr[u].r >> 1;
	int res = 0;
	if (l <= mid) res = max(res, query(u << 1, l, r));
	if (r > mid) res = max(res, query(u << 1 | 1, l, r));
	return res;
}

void modify(int u, int l, int r)
{
	if (l <= tr[u].l && r >= tr[u].r)
	{
		tr[u].lazy = 1;
		tr[u].mx = 0;
		return;
	}
	pushdown(u);
	int mid = tr[u].l + tr[u].r >> 1;
	if (l <= mid) modify(u << 1, l, r);
	if (r > mid) modify(u << 1 | 1, l, r);
	pushup(u);
}

int main() {
	// freopen("in.txt", "r", stdin);
	int t;
	scanf("%d", &t);
	while (t--) {
		int m;
		scanf("%d%d", &n, &m);
		for (int i = 1; i <= n; i++) scanf("%d", a + i);
		build(1, 1, n);
		while (m--) {
			int l, r;
			scanf("%d%d", &l, &r);
			modify(1, l, r);
			int ans = query(1, 1, n);
			printf("%d\n", ans);
		}
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值