Submission #1071635

# Submission time Handle Problem Language Result Execution time Memory
1071635 2024-08-23T09:29:49 Z 김은성(#11136) Cultivation (JOI17_cultivation) C++17
80 / 100
2000 ms 1236 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1.557e18;
vector<pair<ll, ll> > points;
ll r, c;
vector<ll> crit;
ll loc[609], hic[609], gapc[609];
ll lotree[1<<11], hitree[1<<11], gaptree[1<<11];
void settree(int v, int l, int r){
	if(l==r){
		lotree[v] = loc[l];
		hitree[v] = hic[l];
		gaptree[v] = gapc[l];
	}
	else{
		int mid = (l+r)/2;
		settree(2*v, l, mid);
		settree(2*v+1, mid+1, r);
		lotree[v] = max(lotree[2*v], lotree[2*v+1]);
		hitree[v] = max(hitree[2*v], hitree[2*v+1]);
		gaptree[v] = max(gaptree[2*v], gaptree[2*v+1]);
	}
}
tuple<ll, ll, ll> maxquery(int v, int l, int r, int s, int e){
	if(e < l || r <s)
		return make_tuple(-INF, -INF, -INF);
	if(s<=l && r<=e)
		return make_tuple(lotree[v], hitree[v], gaptree[v]);
	int mid = (l+r)/2;
	tuple<ll, ll, ll> lt = maxquery(2*v, l, mid, s, e), rt = maxquery(2*v+1, mid+1, r, s, e);
	return make_tuple(max(get<0>(lt), get<0>(rt)), max(get<1>(lt), get<1>(rt)), max(get<2>(lt), get<2>(rt)));
}
ll minc2(ll rd, ll ru){
	vector<ll> crit;
	for(auto [y, x]: points){
		crit.push_back(max(y - rd, 1ll));
		crit.push_back(min(y + ru, r) + 1);
	}
	crit.push_back(1);
	crit.push_back(r+1);
	sort(crit.begin(), crit.end());
	crit.erase(unique(crit.begin(), crit.end()), crit.end());
	crit.pop_back();
	int sz = crit.size(), i, j;
	vector<vector<ll> > row(sz);
	for(auto [y, x]: points){
		int lo = lower_bound(crit.begin(), crit.end(), max(y - rd, 1ll)) - crit.begin();
		int hi = lower_bound(crit.begin(), crit.end(), min(y + ru, r) + 1) - crit.begin() - 1;
		for(i=lo; i<=hi; i++)
			row[i].push_back(x);
	}
	ll cd = 0, cu = 0, sum = 0;
	for(i=0; i<sz; i++){
		sort(row[i].begin(), row[i].end());
		if(row[i].empty()){
			sum = INF;
			break;
		}
		cd = max(cd, row[i].front() - 1);
		cu = max(cu, c - row[i].back());
		for(j=0; j+1<row[i].size(); j++){
			sum = max(sum, row[i][j+1] - row[i][j] - 1);
		}
	}
	return max(cd + cu, sum);
}
void minc(ll rd, ll ru){
	crit.clear();
	for(auto [y, x]: points){
		crit.push_back(y - rd);
		crit.push_back(y + ru + 1);
	}
	crit.push_back(-INF);
	crit.push_back(INF);
	sort(crit.begin(), crit.end());
	crit.erase(unique(crit.begin(), crit.end()), crit.end());
	int sz = crit.size(), i, j;
	vector<vector<ll> > add(sz), del(sz);
	for(auto [y, x]: points){
		int lo = lower_bound(crit.begin(), crit.end(), y - rd) - crit.begin();
		int hi = lower_bound(crit.begin(), crit.end(), y + ru + 1) - crit.begin();
		//printf("y=%lld lo=%d hi=%d\n", y, lo, hi);
		add[lo].push_back(x);
		del[hi].push_back(x);
	}
	ll cd = 0, cu = 0, sum = 0;
	multiset<ll> s, gap;
	for(i=0; i<sz; i++){
		for(ll x: add[i]){
			auto it = s.insert(x);
			//printf("x=%lld s.size=%d\n", x, s.size());
			if(it != s.begin()){
				auto itl = --it;
				it++;
				gap.insert((*it) - (*itl));
				auto itr = ++it;
				it--;
				if(itr != s.end()){
					gap.insert((*itr) - (*it));
					gap.erase(gap.find((*itr) - (*itl)));
				}
			}
			else{
				auto itr = ++it;
				it--;
				if(itr != s.end()){
					gap.insert((*itr) - (*it));
				}
			}
		}
		for(ll x: del[i]){
			auto itr = s.erase(s.find(x));
			if(itr != s.end()){
				gap.erase(gap.find((*itr) - x));
				if(itr != s.begin()){
					auto itl = --itr;
					itr++;
					gap.erase(gap.find(x - (*itl)));
					gap.insert((*itr) - (*itl));
				}
			}
			else if(itr != s.begin()){
				auto itl = --itr;
				gap.erase(gap.find(x - (*itl)));
			}
		}
		if(s.empty()){
			loc[i] = INF;
			hic[i] = INF;
		}
		else{
			loc[i] = *(s.begin()) - 1;
			hic[i] = c - *(--s.end());
		}
		if(gap.empty())
			gapc[i] = -1;
		else
			gapc[i] = *(--gap.end()) -1;
		//printf("i=%d lo=%lld hi=%lld gap=%lld\n", i, loc[i], hic[i], gapc[i]);
	}
	settree(1, 0, sz-1);
}
int main(){
	ll y, x, rd, ru;
	int n, i, j;
	scanf("%lld %lld %d", &r, &c, &n);
	for(i=1; i<=n; i++){
		scanf("%lld %lld", &y, &x);
		points.push_back(make_pair(y, x));
	}
	vector<ll> critd, critu, critdu;
	for(auto [y, x]: points){
		critd.push_back(y - 1);
		critu.push_back(r - y);
	}
	for(auto [y1, x1]: points){
		for(auto [y2, x2]: points){
			if(y2 > y1)
				critdu.push_back(y2 - y1 - 1);
		}
	}
	sort(critd.begin(), critd.end());
	critd.erase(unique(critd.begin(), critd.end()), critd.end());
	sort(critu.begin(), critu.end());
	critu.erase(unique(critu.begin(), critu.end()), critu.end());
	sort(critdu.begin(), critdu.end());
	critdu.erase(unique(critdu.begin(), critdu.end()), critdu.end());
	ll ans = INF;
	for(auto rd: critd){
		for(auto ru: critu){
			ans = min(ans, rd+ru+minc2(rd,ru));
		}
	}
	for(auto temp: critdu){
		minc(0, temp);
		for(auto rd: critd){
			if(temp - rd < 0)
				continue;
			int lo = upper_bound(crit.begin(), crit.end(), rd+1) - crit.begin() - 1;
			int hi = upper_bound(crit.begin(), crit.end(), rd+r) - crit.begin() - 1;
			tuple<ll, ll, ll> ret = maxquery(1, 0, (int)crit.size() -1, lo, hi);
			//printf("rd=%lld ru=%lld ret=%lld\n", rd, temp-rd, temp+max(get<0>(ret)+get<1>(ret), get<2>(ret)));
			ans = min(ans, temp + max(get<0>(ret)+get<1>(ret), get<2>(ret)));
		}
		for(auto ru: critu){
			ll rd = temp - ru;
			if(rd < 0)
				continue;
			int lo = upper_bound(crit.begin(), crit.end(), rd+1) - crit.begin() - 1;
			int hi = upper_bound(crit.begin(), crit.end(), rd+r) - crit.begin() - 1;
			tuple<ll, ll, ll> ret = maxquery(1, 0, (int)crit.size() -1, lo, hi);
			//printf("rd=%lld ru=%lld ret=%lld\n", rd, ru, rd+ru+max(get<0>(ret)+get<1>(ret), get<2>(ret)));
			ans = min(ans, temp + max(get<0>(ret)+get<1>(ret), get<2>(ret)));
		}
	}
	printf("%lld\n", ans);
	return 0;
}

Compilation message

cultivation.cpp: In function 'll minc2(ll, ll)':
cultivation.cpp:62:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   62 |   for(j=0; j+1<row[i].size(); j++){
      |            ~~~^~~~~~~~~~~~~~
cultivation.cpp: In function 'void minc(ll, ll)':
cultivation.cpp:78:27: warning: unused variable 'j' [-Wunused-variable]
   78 |  int sz = crit.size(), i, j;
      |                           ^
cultivation.cpp:87:5: warning: unused variable 'cd' [-Wunused-variable]
   87 |  ll cd = 0, cu = 0, sum = 0;
      |     ^~
cultivation.cpp:87:13: warning: unused variable 'cu' [-Wunused-variable]
   87 |  ll cd = 0, cu = 0, sum = 0;
      |             ^~
cultivation.cpp:87:21: warning: unused variable 'sum' [-Wunused-variable]
   87 |  ll cd = 0, cu = 0, sum = 0;
      |                     ^~~
cultivation.cpp: In function 'int main()':
cultivation.cpp:145:11: warning: unused variable 'rd' [-Wunused-variable]
  145 |  ll y, x, rd, ru;
      |           ^~
cultivation.cpp:145:15: warning: unused variable 'ru' [-Wunused-variable]
  145 |  ll y, x, rd, ru;
      |               ^~
cultivation.cpp:146:12: warning: unused variable 'j' [-Wunused-variable]
  146 |  int n, i, j;
      |            ^
cultivation.cpp:147:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  147 |  scanf("%lld %lld %d", &r, &c, &n);
      |  ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
cultivation.cpp:149:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  149 |   scanf("%lld %lld", &y, &x);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 444 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 0 ms 440 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 440 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 444 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 0 ms 440 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 440 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 444 KB Output is correct
17 Correct 1 ms 444 KB Output is correct
18 Correct 13 ms 348 KB Output is correct
19 Correct 3 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 17 ms 516 KB Output is correct
22 Correct 80 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 142 ms 740 KB Output is correct
25 Correct 108 ms 604 KB Output is correct
26 Correct 281 ms 980 KB Output is correct
27 Correct 274 ms 980 KB Output is correct
28 Correct 130 ms 860 KB Output is correct
29 Correct 248 ms 984 KB Output is correct
30 Correct 273 ms 980 KB Output is correct
31 Correct 281 ms 984 KB Output is correct
32 Correct 286 ms 984 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 444 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 0 ms 440 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 440 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 444 KB Output is correct
17 Correct 1 ms 444 KB Output is correct
18 Correct 13 ms 348 KB Output is correct
19 Correct 3 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 17 ms 516 KB Output is correct
22 Correct 80 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 142 ms 740 KB Output is correct
25 Correct 108 ms 604 KB Output is correct
26 Correct 281 ms 980 KB Output is correct
27 Correct 274 ms 980 KB Output is correct
28 Correct 130 ms 860 KB Output is correct
29 Correct 248 ms 984 KB Output is correct
30 Correct 273 ms 980 KB Output is correct
31 Correct 281 ms 984 KB Output is correct
32 Correct 286 ms 984 KB Output is correct
33 Correct 1 ms 856 KB Output is correct
34 Correct 289 ms 1228 KB Output is correct
35 Correct 304 ms 980 KB Output is correct
36 Correct 308 ms 980 KB Output is correct
37 Correct 208 ms 984 KB Output is correct
38 Correct 309 ms 984 KB Output is correct
39 Correct 293 ms 980 KB Output is correct
40 Correct 309 ms 980 KB Output is correct
41 Correct 36 ms 860 KB Output is correct
42 Correct 146 ms 980 KB Output is correct
43 Correct 261 ms 1236 KB Output is correct
44 Correct 302 ms 984 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 344 KB Output is correct
2 Correct 7 ms 348 KB Output is correct
3 Correct 8 ms 348 KB Output is correct
4 Correct 8 ms 492 KB Output is correct
5 Correct 8 ms 344 KB Output is correct
6 Correct 6 ms 348 KB Output is correct
7 Correct 2 ms 344 KB Output is correct
8 Correct 8 ms 348 KB Output is correct
9 Correct 4 ms 348 KB Output is correct
10 Correct 8 ms 444 KB Output is correct
11 Correct 8 ms 344 KB Output is correct
12 Correct 8 ms 344 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 3 ms 348 KB Output is correct
15 Correct 4 ms 344 KB Output is correct
16 Correct 9 ms 344 KB Output is correct
17 Correct 13 ms 604 KB Output is correct
18 Correct 6 ms 348 KB Output is correct
19 Correct 10 ms 348 KB Output is correct
20 Correct 7 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 344 KB Output is correct
2 Correct 7 ms 348 KB Output is correct
3 Correct 8 ms 348 KB Output is correct
4 Correct 8 ms 492 KB Output is correct
5 Correct 8 ms 344 KB Output is correct
6 Correct 6 ms 348 KB Output is correct
7 Correct 2 ms 344 KB Output is correct
8 Correct 8 ms 348 KB Output is correct
9 Correct 4 ms 348 KB Output is correct
10 Correct 8 ms 444 KB Output is correct
11 Correct 8 ms 344 KB Output is correct
12 Correct 8 ms 344 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 3 ms 348 KB Output is correct
15 Correct 4 ms 344 KB Output is correct
16 Correct 9 ms 344 KB Output is correct
17 Correct 13 ms 604 KB Output is correct
18 Correct 6 ms 348 KB Output is correct
19 Correct 10 ms 348 KB Output is correct
20 Correct 7 ms 348 KB Output is correct
21 Correct 1138 ms 652 KB Output is correct
22 Correct 1390 ms 624 KB Output is correct
23 Correct 1280 ms 640 KB Output is correct
24 Correct 1237 ms 636 KB Output is correct
25 Correct 1394 ms 612 KB Output is correct
26 Correct 363 ms 592 KB Output is correct
27 Correct 1465 ms 652 KB Output is correct
28 Correct 1439 ms 656 KB Output is correct
29 Correct 1356 ms 628 KB Output is correct
30 Correct 1335 ms 600 KB Output is correct
31 Correct 1365 ms 620 KB Output is correct
32 Correct 1301 ms 852 KB Output is correct
33 Correct 1274 ms 848 KB Output is correct
34 Correct 1403 ms 600 KB Output is correct
35 Correct 1438 ms 600 KB Output is correct
36 Correct 1369 ms 644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 444 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 0 ms 440 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 440 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 444 KB Output is correct
17 Correct 1 ms 444 KB Output is correct
18 Correct 13 ms 348 KB Output is correct
19 Correct 3 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 17 ms 516 KB Output is correct
22 Correct 80 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 142 ms 740 KB Output is correct
25 Correct 108 ms 604 KB Output is correct
26 Correct 281 ms 980 KB Output is correct
27 Correct 274 ms 980 KB Output is correct
28 Correct 130 ms 860 KB Output is correct
29 Correct 248 ms 984 KB Output is correct
30 Correct 273 ms 980 KB Output is correct
31 Correct 281 ms 984 KB Output is correct
32 Correct 286 ms 984 KB Output is correct
33 Correct 1 ms 856 KB Output is correct
34 Correct 289 ms 1228 KB Output is correct
35 Correct 304 ms 980 KB Output is correct
36 Correct 308 ms 980 KB Output is correct
37 Correct 208 ms 984 KB Output is correct
38 Correct 309 ms 984 KB Output is correct
39 Correct 293 ms 980 KB Output is correct
40 Correct 309 ms 980 KB Output is correct
41 Correct 36 ms 860 KB Output is correct
42 Correct 146 ms 980 KB Output is correct
43 Correct 261 ms 1236 KB Output is correct
44 Correct 302 ms 984 KB Output is correct
45 Correct 2 ms 344 KB Output is correct
46 Correct 7 ms 348 KB Output is correct
47 Correct 8 ms 348 KB Output is correct
48 Correct 8 ms 492 KB Output is correct
49 Correct 8 ms 344 KB Output is correct
50 Correct 6 ms 348 KB Output is correct
51 Correct 2 ms 344 KB Output is correct
52 Correct 8 ms 348 KB Output is correct
53 Correct 4 ms 348 KB Output is correct
54 Correct 8 ms 444 KB Output is correct
55 Correct 8 ms 344 KB Output is correct
56 Correct 8 ms 344 KB Output is correct
57 Correct 1 ms 348 KB Output is correct
58 Correct 3 ms 348 KB Output is correct
59 Correct 4 ms 344 KB Output is correct
60 Correct 9 ms 344 KB Output is correct
61 Correct 13 ms 604 KB Output is correct
62 Correct 6 ms 348 KB Output is correct
63 Correct 10 ms 348 KB Output is correct
64 Correct 7 ms 348 KB Output is correct
65 Correct 1138 ms 652 KB Output is correct
66 Correct 1390 ms 624 KB Output is correct
67 Correct 1280 ms 640 KB Output is correct
68 Correct 1237 ms 636 KB Output is correct
69 Correct 1394 ms 612 KB Output is correct
70 Correct 363 ms 592 KB Output is correct
71 Correct 1465 ms 652 KB Output is correct
72 Correct 1439 ms 656 KB Output is correct
73 Correct 1356 ms 628 KB Output is correct
74 Correct 1335 ms 600 KB Output is correct
75 Correct 1365 ms 620 KB Output is correct
76 Correct 1301 ms 852 KB Output is correct
77 Correct 1274 ms 848 KB Output is correct
78 Correct 1403 ms 600 KB Output is correct
79 Correct 1438 ms 600 KB Output is correct
80 Correct 1369 ms 644 KB Output is correct
81 Execution timed out 2061 ms 1212 KB Time limit exceeded
82 Halted 0 ms 0 KB -