http://acm.fzu.edu.cn/problem.php?pid=2022

刚开始MLE,用map对应,果断爆内存了,然后改用去重,离散化, lowbound查找元素位置,速度还不错,不过pushup写也是醉了,一遍遍错,最后发现

if(o[rt].left==INF||o[rt].right==INF||o[rt].left==o[rt].right)l=INF;这一句第三个判断必须加,发现这样可以避免那种只由一个点往上更新的错误,也是非常的伤感。。。。。

/*96655 's source code for D
Memory: 30072 KB Time: 265 MS
Language: GNU C++ Result: Accepted
*/
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<vector>
#include<stack>
#include<cmath>
#include<queue>
#include<map>
using namespace std;
const int maxn=+;
const int INF=1e9+;
struct asd
{
int x;
char c;
} q[maxn];
bool vis[maxn];
int a[maxn];
struct node
{
int left,right,v;
} o[maxn<<];
void pushup(int rt)
{
o[rt].left=min(o[rt*].left,o[rt*+].left);
if(o[rt*+].right!=INF) o[rt].right=o[rt*+].right;
else o[rt].right=o[rt*].right;
int l=fabs(o[rt*].right-o[rt*+].left);
if(o[rt].left==INF||o[rt].right==INF||o[rt].left==o[rt].right)l=INF;
o[rt].v=min(min(o[rt*].v,o[rt*+].v),l);
}
void build(int rt,int l,int r)
{
if(l==r)
{
o[rt].left=o[rt].right=o[rt].v=INF;
return;
}
int m=(l+r)>>;
build(rt*,l,m);
build(rt*+,m+,r);
pushup(rt);
}
void change(int rt,int l,int r,int pos,int p)
{
if(l==r)
{
if(p)o[rt].left=o[rt].right=a[l-];
else o[rt].left=o[rt].right=INF;
return;
}
int m=(l+r)>>;
if(pos<=m)change(rt*,l,m,pos,p);
else change(rt*+,m+,r,pos,p);
pushup(rt);
}
int main()
{
int T;
char s[];
while(~scanf("%d",&T))
{
int cnt=,sum=,k=;
for(int i=; i<=T; ++i)
{
scanf("%s",s);
q[i].c=s[];
if(s[]=='m')continue;
scanf("%d",&q[i].x);
a[k++]=q[i].x;
}
sort(a,a+k);
for(int i=; i<k; i++)
if(a[i]!=a[i-])a[cnt++]=a[i];
build(,,cnt);
memset(vis,,sizeof(vis));
for(int i=; i<=T; i++)
{
if(q[i].c=='a')
{
int pos=lower_bound(a,a+cnt,q[i].x)-a;
pos++;
if(vis[pos])continue;
vis[pos]=;
++sum;
change(,,cnt,pos,);
}
else if(q[i].c=='d')
{
int pos=lower_bound(a,a+cnt,q[i].x)-a;
pos++;
if(!vis[pos])continue;
vis[pos]=;
--sum;
change(,,cnt,pos,);
}
else
{
if(sum<)printf("0\n");
else printf("%d\n",o[].v);
}
}
}
return ;
}

唯一值得欣慰的是速度还不错。。。。QAQ

