zoukankan      html  css  js  c++  java
  • (线段树)poj3225-Help with Intervals

    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)

      1 #include <iostream>
      2 //#include<bits/stdc++.h>
      3 #include <stack>
      4 #include <queue>
      5 #include <cstdio>
      6 #include <cstring>
      7 #include <algorithm>
      8 using namespace std;
      9 typedef long long ll;
     10 typedef unsigned long long ull;
     11 const int MAX=65535;
     12 struct node
     13 {
     14     int st,rev;
     15 }a[500*MAX];
     16 bool vi[3*MAX];
     17 
     18 void xxor(int k)
     19 {
     20     if(a[k].st!=-1)//如果区间状态一致,直接改变区间的情况
     21         a[k].st^=1;
     22     else
     23         a[k].rev^=1;//不然标记区间0,1互换1次
     24 }
     25 void pushdown(int k)
     26 {
     27     if(a[k].st!=-1)//如果区间状态一致
     28     {
     29         a[2*k].st=a[2*k+1].st=a[k].st;//将父结点的状态传递到子节点
     30         a[2*k].rev=a[2*k+1].rev=0;
     31         a[k].st=-1;//并且将父结点状态改回
     32     }
     33     if(a[k].rev)//如果需要0,1互换
     34     {
     35         xxor(2*k);
     36         xxor(2*k+1);
     37         a[k].rev=0;
     38     }
     39 }
     40 void update(char operation,int l,int r,int L,int R,int k)//[l,r]是目标区间,[L,R]是此时区间
     41 {
     42     if(L>=l&&R<=r)//如果当前区间已经包含在了目标区间之中
     43     {
     44         if(operation=='U')//如果取并集
     45         {
     46             a[k].st=1;//T区间所有元素值都赋1
     47             a[k].rev=0;//0,1互换情况初始化为0
     48         }
     49         else if(operation=='D')//如果取差集,差掉T
     50         {
     51             a[k].st=0;//T区间所有元素都赋0
     52             a[k].rev=0;//0,1互换情况初始化为0
     53         }
     54         else if(operation=='C'||operation=='S')//如果是原集合被差掉或异或集
     55         {
     56             xxor(k);
     57         }
     58         return;
     59     }
     60 //    if(L+1==R)
     61 //        return;
     62     pushdown(k);
     63     if(l<=(L+R)/2)//如果当前区间左半部分存在有部分在目标区间内的可能
     64     {
     65         update(operation,l,r,L,(L+R)/2,2*k);
     66     }
     67     else//若不然,当前区间左半部分已经在目标区间之外
     68     {
     69         if(operation=='I'||operation=='C')
     70         {
     71             a[2*k].st=0;a[2*k].rev=0;
     72         }
     73     }
     74     if(r>(L+R)/2)//如果当前区间的右半部分存在有部分在目标区间内的可能
     75     {
     76         update(operation,l,r,(L+R)/2+1,R,2*k+1);
     77     }
     78     else
     79     {
     80         if(operation=='I'||operation=='C')
     81         {
     82             a[2*k+1].st=a[2*k+1].rev=0;
     83         }
     84     }
     85 }
     86 void query(int l,int r,int k)
     87 {
     88     if(a[k].st==1)//如果这个区间完整的都是状态1
     89     {
     90         for(int i=l;i<=r;i++)
     91             vi[i]=true;
     92         return;
     93     }
     94     else if(a[k].st==0)//若此区间状态是已经完整的标明为状态0的,就不用再往下看了
     95         return;
     96     if(l==r)
     97         return;
     98     pushdown(k);
     99     query(l,(l+r)/2,2*k);
    100     query((l+r)/2+1,r,2*k+1);
    101 }
    102 int main()
    103 {
    104 //    int n;
    105 //    cin>>n;
    106 //    scanf("%d
    ",&n);
    107     char op,ls,rs,tem;//tem接收逗号
    108     int l,r;
    109 //    memset(vi,false,sizeof(vi));
    110     a[1].st=a[1].rev=0;
    111 //    while(--n)
    112 //    for(int q=1;q<=n;q++)
    113 
    114 //        scanf("%c %c%d,%d%c
    ",&op,&ls,&l,&r,&rs);
    115         while(~scanf("%c %c%d,%d%c
    ",&op,&ls,&l,&r,&rs))
    116 //        cin>>op>>ls>>l>>tem>>r>>rs;
    117       {
    118 //          scanf("%c %c%d,%d%c
    ",&op,&ls,&l,&r,&rs);
    119         l*=2;
    120         r*=2;
    121         if(ls=='(')
    122             l++;
    123         if(rs==')')
    124             r--;
    125         update(op,l,r,0,2*MAX,1);
    126 //        printf("%d
    ",n);
    127     }
    128 //    printf("~~!
    ");
    129     query(0,2*MAX,1);
    130 //    printf("!!
    ");
    131     int st=-1,en;//输出时的区间
    132     bool flag=false;
    133     for(int i=0;i<=2*MAX;i++)
    134     {
    135         if(vi[i])
    136         {
    137             if(st==-1)
    138                 st=i;
    139             en=i;
    140         }
    141         else
    142         {
    143             if(st!=-1)
    144             {
    145                 if(flag)
    146                     printf(" ");
    147 //                if(st%2==0)
    148 //                    printf("[");
    149 //                else
    150 //                    printf("(");
    151                 printf("%c%d,%d%c",st&1?'(':'[',st/2,(en+1)/2,en&1?')':']');//因为区间右侧做的是--,故需要+1再除二
    152 //                if(en%2==0)
    153 //                    printf("]");
    154 //                else
    155 //                    printf(")");
    156                 flag=true;
    157                 st=-1;
    158             }
    159 
    160         }
    161     }
    162     if(!flag)
    163         printf("empty set");
    164     return 0;
    165 }
  • 相关阅读:
    3.约束及修改数据表
    RSA总结
    消息队列面试官爱问的问题(一)
    Maven模块化开发
    系统初始化脚本和检查初始化结果脚本(centos7)
    kubectl 命令自动补全
    Kubernetes1.13.1部署Kuberneted-dashboard v1.10.1
    python实现连接MySQL、Redis并获取数据
    shell 脚本实现退点输出
    理论经典:TCP协议的3次握手与4次挥手过程详解
  • 原文地址:https://www.cnblogs.com/quintessence/p/6422184.html
Copyright © 2011-2022 走看看