답안 #372972

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
372972 2021-03-02T18:59:21 Z luciocf Examination (JOI19_examination) C++14
100 / 100
2358 ms 348628 KB
#include <bits/stdc++.h>

#define ff first
#define ss second

using namespace std;

typedef pair<int, int> pii;

const int maxn = 2e5+10;

struct Node
{
	int v;
	Node *l, *r;

	Node()
	{
		l = r = NULL;
		v = 0;
	}
};

struct SegmentTree
{
	Node *root;

	int get(Node *node)
	{
		return (node ? node->v : 0);
	}

	void upd(Node *node, int l, int r, int pos)
	{
		if (l == r)
		{
			node->v++;
			return;
		}

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

		if (pos <= mid)
		{
			if (!node->l) node->l = new Node();
			upd(node->l, l, mid, pos);
		}
		else
		{
			if (!node->r) node->r = new Node();
			upd(node->r, mid+1, r, pos);
		}

		node->v = get(node->l) + get(node->r);
	}

	int query(Node *node, int l, int r, int a, int b)
	{
		if (!node || l > b || r < a) return 0;
		if (l >= a && r <= b) return node->v;

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

		return query(node->l, l, mid, a, b) + query(node->r, mid+1, r, a, b);
	}
};

struct BIT
{
	SegmentTree bit[maxn];

	void upd(int x, int y)
	{
		for (; x < maxn; x += (x&-x))
		{
			if (!bit[x].root) bit[x].root = new Node();
			bit[x].upd(bit[x].root, 1, maxn-1, y);
		}
	}

	int soma(int x, int y)
	{
		int ans = 0;

		for (; x > 0; x -= (x&-x))
			ans += bit[x].query(bit[x].root, 1, maxn-1, 1, y);

		return ans;
	}
} BIT;

struct Query
{
	int x, y, z, ind;
} query[maxn];

pii a[maxn];
int ans[maxn];

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

	map<int, int> mpx, mpy;

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

		mpx[a[i].ff] = 0;
		mpy[a[i].ss] = 0;
	}

	for (int i = 1; i <= q; i++)
	{
		scanf("%d %d %d", &query[i].x, &query[i].y, &query[i].z);
		query[i].ind = i;

		mpx[query[i].x] = 0;
		mpy[query[i].y] = 0;
	}

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

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

	sort(a+1, a+n+1, [&] (pii a, pii b) {return a.ff+a.ss > b.ff+b.ss;});
	sort(query+1, query+q+1, [&] (Query a, Query b) {return a.z > b.z;});

	int ptr = 1;

	for (int i = 1; i <= q; i++)
	{
		while (ptr <= n && a[ptr].ff+a[ptr].ss >= query[i].z)
		{
			BIT.upd(maxn-mpx[a[ptr].ff], maxn-mpy[a[ptr].ss]);
			ptr++;
		}

		ans[query[i].ind] = BIT.soma(maxn-mpx[query[i].x], maxn-mpy[query[i].y]);
	}

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

Compilation message

examination.cpp: In function 'int main()':
examination.cpp:103:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  103 |  scanf("%d %d", &n, &q);
      |  ~~~~~^~~~~~~~~~~~~~~~~
