附动态线段树AC代码

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

因为昨天做了一道动态线段树的缘故,今天遇到了这题没有限制范围的题就自然而然想到了动态线段树的解法,写完看题解发现原来只要离散化就好了(干。。),总结了一下这题和昨天hdu5367的区别在于,虽然都是两题范围超级大的线段树,但是昨天的强制要求在线求解,只能选择空间复杂度更大一些的动态线段树来求解,而今天的这题可以选择离线操作,因而可以采用先读入所有输入,离散化之后建树的方法来操作。下次遇到这样的题还是应当优先考虑离散化。

在一个全部涂黑色的条子上涂上一些白色或黑色的片段,问最大白色片段。

仅仅从线段树维护节点的角度上来看很简单,维护最大白色片段,左边最大白色片段,右边最大白色片段就好了。

由于条子的长度长达1-INT_MAX;

采用离散化,或者像我一样失了智去用动态线段树的方法,也能AC

#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional>
#define For(i, x, y) for(int i=x; i<=y; i++)
#define _For(i, x, y) for(int i=x; i>=y; i--)
#define Mem(f, x) memset(f, x, sizeof(f))
#define Sca(x) scanf("%d", &x)
#define Scl(x) scanf("%lld",&x);
#define Pri(x) printf("%d\n", x)
#define Prl(x) printf("%lld\n",x);
#define CLR(u) for(int i = 0; i <= N ; i ++) u[i].clear();
#define LL long long
#define ULL unsigned long long
#define mp make_pair
#define PII pair<int,int>
#define PIL pair<int,long long>
#define PLL pair<long long,long long>
#define pb push_back
#define fi first
#define se second
using namespace std;
typedef vector<int> VI;
const double eps = 1e-;
const int maxn = ;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + ;
inline int read()
{
int now=;register char c=getchar();
for(;!isdigit(c);c=getchar());
for(;isdigit(c);now=now*+c-'',c=getchar());
return now;
}
int N,M;
struct Tree{
LL sum; //最大连续
LL lsum; //左连续
LL rsum; //右连续
int lt;
int rt;
int lazy;
void init(){
lsum = rsum = sum = lt = rt = ;
lazy = -;
}
}tree[maxn * ];
int tot;
void check(int &t){
if(t) return;
t = ++tot;
tree[t].init();
}
void add(int &t,LL L,LL R,int v){
if(v){
tree[t].sum = tree[t].lsum = tree[t].rsum = R - L + ;
}else{
tree[t].sum = tree[t].lsum = tree[t].rsum = ;
}
tree[t].lazy = v;
}
void Pushdown(int& t,LL L,LL R){
if(tree[t].lazy == -) return;
int &lt = tree[t].lt; int &rt = tree[t].rt;
LL M = (L + R) >> ;
check(lt); check(rt);
add(lt,L,M,tree[t].lazy);
add(rt,M + ,R,tree[t].lazy);
tree[t].lazy = -;
}
void Pushup(int t,LL L,LL R){
int &ls = tree[t].lt; int &rs = tree[t].rt;
LL M = (L + R) >> ;
check(ls); check(rs);
tree[t].sum = max(tree[ls].sum,tree[rs].sum);
tree[t].sum = max(tree[t].sum,tree[ls].rsum + tree[rs].lsum);
tree[t].lsum = tree[ls].lsum;
if(tree[ls].lsum == M - L + ){
tree[t].lsum = tree[ls].lsum + tree[rs].lsum;
}
tree[t].rsum = tree[rs].rsum;
if(tree[rs].rsum == R - M){
tree[t].rsum = tree[rs].rsum + tree[ls].rsum;
}
}
void update(int &t,int q1,int q2,LL L,LL R,int v){
check(t);
if(q1 <= L && R <= q2){
add(t,L,R,v);
return;
}
Pushdown(t,L,R);
LL m = (L + R) >> ;
if(q1 <= m) update(tree[t].lt,q1,q2,L,m,v);
if(q2 > m) update(tree[t].rt,q1,q2,m + ,R,v);
Pushup(t,L,R);
}
LL Left,Right;
void query(int t,LL L,LL R){
if(L == R){
Left = L;
Right = R;
return;
}
check(tree[t].lt); check(tree[t].rt);
int ls = tree[t].lt; int rs = tree[t].rt;
LL M = (L + R) >> ;
if(tree[ls].sum == tree[t].sum) query(ls,L,M);
else if(tree[rs].sum == tree[t].sum) query(rs,M + ,R);
else{
Left = M - tree[ls].rsum + ;
Right = M + tree[rs].lsum;
return;
} }
int main()
{
while(~Sca(N)){
LL L = ; LL R = ;
tot = ;
int root = ;
update(root,L,R,L,R,);
For(i,,N){
LL l,r;
char op[];
scanf("%lld%lld%s",&l,&r,&op);
if(op[] == 'w'){
update(root,l,r,L,R,);
}else{
update(root,l,r,L,R,);
}
}
if(!tree[root].sum){
puts("Oh, my god");
continue;
}
query(root,L,R);
printf("%lld %lld\n",Left,Right);
}
return ;
}

