当前位置: 首页 > news >正文

hdu 3911 Black And White 线段树

代码写得比较乱。

区间最长连续黑色:maxb

区间最长连续白色:maxw

区间左端点颜色:lcolor

区间右端点颜色:rcolor

左端点起最长连续数:lmax

右端点起最长连续数:rmax

标记区间是否被更改过:flag

#include<iostream>

using namespace std;

struct tree
{
    int left;
    int right;
    int maxb,maxw;
    int lcolor,rcolor;
    int lmax,rmax;
    int flag;
}t[400000];

inline int max(int a,int b)
{
    return a>b? a:b;
}
inline int min(int a,int b)
{
    return a>b? b:a;
}

int key[100005];

void create(int l,int r,int now);
void add_node(int now);
void update_node(int now);
void update_tree(int l,int r,int now);
void change_node(int now);
int query(int l,int r,int now);

int main()
{
    int n,m,x,l,r,ans;

    while(scanf("%d",&n)!=EOF)
    {
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&key[i]);
        }

        create(1,n,1);
        add_node(1);

        cin>>m;
        while(m--)
        {
            scanf("%d%d%d",&x,&l,&r);

            if(x==0)
            {
                ans=query(l,r,1);
                printf("%d\n",ans);
            }
            else
                update_tree(l,r,1);
        }
    }
    return 0;
}

void create(int l,int r,int now)
{
    t[now].left=l;
    t[now].right=r;

    t[now].lmax=0;
    t[now].rmax=0;

    t[now].maxb=0;
    t[now].maxw=0;

    t[now].rcolor=0;
    t[now].lcolor=0;

    t[now].flag=0;

    if(l<r)
    {
        int mid=(r+l)>>1;

        create(l,mid,now<<1);
        create(mid+1,r,(now<<1)+1);        
    }

    return;
}

void add_node(int now)
{
    if(t[now].left==t[now].right)
    {
        t[now].lcolor=t[now].rcolor=key[t[now].left];

        t[now].lmax=t[now].rmax=1;

        t[now].maxb=key[t[now].left];
        t[now].maxw=1-key[t[now].left];

        t[now].flag=0;

        return ;
    }

    add_node(now<<1);
    add_node( (now<<1)+1 );
    update_node(now);
    return ;
}

void update_node(int now)
{
    int left=now<<1,right=(now<<1)+1;

    t[now].lcolor=t[left].lcolor;
    t[now].rcolor=t[right].rcolor;

    if(t[left].lmax==t[left].right-t[left].left+1 && t[left].rcolor==t[right].lcolor)
        t[now].lmax=t[left].lmax+t[right].lmax;
    else
        t[now].lmax=t[left].lmax;

    if(t[right].rmax==t[right].right-t[right].left+1 && t[right].lcolor==t[left].rcolor)
        t[now].rmax=t[right].rmax+t[left].rmax;
    else
        t[now].rmax=t[right].rmax;

    t[now].maxb=max(t[right].maxb,t[left].maxb);
    t[now].maxw=max(t[right].maxw,t[left].maxw);

    if(t[left].rcolor==t[right].lcolor)
    {
        if(t[left].rcolor==1)
            t[now].maxb=max(t[now].maxb,t[left].rmax+t[right].lmax);
        else if(t[left].rcolor==0)
            t[now].maxw=max(t[now].maxw,t[left].rmax+t[right].lmax);
    }
}

void update_tree(int l,int r,int now)
{
    if(t[now].left==l && t[now].right==r)
    {
        change_node(now);
        return ;
    }

    if(t[now].flag==1)
    {
        change_node(now<<1);
        change_node( (now<<1) +1);

        t[now].flag=0;
    }

    int mid=(t[now].left+t[now].right)>>1;

    if(r<=mid)
        update_tree(l,r,now<<1);
    else if(l>mid)
        update_tree(l,r,(now<<1)+1);
    else
    {
        update_tree(l,mid,now<<1);
        update_tree(mid+1,r,(now<<1)+1);
    }
    update_node(now);
}

void change_node(int now)
{
    t[now].lcolor=t[now].lcolor^1;
    t[now].rcolor=t[now].rcolor^1;

    t[now].maxb=t[now].maxb^t[now].maxw;
    t[now].maxw=t[now].maxb^t[now].maxw;
    t[now].maxb=t[now].maxb^t[now].maxw;

    t[now].flag=t[now].flag^1;
}

