poj 2777

来源:互联网 发布:mac 登陆apple store 编辑:程序博客网 时间:2024/05/17 09:44

        题意:在一个长度为n的数轴上染色,有m种颜色,用1到m表示,o次操作,每次操作为P A B或C A B C,C A B C 表示把区间[A,B]染成颜色C,P A B表示询问区间[A,B]有多少种颜色。

       分析:线段树的入门题,旨在考lazy操作,也便是对区间的更新用一个标记记录下来,以便不用每次都一直更新至叶子结点。另外就是要用上位操作,因为颜色数目较少,最多只有30种,用一个int型变量就能表示所有颜色了,因此,我将线段树的结点存颜色的种类和数量,即对应颜色的集合,查询一个结点的颜色信息也只花常量时间。

                  另外要注意的是,由于线段树结点存的是区间颜色的集合,因此在查询多个区间的颜色总数量时,要先将集合做并运算(在这里也就是将表示对应集合的数字做或运算),再算颜色数量,否则可能会有重复。

     代码如下:


#include <cstdio>#include <stack>#include <set>#include <iostream>#include <string>#include <vector>#include <queue>#include <list>#include <functional>#include <cstring>#include <algorithm>#include <cctype>#include <string>#include <map>#include <iomanip>#include <cmath>#define LL long long#define ULL unsigned long long#define SZ(x) (int)x.size()#define Lowbit(x) ((x) & (-x))#define MP(a, b) make_pair(a, b)#define MS(arr, num) memset(arr, num, sizeof(arr))#define PB push_back#define F first#define S second#define ROP freopen("input.txt", "r", stdin);#define MID(a, b) (a + ((b - a) >> 1))#define lson l,mid,rt<<1#define rson mid+1,r,rt<<1|1#define lrt rt << 1#define rrt rt << 1|1#define root 1,n,1#define BitCount(x) __builtin_popcount(x)#define BitCountll(x) __builtin_popcountll(x)#define LeftPos(x) 32 - __builtin_clz(x) - 1#define LeftPosll(x) 64 - __builtin_clzll(x) - 1const double PI = acos(-1.0);const int INF = 0x3f3f3f3f;using namespace std;const double eps = 1e-5;const int MAXN = 300 + 10;const int MOD = 1000007;const double M=1e-8;const int N=1e6+10;typedef pair<int, int> pii;typedef pair<int, string> pis;const int d[4][2]={{0,1},{0,-1},{-1,0},{1,0}};int n,m,st[N],lazy[N];struct node{    int x,y;};void pushdown(int rt){    if (lazy[rt]) {        st[lrt]=st[rt];        st[rrt]=st[rt];        lazy[lrt]=lazy[rt];        lazy[rrt]=lazy[rt];        lazy[rt]=0;    }}void fun(int rt,int c){    st[rt]=(1<<(c-1));    lazy[rt]=c;}int get(int x){    int ans=0;    for (int i=0;i<=m;i++) if (x&(1<<i)) ans++;    return ans;}void updata(int a,int b,int c,int l,int r,int rt){    if (a>r || b<l) return ;    if (a<=l && r<=b) {        fun(rt,c);        return ;    }    pushdown(rt);    int mid=MID(l,r);    if (a<=mid) updata(a,b,c,lson);    if (b>mid) updata(a,b,c,rson);    int t=st[rrt]|st[lrt];    st[rt]=t;}int query(int a,int b,int l,int r,int rt){    if (a>r || b<l) return -1;    if (a<=l && r<=b) {        return st[rt];    }    pushdown(rt);    int mid=MID(l,r);    int u=query(a,b,lson);    int v=query(a,b,rson);    if (u<0) return v;    if (v<0) return u;    return u|v;}int main(){    int i,j,o;    while(~scanf("%d%d%d",&n,&m,&o))    {        MS(lazy,0);        MS(st,0);        updata(root,root);        for (j=0;j<o;j++) {            char c[2];            cin>>c;            if (c[0]=='C') {                int x,y,z;                if (x>y) swap(x,y);                scanf("%d%d%d",&x,&y,&z);                updata(x,y,z,root);            }            else {                int x,y;                scanf("%d%d",&x,&y);                if (x>y) swap(x,y);                cout<<get(query(x,y,root)) <<endl;            }        }    }}/*3 3 33C 1 1 1C 2 2 2C 3 3 3P 1 1P 2 2P 3 3P 1 2P 1 38 8 88C 2 2 2C 3 3 2C 4 4 2P 2 3*/


0 0
原创粉丝点击