HDU 3911 线段树区间合并

北京赛区快了,准备袭击数据结构和图论。倒计时 18天,线段树区间合并。维护一个最长连续。。

题意:给一个01串,以下有一些操作,问区间最长的连续的1的个数

思路:非常裸的线段树区间合并

#include<iostream>
#include<cstdio>
#include<map>
#include<set>
#include<cmath>
#define lson id << 1
#define rson id << 1|1
using namespace std;
const int  M = 1e6+8;
int a[M];
struct tree{
    int l,r;
    int rsum1,lsum1,msum1,lsum0,rsum0,msum0;
    int flag;
    int mid(){
        return (l+r)/2;
    }
}node[M];
void pushdown(int id){
    if(node[id].flag){
        node[lson].flag ^= 1;
        node[rson].flag ^= 1;
        node[id].flag = 0;
        swap(node[lson].lsum1,node[lson].lsum0);
        swap(node[lson].rsum1,node[lson].rsum0);
        swap(node[lson].msum1,node[lson].msum0);

        swap(node[rson].lsum1,node[rson].lsum0);
        swap(node[rson].rsum1,node[rson].rsum0);
        swap(node[rson].msum1,node[rson].msum0);
    }
}
void pushup(int id){
    int ll = node[lson].r-node[lson].l + 1;
    int rl = node[rson].r-node[rson].l + 1;
    node[id].lsum1 = node[lson].lsum1;
    if(node[lson].lsum1 == ll)node[id].lsum1 += node[rson].lsum1;
    node[id].rsum1 = node[rson].rsum1;
    if(node[rson].rsum1 == rl)node[id].rsum1 +=  node[lson].rsum1;
    node[id].msum1 = max(max(node[rson].msum1,node[lson].msum1),node[lson].rsum1+node[rson].lsum1);

    node[id].lsum0 = node[lson].lsum0;
    if(node[lson].lsum0 == ll)node[id].lsum0 += node[rson].lsum0;
    node[id].rsum0 = node[rson].rsum0;
    if(node[rson].rsum0 == rl)node[id].rsum0 +=  node[lson].rsum0;
    node[id].msum0 = max(max(node[rson].msum0,node[lson].msum0),node[lson].rsum0+node[rson].lsum0);

}
void build(int l,int r,int id){
    node[id].l = l;
    node[id].r = r;
    node[id].flag = 0;
    if(l == r){
        if(a[l] == 1){
            node[id].lsum1 = node[id].rsum1 = node[id].msum1 = 1;
            node[id].lsum0 = node[id].rsum0 = node[id].msum0 = 0;
        }else{
            node[id].lsum1 = node[id].rsum1 = node[id].msum1 = 0;
            node[id].lsum0 = node[id].rsum0 = node[id].msum0 = 1;
        }
        return;
    }
    int mid = node[id].mid();
    build(l,mid,lson);
    build(mid+1,r,rson);
    pushup(id);
}
void update(int id,int l,int r){
    if(node[id].l == l && node[id].r == r){
        node[id].flag ^= 1;
        swap(node[id].lsum1,node[id].lsum0);
        swap(node[id].rsum1,node[id].rsum0);
        swap(node[id].msum1,node[id].msum0);
        return ;
    }
    pushdown(id);
    int mid = node[id].mid();
    if(r <= mid)update(lson,l,r);
    else if(l>mid)update(rson,l,r);
    else {
        update(lson,l,mid);
        update(rson,mid+1,r);
    }
    pushup(id);
}
int query(int id,int l,int r){
    if(node[id].l == l && node[id].r ==r){
        return node[id].msum1;
    }
    pushdown(id);
    int mid = node[id].mid();
    if(r <=mid)return query(lson,l,r);
    else
        if(l > mid)return query(rson,l,r);
    else {
        int ll = query(lson,l,mid);
        int rr = query(rson,mid+1,r);
        int a = node[lson].rsum1;
        if(a > (node[lson].r - l +1))a = node[lson].r - l +1 ;

        int b = node[rson].lsum1;
        if(b > (r - node[rson].l+1))b = r - node[rson].l+1;
        return max(max(ll,rr),a+b);
    }

}
int main(){
    int n,m,op,l,r;
    while(~scanf("%d",&n)){
        for(int i=1;i<=n;i++)scanf("%d",&a[i]);
        build(1,n,1);
        scanf("%d",&m);
        while(m--){
            scanf("%d%d%d",&op,&l,&r);
            if(!op)printf("%d\n",query(1,l,r));
            else update(1,l,r);
        }
    }
}
时间: 2024-12-25 05:23:34

HDU 3911 线段树区间合并的相关文章

HDU 4339 线段树区间合并

Query Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2573    Accepted Submission(s): 851 Problem Description You are given two strings s1[0..l1], s2[0..l2] and Q - number of queries.Your task

hdu 3308(线段树区间合并)

LCIS Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6069    Accepted Submission(s): 2635 Problem Description Given n integers.You have two operations:U A B: replace the Ath number by B. (index

hdu 3308 线段树 区间合并+单点更新+区间查询

LCIS Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6592    Accepted Submission(s): 2866 Problem Description Given n integers.You have two operations:U A B: replace the Ath number by B. (index

hdu 1806(线段树区间合并)

Frequent values Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1476    Accepted Submission(s): 541 Problem Description You are given a sequence of n integers a1 , a2 , ... , an in non-decreasin

HDU 3911 Black And White(线段树区间合并)

Problem Description There are a bunch of stones on the beach; Stone color is white or black. Little Sheep has a magic brush, she can change the color of a continuous stone, black to white, white to black. Little Sheep like black very much, so she wan

HDU 3308 LCIS (线段树区间合并)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3308 题目很好懂,就是单点更新,然后求区间的最长上升子序列. 线段树区间合并问题,注意合并的条件是a[mid + 1] > a[mid],写的细心点就好了. 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 using namespace std; 5 const int MAXN = 1

HDU 5316 Magician(线段树区间合并入门)

本文纯属原创,转载请注明出处谢谢.http://blog.csdn.net/zip_fan. 题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=5316 Time Limit: 18000/9000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem Description Fantasy magicians usually gain their ability

HDU 3308 LCIS(最长连续上升子序列)(线段树区间合并)

题意:给你n个整数,有两种操作,U A B把第A个数变成B,Q A B查询区间[A,B]的最长连续上升序列. 思路:还是查询和更新操作,而且也是询问区间中满足条件的连续最长区间 ,所以是线段树区间合并类型的题,通法是开三棵线段树,一个记录此区间内的LCIS的最长长度,一个记录从左边第一个数开始的LCIS长度,另一个记录从右边最后一个数结尾的LCIS长度.然后试图找到父亲与儿子关系维护的递推关系式就好 本题中的递推关系是: 1. 左儿子最右边的值 < 右儿子最左边的值 lmx = (左儿子的lmx

hdu3911 线段树 区间合并

1 //Accepted 3911 750MS 9872K 2 //线段树 区间合并 3 #include <cstdio> 4 #include <cstring> 5 #include <iostream> 6 #include <queue> 7 #include <cmath> 8 #include <algorithm> 9 using namespace std; 10 /** 11 * This is a documen