#include <bits/stdc++.h>
using namespace std;
int main() {
int n, q;
cin >> n >> q;
vector<long long> x1(n), y1(n), x2(n), y2(n), x, y;
vector<int> c(n);
for(int i=0;i<n;i++) {
int t, a, b;
cin >> t >> a >> b >> c[i];
long long st = t, sx = a;
long long et = t + abs(b - a), ex = b;
x1[i] = st + sx;
y1[i] = st - sx;
x2[i] = et + ex;
y2[i] = et - ex;
x.push_back(x1[i]);
x.push_back(x2[i]);
y.push_back(y1[i]);
y.push_back(y2[i]);
c[i] /= 2;
}
sort(x.begin(),x.end());
sort(y.begin(),y.end());
long long dp[2*n][2*n], xdelta[2*n-1][2*n], ydelta[2*n][2*n-1];
memset(xdelta,0,sizeof(xdelta));
memset(ydelta,0,sizeof(ydelta));
memset(dp,0,sizeof(dp));
for(int i=0;i<n;i++) {
x1[i] = lower_bound(x.begin(),x.end(),x1[i]) - x.begin();
y1[i] = lower_bound(y.begin(),y.end(),y1[i]) - y.begin();
x2[i] = lower_bound(x.begin(),x.end(),x2[i]) - x.begin();
y2[i] = lower_bound(y.begin(),y.end(),y2[i]) - y.begin();
assert(x1[i] == x2[i] || y1[i] == y2[i]);
if(x1[i] == x2[i])
for(int j=y1[i];j<y2[i];j++)
ydelta[x1[i]][j] += c[i] * (y[j+1] - y[j]);
else
for(int j=x1[i];j<x2[i];j++)
xdelta[j][y1[i]] += c[i] * (x[j+1] - x[j]);
}
for(int i=2*n;i--;)
for(int j=2*n;j--;) {
if(i<2*n-1)
dp[i][j] = max(dp[i][j],dp[i+1][j]+xdelta[i][j]);
if(j<2*n-1)
dp[i][j] = max(dp[i][j],dp[i][j+1]+ydelta[i][j]);
}
while(q--) {
int t, p;
cin >> t >> p;
int cx = t + p, cy = t - p;
int gx = lower_bound(x.begin(),x.end(),cx) - x.begin();
int gy = lower_bound(y.begin(),y.end(),cy) - y.begin();
if(gx >= 2 * n || gy >= 2 * n)
cout << "0\n";
else {
long long ans = dp[gx][gy];
if(gx)
for(int i=gy;i<2*n;i++)
ans = max(ans,xdelta[gx-1][i] / (x[gx]-x[gx-1]) * (x[gx] - cx) + dp[gx][i]);
if(gy)
for(int i=gx;i<2*n;i++)
ans = max(ans,ydelta[i][gy-1] / (y[gy]-y[gy-1]) * (y[gy] - cy) + dp[i][gy]);
cout << ans << "\n";
}
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |