Submission #310763

# Submission time Handle Problem Language Result Execution time Memory
310763 2020-10-07T20:32:12 Z luciocf None (JOI16_matryoshka) C++14
100 / 100
813 ms 50168 KB
#include <bits/stdc++.h>

#define ff first
#define ss second

using namespace std;

typedef pair<int, int> pii;

const int maxn = 4e5+10;

struct SegmentTree
{
	int tree[3*maxn];

	void upd(int node, int l, int r, int pos, int v)
	{
		if (l == r)
		{
			tree[node] = v;
			return;
		}

		int mid = (l+r)>>1;

		if (pos <= mid) upd(2*node, l, mid, pos, v);
		else upd(2*node+1, mid+1, r, pos, v);

		tree[node] = max(tree[2*node], tree[2*node+1]);
	}

	int query(int node, int l, int r, int a, int b)
	{
		if (l > r || l > b || r < a) return 0;
		if (l >= a && r <= b) return tree[node];

		int mid = (l+r)>>1;

		return max(query(2*node, l, mid, a, b), query(2*node+1, mid+1, r, a, b)); 
	}
} seg;

struct Query
{
	int r, h, ind;
} qry[maxn];

int n, q;
pii a[maxn];

int ans[maxn];

vector<int> pos[maxn];

void compress(void)
{
	map<int, int> mp;

	for (int i = 1; i <= n; i++)
		mp[a[i].ss] = 0;
	for (int i = 1; i <= q; i++)
		mp[qry[i].h] = 0;

	int aux = 0;
	for (auto &x: mp)
		x.second = ++aux;

	for (int i = n; i >= 1; i--)
	{
		a[i].ss = mp[a[i].ss];
		pos[a[i].ss].push_back(i);
	}

	for (int i = 1; i <= q; i++)
		qry[i].h = mp[qry[i].h];
}

bool comp(pii x, pii y)
{
	if (x.ff == y.ff) return x.ss > y.ss;
	return x.ff < y.ff;
}

int main(void)
{
	scanf("%d %d", &n, &q);

	for (int i = 1; i <= n; i++)
		scanf("%d %d", &a[i].ff, &a[i].ss);

	for (int i = 1; i <= q; i++)
	{
		scanf("%d %d", &qry[i].r, &qry[i].h);
		qry[i].ind = i;
	}

	sort(a+1, a+n+1, comp);
	sort(qry+1, qry+q+1, [&] (Query a, Query b) {return a.h < b.h;});

	compress();

	int ptr = 1;

	for (int i = 1; i <= q; i++)
	{
		while (ptr <= qry[i].h)
		{
			for (auto j: pos[ptr])
			{
				int mx = seg.query(1, 1, n, j+1, n)+1;
				seg.upd(1, 1, n, j, mx);
			}

			ptr++;
		}

		int pos = (int)(lower_bound(a+1, a+n+1, make_pair(qry[i].r, 0))-a);

		ans[qry[i].ind] = seg.query(1, 1, n, pos, n);
	}

	for (int i = 1; i <= q; i++)
		printf("%d\n", ans[i]);
}

Compilation message

matryoshka.cpp: In function 'int main()':
matryoshka.cpp:86:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   86 |  scanf("%d %d", &n, &q);
      |  ~~~~~^~~~~~~~~~~~~~~~~
