码迷,mamicode.com
首页 > 其他好文 > 详细

UVA 11402 - Ahoy, Pirates!(线段树)

时间:2014-07-30 23:54:35      阅读:268      评论:0      收藏:0      [点我收藏+]

标签:style   http   color   os   io   for   代码   ar   

UVA 11402 - Ahoy, Pirates!

题目链接

题意:总的来说意思就是给一个01串,然后有3种操作
1、把一个区间变成1
2、把一个区间变成0
3、把一个区间翻转(0变1,1变0)

思路:线段树搞,开一个延迟标记当前操作即可,注意几种状态间的转变方式即可

代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

#define INF 0x3f3f3f3f
#define lson(x) ((x<<1)+1)
#define rson(x) ((x<<1)+2)

const int N = 1100005;

int t, s[N], n, sn;
char str[55];

struct Node {
    int l, r, b, flag;
    Node() {}
    Node(int l, int r) {
	this->l = l; this->r = r;
	b = flag = 0;
    }
    int len() {
	return r - l + 1;
    }
} node[4 * N];

void pushup(int x) {
    node[x].b = node[lson(x)].b + node[rson(x)].b;
}

void build(int l, int r, int x = 0) {
    node[x] = Node(l, r);
    if (l == r) {
	node[x].b = s[l];
	return;
    }
    int mid = (node[x].l + node[x].r) / 2;
    build(l, mid, lson(x));
    build(mid + 1, r, rson(x));
    pushup(x);
}

void init() {
    scanf("%d", &n);
    sn = 0;
    while (n--) {
	int num;
	scanf("%d", &num);
	scanf("%s", str);
	int len = strlen(str);
	while (num--) {
	    for (int i = 0; i < len; i++)
		s[sn++] = str[i] - '0';
	}
    }
    build(0, sn - 1);
}

//0不变,1黑,2白,3翻转
int getS(int a, int b) {
    if (a == 3 && b == 3) return 0;
    if (a == 3 && b == 1) return 2;
    if (a == 3 && b == 2) return 1;
    if (a == 3 && b == 0) return 3;
    if (a == 2) return 2;
    if (a == 1) return 1;
    return 0;
}

void pushdown(int x) {
    if (node[x].flag) {
	int ls = getS(node[x].flag, node[lson(x)].flag);
	node[lson(x)].flag = ls;
	if (node[x].flag == 2) node[lson(x)].b = 0;
	if (node[x].flag == 1) node[lson(x)].b = node[lson(x)].len();
	if (node[x].flag == 3) node[lson(x)].b = node[lson(x)].len() - node[lson(x)].b;
	int rs = getS(node[x].flag, node[rson(x)].flag);
	node[rson(x)].flag = rs;
	if (node[x].flag == 2) node[rson(x)].b = 0;
	if (node[x].flag == 1) node[rson(x)].b = node[rson(x)].len();
	if (node[x].flag == 3) node[rson(x)].b = node[rson(x)].len() - node[rson(x)].b;
	node[x].flag = 0;
    }
}

void set(int l, int r, int v, int x = 0) {
    if (node[x].l >= l && node[x].r <= r) {
	int ts = getS(v, node[x].flag);
	if (v == 1)
	    node[x].b = node[x].len();
	else if (v == 2)
	    node[x].b = 0;
	else
	    node[x].b = node[x].len() - node[x].b;
	node[x].flag = ts;
	return;
    }
    pushdown(x);
    int mid = (node[x].l + node[x].r) / 2;
    if (l <= mid) set(l, r, v, lson(x));
    if (r > mid) set(l, r, v, rson(x));
    pushup(x);
}

int S(int l, int r, int x = 0) {
    if (node[x].l >= l && node[x].r <= r)
	return node[x].b;
    int mid = (node[x].l + node[x].r) / 2;
    int ans = 0;
    pushdown(x);
    if (l <= mid) ans += S(l, r, lson(x));
    if (r > mid) ans += S(l, r, rson(x));
    pushup(x);
    return ans;
}

int main() {
    int cas = 0;
    scanf("%d", &t);
    while (t--) {
	init();
	int q;
	scanf("%d", &q);
	char Q[5]; int a, b;
	printf("Case %d:\n", ++cas);
	int Qcas = 0;
	while (q--) {
	    scanf("%s%d%d", Q, &a, &b);
	    if (Q[0] == 'F') set(a, b, 1);
	    if (Q[0] == 'E') set(a, b, 2);
	    if (Q[0] == 'I') set(a, b, 3);
	    if (Q[0] == 'S') printf("Q%d: %d\n", ++Qcas, S(a, b));
	}
    }
    return 0;
}


UVA 11402 - Ahoy, Pirates!(线段树),布布扣,bubuko.com

UVA 11402 - Ahoy, Pirates!(线段树)

标签:style   http   color   os   io   for   代码   ar   

原文地址:http://blog.csdn.net/accelerator_/article/details/38308731

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!