Submission #128468

# Submission time Handle Problem Language Result Execution time Memory
128468 2019-07-11T01:05:21 Z qkxwsm New Home (APIO18_new_home) C++14
57 / 100
5000 ms 387856 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;

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;
}

struct custom_hash
{
	template<class T>
	unsigned long long operator()(T v) const
	{
		unsigned long long x = v;
		x = (x ^ (x >> 27)) * 10723151780598845931ull;
		return x ^ (x >> 31);
	}
};
template<class T, class U> using hash_table = gp_hash_table<T, U, custom_hash>;

#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;
hash_table<ll, vi> cancer;
vpi seg[MAXN << 2];
int iter[MAXN << 2];
vi compress;

void ins(int l, int r, int t)
{
	l = (l + r + 1) >> 1;
	cancer[1ll * INF * l + r].PB(SZ(segments));
	segments.PB({l, r, t, Q + 2});
}
void del(int l, int r, int t)
{
	l = (l + r + 1) >> 1;
	auto it = cancer.find(1ll * INF * l + r);
	segments[it -> se.back()][3] = t;
	it -> se.pop_back();
}

void inseg(int w, int L, int R, int a, int b, pii p)
{
	if (a <= L && R <= b)
	{
		if (seg[w].empty() || p.se > seg[w].back().se) 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;
}
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()
{
	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});
	}
	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]});
	}
	FOR(i, 0, Q)
	{
		ckmax(ans[quer[i].se], query(1, 0, Q + 2, quer[i].fi.se + 1, quer[i].fi.fi));
	}
}
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);
	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;
	}
	reverse(quer, quer + Q);
	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:180: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:181: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);
   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 41 ms 42616 KB Output is correct
