SGU 531. Bonnie and Clyde 线段树
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;
}