Submission #458283

# Submission time Handle Problem Language Result Execution time Memory
458283 2021-08-08T03:47:20 Z 8e7 Bodyguard (JOI21_bodyguard) C++17
100 / 100
4729 ms 662196 KB
//Challenge: Accepted
#include <iostream>
#include <algorithm>
#include <utility>
#include <vector>
#include <stack>
#include <cmath>
#include <queue>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
//using namespace __gnu_pbds;
void debug(){cout << endl;}
template<class T, class ... U> void debug(T a, U ... b){cout << a << " ", debug(b ...);};
template<class T> void pary(T l, T r) {
	while (l != r) cout << *l << " ", l++;
	cout << endl;
}
#define ll long long
#define maxn 6005
#define maxq 3000005
#define pii pair<ll, ll>
#define ff first
#define ss second
#define io ios_base::sync_with_stdio(0);cin.tie(0);
//#define __int128 long long
struct seg{
	pii st, ed;
	ll cost;
	seg(){st = {0, 0}, ed = {0, 0}, cost = 0;}
	seg(pii x, pii y, ll c){st = x, ed = y, cost = c;};
} arr[maxn];
vector<ll> xv, yv;
vector<vector<pii> > cost;
vector<vector<ll> > dp;

struct query{
	ll x, y, ans;
	int xi, yi;
	query() {x = y = ans = 0, xi = yi = 0;};
	query(ll px, ll py) {
		x = px, y = py, ans = 0;
		xi = lower_bound(xv.begin(), xv.end(), x) - xv.begin();
		yi = lower_bound(yv.begin(), yv.end(), y) - yv.begin();
	}
} que[maxq];

