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\n", &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("\n");
return 0;
}
POJ 3225 Help with Intervals(线段树)的更多相关文章
- poj 3225 Help with Intervals(线段树,区间更新)
Help with Intervals Time Limit: 6000MS Memory Limit: 131072K Total Submissions: 12474 Accepted: ...
- POJ 3225 Help with Intervals --线段树区间操作
题意:给你一些区间操作,让你输出最后得出的区间. 解法:区间操作的经典题,借鉴了网上的倍增算法,每次将区间乘以2,然后根据区间开闭情况做微调,这样可以有效处理开闭区间问题. 线段树维护两个值: cov ...
- (中等) POJ 3225 Help with Intervals , 线段树+集合。
Description LogLoader, Inc. is a company specialized in providing products for analyzing logs. While ...
- POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化)
POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化) 题意分析 贴海报,新的海报能覆盖在旧的海报上面,最后贴完了,求问能看见几张海报. 最多有10000张海报,海报 ...
- POJ 2528 Mayor's posters(线段树+离散化)
Mayor's posters 转载自:http://blog.csdn.net/winddreams/article/details/38443761 [题目链接]Mayor's posters [ ...
- POJ 2528 Mayor's posters (线段树)
题目链接:http://poj.org/problem?id=2528 题目大意:有一个很上的面板, 往上面贴海报, 问最后最多有多少个海报没有被完全覆盖 解题思路:将贴海报倒着想, 对于每一张海报只 ...
- POJ 2892 Tunnel Warfare(线段树单点更新区间合并)
Tunnel Warfare Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 7876 Accepted: 3259 D ...
- POJ 2777 Count Color(线段树染色,二进制优化)
Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 42940 Accepted: 13011 Des ...
- Codeforces Gym 100231B Intervals 线段树+二分+贪心
Intervals 题目连接: http://codeforces.com/gym/100231/attachments Description 给你n个区间,告诉你每个区间内都有ci个数 然后你需要 ...
随机推荐
- FCC高级编程篇之Symmetric Difference
Symmetric Difference Create a function that takes two or more arrays and returns an array of the sym ...
- Vue项目结合vux使用
引入vux 1.直接安装或者更新: npm install vux --save 或者使用 yarn yarn add vux // 安装 yarn upgrade vux // 更新 2.vux2必 ...
- 12种CSS BUG解决方法与技巧
一. 针对浏览器的选择器 这些选择器在你需要针对某款浏览器进行css设计时将非常有用. IE6及其更低版本,本文由52CSS.com整理,转载请注明出处! * html {} IE7及其更低版本 *: ...
- NOIp2018模拟赛四十二
今天看标题终于回到了“NOIP模拟赛”,十分高兴啊! 然后一打开题目: ********** 所以今天又是一场NOIPlus模拟赛(微笑) 成绩:0+70+0=70 A题想了个贪心被myh两分钟cha ...
- 小程序使用wepy框架自定义loading组件
1:定义组 <template> <view class="app-loading-container" style="{{options.cssTex ...
- IIFE 萌新学习笔记
立即执行函数表达式(IIFE) IIFE:Immediately-Invoked Function Expression(立即执行函数表达式) 一 常用写法: //经常使用的写法(function() ...
- HDU-1215 七夕节 数论 唯一分解定理 求约数之和
题目链接:https://cn.vjudge.net/problem/HDU-1215 题意 中文题,自己去看吧,懒得写:) 思路 \[ Ans=\prod \sum p_i^j \] 唯一分解定理 ...
- [LeetCode] 122. 买卖股票的最佳时机ii best-time-to-buy-and-sell-stock-ii(贪心算法)
思路: 只要第二天的价格高于第一天,就进行交易.(这样的话就默认可以同一天内先卖出再买进) class Solution(object): def maxProfit(self, prices): & ...
- javascript位操作符右移>>>的妙用
var len=arr.length>>>0; 在arr.length为null或undefined的时间,强制转换为0;
- centos7.x86_64搭建饥荒服务器
http://blog.ttionya.com/article-1233.html 在centos7下找不到 libcurl-gnutls.so.4,而且必须要安装32位的才行 yum install ...