matryoshka.cpp:89:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   89 |   scanf("%d %d", &a[i].ff, &a[i].ss);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
matryoshka.cpp:93:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   93 |   scanf("%d %d", &qry[i].r, &qry[i].h);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9728 KB Output is correct
2 Correct 7 ms 9728 KB Output is correct
3 Correct 7 ms 9728 KB Output is correct
4 Correct 8 ms 9728 KB Output is correct
5 Correct 7 ms 9728 KB Output is correct
6 Correct 7 ms 9728 KB Output is correct
7 Correct 7 ms 9728 KB Output is correct
8 Correct 7 ms 9728 KB Output is correct
9 Correct 7 ms 9728 KB Output is correct
10 Correct 7 ms 9728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9728 KB Output is correct
2 Correct 7 ms 9728 KB Output is correct
3 Correct 7 ms 9728 KB Output is correct
4 Correct 8 ms 9728 KB Output is correct
5 Correct 7 ms 9728 KB Output is correct
6 Correct 7 ms 9728 KB Output is correct
7 Correct 7 ms 9728 KB Output is correct
8 Correct 7 ms 9728 KB Output is correct
9 Correct 7 ms 9728 KB Output is correct
10 Correct 7 ms 9728 KB Output is correct
11 Correct 7 ms 9760 KB Output is correct
12 Correct 7 ms 9728 KB Output is correct
13 Correct 7 ms 9728 KB Output is correct
14 Correct 7 ms 9728 KB Output is correct
15 Correct 7 ms 9728 KB Output is correct
16 Correct 7 ms 9728 KB Output is correct
17 Correct 7 ms 9728 KB Output is correct
18 Correct 7 ms 9728 KB Output is correct
19 Correct 7 ms 9728 KB Output is correct
20 Correct 7 ms 9728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9728 KB Output is correct
2 Correct 7 ms 9728 KB Output is correct
3 Correct 7 ms 9728 KB Output is correct
4 Correct 8 ms 9728 KB Output is correct
5 Correct 7 ms 9728 KB Output is correct
6 Correct 7 ms 9728 KB Output is correct
7 Correct 7 ms 9728 KB Output is correct
8 Correct 7 ms 9728 KB Output is correct
9 Correct 7 ms 9728 KB Output is correct
10 Correct 7 ms 9728 KB Output is correct
11 Correct 7 ms 9760 KB Output is correct
12 Correct 7 ms 9728 KB Output is correct
13 Correct 7 ms 9728 KB Output is correct
14 Correct 7 ms 9728 KB Output is correct
15 Correct 7 ms 9728 KB Output is correct
16 Correct 7 ms 9728 KB Output is correct
17 Correct 7 ms 9728 KB Output is correct
18 Correct 7 ms 9728 KB Output is correct
19 Correct 7 ms 9728 KB Output is correct
20 Correct 7 ms 9728 KB Output is correct
21 Correct 7 ms 9728 KB Output is correct
22 Correct 10 ms 9984 KB Output is correct
23 Correct 11 ms 10112 KB Output is correct
24 Correct 11 ms 10112 KB Output is correct
25 Correct 11 ms 10112 KB Output is correct
26 Correct 11 ms 10112 KB Output is correct
27 Correct 11 ms 10112 KB Output is correct
28 Correct 11 ms 10112 KB Output is correct
29 Correct 11 ms 10112 KB Output is correct
30 Correct 9 ms 9984 KB Output is correct
31 Correct 11 ms 10164 KB Output is correct
32 Correct 11 ms 10112 KB Output is correct
33 Correct 13 ms 10112 KB Output is correct
34 Correct 11 ms 10112 KB Output is correct
35 Correct 11 ms 10112 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9728 KB Output is correct
2 Correct 7 ms 9728 KB Output is correct
3 Correct 7 ms 9728 KB Output is correct
4 Correct 8 ms 9728 KB Output is correct
5 Correct 7 ms 9728 KB Output is correct
6 Correct 7 ms 9728 KB Output is correct
7 Correct 7 ms 9728 KB Output is correct
8 Correct 7 ms 9728 KB Output is correct
9 Correct 7 ms 9728 KB Output is correct
10 Correct 7 ms 9728 KB Output is correct
11 Correct 7 ms 9760 KB Output is correct
12 Correct 7 ms 9728 KB Output is correct
13 Correct 7 ms 9728 KB Output is correct
14 Correct 7 ms 9728 KB Output is correct
15 Correct 7 ms 9728 KB Output is correct
16 Correct 7 ms 9728 KB Output is correct
17 Correct 7 ms 9728 KB Output is correct
18 Correct 7 ms 9728 KB Output is correct
19 Correct 7 ms 9728 KB Output is correct
20 Correct 7 ms 9728 KB Output is correct
21 Correct 7 ms 9728 KB Output is correct
22 Correct 10 ms 9984 KB Output is correct
23 Correct 11 ms 10112 KB Output is correct
24 Correct 11 ms 10112 KB Output is correct
25 Correct 11 ms 10112 KB Output is correct
26 Correct 11 ms 10112 KB Output is correct
27 Correct 11 ms 10112 KB Output is correct
28 Correct 11 ms 10112 KB Output is correct
29 Correct 11 ms 10112 KB Output is correct
30 Correct 9 ms 9984 KB Output is correct
31 Correct 11 ms 10164 KB Output is correct
32 Correct 11 ms 10112 KB Output is correct
33 Correct 13 ms 10112 KB Output is correct
34 Correct 11 ms 10112 KB Output is correct
35 Correct 11 ms 10112 KB Output is correct
36 Correct 655 ms 33984 KB Output is correct
37 Correct 345 ms 22776 KB Output is correct
38 Correct 191 ms 24696 KB Output is correct
39 Correct 301 ms 21240 KB Output is correct
40 Correct 305 ms 21752 KB Output is correct
41 Correct 384 ms 23032 KB Output is correct
42 Correct 215 ms 20472 KB Output is correct
43 Correct 175 ms 20332 KB Output is correct
44 Correct 813 ms 49996 KB Output is correct
45 Correct 810 ms 50028 KB Output is correct
46 Correct 811 ms 50168 KB Output is correct
47 Correct 549 ms 49784 KB Output is correct
48 Correct 550 ms 50168 KB Output is correct
49 Correct 553 ms 49656 KB Output is correct
50 Correct 551 ms 50168 KB Output is correct