zoukankan      html  css  js  c++  java
  • 【线段树】POJ3225 Help with intervals

    【Problem】

      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:

    OperationNotation

    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)

      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:

    CommandSemantics
    U T S ← S ∪ T
    I T S ← S ∩ T
    D T S ← S − T
    C T S ← T − S
    S T S ← S ⊕ 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] (ab ∈ 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】

      (2,3)

    【Analysis】

      需要两个标记的线段树,mk[]记标记0/1,Xor[]记是否取反,重点在于pushdown()中对于两个标记的处理顺序,先mk[],后Xor[];

      U:把给出区间[l,r]均标为1

      I:把[-∞,l)∪(r,+∞]标为0

      D:把[l,r]标为0

      C:把[-∞,l)∪(r,+∞]标为0,区间[l,r]取反

      S:区间[l,r]取反  

      理解‘[’‘(’在整颗线段树中的表示方式,相当于把区间倍长,左区间以偶数表'[',奇数表'(',右区间以偶数表')',奇数表']'。

    【Code】

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<algorithm>
     4 using namespace std;
     5 const int n = 65535*2+2;
     6 
     7 char ind,u,v;
     8 int l,r;
     9 int Xo[(n<<2)+10],mk[(n<<2)+10],mark[(n<<1)+10];
    10 bool flag;
    11 
    12 void Xorup(int now) {
    13     if(mk[now]!=-1) mk[now]^=1;
    14     else Xo[now]^=1;
    15 }
    16 
    17 void pushdown(int now) {
    18     if(mk[now]!=-1) {
    19         mk[now<<1]=mk[now<<1|1]=mk[now];
    20         Xo[now<<1]=Xo[now<<1|1]=0;
    21         mk[now]=-1;
    22     }    
    23     if(Xo[now]) {
    24         Xorup(now<<1);
    25         Xorup(now<<1|1);
    26         Xo[now]=0;
    27     }
    28 }
    29 
    30 void query(char a,int L,int R,int now,int l,int r) {
    31     if(L<=l&&r<=R) {
    32         if(a == 'U') mk[now]=1,Xo[now]=0;
    33         if(a == 'D') mk[now]=Xo[now]=0;
    34         if(a=='C'||a=='S') Xorup(now);
    35         return ;
    36     }
    37     pushdown(now);
    38     int m=(l+r)>>1;
    39     if(L<=m) query(a,L,R,now<<1,l,m);
    40     else if(a=='I'||a=='C') mk[now<<1]=Xo[now<<1]=0;
    41     if(m+1<=R) query(a,L,R,now<<1|1,m+1,r);
    42     else if(a=='I'||a=='C') mk[now<<1|1]=Xo[now<<1|1]=0;
    43 }
    44 
    45 void traverse(int now,int l,int r)
    46 {
    47     if(mk[now]==1)
    48     {   
    49         for(int i=l;i<=r;++i)mark[i]=1;
    50         return;
    51     }
    52     if(mk[now]==0)return;
    53     if(l==r)return;
    54     pushdown(now);
    55     int m=(l+r)>>1;
    56     traverse(now<<1,l,m);
    57     traverse(now<<1|1,m+1,r);
    58 }
    59 
    60 int main() {
    61 
    62     //freopen("p.in","r",stdin);
    63     while(scanf("%c %c%d,%d%c
    ",&ind,&u,&l,&r,&v)!=EOF){
    64         l<<=1;r<<=1;
    65         if(u=='(')++l;
    66         if(v==')')--r;
    67         if(l>r) {
    68             if(ind=='I'||ind=='C')mk[1]=Xo[1]=0;
    69         }
    70         else query(ind,l,r,1,0,n);//
    71     }
    72     traverse(1,0,n);flag=0;
    73     for(int i=0,st=-1,t=-1;i<n;++i){
    74         if(mark[i]) st = ((st==-1)?i:st), t=i;//
    75         else 
    76         if(st!=-1) {
    77             if(flag)printf(" ");else flag=1;
    78             printf("%c%d,%d%c",st&1 ? '(' : '[',st>>1,(t+1)>>1,t&1 ? ')' : ']');
    79             st=-1;
    80         }
    81     }
    82     if(!flag)printf("empty set");
    83     printf("
    ");
    84     return 0;
    85 }



  • 相关阅读:
    C++学生成绩管理系统
    蓝桥杯算法训练 最大最小公倍数
    蓝桥杯基础练习 完美的代价
    vim编辑器的使用技巧
    C语言中static关键字的用法
    在linux环境下编译运行OpenCV程序的两种方法
    Linux中gcc编译器的用法
    浅谈Java中的hashcode方法
    读CopyOnWriteArrayList有感
    徐汉彬:Web系统大规模并发——电商秒杀与抢购(技术实现)
  • 原文地址:https://www.cnblogs.com/Etta/p/6753529.html
Copyright © 2011-2022 走看看