• [POJ2777]Count Color(线段树)


    题目链接:http://poj.org/problem?id=2777

    给你一个长为L想线段,向上面染色,颜色不超过30种,一共有O次操作,操作有两种:

    C a b c 在[a,b]上染上c颜色

    P a b 查询[a,b]上所有颜色数。

    思路:线段树维护每个线段上颜色种类,用位来存颜色。好题。

      1 /*
      2 ━━━━━┒ギリギリ♂ eye!
      3 ┓┏┓┏┓┃キリキリ♂ mind!
      4 ┛┗┛┗┛┃\○/
      5 ┓┏┓┏┓┃ /
      6 ┛┗┛┗┛┃ノ)
      7 ┓┏┓┏┓┃
      8 ┛┗┛┗┛┃
      9 ┓┏┓┏┓┃
     10 ┛┗┛┗┛┃
     11 ┓┏┓┏┓┃
     12 ┛┗┛┗┛┃
     13 ┓┏┓┏┓┃
     14 ┃┃┃┃┃┃
     15 ┻┻┻┻┻┻
     16 */
     17 #include <algorithm>
     18 #include <iostream>
     19 #include <iomanip>
     20 #include <cstring>
     21 #include <climits>
     22 #include <complex>
     23 #include <fstream>
     24 #include <cassert>
     25 #include <cstdio>
     26 #include <bitset>
     27 #include <vector>
     28 #include <deque>
     29 #include <queue>
     30 #include <stack>
     31 #include <ctime>
     32 #include <set>
     33 #include <map>
     34 #include <cmath>
     35 using namespace std;
     36 #define fr first
     37 #define sc second
     38 #define cl clear
     39 #define BUG puts("here!!!")
     40 #define W(a) while(a--)
     41 #define pb(a) push_back(a)
     42 #define Rint(a) scanf("%d", &(a))
     43 #define Rll(a) scanf("%I64d", &a)
     44 #define Rs(a) scanf("%s", a)
     45 #define Cin(a) cin >> a
     46 #define FRead() freopen("in", "r", stdin)
     47 #define FWrite() freopen("out", "w", stdout)
     48 #define Rep(i, len) for(int i = 0; i < (len); i++)
     49 #define For(i, a, len) for(int i = (a); i < (len); i++)
     50 #define Cls(a) memset((a), 0, sizeof(a))
     51 #define Clr(a, x) memset((a), (x), sizeof(a))
     52 #define Full(a) memset((a), 0x7f, sizeof(a))
     53 #define lrt rt << 1
     54 #define rrt rt << 1 | 1
     55 #define pi 3.14159265359
     56 #define RT return
     57 #define lowbit(x) x & (-x)
     58 #define onenum(x) __builtin_popcount(x)
     59 typedef long long LL;
     60 typedef long double LD;
     61 typedef unsigned long long Uint;
     62 typedef pair<int, int> pii;
     63 typedef pair<LL, LL> pLL;
     64 typedef pair<string, LL> psi;
     65 typedef map<string, LL> msi;
     66 typedef vector<LL> vi;
     67 typedef vector<LL> vl;
     68 typedef vector<vl> vvl;
     69 typedef vector<bool> vb;
     70 
     71 #define lson l, m, rt << 1
     72 #define rson m + 1, r, rt << 1 | 1
     73 const int maxn = 100010;
     74 int sum[maxn<<2];
     75 int add[maxn<<2];
     76 int L, T, O;
     77 
     78 void pushUP(int rt) {
     79     sum[rt] = sum[lrt] | sum[rrt];
     80 }
     81 
     82 void pushDOWN(int rt) {
     83     if(add[rt] != -1) {
     84         sum[lrt] = sum[rrt] = (1 << add[rt]);
     85         add[lrt] = add[rrt] = add[rt];
     86         add[rt] = -1;
     87     }
     88 }
     89 
     90 void build(int l, int r, int rt) {
     91     sum[rt] = 1; add[rt] = 1;
     92     if(l == r) return;
     93     int m = (l + r) >> 1;
     94     build(l, m, lrt);
     95     build(m+1, r, rrt);
     96 }
     97 
     98 void update(int L, int R, int c, int l, int r, int rt) {
     99     if(L <= l && r <= R) {
    100         add[rt] = c;
    101         sum[rt] = 1 << c;
    102         return;
    103     }
    104     pushDOWN(rt);
    105     int m = (l + r) >> 1;
    106     if(L <= m) update(L, R, c, lson);
    107     if(m < R) update(L, R, c, rson);
    108     pushUP(rt);
    109 }
    110 
    111 int query(int L, int R, int l, int r, int rt) {
    112     if(L <= l && r <= R) return sum[rt];
    113     pushDOWN(rt);
    114     int m = (l + r) >> 1;
    115     int ret = 0;
    116     if(L <= m) ret |= query(L, R, lson);
    117     if(m < R) ret |= query(L, R, rson);
    118     return ret;
    119 }
    120 
    121 int main() {
    122 //    FRead();
    123     char cmd[2];
    124     int l, r, c;
    125     while(~scanf("%d%d%d", &L, &T, &O)) {
    126         build(1, L, 1);
    127         W(O) {
    128             Rs(cmd);
    129             if(cmd[0] == 'C') {
    130                 scanf("%d%d%d",&l,&r,&c);
    131                 if(l > r) swap(l, r);
    132                 update(l, r, c, 1, L, 1);
    133             }
    134             else {
    135                 scanf("%d%d",&l,&r);
    136                 if(l > r) swap(l, r);
    137                 int p = query(l, r, 1, L, 1);
    138                 int ret = 0;
    139                 while(p) {
    140                     if(p & 1) ret++;
    141                     p >>= 1;
    142                 }
    143                 printf("%d
    ", ret);
    144             }
    145         }
    146     }
    147     RT 0;
    148 }
  • 相关阅读:
    Vue3+Element-Plus-admin
    uniapp UI库推荐 uView官网/文档
    js 获取本月月初和月末时间操作
    el-dialog 无法弹出来
    Vue核心技术 Vue+Vue-Router+Vuex+SSR实战精讲
    sed 相关命令
    docker 启动 ubuntu
    vue 配置 history 模式
    typescript 相关
    fiddler 图片下载
  • 原文地址:https://www.cnblogs.com/kirai/p/5750684.html
Copyright © 2020-2023  润新知