Topic link: click here~
Topic
- n cabinets, each cabinet’s gift number is
, q times inquired.
- 1 x, which means removing the gift from the x cabinet. 2.
Indicates whether there are two gifts with the same number in the query interval [xi,yi].
- For each query output 0/1.
Ideas
- At first glance, it is a data structure problem, but it is also a thinking problem.
- For each cabinet i, pre[i] represents the subscript of the nearest cabinet with the same gift number as ai before i, and pos[i] represents the subscript of the nearest cabinet with the same gift number as ai after i.
- To construct a line segment tree for pre[i], you only need to query the maximum value of the interval
when querying. If the maximum value of this interval >= , it means that there is a gift in the interval and the previous position is also in the interval. If the condition is met, output 1; otherwise, output 0.
- Then there is the interval modification . If the cabinet x withdraws the gift, then the value in the line segment tree should not work, so we can directly change it to 0 , and it will not affect others. At the same time, we need to update the cabinet j of pre[x] so that pos[j] is no longer x, but pos[x] , which is equivalent to a linked list deletion operation.
ac code
#include<bits/stdc++.h>
using namespace std;
#define io cin.tie(0);ios::sync_with_stdio(false);
#define debug(x) cout<<#x<<"="<<x<<endl
#define lowbit(x) x&(-x)
#define pii pair<int,int>
#define mk make_pair
#define ll long long
#define rs p<<1|1
#define ls p<<1
const int maxn = 1e6 + 5;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
inline ll read(){
ll p=0,f=1;char c=getchar();
while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
while(c>='0'&&c<='9'){p=(p<<1)+(p<<3)+(c^48),c=getchar();}
return f*p;
}
struct node{
int l, r;
int lazy, sum, mx;
}tree[maxn << 2];
int a[maxn], pre[maxn], pos[maxn];
void pushup(int p){
tree[p].mx = max(tree[ls].mx, tree[rs].mx);
}
void build(int p, int l, int r){
tree[p].l = l; tree[p].r = r;
if(l == r){
tree[p].mx = pre[l];
return;
}
int mid = l + r >> 1;
build(ls, l, mid);
build(rs, mid + 1, r);
pushup(p);
}
void update(int p, int l, int r, int val) {//区间的每个值都改为val
if(l <= tree[p].l && tree[p].r <= r){
tree[p].mx = val;
return;
}
int mid = tree[p].l + tree[p].r >> 1;
if(mid >= l) update(ls, l, r, val);
if(mid + 1 <= r) update(rs, l, r, val);
pushup(p);
}
int query(int p, int l, int r){
if(l <= tree[p].l && tree[p].r <= r)
return tree[p].mx;
int ans = -inf;
int mid = tree[p].l + tree[p].r >> 1;
if(mid >= l) ans = max(ans, query(ls, l, r));
if(mid + 1 <= r) ans = max(ans, query(rs, l, r));
return ans;
}
void solve(){
int n, m;
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; i ++){
a[i] = pre[i] = 0;
pos[i]= n + 1;
}
for(int i = 1; i <= n; i ++){
int x; scanf("%d", &x);
pos[a[x]] = i;
pre[i] = a[x];
a[x] = i; //当前x出现的最近位置
}
build(1, 1, n);
while(m --){
int op, x;
scanf("%d", &op);
if(op == 1){
scanf("%d", &x);
update(1, x, x, 0);
if(pos[x] <= n){
update(1, pos[x], pos[x], pre[x]); //如果礼物ax后面还有的话更新
pos[pre[x]] = pos[x];
}
}else{
int l, r;
scanf("%d%d", &l, &r);
printf("%d\n", query(1, l, r) >= l);
}
}
}
int main(){
solve();
return 0;
}