int query(int l,int r,int now)
{
    if(t[now].left==l && t[now].right==r)
        return t[now].maxb;

    if(t[now].flag==1)
    {
        change_node(now<<1);
        change_node( (now<<1)+1 );
        t[now].flag=0;
    }

    int mid=(t[now].left+t[now].right)>>1;
//    int left=now<<1,right=(now<<1)+1;
    int t1=0,t2=0,t3=0,t4=0,t5=0;

    if(r<=mid)
        t1=query(l,r,now<<1);
    else if(l>mid)
        t2=query(l,r,(now<<1)+1);
    else
    {
        t3=query(l,mid,now<<1);
        t4=query(mid+1,r,(now<<1)+1);

        if(t[now<<1].rcolor==1 && t[ (now<<1)+1 ].lcolor==1)
            t5=min(mid-l+1,t[now<<1].rmax)+min(r-mid,t[ (now<<1)+1].lmax);
    }
    update_node(now);
    return max( max( max(t1,t2),max(t3,t4) ) , t5);
}


相关文章:

  • hdu 1068 Girls and Boys 二分匹配
  • 穿越红尘不扰关,回旋天地去复还
  • The guide to implementing 2D platformers(2D动作游戏开发与实现)
  • 2D动作游戏开发与实现(翻译)
  • 关于在WP7的XNA开发模式中引入广告(Ad)
  • HTML5全球普及加速:预计将终结iOS与Android界限(转载)
  • 更改ubuntu的挂载点
  • 学习旅程——轻松快乐
  • ACM模板列表
  • LGame开始进行0.3.3正式发布前的最终代码整合
  • 最近的小问题
  • Android的手势识别GestureDetector
  • 父View禁用touch 如何让子view还能获取touch event
  • JAVA_OPTS参数-Xms和-Xmx的作用
  • 浅谈Java游戏引擎在智能机领域的发展
  • 【跃迁之路】【641天】程序员高效学习方法论探索系列(实验阶段398-2018.11.14)...
  • Android Volley源码解析
  • cookie和session
  • ES2017异步函数现已正式可用
  • HTTP传输编码增加了传输量,只为解决这一个问题 | 实用 HTTP
  • HTTP中GET与POST的区别 99%的错误认识
  • Linux中的硬链接与软链接
  • python 学习笔记 - Queue Pipes,进程间通讯
  • Python_OOP
  • Vue.js源码(2):初探List Rendering
  • 大数据与云计算学习:数据分析(二)
  • 官方新出的 Kotlin 扩展库 KTX,到底帮你干了什么?
  • 规范化安全开发 KOA 手脚架
  • 汉诺塔算法
  • 近期前端发展计划
  • 入口文件开始,分析Vue源码实现
  • 实现菜单下拉伸展折叠效果demo
  • 双管齐下,VMware的容器新战略
  • 走向全栈之MongoDB的使用
  • Mac 上flink的安装与启动
  • 新年再起“裁员潮”,“钢铁侠”马斯克要一举裁掉SpaceX 600余名员工 ...
  • #、%和$符号在OGNL表达式中经常出现
  • #大学#套接字
  • (pytorch进阶之路)扩散概率模型
  • (附源码)ssm高校升本考试管理系统 毕业设计 201631
  • (附源码)ssm考试题库管理系统 毕业设计 069043
  • (论文阅读23/100)Hierarchical Convolutional Features for Visual Tracking
  • (四)搭建容器云管理平台笔记—安装ETCD(不使用证书)
  • (算法设计与分析)第一章算法概述-习题
  • (图)IntelliTrace Tools 跟踪云端程序
  • (心得)获取一个数二进制序列中所有的偶数位和奇数位, 分别输出二进制序列。
  • (一)【Jmeter】JDK及Jmeter的安装部署及简单配置
  • (一)Linux+Windows下安装ffmpeg
  • (一)RocketMQ初步认识
  • (已解决)报错:Could not load the Qt platform plugin “xcb“
  • (转) ns2/nam与nam实现相关的文件
  • (转)c++ std::pair 与 std::make
  • (转)负载均衡,回话保持,cookie
  • (状压dp)uva 10817 Headmaster's Headache
  • .L0CK3D来袭:如何保护您的数据免受致命攻击