设为首页 加入收藏

TOP

POJ 3225――Help with Intervals(线段树,成段替换+区间异或+hash)
2015-07-20 17:43:31 来源: 作者: 【 】 浏览:2
Tags:POJ 3225 Help with Intervals 线段 成段 替换 区间 hash

Help with Intervals
Time Limit: 6000MS Memory Limit: 131072K
Total Submissions: 10444 Accepted: 2551
Case Time Limit: 2000MS

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:

Operation Notation

Definition

Union AB {x : xA or xB}
Intersection AB {x : xA and xB}
Relative complementation A ? B {x : xA but x ? B}
Symmetric difference A ? B (A ? B) ∪ (B ? A)

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:

Command Semantics
U T SST
I T SST
D T SS ? T
C T ST ? S
S T SS ? T

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

X T

where X is 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, bZ, 0 ≤ ab ≤ 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

(2,3)


――――――――――――――――――――――分割线――――――――――――――――――――

题目大意:

就是实现区间的交,并,补,异或操作


思路:

对于区间的开,闭,将区间乘2,开的话,左端点+1,右端点-1.奇数表示开,偶数表示闭

例如(3,6)变成[7,11],左右端点为奇数,所以左右为开,退回来的时候判断7为奇数,11也为奇数,就是( 7/2, (11+1)/2 )

覆盖标记0、1,-1 :0表示不包含,1表示包含,-1表示既有包含也有不包含

异或标记0、1:0表示没有标记,1表示有标记


则:

U L,R 将区间L,R覆盖为1

D L,R 将区间L,R覆盖为0

I L,R 将区间 R覆盖为0

C L,R 将区间 R覆盖为0,对区间L,R 0,1进行对换(异或)

S L,R 将区间L,R 0,1进行对换(异或)


对于覆盖标记,如果存在异或标记,则将异或标记清0

对于异或标记,优先考虑是否存在覆盖标记,如果存在,则对覆盖标记异或,否则对异或标记异或


PS:还要熟练掌握push_down的内涵才行~~~

#include 
  
   
#include 
   
     #include 
    
      #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 const int maxn=131071; using namespace std; bool hash[maxn+5]; int cover[maxn<<2],Xor[maxn<<2]; void fxor(int rt) { if(cover[rt]!=-1) cover[rt]^=1; else Xor[rt]^=1; } void push_down(int rt) { if(cover[rt]!=-1){ cover[rt<<1]=cover[rt<<1|1]=cover[rt]; cover[rt]=-1; Xor[rt]=0; } if(Xor[rt]){ fxor(rt<<1); fxor(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; } if(op=='D'){ Xor[rt]=cover[rt]=0; } if(op=='C'||op=='S'){ fxor(rt); } return ; } push_down(rt); int m=(l+r)>>1; if(L<=m) update(op,L,R,lson); else if(op=='I'||op=='C'){ Xor[rt<<1]=cover[rt<<1]=0; } if(m
     
      >1; query(lson); query(rson); } int main() { char op,l,r;int a,b; while(scanf("%c %c%d,%d%c\n",&op,&l,&a,&b,&r)!=EOF){ a<<=1,b<<=1; if(l=='(') a++; if(r==')') b--; if(a>b){ if(op=='I'||op=='C'){ Xor[1]=cover[1]=0; } }else{ update(op,a,b,0,maxn,1); } } query(0,maxn,1); int s=-1,e; bool flag=false; for(int i=0;i<=maxn;++i){ if(hash[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>>1,(e+1)>>1,e&1?')':']'); s=-1; } } } if(!flag) printf("empty set\n"); printf("\n"); return 0; } 
     
    
   
  



】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Leetcode--Subsets II 下一篇C++ vector长度扩展机制的探究

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·C++中智能指针的性能 (2025-12-25 03:49:29)
·如何用智能指针实现c (2025-12-25 03:49:27)
·如何在 C 语言中管理 (2025-12-25 03:20:14)
·C语言和内存管理有什 (2025-12-25 03:20:11)
·为什么C语言从不被淘 (2025-12-25 03:20:08)