examination.cpp:109:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  109 |   scanf("%d %d", &a[i].ff, &a[i].ss);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
examination.cpp:117:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  117 |   scanf("%d %d %d", &query[i].x, &query[i].y, &query[i].z);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 0 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 27 ms 6892 KB Output is correct
8 Correct 25 ms 6892 KB Output is correct
9 Correct 23 ms 6892 KB Output is correct
10 Correct 15 ms 3436 KB Output is correct
11 Correct 9 ms 1644 KB Output is correct
12 Correct 4 ms 492 KB Output is correct
13 Correct 24 ms 7040 KB Output is correct
14 Correct 31 ms 7020 KB Output is correct
15 Correct 25 ms 7020 KB Output is correct
16 Correct 9 ms 1004 KB Output is correct
17 Correct 11 ms 3052 KB Output is correct
18 Correct 3 ms 364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1907 ms 265056 KB Output is correct
2 Correct 1985 ms 267876 KB Output is correct
3 Correct 1911 ms 267756 KB Output is correct
4 Correct 611 ms 63468 KB Output is correct
5 Correct 432 ms 30572 KB Output is correct
6 Correct 126 ms 4716 KB Output is correct
7 Correct 1813 ms 258052 KB Output is correct
8 Correct 1787 ms 258412 KB Output is correct
9 Correct 1527 ms 249128 KB Output is correct
10 Correct 232 ms 13932 KB Output is correct
11 Correct 473 ms 51308 KB Output is correct
12 Correct 79 ms 4460 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1907 ms 265056 KB Output is correct
2 Correct 1985 ms 267876 KB Output is correct
3 Correct 1911 ms 267756 KB Output is correct
4 Correct 611 ms 63468 KB Output is correct
5 Correct 432 ms 30572 KB Output is correct
6 Correct 126 ms 4716 KB Output is correct
7 Correct 1813 ms 258052 KB Output is correct
8 Correct 1787 ms 258412 KB Output is correct
9 Correct 1527 ms 249128 KB Output is correct
10 Correct 232 ms 13932 KB Output is correct
11 Correct 473 ms 51308 KB Output is correct
12 Correct 79 ms 4460 KB Output is correct
13 Correct 1709 ms 268040 KB Output is correct
14 Correct 2005 ms 268728 KB Output is correct
15 Correct 1933 ms 267572 KB Output is correct
16 Correct 537 ms 63912 KB Output is correct
17 Correct 374 ms 30828 KB Output is correct
18 Correct 127 ms 4716 KB Output is correct
19 Correct 1668 ms 268140 KB Output is correct
20 Correct 1838 ms 268012 KB Output is correct
21 Correct 1607 ms 261100 KB Output is correct
22 Correct 222 ms 13932 KB Output is correct
23 Correct 456 ms 51308 KB Output is correct
24 Correct 83 ms 4332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 0 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 27 ms 6892 KB Output is correct
8 Correct 25 ms 6892 KB Output is correct
9 Correct 23 ms 6892 KB Output is correct
10 Correct 15 ms 3436 KB Output is correct
11 Correct 9 ms 1644 KB Output is correct
12 Correct 4 ms 492 KB Output is correct
13 Correct 24 ms 7040 KB Output is correct
14 Correct 31 ms 7020 KB Output is correct
15 Correct 25 ms 7020 KB Output is correct
16 Correct 9 ms 1004 KB Output is correct
17 Correct 11 ms 3052 KB Output is correct
18 Correct 3 ms 364 KB Output is correct
19 Correct 1907 ms 265056 KB Output is correct
20 Correct 1985 ms 267876 KB Output is correct
21 Correct 1911 ms 267756 KB Output is correct
22 Correct 611 ms 63468 KB Output is correct
23 Correct 432 ms 30572 KB Output is correct
24 Correct 126 ms 4716 KB Output is correct
25 Correct 1813 ms 258052 KB Output is correct
26 Correct 1787 ms 258412 KB Output is correct
27 Correct 1527 ms 249128 KB Output is correct
28 Correct 232 ms 13932 KB Output is correct
29 Correct 473 ms 51308 KB Output is correct
30 Correct 79 ms 4460 KB Output is correct
31 Correct 1709 ms 268040 KB Output is correct
32 Correct 2005 ms 268728 KB Output is correct
33 Correct 1933 ms 267572 KB Output is correct
34 Correct 537 ms 63912 KB Output is correct
35 Correct 374 ms 30828 KB Output is correct
36 Correct 127 ms 4716 KB Output is correct
37 Correct 1668 ms 268140 KB Output is correct
38 Correct 1838 ms 268012 KB Output is correct
39 Correct 1607 ms 261100 KB Output is correct
40 Correct 222 ms 13932 KB Output is correct
41 Correct 456 ms 51308 KB Output is correct
42 Correct 83 ms 4332 KB Output is correct
43 Correct 2120 ms 348628 KB Output is correct
44 Correct 2145 ms 348508 KB Output is correct
45 Correct 2358 ms 348536 KB Output is correct
46 Correct 733 ms 108012 KB Output is correct
47 Correct 513 ms 46060 KB Output is correct
48 Correct 114 ms 4716 KB Output is correct
49 Correct 2138 ms 338212 KB Output is correct
50 Correct 1928 ms 333224 KB Output is correct
51 Correct 1894 ms 321632 KB Output is correct
52 Correct 364 ms 24940 KB Output is correct
53 Correct 651 ms 93840 KB Output is correct