CF 848C

I heard a good oier is entitled to feed out.

The meaning of problems

Given an array of length n, in the definition of a digital X [l, r] value in a digital X [l, r] in the last occurrence position index by subtracting the index of the first occurrence position

M times a given query, each query contains three integers a, b, c asked the following rules:

When a = 1, b of the inner element of the array to c Change

When a = 2, seek range [b, c], and all digital values

Problem-solving ideas

For difficult to think of each point, a record of his weight and the same index point (which may be called a precursor), provided his weight difference between the two values ​​of the subscripts.

Can then be found [l, r] represents the weight of the weights may be substantially all points is [l, r], and the.

But we soon discovered that we pay more some.

Then correction for all the precursor [l, r] are [l, r] weights and points within.

Then it looked like a query on a two-dimensional plane and a rectangle.

CDQ can divide and conquer

``

include<bits/stdc++.h>

using namespace std;
typedef long long ll;
const int sz=7e5+527;
struct hh{
int x,y,t,val;
}tmp[sz],q[sz];
int n,m;
int tot,cnt;
int type,x,y;
ll a[sz],f[sz];
ll ans[sz];
set S[sz];
set ::iterator it;
int head[sz],lst[sz];
void update(int x,int sum){
for(;x<=n+1;x+=(x&(-x))) f[x]+=sum;
}
ll query(int x){
ll ret=0;
for(;x;x-=x&(-x)) ret+=f[x];
return ret;
}
void cdq(int l,int r){
if(l==r) return;
int mid=(l+r)>>1;
cdq(l,mid),cdq(mid+1,r);
int i=l,j=mid+1,k=l-1;
while(i<=mid||j<=r){
if(j>r || i<=mid && (q[i].x<q[j].x || q[i].x==q[j].x &&q[i].t<q[j].t)){
if(!q[i].t) update(q[i].y,q[i].val);
tmp[++k]=q[i];
i++;
}
else{
if(q[j].t){
int num=abs(q[j].val);
if(q[j].val>0) ans[num]+=query(q[j].y);
else ans[num]-=query(q[j].y);
}
tmp[++k]=q[j];
j++;
}
}
for(int i=l;i<=mid;i++) if(!q[i].t) update(q[i].y,-q[i].val);
for(int i=l;i<=r;i++) q[i]=tmp[i];
}
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
S[a[i]].insert(i);
lst[i]=head[a[i]],head[a[i]]=i;
q[++tot]=(hh){i,lst[i],0,i-lst[i]};
}
for (int i=1; i<=m; ++i){
scanf("%d%d%d",&type,&x,&y);
if (type==1){
int p1=0,n1=0;//前驱 后继
it=S[a[x]].find(x);
if (it!=S[a[x]].begin()) --it, p1= it, ++it;
if ((++it)!=S[a[x]].end()) n1=
it; --it;
S[a[x]].erase( it); q[++tot]=(hh){x,lst[x],0,lst[x]-x};
if(n1){
q[++tot]=(hh){n1,lst[n1],0,lst[n1]-n1};
lst[n1]=p1;
q[++tot]=(hh){n1,lst[n1],0,n1-lst[n1]};
}
int p2=0,n2=0;
a[x]=y; S[a[x]].insert(x);
it=S[a[x]].find(x);
if (it!=S[a[x]].begin()) --it, p2=
it, ++it;
if ((++it)!=S[a[x]].end()) n2=*it; --it;
lst[x]=p2; q[++tot]=(hh){x,lst[x],0,x-lst[x]};
if (n2){
q[++tot]=(hh){n2,lst[n2],0,lst[n2]-n2};
lst[n2]=x;
q[++tot]=(hh){n2,lst[n2],0,n2-lst[n2]};
}
}
else{
++cnt;
q[++tot]=(hh){x-1,x-1,1,cnt};
q[++tot]=(hh){y,y,1,cnt};
q[++tot]=(hh){x-1,y,1,-cnt};
q[++tot]=(hh){y,x-1,1,-cnt};
}
}
for (int i=1; i<=tot; ++i) q[i].x++, q[i].y++;
cdq(1,tot);
for(int i=1;i<=cnt;i++) printf("%lld\n",ans[i]);
}

Guess you like

Origin www.cnblogs.com/river-flows-in-you/p/11291811.html