题目大意
给你一个序列,让你进行以下操作:
- 修改一个区间的数
- 查询区间不同数字个数
解题思路
因为数字个数很少,可以直接用bitset存,然后套线段树
code
#include<bitset>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define ll long long
#define N 100100
using namespace std;
int n,m,k,x,y,z;
char c;
struct node
{bitset<40>d;
};
node merge(node a,node b)
{node c;c.d=a.d|b.d;return c;
}
struct Tree
{#define ls x*2#define rs x*2+1int lazy[N<<2];node s[N<<2];void push_up(int x){s[x]=merge(s[ls],s[rs]);return;}void get(int x,int y){s[x].d.reset();s[x].d.set(y,1);lazy[x]=y;return;}void push_down(int x){if(lazy[x]){get(ls,lazy[x]);get(rs,lazy[x]);lazy[x]=0;}return;}void change(int x,int L,int R,int l,int r,int y){if(L==l&&R==r){get(x,y);return;}push_down(x);int mid=L+R>>1;if(r<=mid)change(ls,L,mid,l,r,y);else if(l>mid)change(rs,mid+1,R,l,r,y);else change(ls,L,mid,l,mid,y),change(rs,mid+1,R,mid+1,r,y);push_up(x);return;}node ask(int x,int L,int R,int l,int r){if(L==l&&R==r)return s[x];push_down(x);int mid=L+R>>1;if(r<=mid)return ask(ls,L,mid,l,r);else if(l>mid)return ask(rs,mid+1,R,l,r);else return merge(ask(ls,L,mid,l,mid),ask(rs,mid+1,R,mid+1,r));}
}T;
int main()
{scanf("%d%d%d",&n,&m,&k);T.get(1,1);while(m--){c=getchar();while(c!='C'&&c!='P')c=getchar();if(c=='C'){scanf("%d%d%d",&x,&y,&z);T.change(1,1,n,x,y,z);}else{scanf("%d%d",&x,&y);printf("%d\n",T.ask(1,1,n,x,y).d.count());}}return 0;
}