【题目链接】:http://codeforces.com/problemset/problem/500/E

【题意】



有n个多米诺骨牌;

你知道它们的长度;

然后问你,如果把第i骨牌往后推倒,然后要求第i到第j个骨牌(j>i)都倒掉;

问你需要把i..j这里面骨牌总共增高多少单位的长度(输出最小值);

【题解】



从最后一个骨牌开始往前处理;

对于每一个骨牌,把p[i]..p[i]+l[i]全都覆盖;

然后对于询问x[i],y[i];

即查询p[x[i]]..p[y[i]]这个区间里面有多少个空格;

这两个操作都能用线段树完成;

写线段树的时候要写坐标压缩.



【Number Of WA】



6



【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)
#define Open() freopen("F:\\rush.txt","r",stdin)
#define Close() ios::sync_with_stdio(0),cin.tie(0) typedef pair<int,int> pii;
typedef pair<LL,LL> pll; const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 2e5+100;
const int MAX_SIZE = 4e5+100; int n,q,ma;
int p[N],l[N];
vector <pii> v[N];
map <int,int> dic;
vector <int> t;
int ans[N],lazy_tag[MAX_SIZE<<2],sum[MAX_SIZE<<2]; void push_down(int rt,int l,int r)
{
if (lazy_tag[rt]==0) return;
lazy_tag[rt] = 0;
sum[rt] = t[r+1]-t[l];
if (l!=r)
lazy_tag[rt<<1] = lazy_tag[rt<<1|1] = 1;
} void up_data(int L,int R,int l ,int r,int rt)
{
push_down(rt,l,r);
if (L <= l && r <= R)
{
lazy_tag[rt] = 1;
push_down(rt,l,r);
return;
}
int m = (l+r)>>1;
if (L <= m)
up_data(L,R,lson);
if (m < R)
up_data(L,R,rson);
push_down(rt<<1,l,m);push_down(rt<<1|1,m+1,r);
sum[rt] = sum[rt<<1] + sum[rt<<1|1];
} int query(int L,int R,int l,int r,int rt)
{
//cout <<L<<' '<<R<<' '<<l<<' '<<r<<' '<<endl;
push_down(rt,l,r);
if (L<=l && r <= R)
return sum[rt];
int m = (l+r)>>1;
int temp1 = 0,temp2 = 0;
if (L<=m)
temp1=query(L,R,lson);
if (m<R)
temp2=query(L,R,rson);
//cout <<temp1+temp2<<endl;
return temp1+temp2;
} int main()
{
ms(sum,0);
ms(lazy_tag,0);
// Open();
Close();//scanf,puts,printf not use
//init??????
cin >> n;
t.pb(-1);
rep1(i,1,n)
{
cin >> p[i] >> l[i];
if (!dic[p[i]])
{
dic[p[i]] = 1;
t.pb(p[i]);
}
if (!dic[p[i]+l[i]])
{
dic[p[i]+l[i]] = 1;
t.pb(p[i]+l[i]);
}
}
sort(t.begin(),t.end());
ma = int(t.size())-1;
dic.clear();
rep1(i,1,ma)
dic[t[i]] = i;
cin >> q;
rep1(i,1,q)
{
int x,y;
cin >>x >> y;
v[x].pb(mp(y,i));
} rep2(i,n,1)
{
int xb1 = dic[p[i]],xb2 = dic[p[i]+l[i]];
//cout <<xb1<<' '<<xb2<<endl;
//cout <<xb1<<' '<<xb2<<endl;
up_data(xb1,xb2-1,1,ma-1,1);
//if (i==3)
//{
// cout <<xb1<<' '<<xb2<<endl;
// return 0;
// }
//cout <<dic[p[i]]<<' '<<dic[p[i+1]]<<endl;
//cout <<xb1<<' '<<xb2<<endl;
//cout << query(dic[p[i]],dic[p[i+1]]-1,1,ma,1) << endl;
//return 0;
int len = v[i].size();
rep1(j,0,len-1)
{ int w = v[i][j].fi,id = v[i][j].se; int xb3 = dic[p[w]];
//cout <<xb1<<' '<<xb3-1<<endl;
ans[id] = p[w]-p[i]-query(xb1,xb3-1,1,ma-1,1);
//cout <<p[w]-p[i]<<endl;
//cout <<query(xb1,xb3-1,1,ma-1,1)<<endl;
}
}
rep1(i,1,q)
cout << ans[i] << endl;
return 0;
}

