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. 使用 iosOverlay.js 创建 iOS 风格的提示和通知

    iosOverlay.js 用于在 Web 项目中实现 iOS 风格的通知和提示效果.为了防止图标加载的时候闪烁,你需要预加载的图像资源.不兼容 CSS 动画的浏览器需要 jQuery 支持.浏览器兼 ...

  2. CSS3动画处理浏览器内核时候前缀(兼容性)

    Gecko内核   css前缀为"-moz-" 火狐浏览器 WebKit内核 css前缀为"-webkit-"    Comodo Drangon(科摩多龙), ...

  3. go语言 rune切片

    go语言 rune切片 示例 package main import ( "fmt" ) //http://www.cnblogs.com/osfipin/ func main() ...

  4. Spring(2) ------ 依赖注入

    spring框架为我们提供了三种注入方式,分别是set注入,构造方法注入,接口注入. 1.set注入: 采用属性的set方法进行初始化,就成为set注入. 1)给普通字符类型赋值. public cl ...

  5. sharepoint2010如何本地化WebPart的Category、WebDisplayName 和 WebDescription 属性

    在项目中经常需要实现多语言其中包括webpart的属性也需要.那么如何实现呢? 首先需要资源文件,利用资源文件实现语言的翻译,如下图: 创建好资源后,下面我们来实现webpart属性的多语言.方法代码 ...

  6. What is research (1)

    This abstract tells me a lot of stories about itself. Here I want to discuss two stories about it. I ...

  7. 【转】C++标准库和标准模板库

    C++强大的功能来源于其丰富的类库及库函数资源.C++标准库的内容总共在50个标准头文件中定义.在C++开发中,要尽可能地利用标准库完成.这样做的直接好处包括:(1)成本:已经作为标准提供,何苦再花费 ...

  8. Swift (if while)

    Swift 分支 if if后的括号可以省略 if后只能接bool值 if后的大括号不能省略 let num1 = 3.0 let num2 = 4.0 let bool : Bool = true ...

  9. 我的android学习经历5

    android在strings.xml文件中,写string对象时,如何加入空格 <string name="password">密    码:</string& ...

  10. 设计模式 之 策略(Strategy)模式

    最近看了<head first 设计模式>一书,便总结了里面的一些内容,今天就简单介绍一下策略模式. 策略模式:定义了算法族,分别封装起来,让他们能够相互替换,此模式让算法的变化独立于使用 ...