답안 #314445

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
314445 2020-10-20T00:02:38 Z MetB Dragon 2 (JOI17_dragon2) C++14
100 / 100
3369 ms 87496 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
 
using namespace __gnu_pbds;
 
#define N 200001
 
using namespace std;
 
 
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
 
const ll INF = 1e18, MOD = 1e9 + 7, MOD2 = 1e6 + 3;
 
struct Pt {
	ll x, y;
 
	Pt (int x, int y) : x (x), y (y) {}
	Pt () : x (0), y (0) {}
 
	ll cross (const Pt& b) const { return x * b.y - b.x * y; }
	bool operator < (const Pt& b) const { return cross (b) < 0; }
	Pt operator - (const Pt& b) const { return Pt (x - b.x, y - b.y); }
	Pt operator - () const { return Pt (-x, -y); }
} s, e;
 
bool OO = true;

vector <int> vs[N];
Pt p[N], pe[N], ps[N];
 
vector < pair <Pt, int> > ords, orde;

gp_hash_table <int, ll> qu[N];
 
ll big[N], sz[N];
int n, m, q, bl = 150, refl[N], g[N];
 
struct BIT {
	int n;
	vector <int> t;

	BIT () {}
	BIT (int n) : n (n), t (vector <int> (n + 1)) {}
 
	void update (int x, int d) {
		for (; x <= n; x = (x | (x + 1)))
			t[x] += d;
	}
 
	int get (int r) {
		int sum = 0;
 
		for (; r >= 0; r = (r & (r + 1)) - 1)
			sum += t[r];
 
		return sum;
	}
 
	int get (int l, int r) { return get (r) - get (l - 1); }
} t1 (N), t2 (N);

int find (Pt x) {
	return lower_bound (orde.begin(), orde.end(), make_pair (x,-1)) - orde.begin ();
}

void attack_to (int to) {
	for (int i : vs[to]) {
		int x = ords[i].second;
		if (!refl[x]) t2.update (find (pe[x]), 1);
	}

	for (int i = 0; i < n; i++) {
		int x = ords[i].second, tribe = g[x];
		int ind = find (pe[x]);

		if (tribe == to) {
			if (!refl[x]) t2.update (ind, -1);
			else t1.update (ind, 1);
			continue;		
		}

		if (qu[tribe].find (to) == qu[tribe].end ()) continue;

		qu[tribe][to] += t2.get (0, ind - 1) + t1.get (ind, n - 1);
	}

	for (int i : vs[to]) {
		int x = ords[i].second, cur = find (pe[x]);
		if (t1.get (cur, cur)) t1.update (cur, -1);
		if (t2.get (cur, cur)) t2.update (cur, -1);
	}
}

void attack_from (int from) {
	for (int i : vs[from]) {
		int x = ords[i].second;
		t2.update (find (pe[x]), 1);
	}

	for (int i = 0; i < n; i++) {
		int x = ords[i].second, tribe = g[x];
		int ind = find (pe[x]);

		if (tribe == from) {
			t1.update (ind, 1);
			t2.update (ind, -1);	
			continue;		
		}

		if (qu[from].find (tribe) == qu[from].end ()) continue;

		if (refl[x]) qu[from][tribe] += t2.get (0, ind - 1);
		else qu[from][tribe] += t1.get (ind, n - 1);
	}

	for (int i : vs[from]) {
		int x = ords[i].second, cur = find (pe[x]);
		if (t1.get (cur, cur)) t1.update (cur, -1);
		if (t2.get (cur, cur)) t2.update (cur, -1);
	}
}

int attack_small (int from, int to) {
	ll sum = 0;

	for (int i : vs[to]) {
		int x = ords[i].second;
		if (!refl[x]) {
			t2.update (find (pe[x]), 1);
		}
	}

	int j = 0;

	for (int i : vs[from]) {
		int x = ords[i].second;
		int ind = find (pe[x]);

		while (j < vs[to].size () && vs[to][j] < i) {
			int cur = ords[vs[to][j]].second;
			if (!refl[cur]) t2.update (find (pe[cur]), -1);
			else t1.update (find (pe[cur]), 1);
			j++;
		}

		sum += t1.get (ind, n - 1) + t2.get (0, ind - 1);
	}

	for (int i : vs[to]) {
		int x = ords[i].second, cur = find (pe[x]);
		if (t1.get (cur, cur)) t1.update (cur, -1);
		if (t2.get (cur, cur)) t2.update (cur, -1);
	}

	return sum;
}
 
