Submission #1072005

# Submission time Handle Problem Language Result Execution time Memory
1072005 2024-08-23T13:14:19 Z TimDee Road Construction (JOI21_road_construction) C++17
100 / 100
3234 ms 23360 KB
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define forn(i,n) for(int i=0; i<n; ++i)
#define vii(a,n) vector<int> a(n); forn(i,n) cin>>a[i];
#define pi pair<int,int>
#define f first
#define s second
#define pb push_back
#define all(x) x.begin(),x.end()

struct BIT {
	vector<int> t; int n;
	BIT(int n) {
		t.assign(n,0); this->n=n;
	}
	void add(int i, int x) {
		for(;i<n;i|=i+1) t[i]+=x;
	}
	int sum(int r) {
		int q=0;
		for(;r>=0;r=(r&(r+1))-1) q+=t[r];
		return q;
	}
};

void solve() {

	int n,k; cin>>n>>k;
	vector<pi> a(n);
	forn(i,n) cin>>a[i].f>>a[i].s;
	
	vector<pi> f,s;
	forn(i,n) {
		f.pb({a[i].f+a[i].s,i});
	}
	sort(all(f));
	forn(i,n) {
		int j=f[i].s;
		s.pb({a[j].f-a[j].s,i});
	}
	sort(all(s));

	//forn(i,n) cout<<f[i].f<<','<<f[i].s<<" "; cout<<'\n';
	//forn(i,n) cout<<s[i].f<<','<<s[i].s<<" "; cout<<'\n';

	int l=1, r=4e9;
	while (l<r) {
		int m=(l+r+1)>>1;
		int lx=0,rx=0;
		BIT ft(n);
		int z = 0;
		forn(i,n) {
			while (rx<n && s[rx].f<=s[i].f+m) {
				//cout<<"> "<<m<<' '<<i<<": "<<rx<<' '<<s[i].f<<' '<<s[rx].f<<'\n';
				ft.add(s[rx].s,1); ++rx;
			}
			while (s[lx].f < s[i].f-m) {
				ft.add(s[lx].s,-1); ++lx;
			}

			int j = s[i].s;

			int ls=0, rs=n-1;
			while (ls<rs) {
				int ms=(ls+rs)>>1;
				if (f[ms].f < f[j].f-m) ls=ms+1;
				else rs=ms;
			}
			int lq = rs;
			ls=0, rs=n-1;
			while (ls<rs) {
				int ms=(ls+rs+1)>>1;
				if (f[ms].f > f[j].f+m) rs=ms-1;
				else ls=ms;
			}
			int rq = rs;
			//cout<<"! "<<i<<' '<<f[j].s+1<<"  "<<lx<<" "<<rx<<"  "<<lq<<' '<<rq<<' '<<ft.sum(rq)-ft.sum(lq-1)-1<<'\n';

			z+=ft.sum(rq) - ft.sum(lq-1) - 1;
		}
		z>>=1;
		//cout<<"? "<<m<<": "<<z<<'\n';
		if (z >= k) r=m-1;
		else l=m;
	}

	vector<int> ans;
	int m = r;
	
	set<int> st;
	int lx=0,rx=0;

	forn(i,n) {
		while (rx<n && s[rx].f<=s[i].f+m) {
			st.insert(s[rx].s); ++rx;
		}
		while (lx<i) {
			st.erase(s[lx].s); ++lx;
		}

		int j = s[i].s;

		int ls=0, rs=n-1;
		while (ls<rs) {
			int ms=(ls+rs)>>1;
			if (f[ms].f < f[j].f-m) ls=ms+1;
			else rs=ms;
		}
		int lq = rs;
		ls=0, rs=n-1;
		while (ls<rs) {
			int ms=(ls+rs+1)>>1;
			if (f[ms].f > f[j].f+m) rs=ms-1;
			else ls=ms;
		}
		int rq = rs;


		int j1 = f[j].s;
		auto it = st.lower_bound(lq);
		while (it!=st.end()) {
			int jj = *it;
			if (jj>rq) break;
			int j2 = f[jj].s;
			if (j1==j2) {++it; continue;}
			int d = abs(a[j1].f-a[j2].f)+abs(a[j1].s-a[j2].s);
			ans.pb(d);
			++it;
		}
	}
	sort(all(ans));

	while (ans.size()<k) ans.pb(m+1);

	for(auto&x:ans) cout<<x<<'\n';

}