HDU1199 动态线段树 // 离散化的更多相关文章

  1. POJ 2528 Mayor's posters(线段树+离散化)

    Mayor's posters 转载自:http://blog.csdn.net/winddreams/article/details/38443761 [题目链接]Mayor's posters [ ...

  2. poj 2528 Mayor's posters(线段树+离散化)

    /* poj 2528 Mayor's posters 线段树 + 离散化 离散化的理解: 给你一系列的正整数, 例如 1, 4 , 100, 1000000000, 如果利用线段树求解的话,很明显 ...

  3. [poj2528] Mayor's posters (线段树+离散化)

    线段树 + 离散化 Description The citizens of Bytetown, AB, could not stand that the candidates in the mayor ...

  4. [UESTC1059]秋实大哥与小朋友(线段树, 离散化)

    题目链接:http://acm.uestc.edu.cn/#/problem/show/1059 普通线段树+离散化,关键是……离散化后建树和查询都要按照基本法!!!RE了不知道多少次………………我真 ...

  5. poj 2528 Mayor's posters 线段树+离散化技巧

    poj 2528 Mayor's posters 题目链接: http://poj.org/problem?id=2528 思路: 线段树+离散化技巧(这里的离散化需要注意一下啊,题目数据弱看不出来) ...

  6. BZOJ_4653_[Noi2016]区间_线段树+离散化+双指针

    BZOJ_4653_[Noi2016]区间_线段树+离散化+双指针 Description 在数轴上有 n个闭区间 [l1,r1],[l2,r2],...,[ln,rn].现在要从中选出 m 个区间, ...

  7. D - Mayor's posters(线段树+离散化)

    题目: The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campai ...

  8. 主席树||可持久化线段树+离散化 || 莫队+分块 ||BZOJ 3585: mex || Luogu P4137 Rmq Problem / mex

    题面:Rmq Problem / mex 题解: 先离散化,然后插一堆空白,大体就是如果(对于以a.data<b.data排序后的A)A[i-1].data+1!=A[i].data,则插一个空 ...

  9. HDU5367 思维map // 动态线段树

    地主毛毛有n座山,这些山在地主家门前排成一条直线.这些山一开始均有相同的高度.  每一天,毛毛都会要求花花开挖机把几座山挖掉一定高度,或者给一些山堆上一些高度.并且要求花花报告现在有多少座山属于“高山 ...

随机推荐

  1. Linux内核第六节 20135332武西垚

    如何描述一个进程:进程描述符的数据结构: 如何创建一个进程:内核是如何执行的,以及新创建的进程从哪里开始执行: 使用gdb跟踪新进程的创建过程. 进程的描述 操作系统三大功能: 进程管理(最核心最基础 ...

  2. Maven入门指南④:仓库

    1 . 仓库简介 没有 Maven 时,项目用到的 .jar 文件通常需要拷贝到 /lib 目录,项目多了,拷贝的文件副本就多了,占用磁盘空间,且难于管理.Maven 使用一个称之为仓库的目录,根据构 ...

  3. FreeMarker boolean Issue

    FreeMarker template error:Can't convert boolean to string automatically, because the "boolean_f ...

  4. 【历史】- Windows NT 之父 - David Cutler

    David Cutler,大卫·卡特勒,一位传奇程序员,1988年去微软前号称硅谷最牛的内核开发人员,是VMS和Windows NT的首席设计师,被人们成为“操作系统天神”.他曾供职于杜邦.DEC等公 ...

  5. auto_increment 自增键的一些说明

    导致auto_increment变小的几种情况: 1. alter table xx auto_increment = yy; 2. truncate table 3. restart mysql 第 ...

  6. webstrom 安装Babel

    https://www.jianshu.com/p/b9bd2ec9ec80 https://www.cnblogs.com/zhishaofei/p/6061568.html https://blo ...

  7. poj 3352 Road Construction(边双连通分量+缩点)

    题目链接:http://poj.org/problem?id=3352 这题和poj 3177 一样,参考http://www.cnblogs.com/frog112111/p/3367039.htm ...

  8. ubuntu python apache2 wsgi django框架

    在ubuntu上通过apatch2和wsgi部署django (亲手做过!!!) 一,我的python.django.apatch2版本: python:python -V 2.7.3 django: ...

  9. mysql 常用命令导入导出修改root密码

    MySQL 忘记口令的解决办法如果 MySQL 正在运行,首先杀之: killall -TERM mysqld. 启动 MySQL :mysql --skip-grant-tables & / ...

  10. BZOJ5018[Snoi2017]英雄联盟——DP

    题目描述 正在上大学的小皮球热爱英雄联盟这款游戏,而且打的很菜,被网友们戏称为「小学生」.现在,小皮球终于受不 了网友们的嘲讽,决定变强了,他变强的方法就是:买皮肤!小皮球只会玩N个英雄,因此,他也只 ...