其他分享
首页 > 其他分享> > GDKOI 2021 提高组 Day2 第二题 群岛(线段树)

GDKOI 2021 提高组 Day2 第二题 群岛(线段树)

作者:互联网

GDKOI 2021 提高组 Day2 第二题 群岛

题目大意

题解

代码

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define N 100010
int a[N];
struct {
	int r, mi, p;
}f[N * 4];
void make(int v, int l, int r) {
	f[v].r = r, f[v].mi = 0;
	if(l == r) return;
	int mid = (l + r) / 2;
	make(v * 2, l, mid), make(v * 2 + 1, mid + 1, r);
}
int find(int v, int l, int r, int x, int y) {
	if(x > y) return -1;
	if(l == x && r == y) {
		if(f[v].mi == 0) return f[v].r;
		return -1;
	}
	else {
		int mid = (l + r) / 2;
		f[v * 2].p += f[v].p, f[v * 2 + 1].p += f[v].p;
		f[v * 2].mi += f[v].p, f[v * 2 + 1].mi += f[v].p;
		f[v].p = 0;
		int s; 
		if(y <= mid) s = find(v * 2, l, mid, x, y);
		else if(x > mid) s = find(v * 2 + 1, mid + 1, r, x, y);
		else {
			int t = find(v * 2 + 1, mid + 1, r, mid + 1, y);
			if(t != -1) s = t; else s = find(v * 2, l, mid, x, mid);
		}
		f[v].mi = min(f[v * 2].mi, f[v * 2 + 1].mi);
		if(f[v * 2].mi < f[v * 2 + 1].mi) f[v].r = f[v * 2].r; else f[v].r = f[v * 2 + 1].r;
		return s;
	}
}
void ins(int v, int l, int r, int x, int y, int c) {
	if(x > y) return;
	if(l == x && r == y) f[v].mi += c, f[v].p += c;
	else {
		int mid = (l + r) / 2;
		f[v * 2].p += f[v].p, f[v * 2 + 1].p += f[v].p;
		f[v * 2].mi += f[v].p, f[v * 2 + 1].mi += f[v].p;
		f[v].p = 0;
		if(y <= mid) ins(v * 2, l, mid, x, y, c);
		else if(x > mid) ins(v * 2 + 1, mid + 1, r, x, y, c);
		else ins(v * 2, l, mid, x, mid, c), ins(v * 2 + 1, mid + 1, r, mid + 1, y, c);
		f[v].mi = min(f[v * 2].mi, f[v * 2 + 1].mi);
		if(f[v * 2].mi < f[v * 2 + 1].mi) f[v].r = f[v * 2].r; else f[v].r = f[v * 2 + 1].r;
	}
}
int main() {
	int n, Q, i, j;
	scanf("%d%d", &n, &Q);
	make(1, 1, n);
	for(i = 1; i <= n; i++) {
		scanf("%d", &a[i]);
		ins(1, 1, n, a[i] + 1, i, 1); 
	}
	while(Q--) {
		int op, x, y;
		scanf("%d", &op);
		if(op == 1) {
			scanf("%d%d", &x, &y);
			ins(1, 1, n, a[x] + 1, x, -1);
			a[x] = y;
			ins(1, 1, n, a[x] + 1, x, 1);
		}
		else {
			scanf("%d", &x);
			int t = find(1, 1, n, 1, x);
			printf("%d\n", t == -1 ? i : t);
		}	
	}
	return 0;
}

自我小结

标签:return,int,mi,Day2,mid,else,2021,GDKOI,ins
来源: https://blog.csdn.net/qq_39565901/article/details/113485889