hdu 4967 Handling the Past
view code//把时间离散化,维护一个线段(线段l到r的和用sum[l,r]表示),pop的时候就在对应的时间减一,push则相反
//那么每次peak的时候(假设在pk是时刻),找一个以pk为结尾的后缀和,这个后缀和为1且前端离pk最近。
//即sum[pu, pk]==1 且pu是最靠近pk的时间。
//理解:假设sum[l,pk]==0, 那么l到pk这段时间的push和pop次数相等,对peak无影响,可以直接忽视。pu时刻就
//可以理解为是最后一次push的时刻,该时刻push的数就是栈顶元素 #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const int INF = 1<<30;
const int N = 50010;
int sum[N<<2], suf[N<<2],id[N<<2];
int cas=1, n, x[N]; struct store
{
char type;
int v, t;
store() {}
store(char _type, int _t, int _v)
{
type = _type;
v = _v;
t = _t;
}
}sto[N]; void Up(int rt)
{
sum[rt] = sum[rt<<1|1] + sum[rt<<1];
suf[rt] = max(suf[rt<<1|1], suf[rt<<1]+sum[rt<<1|1]);
} void update(int p, int c, int pos, int l, int r, int rt)
{
if(l==r)
{
sum[rt] += c;
suf[rt] += c;
id[rt] = pos;
return ;
}
int m = (l+r)>>1;
if(p<=m) update(p, c, pos, lson);
else update(p, c, pos, rson);
Up(rt);
} int query(int L, int R, int l, int r, int rt)
{
if(L<=l && R>=r) return sum[rt];
int m = (l+r)>>1, ans = 0;
if(L<=m) ans += query(L, R, lson);
if(R>m) ans += query(L, R, rson);
return ans;
} int peak(int L, int R, int t, int l, int r, int rt)
{
// printf("peak(L=%d, R=%d, t=%d, l=%d, r=%d)=%d\n", L, R, t, l, r, id[rt]);
int m = (l+r)>>1, ans=0;
if(L<=l && R>=r)
{
if(l==r) return id[rt];
if(suf[rt<<1|1]>t) return peak(m+1, R, t, rson);
else if(suf[rt<<1]>t-sum[rt<<1|1]) return peak(L, m, t-sum[rt<<1|1], lson);
return 0;
}
if(R<=m) return peak(L, R, t, lson);
if(L>m) return peak(L, R, t, rson);
ans = peak(m+1, R, t, rson);
// ans = peak(L, R, t, rson);这样递归下去就错了
if(ans) return ans;
return peak(L, m, t-query(m+1, R, 1, n, 1),lson);
// return peak(L, R, t-query(m+1, R, 1, n, 1),lson);这样递归下去就错了
} void show()
{
for(int i=1; i<=n; i++)
{
if(sto[i].type == 'u')
printf("push %d %d\n", sto[i].t, sto[i].v);
else if(sto[i].type == 'e') printf("peak %d\n", sto[i].t);
else printf(" pop %d\n", sto[i].t);
}
} int main()
{
// freopen("in.txt", "r", stdin);
while(scanf("%d", &n)>0 && n)
{
memset(sum, 0, sizeof(sum));
memset(suf, 0, sizeof(suf));
memset(id, 0, sizeof(id));
char str[10];
int v, tm;
for(int i=1; i<=n; i++)
{
scanf("%s", str);
if(str[1]=='u') scanf("%d%d", &v, &tm);
else scanf("%d", &tm);
sto[i] = store(str[1], tm, v);
x[i] = tm;
}
sort(x+1, x+n+1);
for(int i=1; i<=n; i++) sto[i].t = lower_bound(x+1, x+1+n, sto[i].t)-x;
// show();
printf("Case #%d:\n", cas++);
for(int i=1; i<=n; i++)
{
tm = sto[i].t;
if(sto[i].type == 'u') update(tm, 1, i, 1, n, 1);
else if(sto[i].type == 'o') update(tm, -1, 0, 1, n, 1);
else
{
int pos = sto[i].t-1;
if(pos>0 && query(1, pos, 1, n, 1)>0)
{
printf("%d\n", sto[peak(1, pos, 0, 1, n, 1)].v);
}
else puts("-1");
}
}
}
return 0;
}
hdu 4967 Handling the Past的更多相关文章
- HDU 4960 Handling the past 2014 多校9 线段树
首先确定的基本思想是按时间离散化后来建线段树,对于每个操作插入到相应的时间点上 但是难就难在那个pop操作,我之前对pop操作的处理是找到离他最近的那个点删掉,但是这样对于后面的peak操作,如果时间 ...
- hdu 4960 Another OCD Patient (最短路 解法
http://acm.hdu.edu.cn/showproblem.php?pid=4960 2014 Multi-University Training Contest 9 Another OCD ...
- HDOJ 2111. Saving HDU 贪心 结构体排序
Saving HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 【HDU 3037】Saving Beans Lucas定理模板
http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...
- hdu 4859 海岸线 Bestcoder Round 1
http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...
- HDU 4569 Special equations(取模)
Special equations Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- HDU 4006The kth great number(K大数 +小顶堆)
The kth great number Time Limit:1000MS Memory Limit:65768KB 64bit IO Format:%I64d & %I64 ...
- HDU 1796How many integers can you find(容斥原理)
How many integers can you find Time Limit:5000MS Memory Limit:32768KB 64bit IO Format:%I64d ...
- hdu 4481 Time travel(高斯求期望)(转)
(转)http://blog.csdn.net/u013081425/article/details/39240021 http://acm.hdu.edu.cn/showproblem.php?pi ...
随机推荐
- [转]精通JS正则表达式
原文路径:http://www.jb51.net/article/25313.htm 正则表达式可以: •测试字符串的某个模式.例如,可以对一个输入字符串进行测试,看在该字符串是否存在一个电话号码模式 ...
- 酷酷的jQuery classicAccordion 手风琴
在线实例 效果一 效果二 效果三 使用方法 手风琴ul li列表 <ul class="accordion"> <li> < ...
- CSS高级选择符
2016-11-07 <css入门经典>第八章 1.属性选择器 选择器 描述 [attribute] 用于选取带有指定属性的元素. [attribute=value] 用于选取带有指定属性 ...
- 为Titanium创建自己的安卓推送模块
在手机应用中,推送是一个非常重要的功能.相对来说ios应用的推送功能很容易做,因为它统一都是用苹果的APNS服务实现的.但安卓这边就比较混乱了,虽然谷歌也推出了类似苹果的官方推送服务,但由于谷歌的服务 ...
- jQuery 重要的知识点归纳
jQuery 对象 jQuery 对象就是通过 jQuery 包装 DOM 对象后产生的对象. jQuery 对象是 jQuery 独有的. 只有 jQuery 对象才能使用 jQuery 的方法,在 ...
- 如何在sharepoint2010中配置Google Anlytics 分析服务
简介 Google Analytics(分析)不仅可以帮助您衡量销售与转化情况,而且能为您提供新鲜的深入信息,帮助您了解访问者如何使用您的网站,他们如何到达您的网站,以及您可以如何吸引他们不断回访 ...
- POI中操作PPT获得每页的TABLE
HSLFSlideShow slideShow = new HSLFSlideShow(bufferInputUtil.getBufferedInputStream()); logger.info(& ...
- sqlite与多线程
数据库支持三种线程模式 Single-thread. In this mode, all mutexes are disabled and SQLite is unsafe to use in mor ...
- sqlite锁的机制
 reserved state 进入reserved state以后,sqlite可以修改数据库中的内容,不过把修改以后的内容写到pager的缓存里,大小由page cache指定. 进入这个状态以 ...
- Android Material design
1.Material Design:扁而不平 2.Android Support Design 库 之 Snackbar使用及源码分析 3.十大Material Design开源项目,直接拿来用!