FOJ2022车站 线段树区间合并
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车站 线段树区间合并的更多相关文章
- POJ 3667 Hotel(线段树 区间合并)
Hotel 转载自:http://www.cnblogs.com/scau20110726/archive/2013/05/07/3065418.html [题目链接]Hotel [题目类型]线段树 ...
- HDU 3911 线段树区间合并、异或取反操作
题目:http://acm.hdu.edu.cn/showproblem.php?pid=3911 线段树区间合并的题目,解释一下代码中声明数组的作用: m1是区间内连续1的最长长度,m0是区间内连续 ...
- HDU 3911 Black And White(线段树区间合并+lazy操作)
开始以为是水题,结果...... 给你一些只有两种颜色的石头,0为白色,1为黑色. 然后两个操作: 1 l r 将[ l , r ]内的颜色取反 0 l r 计算[ l , r ]内最长连续黑色石头的 ...
- HYSBZ 1858 线段树 区间合并
//Accepted 14560 KB 1532 ms //线段树 区间合并 /* 0 a b 把[a, b]区间内的所有数全变成0 1 a b 把[a, b]区间内的所有数全变成1 2 a b 把[ ...
- poj3667 线段树 区间合并
//Accepted 3728 KB 1079 ms //线段树 区间合并 #include <cstdio> #include <cstring> #include < ...
- hdu3911 线段树 区间合并
//Accepted 3911 750MS 9872K //线段树 区间合并 #include <cstdio> #include <cstring> #include < ...
- 线段树(区间合并) POJ 3667 Hotel
题目传送门 /* 题意:输入 1 a:询问是不是有连续长度为a的空房间,有的话住进最左边 输入 2 a b:将[a,a+b-1]的房间清空 线段树(区间合并):lsum[]统计从左端点起最长连续空房间 ...
- HDU 3308 LCIS (线段树区间合并)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3308 题目很好懂,就是单点更新,然后求区间的最长上升子序列. 线段树区间合并问题,注意合并的条件是a[ ...
- SPOJ GSS1_Can you answer these queries I(线段树区间合并)
SPOJ GSS1_Can you answer these queries I(线段树区间合并) 标签(空格分隔): 线段树区间合并 题目链接 GSS1 - Can you answer these ...
随机推荐
- 【POJ3243】拓展BSGS(附hash版)
上一篇博文中说道了baby step giant step的方法(简称BSGS),不过对于XY mod Z = K ,若x和z并不互质,则不能直接套用BSGS的方法了. 为什么?因为这时候不存在逆元了 ...
- 小米2000万买域名mi.com
来源:互联网的一些事 移动互联网之下,域名对于企业的吸引力将会越来越低,因为网站的入口多元化,不再仅凭域名.小米用超2000万人民币的代价购买mi.com域名,仅仅是为了所谓的国际化吗?小米此举, ...
- DP:斐波纳契数
题目:输出第 n 个斐波纳契数(Fibonacci) 方法一.简单递归 这个就不说了,小n怡情,大n伤身啊……当n=40的时候,就明显感觉到卡了,不是一般的慢. //输出第n个 Fibonacci 数 ...
- lintcode 中等题:Evaluate Reverse Polish notation逆波兰表达式求值
题目 逆波兰表达式求值 在逆波兰表达法中,其有效的运算符号包括 +, -, *, / .每个运算对象可以是整数,也可以是另一个逆波兰计数表达. 样例 ["2", "1&q ...
- lintcode 中等题: Implement Trie
题目 Implement Trie Implement a trie with insert, search, and startsWith methods. 样例 注意 You may assu ...
- lintcode:Remove Nth Node From End of Lis 删除链表中倒数第n个节点
题目: 删除链表中倒数第n个节点 给定一个链表,删除链表中倒数第n个节点,返回链表的头节点. 样例 给出链表1->2->3->4->5->null和 n = 2. 删除 ...
- *[topcoder]TheMatrix
http://community.topcoder.com/stat?c=problem_statement&pm=13035 矩阵棋盘的题,比较典型.可以定两条column夹住,然后上下扫, ...
- React-Flux 介绍及实例演示
一.Flux架构 二.例子 1.TodoApp.react.js /** * Copyright (c) 2014-2015, Facebook, Inc. * All rights reserved ...
- 一个简单的将GUI程序的log信息输出到关联的Console窗口中(AllocConsole SetConsoleTitle WriteConsole 最后用ShowWindow(GetConsoleWindow)进行显示)
// .h 文件 #pragma once class CConsoleDump { public: explicit CConsoleDump(LPCTSTR lpszWindowTitle = N ...
- player/stage 学习---安装
环境 ubuntu 14.04 一,工具安装 sudo apt-get install git cmake g++ fltk1.1-dev libjpeg8-dev libpng12-dev libg ...