int main () {
	cin >> n >> m;
 
	for (int i = 0; i < n; i++) {
		int a, b, c;
		cin >> a >> b >> c;
		g[i] = c - 1;
		p[i] = Pt (a, b);
		sz[c-1]++;
	}
 
	int x, y;
 
	cin >> x >> y;
	s = Pt (x, y);
 
	cin >> x >> y;
	e = Pt (x, y);
 
	for (int i = 0; i < n; i++) {
		ps[i] = p[i] - s;
		pe[i] = p[i] - e;
		if (ps[i].cross (e - s) > 0) {
			refl[i] = 1;
			ps[i] = -ps[i], pe[i] = -pe[i];
		}
		orde.push_back ({pe[i], i});
		ords.push_back ({ps[i], i});
	}
 
	sort (ords.begin(), ords.end());
	sort (orde.begin(), orde.end());

	for (int i = 0; i < n; i++) {
		vs[g[ords[i].second]].push_back (i);
	}
 
	cin >> q;
 
	vector < pair <int, int> > e;

	for (int i = 0; i < q; i++) {
		int a, b;
		cin >> a >> b;
		a--, b--;
		e.emplace_back (a, b);

		qu[a][b] = 0;
	}

	for (int i = 0; i < m; i++) {
		if (sz[i] > bl) {
			big[i] = 1;
			attack_from (i);
			attack_to (i);
		}
	}

	for (auto [a, b] : e) {
		if (!big[a] && !big[b]) cout << attack_small (a, b) << '\n';
		else if (big[a] && big[b]) cout << qu[a][b] / 2 << '\n';
		else cout << qu[a][b] << '\n';
	}
}

Compilation message

dragon2.cpp: In function 'int attack_small(int, int)':
dragon2.cpp:142:12: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  142 |   while (j < vs[to].size () && vs[to][j] < i) {
      |          ~~^~~~~~~~~~~~~~~~
dragon2.cpp: In function 'int main()':
dragon2.cpp:219:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  219 |  for (auto [a, b] : e) {
      |            ^
# 결과 실행 시간 메모리 Grader output
1 Correct 79 ms 77432 KB Output is correct
2 Correct 85 ms 77304 KB Output is correct
3 Correct 157 ms 77688 KB Output is correct
4 Correct 333 ms 84716 KB Output is correct
5 Correct 258 ms 86124 KB Output is correct
6 Correct 80 ms 77432 KB Output is correct
7 Correct 79 ms 77432 KB Output is correct
8 Correct 78 ms 77304 KB Output is correct
9 Correct 76 ms 77304 KB Output is correct
10 Correct 75 ms 77308 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 186 ms 78980 KB Output is correct
2 Correct 271 ms 79148 KB Output is correct
3 Correct 1093 ms 79148 KB Output is correct
4 Correct 145 ms 79148 KB Output is correct
5 Correct 150 ms 79532 KB Output is correct
6 Correct 164 ms 78972 KB Output is correct
7 Correct 161 ms 79020 KB Output is correct
8 Correct 178 ms 78956 KB Output is correct
9 Correct 146 ms 79020 KB Output is correct
10 Correct 151 ms 79064 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 79 ms 77432 KB Output is correct
2 Correct 85 ms 77304 KB Output is correct
3 Correct 157 ms 77688 KB Output is correct
4 Correct 333 ms 84716 KB Output is correct
5 Correct 258 ms 86124 KB Output is correct
6 Correct 80 ms 77432 KB Output is correct
7 Correct 79 ms 77432 KB Output is correct
8 Correct 78 ms 77304 KB Output is correct
9 Correct 76 ms 77304 KB Output is correct
10 Correct 75 ms 77308 KB Output is correct
11 Correct 186 ms 78980 KB Output is correct
12 Correct 271 ms 79148 KB Output is correct
13 Correct 1093 ms 79148 KB Output is correct
14 Correct 145 ms 79148 KB Output is correct
15 Correct 150 ms 79532 KB Output is correct
16 Correct 164 ms 78972 KB Output is correct
17 Correct 161 ms 79020 KB Output is correct
18 Correct 178 ms 78956 KB Output is correct
19 Correct 146 ms 79020 KB Output is correct
20 Correct 151 ms 79064 KB Output is correct
21 Correct 183 ms 79024 KB Output is correct
22 Correct 279 ms 79020 KB Output is correct
23 Correct 1266 ms 79404 KB Output is correct
24 Correct 2649 ms 86736 KB Output is correct
25 Correct 535 ms 86056 KB Output is correct
26 Correct 398 ms 84396 KB Output is correct
27 Correct 178 ms 80172 KB Output is correct
28 Correct 178 ms 80044 KB Output is correct
29 Correct 347 ms 84264 KB Output is correct
30 Correct 327 ms 84008 KB Output is correct
31 Correct 365 ms 84136 KB Output is correct
32 Correct 1147 ms 84136 KB Output is correct
33 Correct 3205 ms 84432 KB Output is correct
34 Correct 391 ms 84204 KB Output is correct
35 Correct 343 ms 84136 KB Output is correct
36 Correct 361 ms 84136 KB Output is correct
37 Correct 386 ms 84264 KB Output is correct
38 Correct 2890 ms 84776 KB Output is correct
39 Correct 3369 ms 87496 KB Output is correct
40 Correct 2883 ms 86184 KB Output is correct
41 Correct 356 ms 85544 KB Output is correct
42 Correct 394 ms 85940 KB Output is correct
43 Correct 482 ms 85928 KB Output is correct
44 Correct 183 ms 81580 KB Output is correct
45 Correct 195 ms 81580 KB Output is correct
46 Correct 202 ms 81708 KB Output is correct
47 Correct 180 ms 80300 KB Output is correct
48 Correct 198 ms 80300 KB Output is correct
49 Correct 203 ms 80304 KB Output is correct