Count Color
Description Chosen Problem Solving and Program design as an optional course, you are required to solve all kinds of problems. Here, we get a new problem. Input First line of input contains L (1 <= L <= 100000), T (1 <= T <= 30) and O (1 <= O <= 100000). Here O denotes the number of operations. Following O lines, each contains "C A B C" or "P A B" (here A, B, C are integers, and A may be larger than B) as an operation defined previously. Output Ouput results of the output operation in order, each line contains a number. Sample Input Sample Output Source |
[Submit] [Go Back] [Status] [Discuss]
题意:操作1:将区间 a - b 都变成 c
操作2:找出区间 a - b 的种类个数。
题解:变成二进制分析要变成某个值的颜色,将该结点更新为 (1<<(vel-1),初始全为 1。
坑点:1, a 不一定比 b 小;
2,记得将 v[](下拉标记清0)
#include<set>
#include<map>
#include<list>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<bitset>
#include<iomanip>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#define eps (1e-8)
#define MAX 0x3f3f3f3f
#define u_max 1844674407370955161
#define l_max 9223372036854775807
#define i_max 2147483647
#define re register
#define pushup() tree[rt]=(tree[rt<<1],tree[rt<<1|1]);
#define nth(k,n) nth_element(a,a+k,a+n); // 将 第K大的放在k位
#define ko() for(int i=2;i<=n;i++) s=(s+k)%i // 约瑟夫
#define ok() v.erase(unique(v.begin(),v.end()),v.end()) // 排序,离散化
#define Catalan C(2n,n)-C(2n,n-1) (1,2,5,14,42,132,429...) // 卡特兰数
using namespace std;
inline int read(){
char c = getchar(); int x = 0, f = 1;
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' & c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
typedef long long ll;
const double pi = atan(1.)*4.;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3fLL;
const int M=63;
const int N=1e5+5;
int tree[N<<2],v[N<<2];
void sett(int l,int r,int rt){
if(l==r){
tree[rt]=1;
return ;
}
int mid=l+r>>1;
sett(l,mid,rt<<1);
sett(mid+1,r,rt<<1|1);
tree[rt]=tree[rt<<1]|tree[rt<<1|1];
}
void fun(int l,int r,int rt){
if(v[rt]){
v[rt<<1]=v[rt];
v[rt<<1|1]=v[rt];
tree[rt<<1]=tree[rt];
tree[rt<<1|1]=tree[rt];
v[rt]=0;
}
}
void upset(int x,int y,int vel,int l,int r,int rt){
if(x<=l&&y>=r){
v[rt]=1<<(vel-1);
tree[rt]=1<<(vel-1);
return ;
}
fun(l,r,rt);
int mid=l+r>>1;
if(x<=mid)
upset(x,y,vel,l,mid,rt<<1);
if(y>mid)
upset(x,y,vel,mid+1,r,rt<<1|1);
tree[rt]=tree[rt<<1]|tree[rt<<1|1];
}
int findd(int x,int y,int l,int r,int rt){
if(x<=l&&y>=r)
return tree[rt];
fun(l,r,rt);
int ans=0;
int mid=l+r>>1;
if(x<=mid)
ans|=findd(x,y,l,mid,rt<<1);
if(y>mid)
ans|=findd(x,y,mid+1,r,rt<<1|1);
return ans;
}
int main(){
int n,t,m;
scanf("%d %d %d",&n,&t,&m);
getchar();
sett(1,n,1);
char c; int a,b,d;
while(m--){
scanf(" %c",&c);
if(c=='C'){
scanf("%d %d %d",&a,&b,&d);
if(b<a) swap(a,b);
upset(a,b,d,1,n,1);
}
else{
scanf("%d %d",&a,&b);
if(b<a) swap(a,b);
int ans=findd(a,b,1,n,1);
// printf("ans == %d\n",ans);
bitset<32>bit(ans);
printf("%d\n",bit.count());
}
}
return 0;
}