代码:
//线段树
#include
#include
#include
#include
#include
using namespace std;
#define MS(x,y) memset(x,y,sizeof(x))
const int MAXN=1e5+5;
const int MAXM=1e6+5;
int n,q;
int col[MAXN];
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
struct Node{
int cnt,l,r,lazy;
Node operator+(const Node& rhs) const {
Node ret;
ret.l=l;
ret.r=rhs.r;
ret.cnt=cnt+rhs.cnt;
ret.lazy=-1;
if(r==rhs.l){
--ret.cnt;
}
return ret;
}
}node[MAXN<<2],tmp;
vector colsgm[MAXM];
inline void pushup(int rt){
int l=(rt<<1),r=(rt<<1|1);
node[rt]=node[l]+node[r];
}
inline void pushdown(int rt){
if(node[rt].lazy==-1) return ;
int l=(rt<<1),r=(rt<<1|1);
node[l].cnt=node[r].cnt=1;
node[l].lazy=node[r].lazy=node[rt].lazy;
node[l].l=node[r].l=node[l].r=node[r].r=node[rt].lazy;
node[rt].lazy=-1;
}
void build(int l,int r,int rt){
if(l==r){
node[rt].l=node[rt].r=col[l];
node[rt].cnt=1;
node[rt].lazy=-1;
return ;
}
int m=(l+r)>>1;
build(lson);
build(rson);
pushup(rt);
}
void update(int x,int L,int R,int l,int r,int rt){
if(L<=l&&r<=R){
node[rt].l=node[rt].r=x;
node[rt].cnt=1;
node[rt].lazy=x;
return ;
}
pushdown(rt);
int m=(l+r)>>1;
if(L<=m) update(x,L,R,lson);
if(m>1;
Node ret;
ret.cnt=0;
ret.l=ret.r=-1;
bool flag=false;
if(L<=m) ret=query(L,R,lson),flag=true;
if(m=0;--i){
update(y,colsgm[x][i].l,colsgm[x][i].r,1,n,1);
colsgm[y].push_back(colsgm[x][i]);
colsgm[x].pop_back();
}
}
else{
printf("%d\n",query(x,y,1,n,1).cnt);
}
}
}
}
//树状数组
#include
#include
#include
#include
using namespace std;
#define MS(x,y) memset(x,y,sizeof(x))
#define lowbit(x) (x&(-x))
const int MAXN=1e5+5;
const int MAXM=1e6+5;
int val[MAXN],ar[MAXN],f[MAXM],siz[MAXM];
int n;
struct Edge{
int v,nxt;
}edge[MAXN];
int head[MAXM],edgenum;
void addedge(int u,int v){
edge[edgenum].v=v;
edge[edgenum].nxt=head[u];
head[u]=edgenum++;
}
void add(int x,int v){
for(;xsiz[y]) swap(x,y);
siz[y]+=siz[x];
siz[x]=0;
f[t]=y;
f[s]=0;
for(int i=head[x];~i;i=edge[i].nxt){
int &v=edge[i].v;
if(val[v]!=val[v-1]) add(v,-1);
if(val[v+1]!=val[v]) add(v+1,-1);
val[v]=y;
if(val[v]!=val[v-1]) add(v,1);
if(val[v+1]!=val[v]) add(v+1,1);
if(edge[i].nxt==-1){
edge[i].nxt=head[y];
head[y]=head[x];
head[x]=-1;
break;
}
}
head[x]=-1;
}
}
}
}