답안 #127811

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
127811 2019-07-10T06:38:32 Z 조승현(#3114) Dragon 2 (JOI17_dragon2) C++14
60 / 100
4000 ms 6872 KB
#include<cstdio>
#include<algorithm>
#include<vector>
#define pii pair<int,int>
using namespace std;
struct point {
	long long x, y;
	int num;
	point operator +(const point &p)const {
		return { x + p.x,y + p.y };
	}
	point operator -(const point &p)const {
		return { x - p.x,y - p.y };
	}
	int dir() const{
		if (x > 0 || (x >= 0 && y > 0))return 0;
		return 1;
	}
	bool operator <(const point &p)const {
		if (dir() != p.dir())return dir() < p.dir();
		return y * p.x < p.y*x;
	}
}w[30100], ori[30100], A, B;
long long ccw(point a, point b, point c) {
	return (b.x - a.x)*(c.y - a.y) - (b.y - a.y)*(c.x - a.x);
}
int Col[30100];
int n, K;
struct AA {
	int x, y;
}P[30100];
struct Rect {
	int bx, by, ex, ey;
}R[30100];
void Make(point O, int ck) {
	int i;
	vector<point>T;
	for (i = 1; i <= n + 1; i++) {
		point t1 = w[i] - O; t1.num = i;
		point t2 = O - w[i]; t2.num = -i;
		T.push_back(t1);
		T.push_back(t2);
	}
	sort(T.begin(), T.end());
	int sz = T.size(), pv = 0, pv2 = 0;
	for (i = 0; i < sz; i++) {
		if (T[i].num == -(n + 1)) {
			pv = i;
		}
		if (T[i].num == n + 1)pv2 = i;
	}
	for (i = 0; i < sz; i++) {
		int t = T[(pv + i) % sz].num;
		if (abs(t) > n)continue;
		if (t > 0) {
			if (!ck)P[t].x = R[t].bx = i + 1;
			else P[t].y = R[t].by = i + 1;
		}
		else {
			t = -t;
			if (!ck)R[t].ex = i + 1;
			else R[t].ey = i + 1;
		}
	}
}
int Q;
const int SZ = 65536;
int BIT[SZ];
void Add(int a, int b) {
	while (a < SZ) {
		BIT[a] += b;
		a += (a&-a);
	}
}
int Sum(int a) {
	int r = 0;
	while (a) {
		r += BIT[a];
		a -= (a&-a);
	}
	return r;
}
struct Seg {
	int x, b, e, ck;
	bool operator<(const Seg &p)const {
		return x < p.x;
	}
}U[SZ];
pii PP[SZ];
vector<int>G[30100];
int Query(int a, int b) {
	int i, c1 = 0, c2 = 0;
	for (auto &t : G[a]) {
		U[c1++] = { R[t].bx,R[t].by,R[t].ey,1 };
		U[c1++] ={ R[t].ex+1,R[t].by,R[t].ey,-1 };
	}
	for (auto &t : G[b]) {
		PP[c2++] = { P[t].x, P[t].y };
	}
	sort(U, U + c1);
	sort(PP, PP + c2);
	int res = 0, pv = 0;
	for (i = 0; i < c2; i++) {
		while (pv < c1 && U[pv].x <= PP[i].first) {
			Add(U[pv].b, U[pv].ck);
			Add(U[pv].e+1, -U[pv].ck);
			pv++;
		}
		res += Sum(PP[i].second);
	}
	while (pv > 0) {
		pv--;
		Add(U[pv].b, -U[pv].ck);
		Add(U[pv].e+1, U[pv].ck);
	}
	return res;
}
int main() {
	int i, a, b;
	scanf("%d%d", &n,&K);
	for (i = 1; i <= n; i++) {
		scanf("%lld%lld%d", &w[i].x, &w[i].y, &Col[i]);
		G[Col[i]].push_back(i);
		w[i].num = i;
	}
	scanf("%lld%lld%lld%lld", &A.x, &A.y, &B.x, &B.y);
	w[n + 1] = B;
	Make(A, 0);
	w[n + 1] = A;
	Make(B, 1);
	for (i = 1; i <= n; i++) {
		if (R[i].ex < R[i].bx)swap(R[i].bx, R[i].ex);
		if (R[i].ey < R[i].by)swap(R[i].by, R[i].ey);
	}
	scanf("%d", &Q);
	while (Q--) {
		scanf("%d%d", &a, &b);
		printf("%d\n", Query(a, b));
	}
}

Compilation message

dragon2.cpp: In function 'void Make(point, int)':
dragon2.cpp:45:29: warning: variable 'pv2' set but not used [-Wunused-but-set-variable]
  int sz = T.size(), pv = 0, pv2 = 0;
                             ^~~
dragon2.cpp: In function 'int main()':
dragon2.cpp:120:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d", &n,&K);
  ~~~~~^~~~~~~~~~~~~~~
dragon2.cpp:122:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%lld%lld%d", &w[i].x, &w[i].y, &Col[i]);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
dragon2.cpp:126:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%lld%lld%lld%lld", &A.x, &A.y, &B.x, &B.y);
  ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
dragon2.cpp:135:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &Q);
  ~~~~~^~~~~~~~~~
