답안 #128455

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
128455 2019-07-11T00:02:45 Z qkxwsm 새 집 (APIO18_new_home) C++14
57 / 100
5000 ms 427168 KB
#include <bits/stdc++.h>

using namespace std;

template<class T, class U>
void ckmin(T &a, U b)
{
	if (a > b) a = b;
}
template<class T, class U>
void ckmax(T &a, U b)
{
	if (a < b) a = b;
}

#define MP make_pair
#define PB push_back
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define FOR(i, a, b) for (auto i = (a); i < (b); i++)
#define FORD(i, a, b) for (auto i = (a) - 1; i >= (b); i--)
#define SZ(x) ((int) ((x).size()))
#define ALL(x) (x).begin(), (x).end()
#define LIM 100000007
#define INF 1000000007
#define LLINF 2696969696969696969ll
#define MAXN 300013

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef vector<pll> vpl;
typedef pair<pii, pii> ppp;
typedef pair<pii, int> ppi;

int N, K, Q, S;
ppp arr[MAXN];
ppi quer[MAXN];
int ans[MAXN];
multiset<int> pts[MAXN];
vector<array<int, 4> > segments;
vector<array<int, 4> > events;
unordered_map<ll, vi> cancer;
vpi seg[MAXN << 2];
int iter[MAXN << 2];
vi compress;

void ins(int l, int r, int t)
{
	// cerr << "ins " << l << ' ' << r << " time " << t << endl;
	l = (l + r + 1) >> 1;
	cancer[1ll * INF * l + r].PB(SZ(segments));
	segments.PB({l, r, t, SZ(events) + 1});
}
void del(int l, int r, int t)
{
	// cerr << "del " << l << ' ' << r << " time " << t << endl;
	l = (l + r + 1) >> 1;
	// cerr << "rem " << (cancer[{l, r}]).size() << endl;
	segments[cancer[1ll * INF * l + r].back()][3] = t;
	cancer[1ll * INF * l + r].pop_back();
}

void inseg(int w, int L, int R, int a, int b, pii p)
{
	if (a <= L && R <= b)
	{
		seg[w].PB(p);
		return;
	}
	int mid = (L + R) >> 1;
	if (a <= mid) inseg(w << 1, L, mid, a, b, p);
	if (mid < b) inseg(w << 1 | 1, mid + 1, R, a, b, p);
	return;
}
void build(int w, int L, int R)
{
	if (L != R)
	{
		int mid = (L + R) >> 1;
		build(w << 1, L, mid);
		build(w << 1 | 1, mid + 1, R);
	}
	FOR(i, 1, SZ(seg[w]))
	{
		ckmax(seg[w][i].se, seg[w][i - 1].se);
	}
}
int query(int w, int L, int R, int a, int x)
{
	int res = -INF;
	while(iter[w] + 1 < SZ(seg[w]) && seg[w][iter[w] + 1].fi <= x)
	{
		iter[w]++;
	}
	if (iter[w] < SZ(seg[w]) && seg[w][iter[w]].fi <= x)
	{
		res = -x + seg[w][iter[w]].se;
	}
	if (L == R) return res;
	int mid = (L + R) >> 1;
	if (a <= mid) ckmax(res, query(w << 1, L, mid, a, x));
	else ckmax(res, query(w << 1 | 1, mid + 1, R, a, x));
	return res;
}

