【题目链接】: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;
}

最新文章

  1. cron(CronTrigger)表达式用法
  2. 大毕设-MATLAB-常用知识回顾
  3. mongodb备份恢复
  4. Android应用开发中的风格和主题(style,themes)
  5. su:认证失败
  6. matlab字符串操作
  7. windows的三种内存管理方法
  8. C语言程序设计第六次作业--循环结构(2)
  9. 【Python实践-5】使用迭代查找一个list中最小和最大值
  10. HttpUtil 【判断网络连接的封装类】
  11. USCiLab cereal json 序列化
  12. springboot2.0 JPA配置自定义repository,并作为基类BaseRepository使用
  13. dokuwiki 安装配置
  14. 以Windows服务方式运行ASP.NET Core程序【转载】
  15. C# 将dll打包到程序中
  16. appium简明教程(1)——appium和它的哲学世界
  17. Ubuntu下制作窗口
  18. UVa 11491 Erasing and Winning (贪心,单调队列或暴力)
  19. 润乾在东方通tongweb5.0上部署手册
  20. [转]SQL Server 「逾时过期」的处理方式

热门文章

  1. chrome隐身模式无法播放flash的解决办法
  2. js面向对象编程: js类定义函数时prototype和this差别?
  3. memcached—向memcached中保存Java实体需注意的问题
  4. 深刻理解Nginx之基本配置和升级(2)
  5. 《学习opencv》笔记——基本数据结构,CvMat,矩阵訪问
  6. apiCloud中的数据库操作mcm-js-sdk的使用
  7. 3.linux(ubuntu)常用服务器搭建
  8. 调试相关blogs收集
  9. ubuntu16.04 安装 docker-compose
  10. USACO Sabotage, 2014 Mar 破坏阴谋(二分+贪心)