E - New Year Domino

思路:我用倍增写哒,离线可以不用倍增。

#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PII pair<int, int>
#define y1 skldjfskldjg
#define y2 skldfjsklejg
using namespace std; const int N = 2e5 + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = ; int n, a[N], b[N], c[N], stk[N], R[N], tot;
int nx[N][], cost[N][]; int main() {
scanf("%d", &n);
for(int i = ; i <= n; i++)
scanf("%d%d", &a[i], &b[i]); for(int i = ; i <= n; i++)
c[i] = a[i] + b[i]; for(int i = n; i >= ; i--) {
while(tot && c[stk[tot]] < c[i]) tot--;
if(!tot) R[i] = ;
else R[i] = stk[tot];
stk[++tot] = i;
} for(int i = ; i <= n; i++) {
nx[i][] = R[i];
cost[i][] = max(, a[R[i]] - a[i] - b[i]);
} for(int j = ; j < ; j++) {
for(int i = ; i <= n; i++) {
nx[i][j] = nx[nx[i][j - ]][j - ];
cost[i][j] = cost[i][j - ] + cost[nx[i][j - ]][j - ];
}
} int q; scanf("%d", &q);
while(q--) {
int l, r; scanf("%d%d", &l, &r);
int ans = ;
for(int i = ; i >= ; i--) {
if(!nx[l][i] || nx[l][i] >= r) continue;
ans += cost[l][i];
l = nx[l][i];
}
ans += max(, a[r] - a[l] - b[l]);
printf("%d\n", ans);
}
return ;
} /*
*/

Good Bye 2014 E - New Year Domino 单调栈+倍增的更多相关文章

  1. HDU - 5033 Building (单调栈+倍增)

    题意:有一排建筑,每座建筑有一定的高度,宽度可以忽略,求在某点的平地上能看到天空的最大角度. 网上的做法基本都是离线的...其实这道题是可以在线做的. 对于向右能看到的最大角度,从右往左倍增维护每个时 ...

  2. Loj#2880-「JOISC 2014 Day3」稻草人【CDQ分治,单调栈,二分】

    正题 题目链接:https://loj.ac/problem/2880 题目大意 给出平面上的\(n\)个点,然后求有多少个矩形满足 左下角和右上角各有一个点 矩形之间没有其他点 \(1\leq n\ ...

  3. hdu5033 Building (单调栈+)

    http://acm.hdu.edu.cn/showproblem.php?pid=5033 2014 ACM/ICPC Asia Regional Beijing Online B 1002 Bui ...

  4. 单调栈 二 nyOj 最大矩形和

    主要思想来自 http://blog.csdn.net/wuyanyi/article/details/7243580 题目的连接,头次提交的同学需要注册 http://acm.nyist.net/J ...

  5. HDU 5033 Building(北京网络赛B题) 单调栈 找规律

    做了三天,,,终于a了... 11724203 2014-09-25 09:37:44 Accepted 5033 781MS 7400K 4751 B G++ czy Building Time L ...

  6. HDU 2870 Largest Submatrix (单调栈)

    http://acm.hdu.edu.cn/showproblem.php? pid=2870 Largest Submatrix Time Limit: 2000/1000 MS (Java/Oth ...

  7. Poj 2796 单调栈

    关于单调栈的性质,和单调队列基本相同,只不过单调栈只使用数组的尾部, 类似于栈. Accepted Code: /******************************************* ...

  8. BZOJ1012: [JSOI2008]最大数maxnumber [线段树 | 单调栈+二分]

    1012: [JSOI2008]最大数maxnumber Time Limit: 3 Sec  Memory Limit: 162 MBSubmit: 8748  Solved: 3835[Submi ...

  9. BZOJ 4453: cys就是要拿英魂![后缀数组 ST表 单调栈类似物]

    4453: cys就是要拿英魂! Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 90  Solved: 46[Submit][Status][Discu ...

随机推荐

  1. SSM与SSH的对比

    struts与springMVC的对比: 1.核心控制器(前端控制器.预处理控制器):负责接收页面请求和返回数据给页面. 对于使用过mvc框架的人来说这个词应该不会陌生,核心控制器的主要用途是处理所有 ...

  2. bzoj 2795 [Poi2012]A Horrible Poem hash+数论

    2795: [Poi2012]A Horrible Poem Time Limit: 50 Sec  Memory Limit: 128 MBSubmit: 640  Solved: 322[Subm ...

  3. lightoj 1010 (水题,找规律)

    lightoj 1010 Knights in Chessboard 链接:http://lightoj.com/volume_showproblem.php?problem=1010 题意:国际象棋 ...

  4. Turkey HSD检验法/W法

    sklearn实战-乳腺癌细胞数据挖掘(博主亲自录视频) https://study.163.com/course/introduction.htm?courseId=1005269003&u ...

  5. myeclipse注册码,可以用到2016年

    myeclipse注册码,可以用到2016年 xiangyang kLR8ZF-655954-61677756068297221

  6. Global Vectors forWord Representation

    参考论文: GloVe: Global Vectors forWord Representation 参考博客:https://blog.csdn.net/coderTC/article/detail ...

  7. 重构改善既有代码设计--重构手法10:Move Method (搬移函数)

    你的程序中,有个函数与其所驻类之外的另一个类进行更多的交流:调用后者,或被后者调用.在该函数最常用引用的类中建立一个有着类似行为的新函数.将旧函数编程一个单纯的委托函数,或是将旧函数完全移除. 动机: ...

  8. 小程序Openid 获取,服务器 encryptedData 解密 遇到的坑

    获取客户 openId 和 unionId 需要以下步骤(都为必须步骤) 1.从验证从客户端传上来code, 获取sessionKey (需要配合小程序appid ,secret 发送到微信服务器) ...

  9. Go语言 6 结构体、方法和接口

    文章由作者马志国在博客园的原创,若转载请于明显处标记出处:http://www.cnblogs.com/mazg/ Go学习群:415660935 结构体(struct)是由一系列具有相同类型或不同类 ...

  10. 在wamp下增加多版本的PHP(PHP5.3,PHP5.4,PHP5.5)支持

    1.安装WAMPServer 根据自己的操作系统选择相应的WAMP版本,我这里选择WAMPSERVER-32 BITS & PHP 5.5-2.5, 双击安装,选择安装目录即可,超级简单.根据 ...