vector<int> ix[maxn], iy[maxn];
struct convexhull{
	struct line{
		ll a, b;
		line() {a = 0, b = 0;};
		line(ll c, ll d) {a = c, b = d;};
		ll calc(ll x) {return a * x + b;}
	};
	vector<line> v; //slope is decreasing
	void ins(ll la, ll lb) {
		line cur = line(la, lb);
		while (v.size() && v.back().a <= cur.a) v.pop_back();
		while (v.size() >= 2) {
			line p2 = v.back(), p3 = *(v.rbegin() + 1);
			if ((__int128)(p2.b - p3.b) * (p3.a - cur.a) <= (__int128)(cur.b - p3.b) * (p3.a - p2.a)) {
				v.pop_back();
			} else {
				break;
			}
		}
		if (v.empty() || (cur.b > v.back().b)) v.push_back(cur);
	}
	ll getval(ll x) {
		int low = -1, up = v.size();
		while (low < up - 1) {
			int mid = (low + up) / 2;
			ll v1 = v[mid].calc(x), v2 = (mid + 1 < v.size() ? v[mid + 1].calc(x) : -(1LL<<60)); 
			if (v2 - v1 < 0) up = mid;
			else low = mid;
		}
		return max(low >= 0 ? v[low].calc(x) : 0, up < v.size() ? v[up].calc(x) : 0);
	}
};
int main() {
	io
	int n, q;
	cin >> n >> q;
	auto turn = [&](pii p) {return make_pair((p.ff - p.ss), (p.ff + p.ss));};
	for (int i = 0;i < n;i++) {
		ll t, a, b, c;
		cin >> t >> a >> b >> c;
		pii p1 = {t, a}, p2 = {t + abs(b - a), b};
		p1 = turn(p1), p2 = turn(p2);
		arr[i] = seg(p1, p2, c / 2);
		xv.push_back(p1.ff), xv.push_back(p2.ff);
		yv.push_back(p1.ss), yv.push_back(p2.ss);
	}
	sort(xv.begin(), xv.end()), sort(yv.begin(), yv.end());
	xv.resize(int(unique(xv.begin(), xv.end()) - xv.begin()));
	yv.resize(int(unique(yv.begin(), yv.end()) - yv.begin()));	
	//pary(xv.begin(), xv.end());
	//pary(yv.begin(), yv.end());
	int xs = xv.size(), ys = yv.size();
	for (int i = 0;i < xs;i++) {
		vector<pii> ac(ys, {0, 0});
		vector<ll> dc(ys, 0);
		cost.push_back(ac), dp.push_back(dc);
	}
	for (int i = 0;i < n;i++) {
		int sx = lower_bound(xv.begin(), xv.end(), arr[i].st.ff) - xv.begin();
		int tx = lower_bound(xv.begin(), xv.end(), arr[i].ed.ff) - xv.begin();
		int sy = lower_bound(yv.begin(), yv.end(), arr[i].st.ss) - yv.begin();
		int ty = lower_bound(yv.begin(), yv.end(), arr[i].ed.ss) - yv.begin();
		if (sx == tx) {
			for (int j = sy;j < ty;j++) cost[sx][j].ss = max(cost[sx][j].ss, arr[i].cost);	
		} else {
			for (int j = sx;j < tx;j++) cost[j][sy].ff = max(cost[j][sy].ff, arr[i].cost);	
		}
	}
	for (int i = xs - 1;i >= 0;i--) {
		for (int j = ys - 1;j >= 0;j--) {
			dp[i][j] = max((i < xs - 1 ? dp[i + 1][j] + (xv[i + 1] - xv[i]) * cost[i][j].ff : 0), 
					(j < ys - 1 ? dp[i][j + 1] + (yv[j + 1] - yv[j]) * cost[i][j].ss : 0));
		}
	}
		
	for (int i = 0;i < q;i++) {
		pii p;
		cin >> p.ff >> p.ss;
		p = turn(p);
		que[i] = query(p.ff, p.ss);
		ix[que[i].xi].push_back(i);	
		iy[que[i].yi].push_back(i);
		/*
		if (x < xs) {
			for (int i = y;i < ys;i++) {
				ans = max(ans, dp[x][i] + (x ? (xv[x] - p.ff) * cost[x-1][i].ff : 0));
			}
		}
		if (y < ys) {
			for (int i = x;i < xs;i++) {
				ans = max(ans, dp[i][y] + (y ? (yv[y] - p.ss) * cost[i][y-1].ss : 0));
			}
		}	
		cout << ans << "\n";
		*/
	}
	for (int i = 0;i < xs;i++) sort(ix[i].begin(), ix[i].end(), [&](int x, int y) {return que[x].yi > que[y].yi;});
	for (int i = 0;i < ys;i++) sort(iy[i].begin(), iy[i].end(), [&](int x, int y) {return que[x].xi > que[y].xi;});
	for (int i = 0;i < xs;i++) {
		convexhull hull;
		int ind = 0;
		for (int j = ys - 1;j >= 0;j--) {
			hull.ins((i ? cost[i-1][j].ff : 0), dp[i][j]);
			while (ind < ix[i].size() && que[ix[i][ind]].yi >= j) {
				int cur = ix[i][ind];
				ind++;
				if (que[cur].yi > j) continue;
				//debug(i, j, cur, hull.getval(xv[i] - que[cur].x));
				que[cur].ans = max(que[cur].ans, hull.getval(xv[i] - que[cur].x));
			}
		}
	}	
	for (int j = 0;j < ys;j++) {
		convexhull hull;
		int ind = 0;
		for (int i = xs - 1;i >= 0;i--) {
			hull.ins((j ? cost[i][j-1].ss : 0), dp[i][j]);
			while (ind < iy[j].size() && que[iy[j][ind]].xi >= i) {
				int cur = iy[j][ind];
				ind++;
				if (que[cur].xi > i) continue;
				que[cur].ans = max(que[cur].ans, hull.getval(yv[j] - que[cur].y));
			}
		}
	}
	for (int i = 0;i < q;i++) cout << que[i].ans << "\n";
}

/*
 
2 2
1 2 1 4
3 1 3 2
1 2
3 3
 
 
 
 
*/

Compilation message

bodyguard.cpp: In member function 'long long int convexhull::getval(long long int)':
bodyguard.cpp:74:42: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<convexhull::line>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   74 |    ll v1 = v[mid].calc(x), v2 = (mid + 1 < v.size() ? v[mid + 1].calc(x) : -(1LL<<60));
      |                                  ~~~~~~~~^~~~~~~~~~
bodyguard.cpp:78:48: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<convexhull::line>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   78 |   return max(low >= 0 ? v[low].calc(x) : 0, up < v.size() ? v[up].calc(x) : 0);
      |                                             ~~~^~~~~~~~~~
