原题
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.
There is a very long board with length L centimeter, L is a positive integer, so we can evenly divide the board into L segments, and they are labeled by 1, 2, ... L from left to right, each is 1 centimeter long. Now we have to color the board - one segment with only one color. We can do following two operations on the board:
1. "C A B C" Color the board from segment A to segment B with color C.
2. "P A B" Output the number of different colors painted between segment A and segment B (including).
In our daily life, we have very few words to describe a color (red, green, blue, yellow…), so you may assume that the total number of different colors T is very small. To make it simple, we express the names of colors as color 1, color 2, ... color T. At the beginning, the board was painted in color 1. Now the rest of problem is left to your.
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.
样例
Input | Output |
---|---|
2 2 4 C 1 1 2 P 1 2 C 2 2 2 P 1 2 | 2 1 |
思路
因为颜色的数量≤30, 所以可以用二进制的方式存储,然后就是线段树维护区间和的模板,把 加法改成 或运算 ( | ) , 二进制的第一位为空 从 2^1 , 2^2 位置开始标记颜色。
代码
#include<iostream>
#include<algorithm>
using namespace std;
#define ll long long
const int N=1e5+5;
ll v[4*N];
ll lazy[4*N];
void push_up(int x) {
v[x]=v[x << 1] | v[x << 1 | 1];
}
void build(int l, int r, int x) {
if(l == r) {
v[x]=2;
return ;
}
int mid = (l + r) >> 1;
build(l, mid, x << 1) ;
build(mid + 1, r, x << 1 | 1) ;
push_up(x);
}
void push_down(int x) {
v[x << 1] = lazy[x];
lazy[x << 1] = lazy[x];
v[x << 1 | 1] = lazy[x];
lazy[x << 1 | 1] = lazy[x];
lazy[x]=0;
}
void modify(int l, int r, int pl, int pr, int p, int color) {
if( pl == l && pr == r ) {
v[p]= (1 << color);
lazy[p] = (1 << color);
return ;
}
if(lazy[p])
push_down(p);
int mid = l + r >> 1;
if (pl <= mid) modify(l, mid, pl, min(mid, pr), p << 1, color);
if (pr > mid) modify(mid+1, r, max(mid+1, pl), pr, p << 1 | 1, color);
push_up(p);
}
int query(int l, int r, int ql, int qr, int k) {
ll res=0;
if( l == ql && r == qr)
return v[k];
if(lazy[k])
push_down( k );
int mid = l + r >> 1;
if(ql <= mid ) res |= query(l, mid, ql, min(mid, qr), k << 1);
if(qr > mid) res |= query(mid+1, r, max(mid+1, ql), qr, k << 1 | 1);
return res;
}
int n,nc,m;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> nc >> m;
build(1,n,1);
while( m-- ) {
char key; int l, r, color;
cin >> key;
if(key == 'C') {
cin >> l >> r >> color;
int L=min(l,r);
int R=max(l,r);
modify(1, n, L, R, 1, color);
}
else {
cin >> l >> r;
int L=min(l,r);
int R=max(l,r);
ll ans= query(1, n, L, R, 1);
ans >>=1;
int cnt=0;
while(ans) {
ans-= ans&-ans;
cnt++;
}
cout<<cnt<<endl;
}
}
// for(int i=1;i<4*n;i++)
// cout<<i<<" "<<v[i]<<" "<<lazy[i]<<endl;
// cout<<endl;
// for(int i=1;i<=n;i++)
// cout<<i<<" "<<query(1, n, i, i, 1)<<endl;
return 0;
}