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 ...
随机推荐
- lodash常用方法1--查询
1.find var _ = require('lodash'); var user1 = { name: 'zhangsan', height: 180, weight: 120 }; var us ...
- I am back
2012年11月13日,博客园最后一篇日志,三年半,i am back.
- angular学习的一些小笔记(中)之表单验证
表单验证 我去,我感觉我这个人其实还是一个很傻逼的一个人,老是因为拼错了一个单词或者怎么样就浪费我很长时间,这样真的不行不行,要正确对待这个问题,好了,说正题吧,angular也有表单验证minlen ...
- sDashboard:简单的,轻量级的 jQuery 仪表板插件
sDashboard 是一个轻量的仪表板 jQuery 插件,转换一个对象数组到仪表板.数组中的每个对象将被呈现为一个仪表板组件,可以通过左右拖 动重新排列. sDashboards 内置渲染 Da ...
- HTML表单提交数据中application/x-www-form-urlencoded和multipart/form-data的区别
在Form元素的语法中,EncType表明提交数据的格式 用 Enctype 属性指定将数据回发到服务器时浏览器使用的编码类型. 下边是说明: application/x-www-form-urlen ...
- [deviceone开发]-天气demo
一.简介 该demo主要实现定位功能,读取天气信息,语音播报功能.其中定位需要配置key,调试二维码请到论坛中下载! 二.效果图 三.相关讨论 http://bbs.deviceone.net/for ...
- 从客户端(?)中检测到有潜在危险的 Request.Path 值 的解决方案
public ActionResult A(string title) { return Redirect("B"+((String.IsNullOrEmpty(title))?& ...
- [Android]RapidFloatingActionButton框架正式出炉
以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/4474748.html RapidFloatingActionB ...
- JavaScript学习12 JS中定义对象的几种方式
JavaScript学习12 JS中定义对象的几种方式 JavaScript中没有类的概念,只有对象. 在JavaScript中定义对象可以采用以下几种方式: 1.基于已有对象扩充其属性和方法 2.工 ...
- IOS开发 App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app's Info.plist file.
xcode自7后不再使用http,而是使用https请求,但目前很多网络请求还只是以http请求,我们可以这样解决 info.plist->添加@“App Transport Security ...