Codeforces 500 E. New Year Domino
[$>Codeforces \space 500 E. New Year Domino
题目大意 : 数轴上有序排列着 \(n\) 块多米诺骨牌, 第 \(i\) 块骨牌坐标为 \(p_i\) 长度为 \(l_i\)
如果推倒坐标为 \(p_i\) 的多米诺骨牌,那么区间 \([p_i, p_i + x_i]\) 中的多米诺骨牌都会被推倒,从而发生连锁反应.
现在有 \(q\) 组询问,每次询问之前可以让一些多米诺骨牌增加任意长度,代价是增加的长度之和。
询问的内容是每次推倒第 \(x\) 块骨牌,求要推倒第 \(y\) 块至少要花费的代价 (询问之间相互独立)

\(2 \leq n , q\leq 2 \times 10^5 \ 1 \leq p_i, l_i \leq 10^9\)
解题思路 :
观察发现,多米诺之间的连锁反应可以合并成一个块,每次从右到左每次新加骨牌的时候可以合并被它覆盖的块。
但是直接按照定义合并的话,只能保证一个块最左边的块可以推倒整个块,也就是说一个块的贡献只有在推倒整块最左边的骨牌时是合法的
仔细想了很久觉得并没有什么做法可以在线求出 \(x\) 对应的合法联通块,不妨大胆离线.
离线后按照询问的左端点排序,从右到左扫,边扫边用一个栈维护此时的联通块的状态和联通块之间的距离
对于每一个扫到的 \(i\),在其合并完之后处理 \(i\) 作为左端点的询问的答案
因为此时 \(i\) 左边的点还没有被加进联通块,所以从 \(i\) 开始推一定是合法的, 对于每一个左端点在 \(i\) 上的询问,算出两个联通块之间的距离就是答案
/*program by mangoyang*/
#include<bits/stdc++.h>
#define inf (0x7f7f7f7f)
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
typedef long long ll;
using namespace std;
template <class T>
inline void read(T &x){
int f = 0, ch = 0; x = 0;
for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = 1;
for(; isdigit(ch); ch = getchar()) x = x * 10 + ch - 48;
if(f) x = -x;
}
#define int ll
#define N (1000005)
int fa[N], l[N], r[N], st[N], sum[N], Ans[N], n, m, top;
struct Query{ int x, id; }; vector<Query> q[N];
inline int ask(int x){ return x == fa[x] ? x : fa[x] = ask(fa[x]); }
main(){
read(n);
for(int i = 1, x, y; i <= n; i++)
read(x), read(y), l[i] = x, r[i] = x + y;
for(int i = 1; i <= n; i++) fa[i] = i;
read(m);
for(int i = 1, L, R; i <= m; i++)
read(L), read(R), q[L].push_back((Query){R, i});
for(int i = n; i >= 1; i--){
int x = i;
while(top && l[st[top]] <= r[x]){
r[x] = Max(r[x], r[st[top]]);
fa[ask(st[top])] = x, top--;
}
if(top) sum[x] = sum[st[top]] + l[st[top]] - r[x];
st[++top] = x;
for(int j = 0; j < q[i].size(); j++){
int id = q[i][j].id, y = q[i][j].x;
Ans[id] = sum[ask(x)] - sum[ask(y)];
}
}
for(int i = 1; i <= m; i++) printf("%lld\n", Ans[i]);
return 0;
}
Codeforces 500 E. New Year Domino的更多相关文章
- CodeForces 500 A. New Year Transportation
Description New Year is coming in Line World! In this world, there are n cells numbered by integers ...
- Codeforces 611C. New Year and Domino 动态规划
C. New Year and Domino time limit per test 3 seconds memory limit per test 256 megabytes input stand ...
- 【codeforces 500E】New Year Domino
[题目链接]:http://codeforces.com/problemset/problem/500/E [题意] 有n个多米诺骨牌; 你知道它们的长度; 然后问你,如果把第i骨牌往后推倒,然后要求 ...
- Codeforces 611C New Year and Domino DP+容斥
"#"代表不能放骨牌的地方,"."是可以放 500*500的矩阵,q次询问 开两个dp数组,a,b,a统计横着放的方案数,b表示竖着放,然后询问时O(1)的,容 ...
- codeforces水题100道 第三题 Codeforces Beta Round #47 A. Domino piling (math)
题目链接:http://www.codeforces.com/problemset/problem/50/A题意:一个NxM的举行中最多能放多少个1x2的矩形.C++代码: #include < ...
- CodeForces 611C New Year and Domino (动态规划,DP)
题意:给定一个h*w的网格,里面只有.和#,.表示空的,#表示禁止的,然后有q个询问,询问中给你两个坐标,分别是左上和右下,求在这两者中间的有多少种(竖着和横着)两个相邻的点. 析:一看到这个题目,肯 ...
- Codeforces 611C New Year and Domino(二维前缀和)
题目大概说给一个n*m个格子,格子'.'表示可以放东西,多次询问矩形区域(x1,y1)-(x2,y2)有几种放一张1*2的骨牌的方案数. 分别考虑横着竖着放,预处理出二维的前缀和,即sum[x][y] ...
- 洛谷 P5089: CodeForces #500 (Div. 1) B / 1012B : Chemical table
题目传送门:洛谷P5089. 题意简述: 一张 \(n \times m\) 的表格,有一些格子有标记,另外一些格子没有标记. 如果 \((r_1,c_1),(r_1,c_2),(r_2,c_1)\) ...
- LibreOJ 题解汇总
目录 #1. A + B Problem #2. Hello, World! #3. Copycat #4. Quine #7. Input Test #100. 矩阵乘法 #101. 最大流 #10 ...
随机推荐
- Anniversary party(树上dp+HDU1520)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1520 题目: 题意:一个学校要办校庆,校长决定邀请员工参加,但是下属和他的直系同时参加的话,下属将会无 ...
- bzoj 1014 splay
首先我们可以用splay来维护这个字符串,那么对于某两个位置的lcp,维护每个节点的子树的hash,然后二分判断就好了. /************************************** ...
- make command explaination 編譯命令解釋
Creating .config file make ARCH=arm CROSS_COMPILE=arm-none-eabi- stm32_defconfig 以上命令是 將變數 ARCH=arm, ...
- python的时间和日期--time、datetime应用
time >>> import time >>> time.localtime() #以time.struct_time类型,打印本地时间 time.struct_ ...
- 【bzoj4896】补退选
傻逼题. 每个点维护下vector,然后随便做. #include<bits/stdc++.h> ; using namespace std; typedef long long ll; ...
- AspxGridView在cell内显示颜色
protected void master_HtmlDataCellPrepared(object sender, ASPxGridViewTableDataCellEventArgs e) { if ...
- sense之间的数据传输
下面介绍一种原界面向目标界面传值 和 两种 由目标界面返回值给原界面的方法 界面解释: a界面和b界面都在导航控制器中, a界面触发时间跳转至b界面,b界面上设定完毕数据后返回a界面 a界面:原界面 ...
- PHP的输出方式
php中,用echo输出一个字符串有三种方式,分别是单引号,双引号和<<<方式.其中,单引号中的变量不会被解析,而会直接输出,而双引号和<<<时,变量会被解析.&l ...
- 堆--LogN的数据结构
我们这里的堆是指用来表示元素集合的一种数据结构 一个二叉树是一个堆是由堆的两个性质决定的(以小根堆为例) 1:任何节点的值都小于或等于其子节点的值 2:该二叉树最多在两层上具有叶节点,其中最底层的叶节 ...
- Python图像处理库(2)
1.4 SciPy SciPy(http://scipy.org/) 是建立在 NumPy 基础上,用于数值运算的开源工具包.SciPy 提供很多高效的操作,可以实现数值积分.优化.统计.信号处理,以 ...