FOJ2022车站 线段树区间合并的更多相关文章

  1. POJ 3667 Hotel(线段树 区间合并)

    Hotel 转载自:http://www.cnblogs.com/scau20110726/archive/2013/05/07/3065418.html [题目链接]Hotel [题目类型]线段树 ...

  2. HDU 3911 线段树区间合并、异或取反操作

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=3911 线段树区间合并的题目,解释一下代码中声明数组的作用: m1是区间内连续1的最长长度,m0是区间内连续 ...

  3. HDU 3911 Black And White(线段树区间合并+lazy操作)

    开始以为是水题,结果...... 给你一些只有两种颜色的石头,0为白色,1为黑色. 然后两个操作: 1 l r 将[ l , r ]内的颜色取反 0 l r 计算[ l , r ]内最长连续黑色石头的 ...

  4. HYSBZ 1858 线段树 区间合并

    //Accepted 14560 KB 1532 ms //线段树 区间合并 /* 0 a b 把[a, b]区间内的所有数全变成0 1 a b 把[a, b]区间内的所有数全变成1 2 a b 把[ ...

  5. poj3667 线段树 区间合并

    //Accepted 3728 KB 1079 ms //线段树 区间合并 #include <cstdio> #include <cstring> #include < ...

  6. hdu3911 线段树 区间合并

    //Accepted 3911 750MS 9872K //线段树 区间合并 #include <cstdio> #include <cstring> #include < ...

  7. 线段树(区间合并) POJ 3667 Hotel

    题目传送门 /* 题意:输入 1 a:询问是不是有连续长度为a的空房间,有的话住进最左边 输入 2 a b:将[a,a+b-1]的房间清空 线段树(区间合并):lsum[]统计从左端点起最长连续空房间 ...

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

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3308 题目很好懂,就是单点更新,然后求区间的最长上升子序列. 线段树区间合并问题,注意合并的条件是a[ ...

  9. SPOJ GSS1_Can you answer these queries I(线段树区间合并)

    SPOJ GSS1_Can you answer these queries I(线段树区间合并) 标签(空格分隔): 线段树区间合并 题目链接 GSS1 - Can you answer these ...

随机推荐

  1. ccflow学习下载网址

    1.ccflow下载:http://ccflow.org/download.aspx 2.说明:http://ccbpm.mydoc.io/ 3.各种文档:bbs.ccflow.org/showtop ...

  2. asp.net后台获取路径的各种方法归纳

    asp.net后台获取路径的各种方法归纳   1.Request.CurrentExecutionFilePath    获取当前请求的虚拟路径,不同于 FilePath,差别在于如果请求已在服务器代 ...

  3. lintcode : 二叉树的层次遍历II

    题目 二叉树的层次遍历 II 给出一棵二叉树,返回其节点值从底向上的层次序遍历(按从叶节点所在层到根节点所在的层遍历,然后逐层从左往右遍历) 样例 给出一棵二叉树 {3,9,20,#,#,15,7}, ...

  4. windows下安装ubantu

        首先声明我是一个linux大菜鸟,之所以学这个,一个是好玩,另外做DL的一些软件如Caffe要在这个平台上运行,所以没事就鼓捣鼓捣.linux是一种内核,市场上支持这种内核的操作系统有uban ...

  5. 【mongoDB高级篇①】聚集运算之group,aggregate

    group 语法 db.collection.group({ key:{field:1},//按什么字段进行分组 initial:{count:0},//进行分组前变量初始化,该处声明的变量可以在以下 ...

  6. 两个List合并,过滤重复记录

    import java.util.ArrayList; import java.util.HashSet; import java.util.Hashtable; import java.util.I ...

  7. 不要将缓存服务器与Tomcat放在单台机器上,否则出现竞争内存问题

    缓存分为本地缓存和远程分布式缓存,本地缓存访问速度更快但缓存数据量有限,同时存在与应用程序争用内存的情况. 1.不要将缓存服务器与Tomcat放在单台机器上,否则出现竞争内存问题 2.不要将缓存服务器 ...

  8. python 编码问题(二)

    >>> a = '中文' >>> chardet.detect(a) {'confidence': 0.7525, 'encoding': 'utf-8'} > ...

  9. VCL源码分析方法论(以TButton.Caption属性的由来为例)

    最近一段时间似乎流行源码分析:)我也来谈谈在过去一段时间里对VCL源码的分析方法方面的一点体会,本文将不探讨VCL类库的构架和设计模式方面的东本,只是以我们常见的控件属性/方法的实现过程作简单的说明, ...

  10. centos下安装JDK8的方法

    判断是否安装 首先,我们得判断机子上是不是安装了jdk,好多人推荐使用java -version命令.我的计算机上使用java -version命令,内容如下: java version " ...