// Problem: 买礼物
// Contest: NowCoder
// URL: https://ac.nowcoder.com/acm/contest/9983/E
// Memory Limit: 524288 MB
// Time Limit: 4000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll>PLL;
typedef pair<int, int>PII;
typedef pair<double, double>PDD;
#define I_int ll
inline ll read()
{
ll x = 0, f = 1;
char ch = getchar();
while(ch < '0' || ch > '9')
{
if(ch == '-')f = -1;
ch = getchar();
}
while(ch >= '0' && ch <= '9')
{
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
inline void out(ll x){
if (x < 0) x = ~x + 1, putchar('-');
if (x > 9) out(x / 10);
putchar(x % 10 + '0');
}
inline void write(ll x){
if (x < 0) x = ~x + 1, putchar('-');
if (x > 9) write(x / 10);
putchar(x % 10 + '0');
puts("");
}
#define read read()
#define closeSync ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define multiCase int T;cin>>T;for(int t=1;t<=T;t++)
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define repp(i,a,b) for(int i=(a);i<(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
#define perr(i,a,b) for(int i=(a);i>(b);i--)
ll ksm(ll a, ll b, ll p)
{
ll res = 1;
while(b)
{
if(b & 1)res = res * a % p;
a = a * a % p;
b >>= 1;
}
return res;
}
const int inf = 0x3f3f3f3f;
#define PI acos(-1)
const int maxn=5e5+100;
vector<int>v[maxn*2];
int n,q,a[maxn];
int las[maxn],nex[maxn];
struct node{
int l,r,val;
}tr[maxn*4];
void pushup(int u){
tr[u].val=min(tr[u<<1].val,tr[u<<1|1].val);
}
void build(int u,int l,int r){
tr[u].l=l,tr[u].r=r;
if(l==r) {
tr[u].val=nex[l];
return ;
}
int mid=(l+r)/2;
build(u<<1,l,mid);build(u<<1|1,mid+1,r);
pushup(u);
}
void update(int u,int pos,int val){
if(tr[u].l==tr[u].r){
tr[u].val=val;
return ;
}
int mid=(tr[u].l+tr[u].r)/2;
if(pos<=mid) update(u<<1,pos,val);
else update(u<<1|1,pos,val);
pushup(u);
}
int query(int u,int l,int r){
if(l==tr[u].l&&r==tr[u].r) return tr[u].val;
int mid=(tr[u].l+tr[u].r)/2;
int res=inf;
if(r<=mid) return query(u<<1,l,r);
else if(l>mid) return query(u<<1|1,l,r);
return min(query(u<<1,l,mid),query(u<<1|1,mid+1,r));
}
int main(){
n=read,q=read;
rep(i,1,n) a[i]=read;
rep(i,1,1e6) v[i].push_back(0);
rep(i,1,n) v[a[i]].push_back(i);
rep(i,1,1e6) v[i].push_back(inf);
rep(i,1,1e6){
for(int j=1;j<v[i].size()-1;j++){
int t=v[i][j];
las[t]=v[i][j-1];
nex[t]=v[i][j+1];
}
}
build(1,1,n);
while(q--){
int op=read;
if(op==1){
int x=read;
update(1,x,inf);
update(1,las[x],nex[x]);
nex[las[x]]=nex[x];
if(nex[x]<inf) las[nex[x]]=las[x];
}
else{
int l=read,r=read;
if(query(1,l,r)<=r) puts("1");
else puts("0");
}
}
return 0;
}
牛客 买礼物(链表 线段树)
最新推荐文章于 2024-04-26 19:02:59 发布