Submission #689881

# Submission time Handle Problem Language Result Execution time Memory
689881 2023-01-29T16:10:08 Z Antekb Bodyguard (JOI21_bodyguard) C++14
100 / 100
7078 ms 1233840 KB
#include<bits/stdc++.h>
 
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("trapv")
 
#define st first
#define nd second
#define pb push_back
#define eb emplace_back
#define pp(x) pop_back(x)
#define mp(a, b) make_pair(a, b)
#define all(x) (x).begin(), (x).end()
#define rev(x) reverse(all(x))
#define sor(x) sort(all(x))
#define sz(x) (int)(x).size()
#define rsz(x) resize(x)
 
using namespace std;
 
///~~~~~~~~~~~~~~~~~~~~~~~~~~
 
template <typename H, typename T> 
ostream& operator<<(ostream& os, pair<H, T> m){
	return os <<"("<< m.st<<", "<<m.nd<<")";
}
template <typename H> 
ostream& operator<<(ostream& os, vector<H> V){
	os<<"{";
	for(int i=0; i<V.size(); i++){
		if(i)os<<" ";
		os<<V[i];
	}
	os<<"}";
	return os;
}
 
void debug(){cerr<<"\n";}
template <typename H, typename... T>
void debug(H h, T... t) {cerr<<h; if (sizeof...(t)) cerr << ", "; debug(t...);}
//#define deb(x...) cerr<<#x<<" = ";debug(x);
#define deb(x...) ;
 
///~~~~~~~~~~~~~~~~~~~~~~~~~
 
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<pii > vii;
typedef vector<ll> vl;
typedef vector<pll> vll;
typedef string str;

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

 
#define BOOST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
 
//mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
 
const int N=5605, INF=1e9+5, mod=1e9+7, mod2=998244353;

struct modint{
	int n=0;
	modint(){}
	modint(ll x){
		n=x%mod;
		if(n<0)n+=mod;
	}
	operator int(){
		return n;
	}
	modint operator+(modint a){a.n = n+a.n; if(a.n>=mod)a.n-=mod;return a;}
	modint operator+=(modint a){return (*this)=(*this)+a;}
	modint operator-(modint a){a.n = n-a.n; if(a.n<0)a.n+=mod;return a;}
	modint operator-=(modint a){return (*this)=(*this)-a;}
	modint operator*(modint a){a.n = (n*1ll*a.n)%mod; return a;}
	modint operator*=(modint a){return (*this)=(*this)*a;}
	modint operator^(const ll &m)const{
		modint a(1);
		if(m==0)return a;
		if(m==1)return (*this);
		a=(*this)^(m/2);
		a*=a;
		return a*((*this)^(m&1));
	}
	modint odw(){
		return (*this)^((ll)mod-2);
	}
	modint operator/(modint a){return (*this)*a.odw();}
	modint operator/=(modint a){return (*this)=(*this)/a;}
	bool operator==(modint a){return a.n==n;}
	friend ostream& operator<<(ostream& os, modint m) {
		return os << m.n; 
	}
};
modint fact[N], fact2[N];
typedef vector<modint> vm;
void factinit(){
    fact[0]=1;
    for(int i=1; i<N; i++){
        fact[i]=(fact[i-1]*modint(i))%mod;
    }
    fact2[N-1]=fact[N-1].odw();
    for(int i=N-2; i>=0; i--){
    	fact2[i]=(fact2[i+1]*modint(i+1))%mod;
    }
}
modint npok(int _n, int _k){
    return fact[_n]*fact2[_k]*fact2[_n-_k];
}
using uint=unsigned int;
const int M=3e6+5;
int edg[2][N][N], X[N], Y[N];
ll dp[N][N];
vii co[N][N];
ll ans[M], ans2[M];
int main(){
	//factinit();
	//BOOST;
	int n, q;
	cin>>n>>q;
	vector<pair<pii, pii> > lin;
	vector<int> cx;
	vector<uint> cy;
	for(int i=0; i<n; i++){
		int t, a, b, c;
		cin>>t>>a>>b>>c;
		lin.eb(mp(t, c), mp(a, b));
		cx.pb(t-a);
		cx.pb(t-b+abs(a-b));
		cy.pb(t+a);
		cy.pb(t+b+abs(a-b));
	}
	vii Q;
	for(int i=0; i<q; i++){
		int t, x;
		cin>>t>>x;
		Q.eb(t-x, t+x);
	}
	//deb(cx, cy);
	sor(cx);
	cx.rsz(unique(all(cx))-cx.begin());
	sor(cy);
	cy.rsz(unique(all(cy))-cy.begin());
	int XX=cx.size(), YY=cy.size();
	for(int i=0; i<n; i++){
		int t=lin[i].st.st, c=lin[i].st.nd, a=lin[i].nd.st, b=lin[i].nd.nd;
		int x1=lower_bound(all(cx), t-a)-cx.begin();
		int x2=lower_bound(all(cx), t-b+abs(a-b))-cx.begin();
		int y1=lower_bound(all(cy), t+a)-cy.begin();
		int y2=lower_bound(all(cy), uint(t+b)+abs(a-b))-cy.begin();
		//deb(x1, y1, x2, y2, c);
		//deb(cx[x1], cy[y1], cx[x2], cy[y2], c);
		assert(y1<=y2);
		assert(x1<=x2);
		c/=2;
		if(x1==x2){
			for(int y=y1; y<y2; y++){
				edg[0][x1][y]=max(edg[0][x1][y], c);
			}
		}
		else{
			for(int x=x1; x<x2; x++){
				edg[1][x][y1]=max(edg[1][x][y1], c);
			}
		}
	}
	for(int x=XX-1; x>=0; x--){
		for(int y=YY-1; y>=0; y--){
			if(x+1!=XX)dp[x][y]=max(dp[x+1][y]+edg[1][x][y]*1ll*(cx[x+1]-cx[x]), dp[x][y]);
			if(y+1!=YY)dp[x][y]=max(dp[x][y+1]+edg[0][x][y]*1ll*(cy[y+1]-cy[y]), dp[x][y]);
		}
	}

	for(int i=0; i<q; i++){
		int x=lower_bound(all(cx), Q[i].st)-cx.begin();
		int y=lower_bound(all(cy), Q[i].nd)-cy.begin();
		if(x<XX && y<YY){
			//deb(cx[x], cy[y]);
			int d=cy[y]-Q[i].nd;
			co[x][y].eb(d, i);
		}
	}
	for(int y=YY-1; y>=0; y--){
		vector<pair<int, ll> > V;
		vector<ld> opt;
		deb(opt);
		for(int x=XX-1; x>=0; x--){
			ll B=dp[x][y];
			int A=0;
			if(y)A=edg[0][x][y-1];
			deb(A, B);
			while(V.size()){
				int A2=V.back().st;
				ll B2=V.back().nd;
				deb(A2, B2);
				if(!opt.size() && A==A2){
					V.back().nd=B;
					break;
				}
				if(!opt.size() && A>A2){
					V.pp();
					break;
				}
				deb(opt.size(), A, A2);
				if(!opt.size() && B==B2 && A<A2){
					deb("a");
					break;
				}
				if(opt.size() && A*opt.back()+B>=A2*opt.back()+B2){
					V.pop_back();
					opt.pop_back();
				}
				else{
					assert(A!=A2);
					opt.pb((B-B2)/ld(A2-A));
					V.eb(A, B);
					break;
				}
			}
			
			if(!V.size()){
				V.eb(A, B);
			}
			deb(V, opt);
			for(pii z:co[x][y]){
				deb(z.st);
				int l=0, r=opt.size();
				while(l<r){
					int m=(l+r)/2;
					if(opt[m]>z.st)l=m+1;
					else r=m;
				}
				ans[z.nd]=V[l].st*1ll*z.st+V[l].nd;
			}
			co[x][y].clear();
		}
	}
	for(int i=0; i<q; i++){
		int x=lower_bound(all(cx), Q[i].st)-cx.begin();
		int y=lower_bound(all(cy), Q[i].nd)-cy.begin();
		if(x<XX && y<YY){
			//deb(cx[x], cy[y]);
			int d=cx[x]-Q[i].st;
			co[x][y].eb(d, i);
		}
	}
	for(int x=XX-1; x>=0; x--){
		vector<pair<int, ll> > V;
		vector<ld> opt;
		deb(V);
		for(int y=YY-1; y>=0; y--){
			ll B=dp[x][y];
			int A=0;
			if(x)A=edg[1][x-1][y];
			deb(A, B);
			while(V.size()){
				int A2=V.back().st;
				ll B2=V.back().nd;
				deb(A2, B2);
				if(!opt.size() && A==A2){
					V.back().nd=B;
					break;
				}
				if(!opt.size() && A>A2){
					V.pp();
					break;
				}
				if(!opt.size() && B==B2 && A<A2)break;
				if(opt.size() && A*opt.back()+B>=A2*opt.back()+B2){
					V.pop_back();
					opt.pop_back();
				}
				else{
					assert(A!=A2);
					opt.pb((B-B2)/ld(A2-A));
					V.eb(A, B);
					break;
				}
			}
			if(!V.size()){
				V.eb(A, B);
			}
			deb(V, opt);
			for(pii z:co[x][y]){
				deb(z.st);
				int l=0, r=opt.size();
				while(l<r){
					int m=(l+r)/2;
					if(opt[m]>z.st)l=m+1;
					else r=m;
				}
				ans2[z.nd]=V[l].st*1ll*z.st+V[l].nd;
			}
		}
	}
	for(int i=0; i<q; i++){
		//cout<<ans[i]<<" "<<ans2[i]<<"\n";
		cout<<max(ans[i], ans2[i])<<"\n";
	}
}
# Verdict Execution time Memory Grader output
1 Correct 5262 ms 1041316 KB Output is correct
2 Correct 5296 ms 1042160 KB Output is correct
3 Correct 2930 ms 915832 KB Output is correct
4 Correct 2837 ms 884364 KB Output is correct
5 Correct 3149 ms 1082784 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1727 ms 977592 KB Output is correct
2 Correct 1704 ms 977344 KB Output is correct
3 Correct 1748 ms 976288 KB Output is correct
4 Correct 356 ms 772304 KB Output is correct
5 Correct 1319 ms 946692 KB Output is correct
6 Correct 1291 ms 931272 KB Output is correct
7 Correct 1384 ms 946460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1727 ms 977592 KB Output is correct
2 Correct 1704 ms 977344 KB Output is correct
3 Correct 1748 ms 976288 KB Output is correct
4 Correct 356 ms 772304 KB Output is correct
5 Correct 1319 ms 946692 KB Output is correct
6 Correct 1291 ms 931272 KB Output is correct
7 Correct 1384 ms 946460 KB Output is correct
8 Correct 1851 ms 977868 KB Output is correct
9 Correct 1920 ms 977148 KB Output is correct
10 Correct 1777 ms 975212 KB Output is correct
11 Correct 366 ms 772428 KB Output is correct
12 Correct 1321 ms 946768 KB Output is correct
13 Correct 1328 ms 931456 KB Output is correct
14 Correct 1476 ms 946584 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1727 ms 977592 KB Output is correct
2 Correct 1704 ms 977344 KB Output is correct
3 Correct 1748 ms 976288 KB Output is correct
4 Correct 356 ms 772304 KB Output is correct
5 Correct 1319 ms 946692 KB Output is correct
6 Correct 1291 ms 931272 KB Output is correct
7 Correct 1384 ms 946460 KB Output is correct
8 Correct 1851 ms 977868 KB Output is correct
9 Correct 1920 ms 977148 KB Output is correct
10 Correct 1777 ms 975212 KB Output is correct
11 Correct 366 ms 772428 KB Output is correct
12 Correct 1321 ms 946768 KB Output is correct
13 Correct 1328 ms 931456 KB Output is correct
14 Correct 1476 ms 946584 KB Output is correct
15 Correct 1889 ms 981068 KB Output is correct
16 Correct 1779 ms 981916 KB Output is correct
17 Correct 1804 ms 978648 KB Output is correct
18 Correct 386 ms 774496 KB Output is correct
19 Correct 1346 ms 949200 KB Output is correct
20 Correct 1435 ms 933932 KB Output is correct
21 Correct 1598 ms 948952 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5262 ms 1041316 KB Output is correct
2 Correct 5296 ms 1042160 KB Output is correct
3 Correct 2930 ms 915832 KB Output is correct
4 Correct 2837 ms 884364 KB Output is correct
5 Correct 3149 ms 1082784 KB Output is correct
6 Correct 1727 ms 977592 KB Output is correct
7 Correct 1704 ms 977344 KB Output is correct
8 Correct 1748 ms 976288 KB Output is correct
9 Correct 356 ms 772304 KB Output is correct
10 Correct 1319 ms 946692 KB Output is correct
11 Correct 1291 ms 931272 KB Output is correct
12 Correct 1384 ms 946460 KB Output is correct
13 Correct 1851 ms 977868 KB Output is correct
14 Correct 1920 ms 977148 KB Output is correct
15 Correct 1777 ms 975212 KB Output is correct
16 Correct 366 ms 772428 KB Output is correct
17 Correct 1321 ms 946768 KB Output is correct
18 Correct 1328 ms 931456 KB Output is correct
19 Correct 1476 ms 946584 KB Output is correct
20 Correct 1889 ms 981068 KB Output is correct
21 Correct 1779 ms 981916 KB Output is correct
22 Correct 1804 ms 978648 KB Output is correct
23 Correct 386 ms 774496 KB Output is correct
24 Correct 1346 ms 949200 KB Output is correct
25 Correct 1435 ms 933932 KB Output is correct
26 Correct 1598 ms 948952 KB Output is correct
27 Correct 7025 ms 1233840 KB Output is correct
28 Correct 7078 ms 1233004 KB Output is correct
29 Correct 5270 ms 1179080 KB Output is correct
30 Correct 2887 ms 925584 KB Output is correct
31 Correct 3138 ms 1067272 KB Output is correct
32 Correct 5261 ms 1122284 KB Output is correct
33 Correct 3511 ms 1119100 KB Output is correct