POJ3225Help with Intervals

开始没看懂题,看懂了之后也不知道如何用线段树来做这题,百度了一下思路

思路:

我们一个一个操作来分析:(用0和1表示是否包含区间,-1表示该区间内既有包含又有不包含)

U:把区间[l,r]覆盖成1

I:把[-∞,l)(r,∞]覆盖成0

D:把区间[l,r]覆盖成0

C:把[-∞,l)(r,∞]覆盖成0 , 且[l,r]区间0/1互换

S:[l,r]区间0/1互换

还有要注意的地方是把数组开为2倍,才可以用节点表示区间;注意a为0时的情况

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <string>
#include <iostream>
#include <map>
#include <cstdlib>
#include <list>
#include <set>
#include <queue>
#include <stack>
using namespace std;
#define INF 0xfffffff
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define maxn 132000
int col[maxn<<2];
void pushdown(int rt)
{
    if(col[rt]==2)
    {
        col[rt<<1]=1-col[rt<<1];
        col[rt<<1|1]=1-col[rt<<1|1];
        col[rt]=-1;
    }
    if(col[rt]!=-1)
    {
        col[rt<<1]=col[rt<<1|1]=col[rt];
        col[rt]=-1;
    }
}
void update(int L,int R,int add,int l,int r,int rt)
{
    if(L<=l&&R>=r)
    {
        if(add==2) col[rt]=1-col[rt];
        else col[rt]=add;
        return ;
    }
    pushdown(rt);
    int m=(l+r)>>1;
    if(L<=m) update(L,R,add,lson);
    if(R>m) update(L,R,add,rson);
}
int query(int key,int l,int r,int rt)
{
    if(l==r) return col[rt];
    pushdown(rt);
    int m=(l+r)>>1;
    if(key<=m) query(key,lson);
    else query(key,rson);
}
int main()
{
    char operation,bracket1,bracket2,nullity;
    int a,b;
    memset(col,0,sizeof(col));
    while(scanf(" %c %c%d %c%d %c",&operation,&bracket1,&a,&nullity,&b,&bracket2)!=-1)
    {
        a=a<<1;
        b=b<<1;
        if(bracket1=='(') a++;
        if(bracket2==')') b--;
        if(operation=='U')
        {
            update(a,b,1,0,maxn,1);
        }
        else if(operation=='I')
        {
            if(a<=0) a=1;
            update(0,a-1,0,0,maxn,1);
            update(b+1,maxn,0,0,maxn,1);
        }
        else if(operation=='D')
        {
            update(a,b,0,0,maxn,1);
        }
        else if(operation=='C')
        {
            update(a,b,2,0,maxn,1);
            if(a<=0) a=1;
            update(0,a-1,0,0,maxn,1);
            update(b+1,maxn,0,0,maxn,1);
        }
        else if(operation=='S')
        {
            update(a,b,2,0,maxn,1);
        }
    }
    int flag=1,num=0;
    for(int i=0;i<maxn;i++)
    {
        int k=query(i,0,maxn,1);
        if(k&&flag)
        {
            num++;
            flag=0;
            if(i&1) printf("(%d,",i>>1);
            else printf("[%d,",i>>1);
        }
        if(!flag&&!k)
        {
            flag=1;
            i--;
            if(i&1) printf("%d) ",(i+1)>>1);
            else printf("%d] ",(i+1)>>1);
        }
    }
    if(!num) printf("empty set\n");
    return 0;
}
时间: 2024-11-05 15:44:42

POJ3225Help with Intervals的相关文章

Poj3225Help with Intervals区间线段树

这题不说了,都是泪.这题也是拆点. #include <cstdio> #include <cstring> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include <cstdlib> #include <list> #include <set&g

(线段树)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

【线段树】POJ3225-Help with Intervals

---恢复内容开始--- [题目大意] (直接引用ACM神犇概括,貌似是notonlysucess?) U:把区间[l,r]覆盖成1 I:把[-∞,l)(r,∞]覆盖成0 D:把区间[l,r]覆盖成0 C:把[-∞,l)(r,∞]覆盖成0 , 且[l,r]区间0/1互换 S:[l,r]区间0/1互换 [思路] 由于涉及到开区间和闭区间,我们如此规定数组下表: 下标      0      1      2      3      4      5…… 含义1   (1    1    (2  

PKU1201 Intervals

Description You are given n closed, integer intervals [ai, bi] and n integers c1, ..., cn. Write a program that: reads the number of intervals, their end points and integers c1, ..., cn from the standard input, computes the minimal size of a set Z of

POJ1201 Intervals查分约束系统(最短路)

Description You are given n closed, integer intervals [ai, bi] and n integers c1, ..., cn. Write a program that: reads the number of intervals, their end points and integers c1, ..., cn from the standard input, computes the minimal size of a set Z of

ural 1346. Intervals of Monotonicity

1346. Intervals of Monotonicity Time limit: 1.0 secondMemory limit: 64 MB It’s well known that a domain of any continuous function may be divided into intervals where the function would increase monotonically or decrease monotonically. A number of in

poj 3225 Help with Intervals(线段树)

题目链接:poj 3225 Help with Intervals 题目大意:模拟集合操作,输出最终的集合. 解题思路:线段树. U l r:[l,r]区间置为1 I l r:[0,l),(r,maxn]置为0 D l r:[l,r]区间置为0 C l r:[0,l),(r,maxn]置为0,[l,r]区间取逆 S l r:[l,r]区间取逆. 然后基本水水的线段树,注意一下区间开和闭. #include <cstdio> #include <cstring> #include &

leetcode || 56、 Merge Intervals

problem: Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6],[8,10],[15,18], return [1,6],[8,10],[15,18]. Hide Tags Array Sort 题意:给定数组区间,合并有覆盖或者相邻的区间 thinking: (1)一開始我想到用hash table的方法,开一个总区间跨度的数组.对于有区间覆盖的数

POJ1375 Intervals(直线与圆切线、线段合并)

题目链接: http://poj.org/problem?id=1375 题目描述: Intervals Description In the ceiling in the basement of a newly open developers building a light source has been installed. Unfortunately, the material used to cover the floor is very sensitive to light. It