void solve()
{
	//consider distnace functions with negative slope ONLY.
	//arr: something of type t and position p in times (t1, t2)
	//quer: query of position p at time t, qid i
	FOR(i, 0, (1 << (33 - __builtin_clz(Q + 2))) + 1)
	{
		seg[i].clear();
		iter[i] = 0;
	}
	events.clear();
	cancer.clear();
	segments.clear();
	FOR(i, 0, K) pts[i].clear();
	FOR(i, 0, N)
	{
		int t1 = arr[i].se.fi, t2 = arr[i].se.se, ty = arr[i].fi.fi, x = arr[i].fi.se;
		if (t1 == t2) continue;
		events.PB({t1, -1, ty, x});
		events.PB({t2, -2, ty, x});
	}
	FOR(i, 0, Q)
	{
		int t = quer[i].fi.se, x = quer[i].fi.fi, qid = quer[i].se;
		events.PB({t, 0, qid, x});
	}
	sort(ALL(events));
	FOR(i, 0, K)
	{
		pts[i].insert(2 * LIM);
		pts[i].insert(-2 * LIM);
		ins(-2 * LIM, 2 * LIM, 0);
	}
	FOR(i, 0, SZ(events))
	{
		int t = events[i][0], typ = events[i][2], pos = events[i][3];
		if (events[i][1] == -1)
		{
			auto it = pts[typ].UB(pos);
			int rt = *it, lt = *prev(it);
			pts[typ].insert(pos);
			ins(lt, pos, t + 1);
			ins(pos, rt, t + 1);
			del(lt, rt, t);
		}
		if (events[i][1] == -2)
		{
			pts[typ].erase(pts[typ].find(pos));
			auto it = pts[typ].UB(pos);
			int rt = *it, lt = *prev(it);
			del(lt, pos, t);
			del(pos, rt, t);
			ins(lt, rt, t + 1);
		}
	}
	sort(ALL(segments));
	FOR(i, 0, SZ(segments))
	{
		int l = segments[i][2], r = segments[i][3]; if (l > r) continue;
		inseg(1, 0, Q + 2, l, r, {segments[i][0], segments[i][1]});
	}
	build(1, 0, Q + 2);
	vector<array<int, 3> > queries;
	// FOR(i, 0, SZ(events))
	// {
	// 	if (events[i][1] == 0)
	// 	{
	// 		int t = events[i][0], qid = events[i][2], pos = events[i][3];
	// 		queries.PB({pos, t, qid});
	// 	}
	// }
	FOR(i, 0, Q)
	{
		queries.PB({quer[i].fi.fi, quer[i].fi.se, quer[i].se});
	}
	sort(ALL(queries));
	for (auto p : queries)
	{
		ckmax(ans[p[2]], query(1, 0, Q + 2, p[1] + 1, p[0]));
	}
}
bool cmp(ppi a, ppi b)
{
	return a.fi.se < b.fi.se;
}

int32_t main()
{
	if (fopen("file.in", "r"))
	{
		freopen("file.in", "r", stdin);
		freopen("file.out", "w", stdout);
	}
	ios_base::sync_with_stdio(0); cin.tie(0);
	cin >> N >> K >> Q;
	FOR(i, 0, N)
	{
		cin >> arr[i].fi.se >> arr[i].fi.fi >> arr[i].se.fi >> arr[i].se.se;
		arr[i].fi.fi--; arr[i].se.se++;
	}
	FOR(i, 0, Q)
	{
		cin >> quer[i].fi.fi >> quer[i].fi.se; quer[i].se = i;
		compress.PB(quer[i].fi.se);
	}
	compress.PB(INF);
	compress.PB(0);
	sort(ALL(compress)); compress.erase(unique(ALL(compress)), compress.end());
	FOR(i, 0, Q)
	{
		quer[i].fi.se = LB(ALL(compress), quer[i].fi.se) - compress.begin();
	}
	FOR(i, 0, N)
	{
		arr[i].se.fi = LB(ALL(compress), arr[i].se.fi) - compress.begin();
		arr[i].se.se = LB(ALL(compress), arr[i].se.se) - compress.begin();
	}
	sort(arr, arr + N);
	sort(quer, quer + Q, cmp);
	solve();
	FOR(i, 0, N)
	{
		arr[i].fi.se = 100000001 - arr[i].fi.se;
	}
	FOR(i, 0, Q)
	{
		quer[i].fi.fi = 100000001 - quer[i].fi.fi;
	}
	solve();
	FOR(i, 0, Q)
	{
		if (ans[i] >= LIM)
		{
			cout << "-1\n";
			continue;
		}
		cout << ans[i] << '\n';
	}
	// cerr << "time elapsed = " << clock() / ((CLOCKS_PER_SEC) / 1000) << " ms" << endl;
	return 0;
}

Compilation message

new_home.cpp: In function 'int32_t main()':
new_home.cpp:203:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   freopen("file.in", "r", stdin);
   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
