第一次了解到线段树的lazy思想
422ms
#include <cstdio>
#include <cstring>
using namespace std;
#define N 100010
struct node{
int l, r;
int col;
int lazy;//延迟标记
}Tree[N * 4];
///
int L, T, O;
void build(int l, int r, int x)
{
Tree[x].l = l;
Tree[x].r = r;
Tree[x].col = 0;
Tree[x].lazy = 0;
if (l == r)return;
int mid = (l + r) / 2;
build(l, mid, x * 2);
build(mid + 1, r, x * 2 + 1);
}
void push_down(int x)
{
Tree[x * 2].col = Tree[x].col;
Tree[x * 2 + 1].col = Tree[x].col;
Tree[x].lazy = 0;
Tree[x * 2].lazy = 1;
Tree[x * 2 + 1].lazy = 1;
}
void update(int l, int r, int col, int x)
{
if (Tree[x].l == l && r == Tree[x].r)
{
Tree[x].lazy = 1;
Tree[x].col = (1 << (col - 1));
return;
}
if (Tree[x].lazy)
push_down(x);
int mid = (Tree[x].l + Tree[x].r) / 2;
if (mid < l)update(l, r, col, x * 2 + 1);
else if (mid >= r)update(l, r, col, x * 2);
else {
update(l, mid, col, x * 2);
update(mid + 1, r, col, x * 2 + 1);
}
Tree[x].col = (Tree[x * 2].col | Tree[x * 2 + 1].col);
}
int find(int l, int r, int x)
{
if ((Tree[x].l == l && r == Tree[x].r) || Tree[x].lazy)
{
return Tree[x].col;
}
int mid = (Tree[x].l + Tree[x].r) / 2;
if (mid < l)return find(l, r, x * 2 + 1);
else if (mid >= r) return find(l, r, x * 2);
else {
return (find(l, mid, x * 2) | find(mid + 1, r, x * 2 + 1)) ;
}
}
int trans(int nu)
{
int ans = 0;
for (int i = 0; i < T; i++)
if ((1 << i) & nu)ans++;
return ans;
}
int main()
{
//FILE* fp = fopen("in.txt", "r");
scanf( "%d %d %d", &L, &T, &O);
build(1, L, 1);
Tree[1].lazy = 1;
Tree[1].col = 1;
char op[3];
int x, y, col;
while (O--)
{
scanf( "%s %d %d", op, &x, &y);
if (x > y)x ^= y, y ^= x, x ^= y;
if (op[0] == 'C')
{
scanf( "%d", &col);
update(x, y, col, 1);
// for (int i = 1; i <= L ;i++)printf("%d ", color[i]);puts("");
}
else {
printf("%d\n", trans(find(x, y, 1)));
}
}
// getchar();
return 0;
}