531. Bonnie and Clyde

题目连接:

http://acm.sgu.ru/problem.php?contest=0&problem=531

Description

Bonnie and Clyde are into robbing banks. This time their target is a town called Castle Rock. There are n banks located along Castle Rock's main street; each bank is described by two positive integers xi, wi, where xi represents the distance between the i-th bank and the beginning of the street and wi represents how much money the i-th bank has. The street can be represented as a straight line segment, that's why values of xi can be regarded as the banks' coordinates on some imaginary coordinate axis.

This time Bonnie and Clyde decided to split, they decided to rob two different banks at a time. As robberies aren't exactly rare in Castle Rock, Bonnie and Clyde hope that the police won't see the connection between the two robberies. To decrease the chance of their plan being discovered by the investigation, they decided that the distance between the two robbed banks should be no less than d.

Help Bonnie and Clyde find two such banks, the distance between which is no less than d and the sum of money in which is maximum.

Input

The first input line contains a pair of integers n, d (1 ≤ n ≤ 2 · 105, 1 ≤ d ≤ 108), where n is the number of banks and d is the minimum acceptable distance between the robberies. Then n lines contain descriptions of banks, one per line. Each line contains two integers xi, wi (1 ≤ xi,wi ≤ 108), xi shows how far the i-th bank is from the beginning of the street and wi shows the number of money in the bank. Positions of no two banks coincide. The banks are given in the increasing order of xi.

Output

Print two integer numbers — indicies of the required banks. The banks are numbered starting from 1 in the order in which they follow in the input data. You may print indicies in any order. If there are many solutions, print any of them. If no such pair of banks exists, print "-1 -1" (without quotes).

Sample Input

6 3

1 1

3 5

4 8

6 4

10 3

11 2

Sample Output

5 3

Hint

题意

在一条街上的有n个银行,银行在xi位置,有ai元,然后有两个抢劫犯

你需要找两个相距不小于d的银行,使得这两个银行的权值加起来最大

题解:

我是线段树加二分就好了,枚举每一个银行,然后再查询d距离以为的最大银行权值就好了

代码

#include<bits/stdc++.h>
using namespace std; typedef pair<int,int> SgTreeDataType;
struct treenode
{
int L , R ;
SgTreeDataType sum;
}; treenode tree[1001500];
inline void build_tree(int L , int R , int o)
{
tree[o].L = L , tree[o].R = R;
if(L==R)
tree[o].sum = make_pair(0,L);
if (R > L)
{
int mid = (L+R) >> 1;
build_tree(L,mid,o*2);
build_tree(mid+1,R,o*2+1);
if(tree[o*2].sum.first>=tree[o*2+1].sum.first)
tree[o].sum = tree[o*2].sum;
else
tree[o].sum = tree[o*2+1].sum;
}
} inline void updata(int QL,int QR,int v,int o)
{
int L = tree[o].L , R = tree[o].R;
if (QL <= L && R <= QR) tree[o].sum.first = v;
else
{
int mid = (L+R)>>1;
if (QL <= mid) updata(QL,QR,v,o*2);
if (QR > mid) updata(QL,QR,v,o*2+1);
if(tree[o*2].sum.first>=tree[o*2+1].sum.first)
tree[o].sum = tree[o*2].sum;
else
tree[o].sum = tree[o*2+1].sum;
}
}
int ans = 0;
inline SgTreeDataType query(int QL,int QR,int o)
{
int L = tree[o].L , R = tree[o].R;
if (QL <= L && R <= QR) return tree[o].sum;
else
{
int mid = (L+R)>>1;
SgTreeDataType res = make_pair(0,0);
if (QL <= mid)
{
pair<int,int> T = query(QL,QR,2*o);
if(T.first>=res.first)
res = T;
}
if (QR > mid)
{
pair<int,int> T = query(QL,QR,2*o+1);
if(T.first>=res.first)
res = T;
}
return res;
}
} vector<int> V;
int x[200005],v[200005];
int main()
{
int n,d;
scanf("%d%d",&n,&d);
build_tree(1,n,1);
V.push_back(-1);
for(int i=1;i<=n;i++)
{
scanf("%d%d",&x[i],&v[i]);
V.push_back(x[i]);
updata(i,i,v[i],1);
}
if(x[n]-x[1]<d)
return puts("-1 -1");
int ans = 0;
int ans1=0,ans2 =0 ;
for(int i=1;i<=n;i++)
{
int x1 = lower_bound(V.begin(),V.end(),x[i]+d)-V.begin();
if(x1==n+1)break;
pair<int,int> T = query(x1,n,1);
if(T.first + v[i] >= ans)
{
ans = T.first + v[i];
ans1 = T.second,ans2 = i;
}
}
cout<<ans1<<" "<<ans2<<endl;
}