int32_t main() {
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	//int t; cin>>t; while (t--)
	solve();
}

Compilation message

road_construction.cpp: In function 'void solve()':
road_construction.cpp:135:19: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
  135 |  while (ans.size()<k) ans.pb(m+1);
      |         ~~~~~~~~~~^~
# Verdict Execution time Memory Grader output
1 Correct 35 ms 5196 KB Output is correct
2 Correct 37 ms 5168 KB Output is correct
3 Correct 34 ms 5056 KB Output is correct
4 Correct 31 ms 5316 KB Output is correct
5 Correct 33 ms 4004 KB Output is correct
6 Correct 2 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1168 ms 20264 KB Output is correct
2 Correct 1155 ms 20264 KB Output is correct
3 Correct 29 ms 5056 KB Output is correct
4 Correct 1133 ms 20084 KB Output is correct
5 Correct 1128 ms 20312 KB Output is correct
6 Correct 1113 ms 20292 KB Output is correct
7 Correct 938 ms 19604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2717 ms 20420 KB Output is correct
2 Correct 2765 ms 20900 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1079 ms 18428 KB Output is correct
5 Correct 1737 ms 20164 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2717 ms 20420 KB Output is correct
2 Correct 2765 ms 20900 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1079 ms 18428 KB Output is correct
5 Correct 1737 ms 20164 KB Output is correct
6 Correct 2758 ms 20668 KB Output is correct
7 Correct 2737 ms 19912 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 2753 ms 20888 KB Output is correct
11 Correct 1091 ms 19140 KB Output is correct
12 Correct 1788 ms 20728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 35 ms 5196 KB Output is correct
2 Correct 37 ms 5168 KB Output is correct
3 Correct 34 ms 5056 KB Output is correct
4 Correct 31 ms 5316 KB Output is correct
5 Correct 33 ms 4004 KB Output is correct
6 Correct 2 ms 344 KB Output is correct
7 Correct 1158 ms 11192 KB Output is correct
8 Correct 1177 ms 11164 KB Output is correct
9 Correct 40 ms 5184 KB Output is correct
10 Correct 1076 ms 10408 KB Output is correct
11 Correct 977 ms 10372 KB Output is correct
12 Correct 359 ms 11152 KB Output is correct
13 Correct 410 ms 10116 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 35 ms 5196 KB Output is correct
2 Correct 37 ms 5168 KB Output is correct
3 Correct 34 ms 5056 KB Output is correct
4 Correct 31 ms 5316 KB Output is correct
5 Correct 33 ms 4004 KB Output is correct
6 Correct 2 ms 344 KB Output is correct
7 Correct 1168 ms 20264 KB Output is correct
8 Correct 1155 ms 20264 KB Output is correct
9 Correct 29 ms 5056 KB Output is correct
10 Correct 1133 ms 20084 KB Output is correct
11 Correct 1128 ms 20312 KB Output is correct
12 Correct 1113 ms 20292 KB Output is correct
13 Correct 938 ms 19604 KB Output is correct
14 Correct 2717 ms 20420 KB Output is correct
15 Correct 2765 ms 20900 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1079 ms 18428 KB Output is correct
18 Correct 1737 ms 20164 KB Output is correct
19 Correct 2758 ms 20668 KB Output is correct
20 Correct 2737 ms 19912 KB Output is correct
21 Correct 1 ms 344 KB Output is correct
22 Correct 1 ms 344 KB Output is correct
23 Correct 2753 ms 20888 KB Output is correct
24 Correct 1091 ms 19140 KB Output is correct
25 Correct 1788 ms 20728 KB Output is correct
26 Correct 1158 ms 11192 KB Output is correct
27 Correct 1177 ms 11164 KB Output is correct
28 Correct 40 ms 5184 KB Output is correct
29 Correct 1076 ms 10408 KB Output is correct
30 Correct 977 ms 10372 KB Output is correct
31 Correct 359 ms 11152 KB Output is correct
32 Correct 410 ms 10116 KB Output is correct
33 Correct 3234 ms 23072 KB Output is correct
34 Correct 3173 ms 23180 KB Output is correct
35 Correct 3080 ms 22572 KB Output is correct
36 Correct 955 ms 23360 KB Output is correct
37 Correct 942 ms 23104 KB Output is correct
38 Correct 1138 ms 21868 KB Output is correct