bodyguard.cpp: In function 'int main()':
bodyguard.cpp:152:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  152 |    while (ind < ix[i].size() && que[ix[i][ind]].yi >= j) {
      |           ~~~~^~~~~~~~~~~~~~
bodyguard.cpp:166:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  166 |    while (ind < iy[j].size() && que[iy[j][ind]].xi >= i) {
      |           ~~~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3612 ms 373572 KB Output is correct
2 Correct 3594 ms 375444 KB Output is correct
3 Correct 2415 ms 212912 KB Output is correct
4 Correct 1973 ms 147644 KB Output is correct
5 Correct 2498 ms 601148 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1222 ms 509708 KB Output is correct
2 Correct 1185 ms 509680 KB Output is correct
3 Correct 1163 ms 504008 KB Output is correct
4 Correct 54 ms 95172 KB Output is correct
5 Correct 1152 ms 509624 KB Output is correct
6 Correct 1069 ms 509676 KB Output is correct
7 Correct 1107 ms 509124 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1222 ms 509708 KB Output is correct
2 Correct 1185 ms 509680 KB Output is correct
3 Correct 1163 ms 504008 KB Output is correct
4 Correct 54 ms 95172 KB Output is correct
5 Correct 1152 ms 509624 KB Output is correct
6 Correct 1069 ms 509676 KB Output is correct
7 Correct 1107 ms 509124 KB Output is correct
8 Correct 1227 ms 509720 KB Output is correct
9 Correct 1219 ms 509660 KB Output is correct
10 Correct 1175 ms 503224 KB Output is correct
11 Correct 52 ms 95380 KB Output is correct
12 Correct 1141 ms 509832 KB Output is correct
13 Correct 1074 ms 509856 KB Output is correct
14 Correct 1159 ms 509816 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1222 ms 509708 KB Output is correct
2 Correct 1185 ms 509680 KB Output is correct
3 Correct 1163 ms 504008 KB Output is correct
4 Correct 54 ms 95172 KB Output is correct
5 Correct 1152 ms 509624 KB Output is correct
6 Correct 1069 ms 509676 KB Output is correct
7 Correct 1107 ms 509124 KB Output is correct
8 Correct 1227 ms 509720 KB Output is correct
9 Correct 1219 ms 509660 KB Output is correct
10 Correct 1175 ms 503224 KB Output is correct
11 Correct 52 ms 95380 KB Output is correct
12 Correct 1141 ms 509832 KB Output is correct
13 Correct 1074 ms 509856 KB Output is correct
14 Correct 1159 ms 509816 KB Output is correct
15 Correct 1365 ms 511632 KB Output is correct
16 Correct 1342 ms 511672 KB Output is correct
17 Correct 1267 ms 506204 KB Output is correct
18 Correct 83 ms 96788 KB Output is correct
19 Correct 1165 ms 511016 KB Output is correct
20 Correct 1117 ms 511140 KB Output is correct
21 Correct 1148 ms 511164 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3612 ms 373572 KB Output is correct
2 Correct 3594 ms 375444 KB Output is correct
3 Correct 2415 ms 212912 KB Output is correct
4 Correct 1973 ms 147644 KB Output is correct
5 Correct 2498 ms 601148 KB Output is correct
6 Correct 1222 ms 509708 KB Output is correct
7 Correct 1185 ms 509680 KB Output is correct
8 Correct 1163 ms 504008 KB Output is correct
9 Correct 54 ms 95172 KB Output is correct
10 Correct 1152 ms 509624 KB Output is correct
11 Correct 1069 ms 509676 KB Output is correct
12 Correct 1107 ms 509124 KB Output is correct
13 Correct 1227 ms 509720 KB Output is correct
14 Correct 1219 ms 509660 KB Output is correct
15 Correct 1175 ms 503224 KB Output is correct
16 Correct 52 ms 95380 KB Output is correct
17 Correct 1141 ms 509832 KB Output is correct
18 Correct 1074 ms 509856 KB Output is correct
19 Correct 1159 ms 509816 KB Output is correct
20 Correct 1365 ms 511632 KB Output is correct
21 Correct 1342 ms 511672 KB Output is correct
22 Correct 1267 ms 506204 KB Output is correct
23 Correct 83 ms 96788 KB Output is correct
24 Correct 1165 ms 511016 KB Output is correct
25 Correct 1117 ms 511140 KB Output is correct
26 Correct 1148 ms 511164 KB Output is correct
27 Correct 4729 ms 662196 KB Output is correct
28 Correct 4601 ms 662188 KB Output is correct
29 Correct 3777 ms 635816 KB Output is correct
30 Correct 1649 ms 201996 KB Output is correct
31 Correct 2966 ms 580784 KB Output is correct
32 Correct 4030 ms 637364 KB Output is correct
33 Correct 2646 ms 613276 KB Output is correct