new_home.cpp:204:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   freopen("file.out", "w", stdout);
   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 40 ms 42616 KB Output is correct
2 Correct 40 ms 42616 KB Output is correct
3 Correct 41 ms 42616 KB Output is correct
4 Correct 40 ms 42588 KB Output is correct
5 Correct 42 ms 42744 KB Output is correct
6 Correct 42 ms 42872 KB Output is correct
7 Correct 43 ms 42948 KB Output is correct
8 Correct 43 ms 42872 KB Output is correct
9 Correct 43 ms 43000 KB Output is correct
10 Correct 43 ms 43000 KB Output is correct
11 Correct 43 ms 42872 KB Output is correct
12 Correct 43 ms 42872 KB Output is correct
13 Correct 42 ms 42744 KB Output is correct
14 Correct 42 ms 42744 KB Output is correct
15 Correct 44 ms 43000 KB Output is correct
16 Correct 43 ms 43000 KB Output is correct
17 Correct 43 ms 42872 KB Output is correct
18 Correct 44 ms 42876 KB Output is correct
19 Correct 43 ms 43000 KB Output is correct
20 Correct 42 ms 42872 KB Output is correct
21 Correct 42 ms 42872 KB Output is correct
22 Correct 42 ms 43000 KB Output is correct
23 Correct 43 ms 42872 KB Output is correct
24 Correct 43 ms 42872 KB Output is correct
25 Correct 43 ms 43000 KB Output is correct
26 Correct 42 ms 42872 KB Output is correct
27 Correct 46 ms 42744 KB Output is correct
28 Correct 42 ms 42872 KB Output is correct
29 Correct 42 ms 42872 KB Output is correct
30 Correct 48 ms 42748 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 40 ms 42616 KB Output is correct
2 Correct 40 ms 42616 KB Output is correct
3 Correct 41 ms 42616 KB Output is correct
4 Correct 40 ms 42588 KB Output is correct
5 Correct 42 ms 42744 KB Output is correct
6 Correct 42 ms 42872 KB Output is correct
7 Correct 43 ms 42948 KB Output is correct
8 Correct 43 ms 42872 KB Output is correct
9 Correct 43 ms 43000 KB Output is correct
10 Correct 43 ms 43000 KB Output is correct
11 Correct 43 ms 42872 KB Output is correct
12 Correct 43 ms 42872 KB Output is correct
13 Correct 42 ms 42744 KB Output is correct
14 Correct 42 ms 42744 KB Output is correct
15 Correct 44 ms 43000 KB Output is correct
16 Correct 43 ms 43000 KB Output is correct
17 Correct 43 ms 42872 KB Output is correct
18 Correct 44 ms 42876 KB Output is correct
19 Correct 43 ms 43000 KB Output is correct
20 Correct 42 ms 42872 KB Output is correct
21 Correct 42 ms 42872 KB Output is correct
22 Correct 42 ms 43000 KB Output is correct
23 Correct 43 ms 42872 KB Output is correct
24 Correct 43 ms 42872 KB Output is correct
25 Correct 43 ms 43000 KB Output is correct
26 Correct 42 ms 42872 KB Output is correct
27 Correct 46 ms 42744 KB Output is correct
28 Correct 42 ms 42872 KB Output is correct
29 Correct 42 ms 42872 KB Output is correct
30 Correct 48 ms 42748 KB Output is correct
31 Correct 1229 ms 92796 KB Output is correct
32 Correct 318 ms 55540 KB Output is correct
33 Correct 1173 ms 91236 KB Output is correct
34 Correct 1143 ms 91460 KB Output is correct
35 Correct 1216 ms 92648 KB Output is correct
36 Correct 1265 ms 92640 KB Output is correct
37 Correct 881 ms 87008 KB Output is correct
38 Correct 894 ms 86840 KB Output is correct
39 Correct 735 ms 80320 KB Output is correct
40 Correct 766 ms 81692 KB Output is correct
41 Correct 966 ms 86164 KB Output is correct
42 Correct 954 ms 86372 KB Output is correct
43 Correct 284 ms 57184 KB Output is correct
44 Correct 918 ms 85712 KB Output is correct
45 Correct 881 ms 82120 KB Output is correct
46 Correct 723 ms 76044 KB Output is correct
47 Correct 582 ms 74308 KB Output is correct
48 Correct 563 ms 72644 KB Output is correct
49 Correct 666 ms 77516 KB Output is correct
50 Correct 776 ms 84340 KB Output is correct
51 Correct 676 ms 75816 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4008 ms 292888 KB Output is correct
2 Correct 4167 ms 269228 KB Output is correct
3 Correct 4051 ms 421536 KB Output is correct
4 Correct 3956 ms 283196 KB Output is correct
5 Correct 4054 ms 264616 KB Output is correct
6 Correct 4137 ms 269868 KB Output is correct
7 Correct 4083 ms 427168 KB Output is correct
8 Correct 4152 ms 297220 KB Output is correct
9 Correct 3860 ms 278436 KB Output is correct
10 Correct 3846 ms 270796 KB Output is correct
11 Correct 3797 ms 265252 KB Output is correct
12 Correct 3591 ms 270656 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 5081 ms 297344 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 40 ms 42616 KB Output is correct
2 Correct 40 ms 42616 KB Output is correct
3 Correct 41 ms 42616 KB Output is correct
4 Correct 40 ms 42588 KB Output is correct
5 Correct 42 ms 42744 KB Output is correct
6 Correct 42 ms 42872 KB Output is correct
7 Correct 43 ms 42948 KB Output is correct
8 Correct 43 ms 42872 KB Output is correct
9 Correct 43 ms 43000 KB Output is correct
10 Correct 43 ms 43000 KB Output is correct
11 Correct 43 ms 42872 KB Output is correct
12 Correct 43 ms 42872 KB Output is correct
13 Correct 42 ms 42744 KB Output is correct
14 Correct 42 ms 42744 KB Output is correct
15 Correct 44 ms 43000 KB Output is correct
16 Correct 43 ms 43000 KB Output is correct
17 Correct 43 ms 42872 KB Output is correct
18 Correct 44 ms 42876 KB Output is correct
19 Correct 43 ms 43000 KB Output is correct
20 Correct 42 ms 42872 KB Output is correct
21 Correct 42 ms 42872 KB Output is correct
22 Correct 42 ms 43000 KB Output is correct
23 Correct 43 ms 42872 KB Output is correct
24 Correct 43 ms 42872 KB Output is correct
25 Correct 43 ms 43000 KB Output is correct
26 Correct 42 ms 42872 KB Output is correct
27 Correct 46 ms 42744 KB Output is correct
28 Correct 42 ms 42872 KB Output is correct
29 Correct 42 ms 42872 KB Output is correct
30 Correct 48 ms 42748 KB Output is correct
31 Correct 1229 ms 92796 KB Output is correct
32 Correct 318 ms 55540 KB Output is correct
33 Correct 1173 ms 91236 KB Output is correct
34 Correct 1143 ms 91460 KB Output is correct
35 Correct 1216 ms 92648 KB Output is correct
36 Correct 1265 ms 92640 KB Output is correct
37 Correct 881 ms 87008 KB Output is correct
38 Correct 894 ms 86840 KB Output is correct
39 Correct 735 ms 80320 KB Output is correct
40 Correct 766 ms 81692 KB Output is correct
41 Correct 966 ms 86164 KB Output is correct
42 Correct 954 ms 86372 KB Output is correct
43 Correct 284 ms 57184 KB Output is correct
44 Correct 918 ms 85712 KB Output is correct
45 Correct 881 ms 82120 KB Output is correct
46 Correct 723 ms 76044 KB Output is correct
47 Correct 582 ms 74308 KB Output is correct
48 Correct 563 ms 72644 KB Output is correct
49 Correct 666 ms 77516 KB Output is correct
50 Correct 776 ms 84340 KB Output is correct
51 Correct 676 ms 75816 KB Output is correct
52 Correct 1258 ms 101196 KB Output is correct
53 Correct 952 ms 92252 KB Output is correct
54 Correct 1168 ms 95212 KB Output is correct
55 Correct 930 ms 92612 KB Output is correct
56 Correct 979 ms 94900 KB Output is correct
57 Correct 981 ms 87420 KB Output is correct
58 Correct 1022 ms 91280 KB Output is correct
59 Correct 1036 ms 94180 KB Output is correct
60 Correct 1042 ms 87776 KB Output is correct
61 Correct 373 ms 70500 KB Output is correct
62 Correct 1026 ms 102660 KB Output is correct
63 Correct 1173 ms 97668 KB Output is correct
64 Correct 1186 ms 96548 KB Output is correct
65 Correct 1193 ms 93900 KB Output is correct
66 Correct 1135 ms 88368 KB Output is correct
67 Correct 387 ms 57952 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 40 ms 42616 KB Output is correct
2 Correct 40 ms 42616 KB Output is correct
3 Correct 41 ms 42616 KB Output is correct
4 Correct 40 ms 42588 KB Output is correct
5 Correct 42 ms 42744 KB Output is correct
6 Correct 42 ms 42872 KB Output is correct
7 Correct 43 ms 42948 KB Output is correct
8 Correct 43 ms 42872 KB Output is correct
9 Correct 43 ms 43000 KB Output is correct
10 Correct 43 ms 43000 KB Output is correct
11 Correct 43 ms 42872 KB Output is correct
12 Correct 43 ms 42872 KB Output is correct
13 Correct 42 ms 42744 KB Output is correct
14 Correct 42 ms 42744 KB Output is correct
15 Correct 44 ms 43000 KB Output is correct
16 Correct 43 ms 43000 KB Output is correct
17 Correct 43 ms 42872 KB Output is correct
18 Correct 44 ms 42876 KB Output is correct
19 Correct 43 ms 43000 KB Output is correct
20 Correct 42 ms 42872 KB Output is correct
21 Correct 42 ms 42872 KB Output is correct
22 Correct 42 ms 43000 KB Output is correct
23 Correct 43 ms 42872 KB Output is correct
24 Correct 43 ms 42872 KB Output is correct
25 Correct 43 ms 43000 KB Output is correct
26 Correct 42 ms 42872 KB Output is correct
27 Correct 46 ms 42744 KB Output is correct
28 Correct 42 ms 42872 KB Output is correct
29 Correct 42 ms 42872 KB Output is correct
30 Correct 48 ms 42748 KB Output is correct
31 Correct 1229 ms 92796 KB Output is correct
32 Correct 318 ms 55540 KB Output is correct
33 Correct 1173 ms 91236 KB Output is correct
34 Correct 1143 ms 91460 KB Output is correct
35 Correct 1216 ms 92648 KB Output is correct
36 Correct 1265 ms 92640 KB Output is correct
37 Correct 881 ms 87008 KB Output is correct
38 Correct 894 ms 86840 KB Output is correct
39 Correct 735 ms 80320 KB Output is correct
40 Correct 766 ms 81692 KB Output is correct
41 Correct 966 ms 86164 KB Output is correct
42 Correct 954 ms 86372 KB Output is correct
43 Correct 284 ms 57184 KB Output is correct
44 Correct 918 ms 85712 KB Output is correct
45 Correct 881 ms 82120 KB Output is correct
46 Correct 723 ms 76044 KB Output is correct
47 Correct 582 ms 74308 KB Output is correct
48 Correct 563 ms 72644 KB Output is correct
49 Correct 666 ms 77516 KB Output is correct
50 Correct 776 ms 84340 KB Output is correct
51 Correct 676 ms 75816 KB Output is correct
52 Correct 4008 ms 292888 KB Output is correct
53 Correct 4167 ms 269228 KB Output is correct
54 Correct 4051 ms 421536 KB Output is correct
55 Correct 3956 ms 283196 KB Output is correct
56 Correct 4054 ms 264616 KB Output is correct
57 Correct 4137 ms 269868 KB Output is correct
58 Correct 4083 ms 427168 KB Output is correct
59 Correct 4152 ms 297220 KB Output is correct
60 Correct 3860 ms 278436 KB Output is correct
61 Correct 3846 ms 270796 KB Output is correct
62 Correct 3797 ms 265252 KB Output is correct
63 Correct 3591 ms 270656 KB Output is correct
64 Execution timed out 5081 ms 297344 KB Time limit exceeded
65 Halted 0 ms 0 KB -