【codeforces 500E】New Year Domino的更多相关文章

  1. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  2. 【codeforces 707E】Garlands

    [题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...

  3. 【codeforces 707C】Pythagorean Triples

    [题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...

  4. 【codeforces 709D】Recover the String

    [题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...

  5. 【codeforces 709B】Checkpoints

    [题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...

  6. 【codeforces 709C】Letters Cyclic Shift

    [题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...

  7. 【Codeforces 429D】 Tricky Function

    [题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...

  8. 【Codeforces 670C】 Cinema

    [题目链接] http://codeforces.com/contest/670/problem/C [算法] 离散化 [代码] #include<bits/stdc++.h> using ...

  9. 【codeforces 515D】Drazil and Tiles

    [题目链接]:http://codeforces.com/contest/515/problem/D [题意] 给你一个n*m的格子; 然后让你用1*2的长方形去填格子的空缺; 如果有填满的方案且方案 ...

随机推荐

  1. Github+Jekyll 搭建个人网站详细教程

    GitHub搭建个人网站,大家在网上一搜能搜到一大把的教程,但是大部分都讲的差不多,并不能满足自己想搭建的网站详细需求.我之前在搭建本站的时候也是查了较多资料,学习了下jekyll语法,参考了几个主题 ...

  2. NOIP2012 同余方程 题解

    描写叙述 求关于x的同余方程ax ≡ 1 (mod b)的最小正整数解. 格式 输入格式 输入仅仅有一行,包括两个正整数a, b,用一个空格隔开. 输出格式 输出仅仅有一行,包括一个正整数x0.即最小 ...

  3. UI组件之AdapterView及其子类(三)Spinner控件具体解释

    Spinner提供了从一个数据集合中高速选择一项值的办法. 默认情况下Spinner显示的是当前选择的值.点击Spinner会弹出一个包括全部可选值的dropdown菜单或者一个dialog对话框,从 ...

  4. 分布式公布订阅消息系统 Kafka 架构设计

    我们为什么要搭建该系统 Kafka是一个消息系统,原本开发自LinkedIn,用作LinkedIn的活动流(activity stream)和运营数据处理管道(pipeline)的基础. 如今它已为多 ...

  5. Unity特殊目录和脚本编译顺序

     特殊目录和脚本编译顺序 大多数情况下,您能够选择不论什么你喜欢的目录在您的项目的名称.但unity储备一些名称以指示内容有一个特殊的用途.这些目录中有些会影响脚本编译的顺序.从根本上说,有四个单 ...

  6. 2015.04.20,外语,读书笔记-《Word Power Made Easy》 11 “如何辱骂敌人” SESSION 30

    1.brothers and sisters, wives and husbands Frater: brothers; soror: sister; uxor: wife; maritus: hus ...

  7. Blur 算法 (Unity 5.0 Shader)

    一:简单 Blur 算法 一个像素的颜色值由其邻近的若干像素和自己的颜色值的平均值重新定义,以此达到模糊的效果. 如下图,红色的像素点的值将由它和它周围的24个像素的平均值重新定义.计算的范围一般由一 ...

  8. 【BZOJ 1878】 HH的项链

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1878 [算法] 显然,在线算法是不可做的,考虑离线算法 笔者的做法是莫队算法,时间复 ...

  9. Noip前紧急抢救

    管他会不会,知道结论就好了 紧急抢救知识 斯特林数 第一类斯特林数 递推公式 \[ S[n][k]=(n-1)\times S[n-1][k]+S[n-1][k-1] \] 处理的问题是将n个数划分为 ...

  10. Python—使用xm.dom解析xml文件

    什么是DOM? 文件对象模型(Document Object Model,简称DOM),是W3C组织推荐的处理可扩展置标语言的标准编程接口. 一个 DOM 的解析器在解析一个 XML 文档时,一次性读 ...