HDU 6406(线段树从后面更需查找1 ~ p - 1和 p + 1 ~ n的最大值下标-2018多校第八场)

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define INF 0x3f3f3f3f
#define maxn 105000
#define maxnn 6000
#define juzheng 300
#define line cout << "-------------------------" << endl;
#define PI acos(-1.0)
#define mem(a,b) memset(a,b,sizeof(a))
#define fill_(a,b,n) fill(a,a + n,b)
#define esp 1e-9

#define ri(n) scanf("%d",&n)
#define ri2(a,b) scanf("%d %d",&a,&b)
#define ri3(a,b,c) scanf("%d %d %d",&a,&b,&c)
#define rd(n) scanf("%lf",&n)
#define rd2(a,b) scanf("%lf %lf",&a,&b)
#define rd3(a,b,c) scanf("%lf %lf %lf",&a,&b,&c)
#define rl(n) scanf("%lld",&n)
#define rl2(a,b) scanf("%lld %lld",&a,&b)
#define rl3(a,b,c) scanf("%lld %lld %lld",&a,&b,&c)
#define rui(n) scanf("%u",&n)
#define rui2(a,b) scanf("%u %u",&a,&b)
#define rui3(a,b,c) scanf("%u %u %u",&a,&b,&c)
#define rs(str) scanf("%s",str)
#define pr(n) cout << n << endl
#define debug(str,x) cout << str << ":" << x << endl
#define ll long long
#define int64 __int64
#define ui unsigned int

using namespace std;

const ll mod = 1e9 + 7;

//Date:2018-8-16
//Author:HarryBlackCat

struct xx {
	int l,r;
	int sum;
} tree[maxn * 4];

int n,m,p,q,max2,arr[maxn],d1[maxn],d2[maxn],index,mark[maxn * 4],ans;

void init() {
	max2 = -INF;
	mem(mark,0);
	mem(d1,0);
	mem(d2,0);
}

void build_tree(int l,int r,int root) {
	tree[root].l = l;
	tree[root].r = r;
	tree[root].sum = 0;

	if(l == r)
		return;

	int mid = (l + r) >> 1;
	build_tree(l,mid,root << 1);
	build_tree(mid + 1,r,root << 1 | 1);
}

void query_max_index(int l,int r,int root,int val) {
	if(tree[root].l == tree[root].r) {
		if(tree[root].sum > val)
			index = min(index,tree[root].l);
		return ;
	}

	int mid = (tree[root].l + tree[root].r) >> 1;
	if(tree[root].l >= l && tree[root].r <= r) {
		if(tree[root << 1].sum > val)
			query_max_index(l,r,root << 1,val);
		else if(tree[root << 1 | 1].sum > val)
			query_max_index(l,r,root << 1 | 1,val);
		return;
	}

	if(l <= mid)
		query_max_index(l,r,root << 1,val);
	if(r > mid)
		query_max_index(l,r,root << 1 | 1,val);
}

void pushdown(int root) {
	tree[root].sum = max(tree[root << 1].sum,tree[root << 1 | 1].sum);
	if(tree[root << 1].sum >= tree[root << 1 | 1].sum)
		mark[root] = mark[root << 1];
	else
		mark[root] = mark[root << 1 | 1];
}

void update(int root,int idx,int val) {
	if(tree[root].l == tree[root].r) {
		tree[root].sum = val;
		mark[root] = tree[root].l;
		return;
	}

	int mid = (tree[root].r + tree[root].l) >> 1;
	if(idx <= mid){
		update(root << 1,idx,val);
	}
	else
		update(root << 1 | 1,idx,val);
	pushdown(root);
}

void query_max_index2(int l,int r,int root) {
	int mid = (tree[root].l + tree[root].r) >> 1;
	if(tree[root].l >= l && r >= tree[root].r) {
		if(tree[root].sum > arr[index])
			index = mark[root];
		return;
	}

	if(l <= mid)
		query_max_index2(l,r,root << 1);
	if(r > mid)
		query_max_index2(l,r,root << 1 | 1);
}

int main() {
	//cin.sync_with_stdio(false);//降低cin,cout时间
	int  t;
	while(~ri(t)) {
		while(t--) {
			init();

			ri2(n,m);
			for(int i = 1; i <= n; i++) {
				ri(arr[i]);
				if(arr[i] > max2) {
					d1[i] = d1[i - 1] + 1;
					max2 = arr[i];
				} else
					d1[i] = d1[i - 1];
			}

			build_tree(1,n,1);

			for(int i = n; i >= 1; i--) {
				index = n + 1;
				query_max_index(1,n,1,arr[i]);
				if(index > n)
					index = 0;
				d2[i] = d2[index] + 1;
				update(1,i,arr[i]);
			}

			while(m--) {
				ri2(p,q);
				ans = index = 0;
				if(p != 1)
					query_max_index2(1,p - 1,1);
				ans += d1[index];

				if(q > arr[index])
					ans++;
				else
					q = arr[index];

				index = n + 1;
				if(p != n)
					query_max_index(p + 1,n,1,q);
				if(index <= n)
					ans += d2[index];

				pr(ans);
			}
		}
	}
	return 0;
}

 

你可能感兴趣的:(HDU 6406(线段树从后面更需查找1 ~ p - 1和 p + 1 ~ n的最大值下标-2018多校第八场))