zoukankan      html  css  js  c++  java
  • 色板游戏 线段树

    题目背景

    阿宝上学了,今天老师拿来了一块很长的涂色板。

    题目描述

    色板长度为L,L是一个正整数,所以我们可以均匀地将它划分成L块1厘米长的小方格。并从左到右标记为1, 2, ... L。

    现在色板上只有一个颜色,老师告诉阿宝在色板上只能做两件事:

    1. "C A B C" 指在A到 B 号方格中涂上颜色 C。
    2. "P A B" 指老师的提问:A到 B号方格中有几种颜色。

    学校的颜料盒中一共有 T 种颜料。为简便起见,我们把他们标记为 1, 2, ... T. 开始时色板上原有的颜色就为1号色。 面对如此复杂的问题,阿宝向你求助,你能帮助他吗?

    输入输出格式

    输入格式:

    第一行有3个整数 L (1 <= L <= 100000), T (1 <= T <= 30) 和 O (1 <= O <= 100000)。 在这里O表示事件数。
    接下来 O 行, 每行以 "C A B C" 或 "P A B" 得形式表示所要做的事情(这里 A, B, C 为整数, 可能A> B,这样的话需要你交换A和B)

    输出格式:

    对于老师的提问,做出相应的回答。每行一个整数。

    输入输出样例

    输入样例#1: 复制
    2 2 4
    C 1 1 2
    P 1 2
    C 2 2 2
    P 1 2
    输出样例#1: 复制
    2
    1
    T<=30!!!二进制压缩就行了;
    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<cstdlib>
    #include<cstring>
    #include<string>
    #include<cmath>
    #include<map>
    #include<set>
    #include<vector>
    #include<queue>
    #include<bitset>
    #include<ctime>
    #include<deque>
    #include<stack>
    #include<functional>
    #include<sstream>
    //#include<cctype>
    //#pragma GCC optimize(2)
    using namespace std;
    #define maxn 900005
    #define inf 0x7fffffff
    //#define INF 1e18
    #define rdint(x) scanf("%d",&x)
    #define rdllt(x) scanf("%lld",&x)
    #define rdult(x) scanf("%lu",&x)
    #define rdlf(x) scanf("%lf",&x)
    #define rdstr(x) scanf("%s",x)
    typedef long long  ll;
    typedef unsigned long long ull;
    typedef unsigned int U;
    #define ms(x) memset((x),0,sizeof(x))
    const long long int mod = 1e9 + 7;
    #define Mod 1000000000
    #define sq(x) (x)*(x)
    #define eps 1e-3
    typedef pair<int, int> pii;
    #define pi acos(-1.0)
    //const int N = 1005;
    #define REP(i,n) for(int i=0;i<(n);i++)
    typedef pair<int, int> pii;
    inline ll rd() {
    	ll x = 0;
    	char c = getchar();
    	bool f = false;
    	while (!isdigit(c)) {
    		if (c == '-') f = true;
    		c = getchar();
    	}
    	while (isdigit(c)) {
    		x = (x << 1) + (x << 3) + (c ^ 48);
    		c = getchar();
    	}
    	return f ? -x : x;
    }
    
    ll gcd(ll a, ll b) {
    	return b == 0 ? a : gcd(b, a%b);
    }
    int sqr(int x) { return x * x; }
    
    
    /*ll ans;
    ll exgcd(ll a, ll b, ll &x, ll &y) {
    	if (!b) {
    		x = 1; y = 0; return a;
    	}
    	ans = exgcd(b, a%b, x, y);
    	ll t = x; x = y; y = t - a / b * y;
    	return ans;
    }
    */
    int n, m;
    struct node {
    	int l, r;
    	int sum;
    	int lazy;
    }tree[maxn];
    void pushup(int rt) {
    	tree[rt].sum = tree[rt << 1].sum | tree[rt << 1 | 1].sum;
    }
    void pushdown(int rt) {
    	if (tree[rt].lazy) {
    		tree[rt << 1].lazy = tree[rt].lazy;
    		tree[rt << 1 | 1].lazy = tree[rt].lazy;
    		tree[rt << 1].sum = tree[rt].lazy;
    		tree[rt << 1 | 1].sum = tree[rt].lazy;
    		tree[rt].lazy = 0;
    	}
    }
    void build(int l, int r, int rt) {
    	tree[rt].l = l; tree[rt].r = r; tree[rt].lazy = 0;
    	if (l == r) {
    		tree[rt].sum = 1; return;
    	}
    	int mid = (l + r) >> 1;
    	build(l, mid, rt << 1); build(mid + 1, r, rt << 1 | 1);
    	pushup(rt);
    }
    
    void upd(int col, int L, int R, int l, int r,int rt) {
    	if (L <= l && r <= R) {
    		tree[rt].sum = (1 << (col - 1));
    		tree[rt].lazy = (1 << (col - 1));
    		return;
    	}
    	pushdown(rt);
    	int mid = (l + r) >> 1;
    	if (L <= mid)upd(col, L, R, l, mid, rt << 1);
    	if (mid < R)upd(col, L, R, mid + 1, r, rt << 1 | 1);
    	pushup(rt);
    }
    int query(int L, int R, int l, int r, int rt) {
    	if (L <= l && r <= R) {
    		return tree[rt].sum;
    	}
    	pushdown(rt);
    	int mid = (l + r) >> 1;
    	int ans = 0;
    	if (L <= mid)ans |= query(L, R, l, mid, rt << 1);
    	if (mid < R)ans |= query(L, R, mid + 1, r, rt << 1 | 1);
    	return ans;
    }
    
    int main() {
    	//ios::sync_with_stdio(0);
    	rdint(n); int T; rdint(T); rdint(m);
    	build(1, n, 1);
    	while (m--) {
    		char op; cin >> op;
    		if (op == 'C') {
    			int a, b, c; rdint(a); rdint(b); rdint(c);
    			if (a > b)swap(a, b);
    			upd(c, a, b, 1, n, 1);
    		}
    		else {
    			int a, b; rdint(a); rdint(b);
    			if (a > b)swap(a, b);
    			int tot = 0;
    			int ans = query(a, b, 1, n, 1);
    			for (int i = 1; i <= T; i++) {
    				if (ans&(1 << (i - 1))) {
    					tot++;
    				}
    			}
    			cout << tot << endl;
    		}
    	}
    	return 0;
    }
    
    
    
    EPFL - Fighting
  • 相关阅读:
    Apache 虚拟主机 VirtualHost 配置
    EAX、ECX、EDX、EBX寄存器的作用
    Python中文文档 目录(转载)
    八度
    POJ 3268 Silver Cow Party (最短路)
    POJ 2253 Frogger (求每条路径中最大值的最小值,Dijkstra变形)
    2013金山西山居创意游戏程序挑战赛——复赛(1) HDU 4557 非诚勿扰 HDU 4558 剑侠情缘 HDU 4559 涂色游戏 HDU 4560 我是歌手
    HDU 4549 M斐波那契数列(矩阵快速幂+欧拉定理)
    UVA 11624 Fire! (简单图论基础)
    HDU 3534 Tree (树形DP)
  • 原文地址:https://www.cnblogs.com/zxyqzy/p/10268685.html
Copyright © 2011-2022 走看看