제출 #1152052

#제출 시각아이디문제언어결과실행 시간메모리
1152052mertbbmTwo Antennas (JOI19_antennas)C++20
100 / 100
381 ms69112 KiB
#include <bits/stdc++.h>
using namespace std;
 
#define int long long 
#define ld long double
#define show(x,y) cout << y << " " << #x << endl;
#define show2(x,y,i,j) cout << y << " " << #x << "  " << j << " " << #i << endl;
#define show3(x,y,i,j,p,q) cout << y << " " << #x << "  " << j << " " << #i << "  " << q << " " << #p << endl;
#define show4(x,y) for(auto it:y) cout << it << " "; cout << #x << endl;
typedef pair<int,int>pii;
typedef pair<pii,int>pi2;
mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count());

inline array<int,3>combine(const array<int,3>a, const array<int,3>b){
	array<int,3>temp;
	temp[0]=min(a[0],b[0]);
	temp[1]=max(a[1],b[1]);
	temp[2]=max(a[1],b[2]);
	return temp;
}

struct node{
	int s,e,m;
	node *l,*r;
	array<int,3>v;
	pii lazyUpd;
	bool lset;
	
	node(int ss, int ee):s(ss),e(ee),m((s+e)>>1),v({(int)1e12,(int)-1e12,-1}),lazyUpd({0,0}),lset(0){
		if(s!=e){
			l=new node(s,m);
			r=new node(m+1,e);
		}
	}
	
	void self_upd(pii x){
		v[2]=max({v[2],x.second-v[0],v[1]-x.first});
		if(lset){
			lazyUpd.first=min(lazyUpd.first,x.first);
			lazyUpd.second=max(lazyUpd.second,x.second);
		}
		else lazyUpd=x;
		lset=true;
	}
	
	void forceProp(){
		if(s==e) return;
		if(lset){
			l->self_upd(lazyUpd);
			r->self_upd(lazyUpd);
			lazyUpd={0,0};
			lset=0;
		}
	}
	
	void rangeUpd(int x, int y, int c){
		if(x<=s&&y>=e){
			self_upd({c,c});
			return;
		}
		forceProp();
		if(x<=m) l->rangeUpd(x,y,c);
		if(y>m) r->rangeUpd(x,y,c);
		v[2]=max({l->v[2],r->v[2],v[2]});
		v[0]=min(l->v[0],r->v[0]);
		v[1]=max(l->v[1],r->v[1]);
	}
	
	void set(int x, pii y){
		if(s==e){
			v[0]=y.first;
			v[1]=y.second;
			return;
		}
		forceProp();
		if(x<=m) l->set(x,y);
		else r->set(x,y);
		v[2]=max({l->v[2],r->v[2],v[2]});
		v[0]=min(l->v[0],r->v[0]);
		v[1]=max(l->v[1],r->v[1]);
	}
	
	int query(int x, int y){
		if(x<=s&&y>=e){
			return v[2];
		}
		forceProp();
		if(y<=m) return l->query(x,y);
		if(x>m) return r->query(x,y);
		return max(l->query(x,m),r->query(m+1,y));
	}
};

void solve(){
	int n;
	cin >> n;
	
	pair<int,pii>arr[n+5];
	for(int x=1;x<=n;x++){
		cin >> arr[x].first >> arr[x].second.first >> arr[x].second.second;
	}
	
	int q;
	cin >> q;
	
	int ans[q];
	vector<pii>que[n+5];
	
	int temp,temp2;
	
	for(int x=0;x<q;x++){
		cin >> temp >> temp2;
		que[temp2].push_back({temp,x});
	}
	
	//sweepline
	vector<int>storage[n+5];
	vector<int>storage2[n+5];
	for(int x=1;x<=n;x++){
		int a=x+arr[x].second.first;
		int b=x+arr[x].second.second+1;
		if(a<n+5)storage[a].push_back(x);
		if(b<n+5)storage2[b].push_back(x);
	}
	
	node st(0,n+5);
	
	for(int x=1;x<=n;x++){
		for(auto it:storage[x]){
			st.set(it,{arr[it].first,arr[it].first});
		}
		for(auto it:storage2[x]){
			st.set(it,{1e12,-1e12});
		}
		
		int a=max(0LL,x-arr[x].second.second);
		int b=max(0LL,x-arr[x].second.first);
		
		st.rangeUpd(a,b,arr[x].first);
		
		//for(int y=0;y<n;y++){
			//cout << st.query(y,y) << " ";
		//}
		//cout << " st" << endl;
		
		for(auto it:que[x]){
			ans[it.second]=st.query(it.first,x);
		}
	}
	
	for(int x=0;x<q;x++){
		cout << ans[x] << "\n";
	}
	
}

int32_t main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	//freopen("in.txt","r",stdin);
	//freopen("in.txt","w",stdout);
	int t=1;	        
	//cin >> t;	
	while(t--){
		solve(); 
	}
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...