• POJ 3225 Help with Intervals(线段树)


    POJ 3225 Help with Intervals

    题目链接

    集合数字有的为1,没有为0,那么几种操作相应就是置为0或置为1或者翻转,这个随便推推就能够了,然后开闭区间的处理方式就是把区间扩大成两倍,偶数存点,奇数存线段就可以

    代码:

    #include <cstdio>
    #include <cstring>
    
    #define lson(x) ((x<<1)+1)
    #define rson(x) ((x<<1)+2)
    
    const int N = 65536 * 2;
    
    struct Node {
    	int l, r, flip, setv;
    } node[N * 4];
    
    int to[N];
    
    void build(int l, int r, int x = 0) {
    	node[x].l = l; node[x].r = r;
    	node[x].flip = 0; node[x].setv = -1;
    	if (l == r) {
    		to[l] = x;
    		return;
    	}
    	int mid = (l + r) / 2;
    	build(l, mid, lson(x));
    	build(mid + 1, r, rson(x));
    }
    
    void pushdown(int x) {
    	if (node[x].setv != -1) {
    		node[lson(x)].setv = node[rson(x)].setv = node[x].setv;
    		node[lson(x)].flip = node[rson(x)].flip = 0;
    		node[x].setv = -1;
    	}
    	if (node[x].flip) {
    		node[lson(x)].flip ^= 1;
    		node[rson(x)].flip ^= 1;
    		node[x].flip = 0;
    	}
    }
    
    void add(int l, int r, int v, int x = 0) {
    	if (l > r) return;
    	if (node[x].l >= l && node[x].r <= r) {
    		if (v != -1) {
    			node[x].setv = v;
    			node[x].flip = 0;
    		} else
    			node[x].flip ^= 1;
    		return;
    	}
    	pushdown(x);
    	int mid = (node[x].l + node[x].r) / 2;
    	if (l <= mid) add(l, r, v, lson(x));
    	if (r > mid) add(l, r, v, rson(x));
    }
    
    void query(int x = 0) {
    	if (node[x].l == node[x].r) {
    		if (node[x].setv == -1) node[x].setv = 0;
    		return;
    	}
    	pushdown(x);
    	int mid = (node[x].l + node[x].r) / 2;
    	query(lson(x));
    	query(rson(x));
    }
    
    char c, a, b;
    int l, r;
    
    int main() {
    	build(0, N - 1);
    	while (~scanf("%c %c%d,%d%c
    ", &c, &a, &l, &r, &b)) {
    		l = l * 2 + (a == '(');
    		r = r * 2 - (b == ')');
    		if (c == 'U') add(l, r, 1);
    		if (c == 'I' || c == 'C') {
    			add(0, l - 1, 0);
    			add(r + 1, N - 1, 0);
    			if (c == 'C') add(l, r, -1);
    		}
    		if (c == 'D') add(l, r, 0);
    		if (c == 'S') add(l, r, -1);
    	}
    	query();
    	int pre = 0, flag = 0, bo = 0;
    	for (int i = 0; i < N; i++) {
    		int id = to[i];
    		int tmp = (node[id].setv^node[id].flip);
    		if (!tmp && flag) {
    			if (bo) printf(" ");
    			else bo = 1;
    			if (pre % 2) printf("(");
    			else printf("[");
    			printf("%d,%d", pre / 2, i / 2);
    			if (i % 2 == 0) printf(")");
    			else printf("]");
    			flag = 0;
    		} else if (tmp && !flag) {
    			pre = i;
    			flag = 1;
    		}
    	}
    	if (bo == 0) printf("empty set");
    	printf("
    ");
    	return 0;
    }


  • 相关阅读:
    C# HttpWebRequest 发送SOAP XML
    浅谈HTTP中Get与Post的区别(转)
    WebService的helloworld,服务器端与客户端的Demo(转)----自己建立的Web Project , 而不是Web Service Project,利用WSDD 自己发布
    C#操作XML(转)
    要学习的知识
    Web service是什么?
    暖式切边线和冷式切变线
    python 形状区分市控国控,颜色表示aqi等级
    Linux常用指令---grep(搜索过滤)
    Axis通过wsdd部署Web Service
  • 原文地址:https://www.cnblogs.com/yfceshi/p/7027373.html
Copyright © 2020-2023  润新知