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. 线程同步API及它们的属性

    头文件:<pthread.h> 编译记得加 -lpthread库 1:互斥锁(mutex) 1.1:互斥锁API 数据类型:pthread_mutex_t // 初始化一个互斥锁 int ...

  2. python学习(十三)进程和线程

    python多进程 from multiprocessing import Process import os def processFunc(name): print("child pro ...

  3. python中如何优雅使用import

    http://note.youdao.com/noteshare?id=c55be6a8565f5eb586aa52244b3af010

  4. Creating a Cron Job in K8S

    Creating a Cron Job Cron jobs require a config file. This example cron job config .spec file prints ...

  5. 线上Redis偶发性链接失败排查记

    问题过程 输入法业务于12月12日上线了词库接受业务,对部分用户根据用户uuid判断进行回传,在12月17日早上8点多开始出现大量的php报错(Redis went away),报错导致了大量的链接积 ...

  6. 不管谁坐了CIO的位置 都必须了解的法则

    目前一些设立了CIO岗位的央企中,CIO也只做到了“IO”(信息官,Information Officer),而没有做到“C”(首席,Chief).老总们总在抱怨没有合适的人选:懂技术的不懂业务,懂业 ...

  7. 在GitHub搭建个人博客 地址: https://douzujun.github.io/

    搭建博客地址:https://douzujun.github.io/ 博客模板:https://github.com/douzujun/douzujun.github.io 显示效果:

  8. linux 在命令行中通过conda使用anaconda

    在 ~/.bash_profile中添加 export PATH="/home/taoke/anaconda/bin:$PATH"

  9. c语言学习笔记.内存管理.

    内存: 每个程序的内存是分区的:堆区.栈区.静态区.代码区. 1.代码区:放置所有的可执行代码,包括main函数. 2.静态区:存放所有的全局变量和静态变量. 3.栈区:栈(stack),先进后出.存 ...

  10. docker-It's possible that too few managers are online. Make sure more than half of the managers are online.

    问题:docker ---- It's possible that too few managers are online. Make sure more than half of the manag ...