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的更多相关文章

  1. HDU 4960 Handling the past 2014 多校9 线段树

    首先确定的基本思想是按时间离散化后来建线段树,对于每个操作插入到相应的时间点上 但是难就难在那个pop操作,我之前对pop操作的处理是找到离他最近的那个点删掉,但是这样对于后面的peak操作,如果时间 ...

  2. hdu 4960 Another OCD Patient (最短路 解法

    http://acm.hdu.edu.cn/showproblem.php?pid=4960 2014 Multi-University Training Contest 9 Another OCD ...

  3. HDOJ 2111. Saving HDU 贪心 结构体排序

    Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  4. 【HDU 3037】Saving Beans Lucas定理模板

    http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...

  5. hdu 4859 海岸线 Bestcoder Round 1

    http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...

  6. HDU 4569 Special equations(取模)

    Special equations Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  7. HDU 4006The kth great number(K大数 +小顶堆)

    The kth great number Time Limit:1000MS     Memory Limit:65768KB     64bit IO Format:%I64d & %I64 ...

  8. HDU 1796How many integers can you find(容斥原理)

    How many integers can you find Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d ...

  9. hdu 4481 Time travel(高斯求期望)(转)

    (转)http://blog.csdn.net/u013081425/article/details/39240021 http://acm.hdu.edu.cn/showproblem.php?pi ...

随机推荐

  1. Fiddler (三) Composer创建和发送HTTP Request

      之前已经写过两篇Fiddler的文章了,分别是[Fiddler教程] [Fiddler script用法].  我准备把Fiddler写成一个系列. Fiddler的功能还有很多, 很多功能都没有 ...

  2. java 基础加强--书籍+题目+上机测试

    scjp test( 在线测试网站):http://scjptest.com/mock-test.xhtml <SCJP Sun® Certified Programmer for Java™ ...

  3. DataSet的灵活,实体类的方便,DTO的效率:SOD框架的数据容器,打造最适合DDD的ORM框架

    引言:DDD的困惑 最近,我看到园子里面有位朋友的一篇博客 <领域驱动设计系列(一):为何要领域驱动设计? >文章中有下面一段话,对DDD使用产生的疑问: •没有正确的使用ORM, 导致数 ...

  4. java1.8的默认方法的坑

    默认方法: 接口的方法一直都是抽象方法,自从1.8出来了之后,新增了一个默认方法.可以在接口中实现方法 1.默认方法需要用default修饰 2.默认方法不能是静态的 3.子接口继承了2个相同签名的默 ...

  5. IKONS – 赞!264 款手工打造的免费矢量图标

    IKONS 是一套免费的矢量图标集,包含264款手工精心制作的可伸缩的矢量图标,分享给网页设计人员和开发人员.这些图标提供了 SVG.AI.ESP.PSD.CSH 和 PNG 格式.所有的图标都可以免 ...

  6. SlimerJS – Web开发人员可编写 JS 控制的浏览器

    SlimerJS 是一个提供给 Web 开发人员,可通过脚本编程控制的浏览器.它可以让你使用 Javascript 脚本操纵一个网页:打开一个网页,点击链接,修改的内容等,这对于做功能测试,页面自动机 ...

  7. (转)高性能JavaScript:加载和运行(动态加载JS代码)

    浏览器是如何加载JS的 当浏览器遇到一个<script>标签时,浏览器首先根据标签src属性下载JavaScript代码,然后运行JavaScript代码,继而继续解析和翻译页面.如果需要 ...

  8. 如何申请Autodesk ReCap 360 photo的云币(Cloud Credit)

    在之前的博客中我介绍过Autodesk的照片建模云服务—Autodesk ReCap 360 photo,通过Autodesk ReCap 360 photo,你可以非常方便的通过照片生成三维模型.如 ...

  9. 【转】HttpClient使用Post和Get提交参数

    package httpclient; import java.io.IOException; import java.net.URLEncoder; import org.apache.common ...

  10. Sharepoint学习笔记—习题系列--70-576习题解析 -(Q88-Q91)

    Question 88  You are designing a SharePoint 2010 application that stores data in a list named Base L ...