2 Correct 40 ms 42616 KB Output is correct
3 Correct 41 ms 42588 KB Output is correct
4 Correct 41 ms 42616 KB Output is correct
5 Correct 43 ms 42744 KB Output is correct
6 Correct 44 ms 43208 KB Output is correct
7 Correct 44 ms 43000 KB Output is correct
8 Correct 43 ms 42972 KB Output is correct
9 Correct 44 ms 42996 KB Output is correct
10 Correct 44 ms 43208 KB Output is correct
11 Correct 43 ms 42872 KB Output is correct
12 Correct 43 ms 42872 KB Output is correct
13 Correct 53 ms 42872 KB Output is correct
14 Correct 42 ms 42872 KB Output is correct
15 Correct 51 ms 43088 KB Output is correct
16 Correct 44 ms 43000 KB Output is correct
17 Correct 44 ms 43180 KB Output is correct
18 Correct 44 ms 43208 KB Output is correct
19 Correct 43 ms 42972 KB Output is correct
20 Correct 43 ms 43108 KB Output is correct
21 Correct 42 ms 42744 KB Output is correct
22 Correct 52 ms 43000 KB Output is correct
23 Correct 44 ms 42972 KB Output is correct
24 Correct 45 ms 43204 KB Output is correct
25 Correct 44 ms 43180 KB Output is correct
26 Correct 85 ms 43128 KB Output is correct
27 Correct 52 ms 42872 KB Output is correct
28 Correct 54 ms 43052 KB Output is correct
29 Correct 44 ms 42904 KB Output is correct
30 Correct 52 ms 42872 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 41 ms 42616 KB Output is correct
2 Correct 40 ms 42616 KB Output is correct
3 Correct 41 ms 42588 KB Output is correct
4 Correct 41 ms 42616 KB Output is correct
5 Correct 43 ms 42744 KB Output is correct
6 Correct 44 ms 43208 KB Output is correct
7 Correct 44 ms 43000 KB Output is correct
8 Correct 43 ms 42972 KB Output is correct
9 Correct 44 ms 42996 KB Output is correct
10 Correct 44 ms 43208 KB Output is correct
11 Correct 43 ms 42872 KB Output is correct
12 Correct 43 ms 42872 KB Output is correct
13 Correct 53 ms 42872 KB Output is correct
14 Correct 42 ms 42872 KB Output is correct
15 Correct 51 ms 43088 KB Output is correct
16 Correct 44 ms 43000 KB Output is correct
17 Correct 44 ms 43180 KB Output is correct
18 Correct 44 ms 43208 KB Output is correct
19 Correct 43 ms 42972 KB Output is correct
20 Correct 43 ms 43108 KB Output is correct
21 Correct 42 ms 42744 KB Output is correct
22 Correct 52 ms 43000 KB Output is correct
23 Correct 44 ms 42972 KB Output is correct
24 Correct 45 ms 43204 KB Output is correct
25 Correct 44 ms 43180 KB Output is correct
26 Correct 85 ms 43128 KB Output is correct
27 Correct 52 ms 42872 KB Output is correct
28 Correct 54 ms 43052 KB Output is correct
29 Correct 44 ms 42904 KB Output is correct
30 Correct 52 ms 42872 KB Output is correct
31 Correct 1266 ms 113776 KB Output is correct
32 Correct 256 ms 53296 KB Output is correct
33 Correct 1073 ms 119688 KB Output is correct
34 Correct 974 ms 110484 KB Output is correct
35 Correct 1160 ms 116548 KB Output is correct
36 Correct 1180 ms 122080 KB Output is correct
37 Correct 820 ms 113284 KB Output is correct
38 Correct 826 ms 115848 KB Output is correct
39 Correct 669 ms 108792 KB Output is correct
40 Correct 705 ms 110856 KB Output is correct
41 Correct 788 ms 106604 KB Output is correct
42 Correct 798 ms 106652 KB Output is correct
43 Correct 227 ms 54120 KB Output is correct
44 Correct 757 ms 106452 KB Output is correct
45 Correct 758 ms 105452 KB Output is correct
46 Correct 658 ms 103408 KB Output is correct
47 Correct 549 ms 102020 KB Output is correct
48 Correct 527 ms 101412 KB Output is correct
49 Correct 658 ms 103816 KB Output is correct
50 Correct 658 ms 105480 KB Output is correct
51 Correct 605 ms 103048 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3576 ms 284272 KB Output is correct
2 Correct 3335 ms 272964 KB Output is correct
3 Correct 3979 ms 381480 KB Output is correct
4 Correct 3599 ms 298668 KB Output is correct
5 Correct 3648 ms 332292 KB Output is correct
6 Correct 3400 ms 288288 KB Output is correct
7 Correct 4027 ms 387856 KB Output is correct
8 Correct 3682 ms 303724 KB Output is correct
9 Correct 3416 ms 269360 KB Output is correct
10 Correct 3336 ms 259348 KB Output is correct
11 Correct 3127 ms 264312 KB Output is correct
12 Correct 3101 ms 259548 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 5102 ms 281756 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 41 ms 42616 KB Output is correct
2 Correct 40 ms 42616 KB Output is correct
3 Correct 41 ms 42588 KB Output is correct
4 Correct 41 ms 42616 KB Output is correct
5 Correct 43 ms 42744 KB Output is correct
6 Correct 44 ms 43208 KB Output is correct
7 Correct 44 ms 43000 KB Output is correct
8 Correct 43 ms 42972 KB Output is correct
9 Correct 44 ms 42996 KB Output is correct
10 Correct 44 ms 43208 KB Output is correct
11 Correct 43 ms 42872 KB Output is correct
12 Correct 43 ms 42872 KB Output is correct
13 Correct 53 ms 42872 KB Output is correct
14 Correct 42 ms 42872 KB Output is correct
15 Correct 51 ms 43088 KB Output is correct
16 Correct 44 ms 43000 KB Output is correct
17 Correct 44 ms 43180 KB Output is correct
18 Correct 44 ms 43208 KB Output is correct
19 Correct 43 ms 42972 KB Output is correct
20 Correct 43 ms 43108 KB Output is correct
21 Correct 42 ms 42744 KB Output is correct
22 Correct 52 ms 43000 KB Output is correct
23 Correct 44 ms 42972 KB Output is correct
24 Correct 45 ms 43204 KB Output is correct
25 Correct 44 ms 43180 KB Output is correct
26 Correct 85 ms 43128 KB Output is correct
27 Correct 52 ms 42872 KB Output is correct
28 Correct 54 ms 43052 KB Output is correct
29 Correct 44 ms 42904 KB Output is correct
30 Correct 52 ms 42872 KB Output is correct
31 Correct 1266 ms 113776 KB Output is correct
32 Correct 256 ms 53296 KB Output is correct
33 Correct 1073 ms 119688 KB Output is correct
34 Correct 974 ms 110484 KB Output is correct
35 Correct 1160 ms 116548 KB Output is correct
36 Correct 1180 ms 122080 KB Output is correct
37 Correct 820 ms 113284 KB Output is correct
38 Correct 826 ms 115848 KB Output is correct
39 Correct 669 ms 108792 KB Output is correct
40 Correct 705 ms 110856 KB Output is correct
41 Correct 788 ms 106604 KB Output is correct
42 Correct 798 ms 106652 KB Output is correct
43 Correct 227 ms 54120 KB Output is correct
44 Correct 757 ms 106452 KB Output is correct
45 Correct 758 ms 105452 KB Output is correct
46 Correct 658 ms 103408 KB Output is correct
47 Correct 549 ms 102020 KB Output is correct
48 Correct 527 ms 101412 KB Output is correct
49 Correct 658 ms 103816 KB Output is correct
50 Correct 658 ms 105480 KB Output is correct
51 Correct 605 ms 103048 KB Output is correct
52 Correct 886 ms 93080 KB Output is correct
53 Correct 794 ms 88580 KB Output is correct
54 Correct 963 ms 122008 KB Output is correct
55 Correct 842 ms 112140 KB Output is correct
56 Correct 845 ms 114304 KB Output is correct
57 Correct 828 ms 108168 KB Output is correct
58 Correct 855 ms 111000 KB Output is correct
59 Correct 897 ms 113164 KB Output is correct
60 Correct 775 ms 107968 KB Output is correct
61 Correct 299 ms 60784 KB Output is correct
62 Correct 815 ms 93496 KB Output is correct
63 Correct 1006 ms 124808 KB Output is correct
64 Correct 1002 ms 113396 KB Output is correct
65 Correct 890 ms 109944 KB Output is correct
66 Correct 819 ms 107400 KB Output is correct
67 Correct 310 ms 53860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 41 ms 42616 KB Output is correct
2 Correct 40 ms 42616 KB Output is correct
3 Correct 41 ms 42588 KB Output is correct
4 Correct 41 ms 42616 KB Output is correct
5 Correct 43 ms 42744 KB Output is correct
6 Correct 44 ms 43208 KB Output is correct
7 Correct 44 ms 43000 KB Output is correct
8 Correct 43 ms 42972 KB Output is correct
9 Correct 44 ms 42996 KB Output is correct
10 Correct 44 ms 43208 KB Output is correct
11 Correct 43 ms 42872 KB Output is correct
12 Correct 43 ms 42872 KB Output is correct
13 Correct 53 ms 42872 KB Output is correct
14 Correct 42 ms 42872 KB Output is correct
15 Correct 51 ms 43088 KB Output is correct
16 Correct 44 ms 43000 KB Output is correct
17 Correct 44 ms 43180 KB Output is correct
18 Correct 44 ms 43208 KB Output is correct
19 Correct 43 ms 42972 KB Output is correct
20 Correct 43 ms 43108 KB Output is correct
21 Correct 42 ms 42744 KB Output is correct
22 Correct 52 ms 43000 KB Output is correct
23 Correct 44 ms 42972 KB Output is correct
24 Correct 45 ms 43204 KB Output is correct
25 Correct 44 ms 43180 KB Output is correct
26 Correct 85 ms 43128 KB Output is correct
27 Correct 52 ms 42872 KB Output is correct
28 Correct 54 ms 43052 KB Output is correct
29 Correct 44 ms 42904 KB Output is correct
30 Correct 52 ms 42872 KB Output is correct
31 Correct 1266 ms 113776 KB Output is correct
32 Correct 256 ms 53296 KB Output is correct
33 Correct 1073 ms 119688 KB Output is correct
34 Correct 974 ms 110484 KB Output is correct
35 Correct 1160 ms 116548 KB Output is correct
36 Correct 1180 ms 122080 KB Output is correct
37 Correct 820 ms 113284 KB Output is correct
38 Correct 826 ms 115848 KB Output is correct
39 Correct 669 ms 108792 KB Output is correct
40 Correct 705 ms 110856 KB Output is correct
41 Correct 788 ms 106604 KB Output is correct
42 Correct 798 ms 106652 KB Output is correct
43 Correct 227 ms 54120 KB Output is correct
44 Correct 757 ms 106452 KB Output is correct
45 Correct 758 ms 105452 KB Output is correct
46 Correct 658 ms 103408 KB Output is correct
47 Correct 549 ms 102020 KB Output is correct
48 Correct 527 ms 101412 KB Output is correct
49 Correct 658 ms 103816 KB Output is correct
50 Correct 658 ms 105480 KB Output is correct
51 Correct 605 ms 103048 KB Output is correct
52 Correct 3576 ms 284272 KB Output is correct
53 Correct 3335 ms 272964 KB Output is correct
54 Correct 3979 ms 381480 KB Output is correct
55 Correct 3599 ms 298668 KB Output is correct
56 Correct 3648 ms 332292 KB Output is correct
57 Correct 3400 ms 288288 KB Output is correct
58 Correct 4027 ms 387856 KB Output is correct
59 Correct 3682 ms 303724 KB Output is correct
60 Correct 3416 ms 269360 KB Output is correct
61 Correct 3336 ms 259348 KB Output is correct
62 Correct 3127 ms 264312 KB Output is correct
63 Correct 3101 ms 259548 KB Output is correct
64 Execution timed out 5102 ms 281756 KB Time limit exceeded
65 Halted 0 ms 0 KB -