POJ 3225 - Help with Intervals(线段树)
2016-07-22 12:28
381 查看
Description
LogLoader, Inc. is a company specialized in providing products for analyzing logs. While Ikki is working on graduation design, he is also engaged in an internship at LogLoader. Among his tasks, one is to write a module for manipulating time intervals, which
have confused him a lot. Now he badly needs your help.
In discrete mathematics, you have studied several basic set operations, namely union, intersection, relative complementation and symmetric difference, which naturally apply to the specialization of sets as intervals.. For your quick reference they are summarized
in the table below:
Ikki has abstracted the interval operations emerging from his job as a tiny programming language. He wants you to implement an interpreter for him. The language maintains a set S, which starts out empty and is modified as specified by the following
commands:
Input
The input contains exactly one test case, which consists of between 0 and 65,535 (inclusive) commands of the language. Each command occupies a single line and appears like
where
0 ≤ a ≤ b ≤ 65,535), which take their usual meanings. The commands are executed in the order they appear in the input.
End of file (EOF) indicates the end of input.
Output
Output the set S as it is after the last command is executed as the union of a minimal collection of disjoint intervals. The intervals should be printed on one line separated by single spaces and appear in increasing order of their endpoints. If S is
empty, just print “
Sample Input
Sample Output
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <cctype>
using namespace std;
const int maxn = 65535 * 2;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
int cover[maxn<<2] = { 0 }, XOR[maxn<<2] = { 0 };
bool vis[maxn << 2] = { false };
void toXOR(int rt)
{
if (cover[rt] != -1) cover[rt] ^= 1;
else XOR[rt] ^= 1;
}
void PushDown(int rt)
{
if (cover[rt] != -1)
{
cover[rt << 1] = cover[rt << 1 | 1] = cover[rt];
XOR[rt << 1] = XOR[rt << 1 | 1] = 0;
cover[rt] = -1;
}
if (XOR[rt])
{
toXOR(rt << 1);
toXOR(rt << 1 | 1);
XOR[rt] = 0;
}
}
void update(char op, int L, int R, int l, int r, int rt)
{
if (L <= l&&r <= R)
{
if (op == 'U')
{
cover[rt] = 1;
XOR[rt] = 0;
}
else if (op == 'D')
{
cover[rt] = 0;
XOR[rt] = 0;
}
else if (op == 'S')
{
toXOR(rt);
}
return;
}
if (l == r) return;
PushDown(rt);
int m = (l + r) >> 1;
if (L <= m) update(op, L, R, lson);
if (R > m) update(op, L, R, rson);
}
void query(int l, int r, int rt)
{
if (cover[rt] == 1)
{
for (int i = l; i <= r; i++)
vis[i] = true;
return;
}
if (cover[rt] == 0) return;
if (l == r) return;
PushDown(rt);
int m = (l + r) >> 1;
query(lson);
query(rson);
}
int main()
{
char op, l, r;
int a, b;
//freopen("d:\\test.txt", "r",stdin);
while (~scanf("%c %c%d,%d%c\n", &op, &l, &a, &b, &r))
{
a *= 2; b *= 2;
if (l == '(') a++;
if (r == ')') b--;
if (op == 'U' || op == 'D' || op == 'S')
update(op, a, b, 0, maxn, 1);
else if (op == 'I')
{
if (a != 0)
update('D', 0, a - 1, 0, maxn, 1);
if (b != maxn)
update('D', b + 1, maxn, 0, maxn, 1);
}
else if (op == 'C')
{
if (a != 0)
update('D', 0, a - 1, 0, maxn, 1);
if (b != maxn)
update('D', b + 1, maxn, 0, maxn, 1);
update('S', a, b, 0, maxn, 1);
}
}
query(0, maxn, 1);
bool flag = false;
int s = -1, e;
for (int i = 0; i <= maxn; i++)
{
if (vis[i])
{
if (s == -1) s = i;
e = i;
}
else if (s != -1)
{
if (flag) printf(" ");
flag = true;
printf("%c%d,%d%c", s & 1 ? '(' : '[', s / 2, (e + 1) / 2, e & 1 ? ')' : ']');
s = -1;
}
}
if (!flag) printf("empty set");
puts("");
return 0;
}
LogLoader, Inc. is a company specialized in providing products for analyzing logs. While Ikki is working on graduation design, he is also engaged in an internship at LogLoader. Among his tasks, one is to write a module for manipulating time intervals, which
have confused him a lot. Now he badly needs your help.
In discrete mathematics, you have studied several basic set operations, namely union, intersection, relative complementation and symmetric difference, which naturally apply to the specialization of sets as intervals.. For your quick reference they are summarized
in the table below:
Operation | Notation | Definition |
---|---|---|
Union | A ∪ B | {x : x ∈ A or x ∈ B} |
Intersection | A ∩ B | {x : x ∈ A and x ∈ B} |
Relative complementation | A − B | {x : x ∈ A but B} |
Symmetric difference | A ⊕ B | (A − B) ∪ (B − A) |
commands:
Command | Semantics |
---|---|
UT | S ← S ∪ T |
IT | S ← S ∩ T |
DT | S ← S − T |
CT | S ← T − S |
ST | S ← S ⊕ T |
The input contains exactly one test case, which consists of between 0 and 65,535 (inclusive) commands of the language. Each command occupies a single line and appears like
XT
where
Xis one of ‘
U’, ‘
I’, ‘
D’, ‘
C’ and ‘
S’ and T is an interval in one of the forms
(a
,b
),
(a
,b
],
[a
,b
)and
[a
,b
](a, b ∈ Z,
0 ≤ a ≤ b ≤ 65,535), which take their usual meanings. The commands are executed in the order they appear in the input.
End of file (EOF) indicates the end of input.
Output
Output the set S as it is after the last command is executed as the union of a minimal collection of disjoint intervals. The intervals should be printed on one line separated by single spaces and appear in increasing order of their endpoints. If S is
empty, just print “
empty set” and nothing else.
Sample Input
U [1,5] D [3,3] S [2,4] C (1,5) I (2,3]
Sample Output
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <cctype>
using namespace std;
const int maxn = 65535 * 2;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
int cover[maxn<<2] = { 0 }, XOR[maxn<<2] = { 0 };
bool vis[maxn << 2] = { false };
void toXOR(int rt)
{
if (cover[rt] != -1) cover[rt] ^= 1;
else XOR[rt] ^= 1;
}
void PushDown(int rt)
{
if (cover[rt] != -1)
{
cover[rt << 1] = cover[rt << 1 | 1] = cover[rt];
XOR[rt << 1] = XOR[rt << 1 | 1] = 0;
cover[rt] = -1;
}
if (XOR[rt])
{
toXOR(rt << 1);
toXOR(rt << 1 | 1);
XOR[rt] = 0;
}
}
void update(char op, int L, int R, int l, int r, int rt)
{
if (L <= l&&r <= R)
{
if (op == 'U')
{
cover[rt] = 1;
XOR[rt] = 0;
}
else if (op == 'D')
{
cover[rt] = 0;
XOR[rt] = 0;
}
else if (op == 'S')
{
toXOR(rt);
}
return;
}
if (l == r) return;
PushDown(rt);
int m = (l + r) >> 1;
if (L <= m) update(op, L, R, lson);
if (R > m) update(op, L, R, rson);
}
void query(int l, int r, int rt)
{
if (cover[rt] == 1)
{
for (int i = l; i <= r; i++)
vis[i] = true;
return;
}
if (cover[rt] == 0) return;
if (l == r) return;
PushDown(rt);
int m = (l + r) >> 1;
query(lson);
query(rson);
}
int main()
{
char op, l, r;
int a, b;
//freopen("d:\\test.txt", "r",stdin);
while (~scanf("%c %c%d,%d%c\n", &op, &l, &a, &b, &r))
{
a *= 2; b *= 2;
if (l == '(') a++;
if (r == ')') b--;
if (op == 'U' || op == 'D' || op == 'S')
update(op, a, b, 0, maxn, 1);
else if (op == 'I')
{
if (a != 0)
update('D', 0, a - 1, 0, maxn, 1);
if (b != maxn)
update('D', b + 1, maxn, 0, maxn, 1);
}
else if (op == 'C')
{
if (a != 0)
update('D', 0, a - 1, 0, maxn, 1);
if (b != maxn)
update('D', b + 1, maxn, 0, maxn, 1);
update('S', a, b, 0, maxn, 1);
}
}
query(0, maxn, 1);
bool flag = false;
int s = -1, e;
for (int i = 0; i <= maxn; i++)
{
if (vis[i])
{
if (s == -1) s = i;
e = i;
}
else if (s != -1)
{
if (flag) printf(" ");
flag = true;
printf("%c%d,%d%c", s & 1 ? '(' : '[', s / 2, (e + 1) / 2, e & 1 ? ')' : ']');
s = -1;
}
}
if (!flag) printf("empty set");
puts("");
return 0;
}
相关文章推荐
- ClassLoader Java 获取当前类文件所在的路径(未测试)
- RecyclerView万能的Adapter
- 在MVC控制器里面使用dynamic和ExpandoObject,实现数据转义的输出
- sharepoint 2016 学习系列篇(20)-文档库应用篇-(2)在文档库中创建文件夹
- 数据结构实验之数组三:快速转置
- java 使用java执行命令简易封装类(未测试)
- VS2010 生成时复制文件或目录到指定目录
- python实现telnet
- Net操作Excel(终极方法NPOI)
- 机器学习入门知识
- java 检测 Java 是否运行在64bit 的JVM上(未测试)
- Hive入门--4.flume-数据收集工具
- HttpClient(Post和Get)
- Linux查看程序端口占用情况
- Code Forces 55 D. Beautiful numbers(数位DP)
- 将System.out输出在控制台的信息保存在文件中(未测试)
- nginx添加geoip支持ip转经纬度配置笔记
- UltraISO制作U盘启动攻略
- 未来人类T5-散热改造final版
- JavaScript常用方法函数收集(未测试)