码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 5023 线段树 区间 2014广东区域赛网赛

时间:2014-09-24 02:10:35      阅读:359      评论:0      收藏:0      [点我收藏+]

标签:des   style   http   color   io   os   ar   for   2014   

http://acm.hdu.edu.cn/showproblem.php?pid=5023

当时大一学弟15minAC

搞得我压力山大 给队友写了

今天重新做了下,题还是很水  但是因为pushdown的时候if(l==r)return没有写  WA了一次

感觉到现在,简单的线段树已经可以随意点写了,就是按照自己的理解写,别慌,错了按树的结构思考下重新写

查询不知道pushdown所有的会不会超时,我的还是区间的查询,凡是子区间被修改过的结点,该节点pushdown后 其co值都被我改为0

然后就是vector存颜色,排序,去重输出

//#pragma comment(linker, "/STACK:102400000,102400000")
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <map>
#include <set>
#include <queue>
using namespace std;

#define ls(rt) rt*2
#define rs(rt) rt*2+1
#define ll long long
#define ull unsigned long long
#define rep(i,s,e) for(int i=s;i<e;i++)
#define repe(i,s,e) for(int i=s;i<=e;i++)
#define CL(a,b) memset(a,b,sizeof(a))
#define IN(s) freopen(s,"r",stdin)
#define OUT(s) freopen(s,"w",stdout)
const ll ll_INF = ((ull)(-1))>>1;
const double EPS = 1e-8;
const double pi = acos(-1.0);
const int INF = 100000000;
const int MAXN = 1000000+100;

struct Node{
    int l,r;
    int co,flag;
}nodes[MAXN*4];

void build(int rt, int l, int r)
{
    nodes[rt].l=l;
    nodes[rt].r=r;
    nodes[rt].co=2;
    nodes[rt].flag=0;
    if(l == r)return;
    int mid=(l+r)/2;
    build(ls(rt),l,mid);
    build(rs(rt),mid+1,r);
}

void pushdown(int rt)
{
    if(nodes[rt].l == nodes[rt].r)return;
    if(nodes[rt].flag)
    {
        nodes[rs(rt)].co=nodes[ls(rt)].co=nodes[rt].co;
        nodes[ls(rt)].flag=nodes[rs(rt)].flag=1;
        nodes[rt].flag=0;
    }

}

void update(int rt, int l, int r, int cc)
{

    if(l<=nodes[rt].l && nodes[rt].r<=r)
    {
        pushdown(rt);
        nodes[rt].flag=1;
        nodes[rt].co=cc;
        return;
    }
    pushdown(rt);
    nodes[rt].co=0;
    int mid=(nodes[rt].l+nodes[rt].r)/2;
    if(l<=mid)update(ls(rt),l,r,cc);
    if(r>mid)update(rs(rt),l,r,cc);

}
vector<int>ans;

void query(int rt, int l, int r)
{
    ///
    //printf("nodes[rt].l=%d nodes[rt].r=%d l=%d r=%d co=%d\n",nodes[rt].l,nodes[rt].r,l,r,nodes[rt].co);
    //////.
    if(nodes[rt].l == nodes[rt].r)
    {
        if(nodes[rt].co)ans.push_back(nodes[rt].co);
        return ;
    }
    if(nodes[rt].l == l && nodes[rt].r==r && nodes[rt].co)
    {
        //pushdown(rt);
        ans.push_back(nodes[rt].co);
        return ;
    }
    pushdown(rt);
    int mid=(nodes[rt].l+nodes[rt].r)/2;
    if(r<=mid)query(ls(rt),l,r);
    else
    {
        if(l>mid)
            query(rs(rt),l,r);
        else
        {
            query(ls(rt),l,mid);
            query(rs(rt),mid+1,r);
        }
    }
}

int n,m;

int main()
{
    //IN("hdu5023.txt");
    char op[20];
    int l,r;
    while(~scanf("%d%d",&n,&m) && n+m)
    {
        build(1,1,n);
        while(m--)
        {
            scanf("%s%d%d",op,&l,&r);
            if(op[0] == 'P')
            {
                int cc;
                scanf("%d",&cc);
                update(1,l,r,cc);
            }
            else
            {
                ans.clear();
                query(1,l,r);
                sort(ans.begin(),ans.end());
                printf("%d",ans[0]);
                for(int i=1;i<ans.size();i++)
                {
                    if(ans[i]!=ans[i-1])printf(" %d",ans[i]);
                }
                putchar('\n');
            }
        }
    }
    return 0;
}



hdu 5023 线段树 区间 2014广东区域赛网赛

标签:des   style   http   color   io   os   ar   for   2014   

原文地址:http://blog.csdn.net/u011026968/article/details/39507265

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!