Submission #689492

# Submission time Handle Problem Language Result Execution time Memory
689492 2023-01-28T16:07:10 Z alvingogo Bodyguard (JOI21_bodyguard) C++14
100 / 100
7298 ms 984676 KB
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#define AquA cin.tie(0);ios_base::sync_with_stdio(0);
#define fs first
#define sc second
#define cd complex<double>
#define p_q priority_queue
#define int long long
using namespace std;

struct pe{
	int t,a,b,c;
};
struct li{
	struct line{
		int m,k;
		line(){
			m=0,k=-1e18;
		}
		line(int a,int b){
			m=a;
			k=b;
		}
		int gv(int x){
			return m*x+k;
		}
	};
	struct no{
		no* lch=nullptr,*rch=nullptr;
		line val;
	};
	no* root=new no();
	void insert(no* v,line x,int L,int R){
		int m=(L+R)/2;
		if(x.gv(m)>v->val.gv(m)){
			swap(x,v->val);
		}
		if(L==R){
			return;
		}
		if(x.m>v->val.m){
			if(!v->rch){
				v->rch=new no();
				v->rch->val=x;
				return;
			}
			insert(v->rch,x,m+1,R);
		}
		else{
			if(!v->lch){
				v->lch=new no();
				v->lch->val=x;
				return;
			}
			insert(v->lch,x,L,m);
		}
	}
	int query(no* v,int p,int L,int R){
		if(!v){
			return -1e18;
		}
		if(L==R){
			return v->val.gv(p);
		}
		int m=(L+R)/2;
		if(p<=m){
			return max(v->val.gv(p),query(v->lch,p,L,m));
		}
		else{
			return max(v->val.gv(p),query(v->rch,p,m+1,R));
		}
	}
	inline void add(int a,int b){
		insert(root,line(a,b),0,1e10);
	}
	inline int qu(int x){
		return query(root,x,0,1e10);
	}
};
struct qu{
	int x,y;
	int lx,ly;
};
signed main(){
	AquA;
	int n,q;
	cin >> n >> q;
	vector<pe> v(n);
	vector<pair<int,int> > vp;
	vector<int> x,y;
	for(int i=0;i<n;i++){
		cin >> v[i].t >> v[i].a >> v[i].b >> v[i].c;
		v[i].c/=2;
		int e=v[i].t+abs(v[i].a-v[i].b),f=v[i].b;
		x.push_back(v[i].t+v[i].a);
		y.push_back(v[i].t-v[i].a);
		x.push_back(e+f);
		y.push_back(e-f);
	}
	x.push_back(1e10);
	y.push_back(1e10);
	sort(x.begin(),x.end());
	sort(y.begin(),y.end());
	x.erase(unique(x.begin(),x.end()),x.end());
	y.erase(unique(y.begin(),y.end()),y.end());
	auto lis=[&](vector<int>& a,int b){
		return lower_bound(a.begin(),a.end(),b)-a.begin();
	};
	int sx=x.size(),sy=y.size();
	vector<vector<int> > ve(sx,vector<int>(sy)),ho=ve,dp=ve;
	for(int i=0;i<n;i++){
		int fx=lis(x,v[i].t+v[i].a),fy=lis(y,v[i].t-v[i].a);
		int e=v[i].t+abs(v[i].a-v[i].b),f=v[i].b;
		int tx=lis(x,e+f),ty=lis(y,e-f);
		assert(fx==tx || fy==ty);
		if(fx==tx){
			assert(fy<ty);
			for(int j=fy+1;j<=ty;j++){
				ho[fx][j]=max(ho[fx][j],v[i].c);
			}
		}
		else{
			assert(fx<tx);
			for(int j=fx+1;j<=tx;j++){
				ve[j][fy]=max(ve[j][fy],v[i].c);
			}
		}
	}
	for(int i=sx-1;i>=0;i--){
		for(int j=sy-1;j>=0;j--){
			if(i+1<sx){
				dp[i][j]=max(dp[i][j],dp[i+1][j]+(x[i+1]-x[i])*ve[i+1][j]);
			}
			if(j+1<sy){
				dp[i][j]=max(dp[i][j],dp[i][j+1]+(y[j+1]-y[j])*ho[i][j+1]);
			}
		}
	}
	vector<qu> vq(q);
	vector<int> ans(q);
	vector<vector<pair<int,int> > > ax(sx),ay(sy);
	for(int i=0;i<q;i++){
		int a,b;
		cin >> a >> b;
		int c=a+b,d=a-b;
		int e=lis(x,c),f=lis(y,d);
		vq[i].x=c;
		vq[i].y=d;
		vq[i].lx=e;
		vq[i].ly=f;
		ax[e].push_back({f,i});
		ay[f].push_back({e,i});
	}
	for(int i=0;i<sx;i++){
		sort(ax[i].begin(),ax[i].end());
		reverse(ax[i].begin(),ax[i].end());
	}
	for(int i=0;i<sy;i++){
		sort(ay[i].begin(),ay[i].end());
		reverse(ay[i].begin(),ay[i].end());
	}
	for(int i=0;i<sx;i++){
		li lst;
		int l=0;
		for(int j=sy-1;j>=0;j--){
			lst.add(ve[i][j],dp[i][j]);
			while(l<ax[i].size() && ax[i][l].fs==j){
				int id=ax[i][l].sc;
				ans[id]=max(ans[id],lst.qu(x[i]-vq[id].x));
				l++;
			}
		}
	}
	for(int i=0;i<sy;i++){
		li lst;
		int l=0;
		for(int j=sx-1;j>=0;j--){
			lst.add(ho[j][i],dp[j][i]);
			while(l<ay[i].size() && ay[i][l].fs==j){
				int id=ay[i][l].sc;
				ans[id]=max(ans[id],lst.qu(y[i]-vq[id].y));
				l++;
			}
		}
	}
	for(int i=0;i<q;i++){
		cout << ans[i] << "\n";
	}
	return 0;
}

Compilation message

bodyguard.cpp: In function 'int main()':
bodyguard.cpp:167:11: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  167 |    while(l<ax[i].size() && ax[i][l].fs==j){
      |          ~^~~~~~~~~~~~~
bodyguard.cpp:179:11: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  179 |    while(l<ay[i].size() && ay[i][l].fs==j){
      |          ~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 5244 ms 600460 KB Output is correct
2 Correct 5028 ms 633256 KB Output is correct
3 Correct 2813 ms 385340 KB Output is correct
4 Correct 1984 ms 279316 KB Output is correct
5 Correct 5565 ms 827020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4437 ms 634144 KB Output is correct
2 Correct 4552 ms 635764 KB Output is correct
3 Correct 4183 ms 608372 KB Output is correct
4 Correct 7 ms 1528 KB Output is correct
5 Correct 3789 ms 523596 KB Output is correct
6 Correct 3868 ms 480860 KB Output is correct
7 Correct 3578 ms 434460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4437 ms 634144 KB Output is correct
2 Correct 4552 ms 635764 KB Output is correct
3 Correct 4183 ms 608372 KB Output is correct
4 Correct 7 ms 1528 KB Output is correct
5 Correct 3789 ms 523596 KB Output is correct
6 Correct 3868 ms 480860 KB Output is correct
7 Correct 3578 ms 434460 KB Output is correct
8 Correct 4273 ms 631928 KB Output is correct
9 Correct 4228 ms 637060 KB Output is correct
10 Correct 3917 ms 605736 KB Output is correct
11 Correct 6 ms 1876 KB Output is correct
12 Correct 3589 ms 523808 KB Output is correct
13 Correct 3462 ms 481348 KB Output is correct
14 Correct 3715 ms 568608 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4437 ms 634144 KB Output is correct
2 Correct 4552 ms 635764 KB Output is correct
3 Correct 4183 ms 608372 KB Output is correct
4 Correct 7 ms 1528 KB Output is correct
5 Correct 3789 ms 523596 KB Output is correct
6 Correct 3868 ms 480860 KB Output is correct
7 Correct 3578 ms 434460 KB Output is correct
8 Correct 4273 ms 631928 KB Output is correct
9 Correct 4228 ms 637060 KB Output is correct
10 Correct 3917 ms 605736 KB Output is correct
11 Correct 6 ms 1876 KB Output is correct
12 Correct 3589 ms 523808 KB Output is correct
13 Correct 3462 ms 481348 KB Output is correct
14 Correct 3715 ms 568608 KB Output is correct
15 Correct 4217 ms 642584 KB Output is correct
16 Correct 4217 ms 641456 KB Output is correct
17 Correct 4080 ms 616488 KB Output is correct
18 Correct 34 ms 5516 KB Output is correct
19 Correct 3565 ms 527676 KB Output is correct
20 Correct 3393 ms 485268 KB Output is correct
21 Correct 3684 ms 577596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5244 ms 600460 KB Output is correct
2 Correct 5028 ms 633256 KB Output is correct
3 Correct 2813 ms 385340 KB Output is correct
4 Correct 1984 ms 279316 KB Output is correct
5 Correct 5565 ms 827020 KB Output is correct
6 Correct 4437 ms 634144 KB Output is correct
7 Correct 4552 ms 635764 KB Output is correct
8 Correct 4183 ms 608372 KB Output is correct
9 Correct 7 ms 1528 KB Output is correct
10 Correct 3789 ms 523596 KB Output is correct
11 Correct 3868 ms 480860 KB Output is correct
12 Correct 3578 ms 434460 KB Output is correct
13 Correct 4273 ms 631928 KB Output is correct
14 Correct 4228 ms 637060 KB Output is correct
15 Correct 3917 ms 605736 KB Output is correct
16 Correct 6 ms 1876 KB Output is correct
17 Correct 3589 ms 523808 KB Output is correct
18 Correct 3462 ms 481348 KB Output is correct
19 Correct 3715 ms 568608 KB Output is correct
20 Correct 4217 ms 642584 KB Output is correct
21 Correct 4217 ms 641456 KB Output is correct
22 Correct 4080 ms 616488 KB Output is correct
23 Correct 34 ms 5516 KB Output is correct
24 Correct 3565 ms 527676 KB Output is correct
25 Correct 3393 ms 485268 KB Output is correct
26 Correct 3684 ms 577596 KB Output is correct
27 Correct 7298 ms 984676 KB Output is correct
28 Correct 7288 ms 983064 KB Output is correct
29 Correct 6252 ms 936568 KB Output is correct
30 Correct 1665 ms 329580 KB Output is correct
31 Correct 5225 ms 722520 KB Output is correct
32 Correct 5990 ms 815220 KB Output is correct
33 Correct 5009 ms 858248 KB Output is correct