【牛客】2021牛客寒假算法基础集训营3 E-买礼物

题链

线段树记录第i个礼物与之相同的后一个礼物的位置;
LL p[MS]; // 记录第i个礼物 后一个礼物位置
LL q[MS]; // 记录第i个礼物 前一个礼物位置
关于修改:
1.删除第pos个礼物:在线段树中将其置为INF(表示该礼物不存在),删除后该礼物左右所指向的礼物位置则需要更改;
2.第pos个礼物与之相同的前一个礼物,该礼物所指向的后一个礼物位置需要更新,即在线段树上修改p[q[pos]]的值为p[pos];
3.第pos个礼物与之相同的后一个礼物,该礼物所指向的前一个礼物位置需要更新,更新q[p[pos]]为q[pos];

#include <bits/stdc++.h>
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
using namespace std;
#define LL long long
#define ll long long
#define ULL unsigned long long
#define ls rt<<1
#define rs rt<<1|1
#define MS 500009
#define INF 1e18
#define mod 998244353
#define Pi acos(-1.0)
#define Pair pair<LL,LL>

LL n,m,k;
LL p[MS]; // 记录第i个礼物 后一个礼物位置 
LL q[MS]; // 记录第i个礼物 前一个礼物位置 
LL a[MS]; // 原数组 
LL ne[1000009];
LL fr[1000009];
LL tr[MS<<2];

void push_up(int rt){
	tr[rt] = min(tr[ls],tr[rs]);
}

void build(int l,int r,int rt){
	if(l == r){
		tr[rt] = p[l];
		return;
	}
	int m = l+r>>1;
	build(l,m,ls);
	build(m+1,r,rs);
	push_up(rt);
}

void update_change(int L,int R,int l,int r,int rt,LL val){
	if(L <= l && r <= R){
		tr[rt] = val;
		return;
	}
	int m = l+r>>1;
	if(m >= L) update_change(L,R,l,m,ls,val);
	if(m <  R) update_change(L,R,m+1,r,rs,val);
	push_up(rt);
}

LL get_min(int L,int R,int l,int r,int rt){
	if(L <= l && r <= R) return tr[rt];
	int m = l+r>>1;
	LL ans = INF;
	if(m >= L) ans = min(ans,get_min(L,R,l,m,ls));
	if(m <  R) ans = min(ans,get_min(L,R,m+1,r,rs));
	return ans;
}

int main() {
	ios::sync_with_stdio(false);
	cin >> n >> m;
	for(int i=1;i<=n;i++) cin >> a[i];
	for(int i=0;i<=1000000;i++){
		fr[i] = 0;
		ne[i] = INF;
	} 
	for(int i=n;i>=1;i--){
		p[i] = ne[a[i]];
		ne[a[i]] = i;
	}
	for(int i=1;i<=n;i++){
		q[i] = fr[a[i]];
		fr[a[i]] = i;
	}
	
	build(1,n,1);
	while(m--){
		LL op,l,r,pos;
		cin >> op;
		if(op == 1){
			cin >> pos;
			update_change(pos,pos,1,n,1,INF);
			if(q[pos] != 0){
				p[q[pos]] = p[pos];
				update_change(q[pos],q[pos],1,n,1,p[pos]);
			} 
			if(p[pos] != INF){
				q[p[pos]] = q[pos];
			}
		}
		else{
			cin >> l >> r;
			if(get_min(l,r,1,n,1) <= r) cout << 1 << endl;
			else cout << 0 << endl;
		}
	}
	
	
	return 0;
}
posted @ 2021-03-03 17:03  棉被sunlie  阅读(37)  评论(0)    收藏  举报