dragon2.cpp:137:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d", &a, &b);
   ~~~~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 1564 KB Output is correct
2 Correct 11 ms 1568 KB Output is correct
3 Correct 51 ms 1572 KB Output is correct
4 Correct 102 ms 1720 KB Output is correct
5 Correct 60 ms 1608 KB Output is correct
6 Correct 7 ms 1688 KB Output is correct
7 Correct 7 ms 1692 KB Output is correct
8 Correct 6 ms 1564 KB Output is correct
9 Correct 5 ms 1568 KB Output is correct
10 Correct 6 ms 1516 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 45 ms 5660 KB Output is correct
2 Correct 95 ms 5620 KB Output is correct
3 Correct 52 ms 5728 KB Output is correct
4 Correct 43 ms 5852 KB Output is correct
5 Correct 45 ms 6108 KB Output is correct
6 Correct 45 ms 5640 KB Output is correct
7 Correct 43 ms 5640 KB Output is correct
8 Correct 43 ms 5644 KB Output is correct
9 Correct 31 ms 5640 KB Output is correct
10 Correct 31 ms 5656 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 1564 KB Output is correct
2 Correct 11 ms 1568 KB Output is correct
3 Correct 51 ms 1572 KB Output is correct
4 Correct 102 ms 1720 KB Output is correct
5 Correct 60 ms 1608 KB Output is correct
6 Correct 7 ms 1688 KB Output is correct
7 Correct 7 ms 1692 KB Output is correct
8 Correct 6 ms 1564 KB Output is correct
9 Correct 5 ms 1568 KB Output is correct
10 Correct 6 ms 1516 KB Output is correct
11 Correct 45 ms 5660 KB Output is correct
12 Correct 95 ms 5620 KB Output is correct
13 Correct 52 ms 5728 KB Output is correct
14 Correct 43 ms 5852 KB Output is correct
15 Correct 45 ms 6108 KB Output is correct
16 Correct 45 ms 5640 KB Output is correct
17 Correct 43 ms 5640 KB Output is correct
18 Correct 43 ms 5644 KB Output is correct
19 Correct 31 ms 5640 KB Output is correct
20 Correct 31 ms 5656 KB Output is correct
21 Correct 46 ms 6028 KB Output is correct
22 Correct 96 ms 5992 KB Output is correct
23 Correct 614 ms 6108 KB Output is correct
24 Correct 1020 ms 6108 KB Output is correct
25 Correct 147 ms 6232 KB Output is correct
26 Correct 110 ms 6524 KB Output is correct
27 Correct 48 ms 6748 KB Output is correct
28 Correct 49 ms 6872 KB Output is correct
29 Execution timed out 4026 ms 6360 KB Time limit exceeded
30 Halted 0 ms 0 KB -