Submission #626706

# Submission time Handle Problem Language Result Execution time Memory
626706 2022-08-11T16:42:28 Z CSQ31 Road Construction (JOI21_road_construction) C++17
0 / 100
206 ms 71452 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define all(a) a.begin(),a.end()
#define pb push_back
#define owo ios_base::sync_with_stdio(0);cin.tie(0);
#define fi first
#define se second 
typedef pair<int,int> pii;
vector<ll>glo;
const int MAXN = 1e6,INF = 2e9+1;
pii t[MAXN];
int ch[2][MAXN];
int ndcnt = 0;
int nwnode(int v = -1){
	ndcnt++;
	int u = ndcnt;
	t[u] = {-INF,0};
	if(v!=-1){
		t[u] = t[v];
		ch[0][u] = ch[0][v];
		ch[1][u] = ch[1][v];
	}
	return u;
}
void pull(int v){
	t[v] = {-INF,0};
	if(ch[0][v])t[v] = max(t[v],t[ch[0][v]]);
	if(ch[1][v])t[v] = max(t[v],t[ch[1][v]]);
}
int build(int l,int r){
	if(l==r){
		int u = nwnode();
		t[u].se = l;
		return u;
	}
	int u = nwnode();
	int tm = (l+r)/2;
	ch[0][u] = build(l,tm);
	ch[1][u] = build(tm+1,r);
	pull(u);
	return u;
}
int upd(int v,int l,int r,int pos,int val){
	if(l==r){
		int u = nwnode();
		t[u] = {val,pos};
		//cout<<l<<" "<<r<<" "<<t[u].fi<<" "<<t[u].se<<'\n';
		return u;
	}
	int u = nwnode(v);
	int tm = (l+r)/2;
	if(pos <= tm)ch[0][u] = upd(ch[0][v],l,tm,pos,val);
	else ch[1][u] = upd(ch[1][v],tm+1,r,pos,val);
	pull(u);
	//cout<<l<<" "<<r<<" "<<t[u].fi<<" "<<t[u].se<<'\n';
	return u;
}
pii query(int v,int l,int r,int tl,int tr){
	if(l>r)return {-INF,0};
	if(l==tl && r==tr)return t[v];
	int tm = (tl+tr)/2;
	return max(query(ch[0][v],l,min(r,tm),tl,tm),
	           query(ch[1][v],max(l,tm+1),r,tm+1,tr));
}
void reset(){
	for(int i=0;i<=ndcnt;i++){
		t[i] = {-INF,0};
		ch[0][i] = ch[1][i] = 0;
	}
	ndcnt = 0;
}
int main()
{
	owo
	int n,k;
	cin>>n>>k;
	vector<pii>p(n),y(n);
	vector<int>id(n);
	for(int i=0;i<n;i++)cin>>p[i].fi>>p[i].se;
	sort(all(p));
	for(int i=0;i<n;i++){
		y[i].fi = p[i].se;
		y[i].se = i;
	}
	sort(all(y));
	map<int,int>r; //right border of fixed value of y
	//cout<<"sort\n";
	for(int i=0;i<n;i++){
		//cout<<p[i].fi<<" "<<p[i].se<<'\n';
		r[y[i].fi] = i;
		id[y[i].se] = i;
	}
	//for(int i=0;i<n;i++)cout<<id[i]<<" ";
	//cout<<'\n';
	int root = build(0,n-1);
	for(int i=0;i<n;i++){
		int cur = root;
		//cout<<"at "<<i<<'\n';
		for(int j=0;j<k;j++){
			pii res = query(cur,0,r[p[i].se],0,n-1);
			//cout<<cur<<" "<<res.fi<<" "<<res.se<<'\n';
			if(res.fi == -INF)break;
			ll c = p[i].fi + p[i].se;
			c-=res.fi;
			//cout<<c<<'\n';
			glo.pb(c);
			cur = upd(cur,0,n-1,res.se,-INF);
		}
		root = upd(root,0,n-1,id[i],p[i].fi+p[i].se);
	}
	reset();
	root = build(0,n-1);
	for(int i=0;i<n;i++){
		int cur = root;
		if(r[p[i].se] != n-1){
			for(int j=0;j<k;j++){
				pii res = query(cur,r[p[i].se]+1,n-1,0,n-1);
				//cout<<cur<<" "<<res.fi<<" "<<res.se<<'\n';
				if(res.fi == -INF)break;
				ll c = p[i].fi - p[i].se;
				c-=res.fi;
				//cout<<c<<'\n';
				glo.pb(c);
				cur = upd(cur,0,n-1,res.se,-INF);
			}
		}
		root = upd(root,0,n-1,id[i],p[i].fi-p[i].se);
	}
	sort(all(glo));
	for(int i=0;i<k;i++)cout<<glo[i]<<'\n';
	//cout<<'\n';
	
	
}
# Verdict Execution time Memory Grader output
1 Runtime error 46 ms 34108 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 107 ms 45804 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 206 ms 71452 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 206 ms 71452 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 46 ms 34108 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 46 ms 34108 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -