답안 #240076

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
240076 2020-06-17T22:58:34 Z alishahali1382 새 집 (APIO18_new_home) C++14
47 / 100
5000 ms 350828 KB
#include <bits/stdc++.h>
#pragma GCC optimize ("O2,unroll-loops")
//#pragma GCC optimize("no-stack-protector,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
typedef pair<pii, pii> pi4;
#define debug(x) cerr<<#x<<'='<<(x)<<endl;
#define debugp(x) cerr<<#x<<"= {"<<(x.first)<<", "<<(x.second)<<"}"<<endl;
#define debug2(x, y) cerr<<"{"<<#x<<", "<<#y<<"} = {"<<(x)<<", "<<(y)<<"}"<<endl;
#define debugv(v) {cerr<<#v<<" : ";for (auto x:v) cerr<<x<<' ';cerr<<endl;}
#define all(x) x.begin(), x.end()
#define pb push_back
#define kill(x) return cout<<x<<'\n', 0;

const ld eps=1e-7;
const int inf=1000000010;
const ll INF=10000000000000010LL;
const int mod=1000000007;
const int MAXN=300020, LOG=20;

int Todo;
pair<int*, int> todo[50000000];
struct SEGMENT{
	int seg[MAXN<<1];
	SEGMENT(){
		memset(seg, -31, sizeof(seg));
	}
	int upd(int pos, int val){
		if (seg[pos]>=val) return 0;
		todo[Todo++]={seg+pos, seg[pos]};
		seg[pos]=val;
		return 1;
	}
	int Maximize(int l, int r, int val){
		int res=0;
		for (l+=MAXN, r+=MAXN; l<r; l>>=1, r>>=1){
			if (l&1) res+=upd(l++, val);
			if (r&1) res+=upd(--r, val);
		}
		return res;
	}
	int Get(int pos){
		int res=seg[0];
		for (pos+=MAXN; pos; pos>>=1) res=max(res, seg[pos]);
		return res;
	}
} seg1, seg2;

inline void undo(){
	Todo--;
	(*todo[Todo].first)=todo[Todo].second;
}

int n, m, k, u, v, x, y, t, a, b, timer;
int typ[MAXN], X[MAXN], T[MAXN], A[MAXN], B[MAXN], L[MAXN], Y[MAXN];
int ans[MAXN];
vector<int> Q1[MAXN], Q2[MAXN], Q[MAXN];
vector<pi4> seg[MAXN<<2];
vector<int> compt={0, inf}, compx={0, inf+1};
multiset<int> st[MAXN];
map<pi4, int> mp;

void Add(int id, int tl, int tr, int l, int r, pi4 p){
//	if (id==1) cerr<<"event in times:"<<l<<","<<r<<" :  ["<<p.first.first<<", "<<p.first.second<<") "<<p.second.first<<' '<<p.second.second<<endl;
	if (r<=tl || tr<=l) return ;
	if (l<=tl && tr<=r){
		seg[id].pb(p);
		return ;
	}
	int mid=(tl+tr)>>1;
	Add(id<<1, tl, mid, l, r, p);
	Add(id<<1 | 1, mid, tr, l, r, p);
}

inline void f(pi4 p){
	if (!mp.count(p)) mp[p]=timer;
	else{
		Add(1, 0, MAXN, mp[p], timer, p);
		mp.erase(p);
	}
}

inline void add(multiset<int> &st, int typ, int x){
//	cerr<<"add in time "<<timer<<" : "<<typ<<", "<<x<<endl;
	if (st.count(x)){
		st.insert(x);
		return ;
	}
	st.insert(x);
	auto it=st.find(x);
	int prv=*--it, nxt=*++++it;
	f({{prv, (prv+nxt+1)/2}, {typ, 1}});
	f({{(prv+nxt+1)/2, nxt+1}, {typ, 2}});
	f({{prv, (prv+x+1)/2}, {typ, 1}});
	f({{(prv+x+1)/2, x+1}, {typ, 2}});
	f({{x, (x+nxt+1)/2}, {typ, 1}});
	f({{(x+nxt+1)/2, nxt+1}, {typ, 2}});
}

inline void rem(multiset<int> &st, int typ, int x){
//	cerr<<"rem in time "<<timer<<" : "<<typ<<", "<<x<<endl;
	st.erase(st.find(x));
	if (st.count(x)) return ;
	auto it=st.lower_bound(x);
	int nxt=*it, prv=*--it;
	f({{prv, (prv+nxt+1)/2}, {typ, 1}});
	f({{(prv+nxt+1)/2, nxt+1}, {typ, 2}});
	f({{prv, (prv+x+1)/2}, {typ, 1}});
	f({{(prv+x+1)/2, x+1}, {typ, 2}});
	f({{x, (x+nxt+1)/2}, {typ, 1}});
	f({{(x+nxt+1)/2, nxt+1}, {typ, 2}});
}

inline int gt(int x){
	return lower_bound(all(compx), x)-compx.begin();
}

void dfs(int id, int tl, int tr){
	int ted=0;
	for (pi4 p:seg[id]){
		if (p.second.second==1) ted+=seg1.Maximize(gt(p.first.first), gt(p.first.second), -p.first.first);
		if (p.second.second==2) ted+=seg2.Maximize(gt(p.first.first), gt(p.first.second), p.first.second-1);
	}
	if (tr-tl==1){
		for (int i:Q[tl]){
			ans[i]=max(ans[i], L[i]+seg1.Get(gt(L[i])));
//			debug(ans[i])
			ans[i]=max(ans[i], seg2.Get(gt(L[i]))-L[i]);
			if (ans[i]>100000000) ans[i]=-1;
		}
	}
	else{
		int mid=(tl+tr)>>1;
		dfs(id<<1, tl, mid);
		dfs(id<<1 | 1, mid, tr);
	}
	while (ted--) undo();
}

int main(){
	ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	cin>>n>>k>>m;
	for (int i=1; i<=k; i++){
		st[i].insert(-inf);
		st[i].insert(inf);
		f({{0, inf+1}, {i, 2}});
		f({{-inf, 0}, {i, 1}});
	}
	for (int i=1; i<=n; i++) cin>>X[i]>>T[i]>>A[i]>>B[i], B[i]++;
	for (int i=1; i<=m; i++){
		cin>>L[i]>>Y[i];
		compx.pb(L[i]);
		compt.pb(Y[i]);
	}
	sort(all(compx));
	sort(all(compt));
	compx.resize(unique(all(compx))-compx.begin());
	compt.resize(unique(all(compt))-compt.begin());
//	debugv(compx)
	for (int i=1; i<=n; i++){
		A[i]=lower_bound(all(compt), A[i])-compt.begin();
		B[i]=lower_bound(all(compt), B[i])-compt.begin();
//		debug2(A[i], B[i])
		Q1[A[i]].pb(i);
		Q2[B[i]].pb(i);
	}
	for (int i=1; i<=m; i++){
		Y[i]=lower_bound(all(compt), Y[i])-compt.begin();
		Q[Y[i]].pb(i);
	}
	for (timer=0; timer<MAXN; timer++){
		for (int i:Q1[timer]) add(st[T[i]], T[i], X[i]);
		for (int i:Q2[timer]) rem(st[T[i]], T[i], X[i]);
	}
	while (mp.size()) f(mp.begin()->first);
	dfs(1, 0, MAXN);
	for (int i=1; i<=m; i++) cout<<ans[i]<<'\n';
	
	return 0;
}
/*
4 2 4
3 1 1 10
9 2 2 4
7 2 5 7
4 1 8 10
5 3
5 6
5 9
1 10

2 1 3
1 1 1 4
1 1 2 6
1 3
1 5
1 7

1 1 1
100000000 1 1 1
1 1


*/
# 결과 실행 시간 메모리 Grader output
1 Correct 49 ms 68472 KB Output is correct
2 Correct 49 ms 68472 KB Output is correct
3 Correct 54 ms 68472 KB Output is correct
4 Correct 50 ms 68472 KB Output is correct
5 Correct 50 ms 68608 KB Output is correct
6 Correct 55 ms 68736 KB Output is correct
7 Correct 60 ms 69240 KB Output is correct
8 Correct 55 ms 68984 KB Output is correct
9 Correct 55 ms 69240 KB Output is correct
10 Correct 55 ms 68812 KB Output is correct
11 Correct 54 ms 68736 KB Output is correct
12 Correct 58 ms 68600 KB Output is correct
13 Correct 53 ms 68600 KB Output is correct
14 Correct 52 ms 68608 KB Output is correct
15 Correct 53 ms 68984 KB Output is correct
16 Correct 54 ms 68984 KB Output is correct
17 Correct 53 ms 68856 KB Output is correct
18 Correct 54 ms 68992 KB Output is correct
19 Correct 55 ms 68984 KB Output is correct
20 Correct 54 ms 68856 KB Output is correct
21 Correct 53 ms 68984 KB Output is correct
22 Correct 55 ms 69216 KB Output is correct
23 Correct 56 ms 69120 KB Output is correct
24 Correct 59 ms 69048 KB Output is correct
25 Correct 54 ms 68856 KB Output is correct
26 Correct 51 ms 68608 KB Output is correct
27 Correct 52 ms 68856 KB Output is correct
28 Correct 55 ms 68724 KB Output is correct
29 Correct 56 ms 68728 KB Output is correct
30 Correct 53 ms 68480 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 49 ms 68472 KB Output is correct
2 Correct 49 ms 68472 KB Output is correct
3 Correct 54 ms 68472 KB Output is correct
4 Correct 50 ms 68472 KB Output is correct
5 Correct 50 ms 68608 KB Output is correct
6 Correct 55 ms 68736 KB Output is correct
7 Correct 60 ms 69240 KB Output is correct
8 Correct 55 ms 68984 KB Output is correct
9 Correct 55 ms 69240 KB Output is correct
10 Correct 55 ms 68812 KB Output is correct
11 Correct 54 ms 68736 KB Output is correct
12 Correct 58 ms 68600 KB Output is correct
13 Correct 53 ms 68600 KB Output is correct
14 Correct 52 ms 68608 KB Output is correct
15 Correct 53 ms 68984 KB Output is correct
16 Correct 54 ms 68984 KB Output is correct
17 Correct 53 ms 68856 KB Output is correct
18 Correct 54 ms 68992 KB Output is correct
19 Correct 55 ms 68984 KB Output is correct
20 Correct 54 ms 68856 KB Output is correct
21 Correct 53 ms 68984 KB Output is correct
22 Correct 55 ms 69216 KB Output is correct
23 Correct 56 ms 69120 KB Output is correct
24 Correct 59 ms 69048 KB Output is correct
25 Correct 54 ms 68856 KB Output is correct
26 Correct 51 ms 68608 KB Output is correct
27 Correct 52 ms 68856 KB Output is correct
28 Correct 55 ms 68724 KB Output is correct
29 Correct 56 ms 68728 KB Output is correct
30 Correct 53 ms 68480 KB Output is correct
31 Correct 2763 ms 176160 KB Output is correct
32 Correct 532 ms 72944 KB Output is correct
33 Correct 2140 ms 161908 KB Output is correct
34 Correct 2480 ms 163832 KB Output is correct
35 Correct 2656 ms 175664 KB Output is correct
36 Correct 2325 ms 174232 KB Output is correct
37 Correct 1670 ms 143852 KB Output is correct
38 Correct 1480 ms 143468 KB Output is correct
39 Correct 947 ms 119276 KB Output is correct
40 Correct 1079 ms 125112 KB Output is correct
41 Correct 1548 ms 134696 KB Output is correct
42 Correct 1535 ms 136312 KB Output is correct
43 Correct 202 ms 74784 KB Output is correct
44 Correct 1402 ms 133224 KB Output is correct
45 Correct 1116 ms 118592 KB Output is correct
46 Correct 777 ms 95480 KB Output is correct
47 Correct 579 ms 96244 KB Output is correct
48 Correct 512 ms 90508 KB Output is correct
49 Correct 739 ms 104796 KB Output is correct
50 Correct 1158 ms 131060 KB Output is correct
51 Correct 665 ms 97884 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 5072 ms 350828 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 5069 ms 332472 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 49 ms 68472 KB Output is correct
2 Correct 49 ms 68472 KB Output is correct
3 Correct 54 ms 68472 KB Output is correct
4 Correct 50 ms 68472 KB Output is correct
5 Correct 50 ms 68608 KB Output is correct
6 Correct 55 ms 68736 KB Output is correct
7 Correct 60 ms 69240 KB Output is correct
8 Correct 55 ms 68984 KB Output is correct
9 Correct 55 ms 69240 KB Output is correct
10 Correct 55 ms 68812 KB Output is correct
11 Correct 54 ms 68736 KB Output is correct
12 Correct 58 ms 68600 KB Output is correct
13 Correct 53 ms 68600 KB Output is correct
14 Correct 52 ms 68608 KB Output is correct
15 Correct 53 ms 68984 KB Output is correct
16 Correct 54 ms 68984 KB Output is correct
17 Correct 53 ms 68856 KB Output is correct
18 Correct 54 ms 68992 KB Output is correct
19 Correct 55 ms 68984 KB Output is correct
20 Correct 54 ms 68856 KB Output is correct
21 Correct 53 ms 68984 KB Output is correct
22 Correct 55 ms 69216 KB Output is correct
23 Correct 56 ms 69120 KB Output is correct
24 Correct 59 ms 69048 KB Output is correct
25 Correct 54 ms 68856 KB Output is correct
26 Correct 51 ms 68608 KB Output is correct
27 Correct 52 ms 68856 KB Output is correct
28 Correct 55 ms 68724 KB Output is correct
29 Correct 56 ms 68728 KB Output is correct
30 Correct 53 ms 68480 KB Output is correct
31 Correct 2763 ms 176160 KB Output is correct
32 Correct 532 ms 72944 KB Output is correct
33 Correct 2140 ms 161908 KB Output is correct
34 Correct 2480 ms 163832 KB Output is correct
35 Correct 2656 ms 175664 KB Output is correct
36 Correct 2325 ms 174232 KB Output is correct
37 Correct 1670 ms 143852 KB Output is correct
38 Correct 1480 ms 143468 KB Output is correct
39 Correct 947 ms 119276 KB Output is correct
40 Correct 1079 ms 125112 KB Output is correct
41 Correct 1548 ms 134696 KB Output is correct
42 Correct 1535 ms 136312 KB Output is correct
43 Correct 202 ms 74784 KB Output is correct
44 Correct 1402 ms 133224 KB Output is correct
45 Correct 1116 ms 118592 KB Output is correct
46 Correct 777 ms 95480 KB Output is correct
47 Correct 579 ms 96244 KB Output is correct
48 Correct 512 ms 90508 KB Output is correct
49 Correct 739 ms 104796 KB Output is correct
50 Correct 1158 ms 131060 KB Output is correct
51 Correct 665 ms 97884 KB Output is correct
52 Correct 2048 ms 211836 KB Output is correct
53 Correct 2130 ms 196584 KB Output is correct
54 Correct 2492 ms 191612 KB Output is correct
55 Correct 1654 ms 163148 KB Output is correct
56 Correct 1716 ms 174268 KB Output is correct
57 Correct 1586 ms 143736 KB Output is correct
58 Correct 1829 ms 163792 KB Output is correct
59 Correct 2011 ms 175868 KB Output is correct
60 Correct 1668 ms 145184 KB Output is correct
61 Correct 739 ms 139748 KB Output is correct
62 Correct 1964 ms 206320 KB Output is correct
63 Correct 2366 ms 195544 KB Output is correct
64 Correct 2409 ms 186552 KB Output is correct
65 Correct 2376 ms 168436 KB Output is correct
66 Correct 1705 ms 143212 KB Output is correct
67 Correct 815 ms 92212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 49 ms 68472 KB Output is correct
2 Correct 49 ms 68472 KB Output is correct
3 Correct 54 ms 68472 KB Output is correct
4 Correct 50 ms 68472 KB Output is correct
5 Correct 50 ms 68608 KB Output is correct
6 Correct 55 ms 68736 KB Output is correct
7 Correct 60 ms 69240 KB Output is correct
8 Correct 55 ms 68984 KB Output is correct
9 Correct 55 ms 69240 KB Output is correct
10 Correct 55 ms 68812 KB Output is correct
11 Correct 54 ms 68736 KB Output is correct
12 Correct 58 ms 68600 KB Output is correct
13 Correct 53 ms 68600 KB Output is correct
14 Correct 52 ms 68608 KB Output is correct
15 Correct 53 ms 68984 KB Output is correct
16 Correct 54 ms 68984 KB Output is correct
17 Correct 53 ms 68856 KB Output is correct
18 Correct 54 ms 68992 KB Output is correct
19 Correct 55 ms 68984 KB Output is correct
20 Correct 54 ms 68856 KB Output is correct
21 Correct 53 ms 68984 KB Output is correct
22 Correct 55 ms 69216 KB Output is correct
23 Correct 56 ms 69120 KB Output is correct
24 Correct 59 ms 69048 KB Output is correct
25 Correct 54 ms 68856 KB Output is correct
26 Correct 51 ms 68608 KB Output is correct
27 Correct 52 ms 68856 KB Output is correct
28 Correct 55 ms 68724 KB Output is correct
29 Correct 56 ms 68728 KB Output is correct
30 Correct 53 ms 68480 KB Output is correct
31 Correct 2763 ms 176160 KB Output is correct
32 Correct 532 ms 72944 KB Output is correct
33 Correct 2140 ms 161908 KB Output is correct
34 Correct 2480 ms 163832 KB Output is correct
35 Correct 2656 ms 175664 KB Output is correct
36 Correct 2325 ms 174232 KB Output is correct
37 Correct 1670 ms 143852 KB Output is correct
38 Correct 1480 ms 143468 KB Output is correct
39 Correct 947 ms 119276 KB Output is correct
40 Correct 1079 ms 125112 KB Output is correct
41 Correct 1548 ms 134696 KB Output is correct
42 Correct 1535 ms 136312 KB Output is correct
43 Correct 202 ms 74784 KB Output is correct
44 Correct 1402 ms 133224 KB Output is correct
45 Correct 1116 ms 118592 KB Output is correct
46 Correct 777 ms 95480 KB Output is correct
47 Correct 579 ms 96244 KB Output is correct
48 Correct 512 ms 90508 KB Output is correct
49 Correct 739 ms 104796 KB Output is correct
50 Correct 1158 ms 131060 KB Output is correct
51 Correct 665 ms 97884 KB Output is correct
52 Execution timed out 5072 ms 350828 KB Time limit exceeded
53 Halted 0 ms 0 KB -