1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
| #include <bits/stdc++.h>
#define inf 1e18
#define pii pair<int,int>
using namespace std;
const int MAXN = 110000;
const int MAXQ = 500;
struct Query{
int l,r,t,id;
bool operator < (const Query &a)const{
return t < a.t;
}
}q[MAXN];
int n,m,Q;
int lb[MAXN],rb[MAXN],bl[MAXN];
struct P{
int a,b,id;
bool operator < (const P &x) const{
if(b != x.b) return b < x.b;
if(a != x.a) return a > x.a;
return 0;
}
}p[MAXN];
double calv(P x,int t){
return double(x.a) + double(x.b)*t;
}
bool cmp(P x,P y,int t){// 前面优,返回1,后面优秀,返回 0
if(calv(x,t) != calv(y,t))
return calv(x,t) > calv(y,t);
else
return x.b > y.b;
}
double calj(P x,P y){
if(x.b == y.b) return inf;
return double(x.a-y.a)/(y.b-x.b);
}
void init(){
scanf("%d %d",&n,&m);
for(int i = 1;i<=n;i++){
scanf("%d %d",&p[i].a,&p[i].b);
p[i].id = i;
}
for(int i = 1;i<=m;i++){
scanf("%d %d %d",&q[i].l,&q[i].r,&q[i].t);
q[i].id = i;
}
sort(q+1,q+m+1);
}
vector<P> V[MAXQ];
vector<P> tmp;
void build(){
Q = sqrt(n);
for(int i = 1;i<=n;i++){
bl[i] = (i-1)/Q + 1;
if(bl[i] != bl[i-1]) lb[bl[i]] = i,rb[bl[i-1]] = i-1;
}
rb[bl[n]] = n;
for(int x = 1;x<=bl[n];x++){
int L = lb[x],R = rb[x];
for(int i = L;i<=R;i++) V[x].push_back(p[i]);
sort(V[x].begin(),V[x].end());
tmp.clear();
tmp.push_back(V[x][0]);
for(int i = 1;i < int(V[x].size());i++){
if(V[x][i].b != V[x][i-1].b) tmp.push_back(V[x][i]);
}
V[x].clear();
V[x].push_back(tmp[0]);
for(int i = 0;i < int(tmp.size())-1;){
int t = i;
for(int j = i+1;j<int(tmp.size());j++)
if(calj(tmp[i],tmp[j]) < calj(tmp[i],tmp[t]))
t = j;
V[x].push_back(tmp[i = t]);
}
V[x].push_back(tmp.back());
}
}
int now[MAXQ];
int ans[MAXN];
void solve(){
for(int i = 1;i<=m;i++){
int L = q[i].l,R = q[i].r,t = q[i].t;
int BL = bl[L],BR = bl[R],tmp = 0;
if(BL == BR){
for(int i = L;i<=R;i++) if(cmp(p[i],p[tmp],t)) tmp = i;
ans[q[i].id] = tmp;
continue;
}
for(int i = BL+1;i<=BR-1;i++){
while(now[i]+1 < int(V[i].size()) && calv(V[i][now[i]],t) <= calv(V[i][now[i]+1],t))
now[i]++;
if(cmp(V[i][now[i]],p[tmp],t)) tmp = V[i][now[i]].id;
}
for(int i = L;i<=rb[BL];i++) if(cmp(p[i],p[tmp],t)) tmp = i;
for(int i = lb[BR];i<=R;i++) if(cmp(p[i],p[tmp],t)) tmp = i;
ans[q[i].id] = tmp;
}
for(int i = 1;i<=m;i++){
printf("%d\n",ans[i]);
}
}
int main(){
init();
build();
solve();
return 0;
}
|