SGU 531. Bonnie and Clyde 线段树的更多相关文章

  1. SGU 531 - Bonnie and Clyde 预处理+二分

    Bonnie and Clyde Description Bonnie and Clyde are into robbing banks. This time their target is a to ...

  2. SGU 311. Ice-cream Tycoon(线段树)

    311. Ice-cream Tycoon Time limit per test: 0.5 second(s)Memory limit: 65536 kilobytes input: standar ...

  3. SGU 319 Kalevich Strikes Back(线段树扫描线)

    题目大意: n个矩形,将一个大矩形分成 n+1 块.矩形之间不重合,可是包括.求这n+1个矩形的面积 思路分析: 用线段树记录他们之间的父子关系.然后dfs 计算面积. 当给出的矩形上边的时候,就要记 ...

  4. SGU 180 Inversions(离散化 + 线段树求逆序对)

    题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=180 解题报告:一个裸的求逆序对的题,离散化+线段树,也可以用离散化+树状数组.因为 ...

  5. SGU 319. Kalevich Strikes Back (线段树)

    319. Kalevich Strikes Back Time limit per test: 0.5 second(s)Memory limit: 65536 kilobytes input: st ...

  6. SGU - 311 Ice-cream Tycoon(线段树)

    Description You've recently started an ice-cream business in a local school. During a day you have m ...

  7. bzoj3932--可持久化线段树

    题目大意: 最近实验室正在为其管理的超级计算机编制一套任务管理系统,而你被安排完成其中的查询部分.超级计算机中的 任务用三元组(Si,Ei,Pi)描述,(Si,Ei,Pi)表示任务从第Si秒开始,在第 ...

  8. codevs 1082 线段树练习 3(区间维护)

    codevs 1082 线段树练习 3  时间限制: 3 s  空间限制: 128000 KB  题目等级 : 大师 Master 题目描述 Description 给你N个数,有两种操作: 1:给区 ...

  9. codevs 1576 最长上升子序列的线段树优化

    题目:codevs 1576 最长严格上升子序列 链接:http://codevs.cn/problem/1576/ 优化的地方是 1到i-1 中最大的 f[j]值,并且A[j]<A[i] .根 ...

随机推荐

  1. LINQ to SQL使用教程

    前些时间用LINQ to SQL做了一些项目,现在打算总结一下,帮助新手快速入门,并写一些别的教程没提到的东西. 一.LINQ to SQL和别的LINQ to XXX有什么关系?二.延迟执行(Def ...

  2. delphi 中字符串与16进制、10进制转换函数

      //字符串转成16进制代码function strToHexStr(str:string):string;varc:char;ss:string;i:integer;beginwhile str& ...

  3. LoadRunner--内存指标介绍

    Threads——线程数当前全部线程数============================================ Available MBytes——物理内存的可用数指计算机上可用于运行 ...

  4. 虚幻4以及DX12将允许开发者利用Xbox One的更多性能(转)

    GamingBolt 最近采访了 Epic Games 的总经理 Ray Davis,讨论了有关旗下虚幻引擎(Unreal Engine)的议题.在这次的访谈中,Ray 解释了关于使用 DirectX ...

  5. [Asp.net MVC]Asp.net MVC5系列——Razor语法

    Razor视图引擎是Asp.net MVC3中新扩展的内容,并且也是它的默认视图引擎.还有另外一种Web Forms视图引擎.通过前面的文章可知在Asp.net mvc5中创建视图,默认使用的是Raz ...

  6. 初识---Qt解析XML文件(QDomDocument)

    关于XML及其使用场景不在此多做介绍,今天主要介绍Qt中对于XML的解析.QtXml模块提供了一个读写XML文件的流,解析方法包含DOM和SAX,两者的区别是什么呢?  DOM(Document Ob ...

  7. HDOJ-ACM1009(JAVA) (传说中的贪心算法)分为数组实现 和 封装类实现

    转载声明:原文转自:http://www.cnblogs.com/xiezie/p/5564311.html 这个道题有几点要注意的: 数组存放的类型:float或double 打印的格式:(如果只是 ...

  8. deep learning 的java库

    deepLearning4j: https://github.com/deeplearning4j/libnd4j/issues/208                       deepLearn ...

  9. CentOS7 network

  10. 如何申请TexturePacker注册码

    TexturePacker是一款很强大的游戏图片制作工具,网上有很多关于它的教程和说明,这里不再说它的作用和好处.这里只是说一下如何申请免费的注册码. 国人都习惯了使用免费或者破解的软件,但是使用破解 ...