Submission #132285

# Submission time Handle Problem Language Result Execution time Memory
132285 2019-07-18T16:17:00 Z sebinkim Dragon 2 (JOI17_dragon2) C++14
100 / 100
1242 ms 14056 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair <ll, ll> pll;

struct vec{
	ll x, y;
	
	vec() {}
	vec(ll x, ll y) : x(x), y(y) {}
	
	vec operator - (vec &v) { return vec(x - v.x, y - v.y); }
	ll operator * (vec &v) { return x * v.y - y * v.x; }
};

struct query{
	ll x, l, r, i;
	
	query() {}
	query(ll x, ll l, ll r, ll i) :
		x(x), l(l), r(r), i(i) {}
};

struct fenwick{
	ll T[101010];
	
	void addval(ll p, ll v) { for(; p<=1e5; p+=p&-p) T[p] += v; }
	ll getval(ll p) { return p? getval(p - (p & -p)) + T[p] : 0; }
	ll getval(ll l, ll r) { return getval(r) - getval(l - 1); }
};

fenwick T;
vector <pll> Q1[30303], Q2[30303];
vector <ll> G[30303], V;
vector <query> K;
vec P[30303];
ll X[101010], Y[101010], A[101010];
ll n, m, q;

void sort(ll f, ll *X)
{
	ll i;
	
	V.clear();
	
	if(!f) V.push_back(3), V.push_back(2);
	else V.push_back(1), V.push_back(0);
	
	for(i=4; i<n+n; i++){
		V.push_back(i);
	}
	
	vec vd = P[f] - P[f ^ 1];
	
	sort(V.begin() + 1, V.end(), [&](ll &a, ll &b){
		vec va = (a & 1? P[f] - P[a >> 1] : P[a >> 1] - P[f]);
		vec vb = (b & 1? P[f] - P[b >> 1] : P[b >> 1] - P[f]);
		bool fa = vd * va > 0;
		bool fb = vd * vb > 0;
		if(fa != fb) return fa;
		else return va * vb > 0;
	});
	
	for(i=0; i<V.size(); i++){
		X[V[i]] = i + 1;
	}
}

void solve(ll f, ll p)
{
	vector <pll> &Q = (!f? Q1[p] : Q2[p]);
	ll i, j;
	ll l11, l12, l21, l22, r11, r12, r21, r22;
	
	K.clear();
	sort(Q.begin(), Q.end());
	
	for(i=0; i<Q.size(); i=j){
		for(j=i; j<Q.size() && Q[i].first == Q[j].first; j++);
		for(ll &t: G[Q[i].first]){
			if(!f){
				tie(l11, r11) = minmax(X[t << 1], X[t << 1 | 1]);
				tie(l22, r22) = minmax(Y[t << 1], Y[t << 1 | 1]);
				K.emplace_back(l11 - 1, l22, r22, -Q[i].second);
				K.emplace_back(r11, l22, r22, Q[i].second);
			}
			else{
				if(X[t << 1] < X[t << 1 | 1]){
					l11 = X[3], r11 = X[t << 1];
					l12 = X[2], r12 = X[t << 1 | 1];
				}
				else{
					l11 = X[t << 1], r11 = n + n;
					l12 = X[t << 1 | 1], r12 = X[2];
				}
				
				if(Y[t << 1] < Y[t << 1 | 1]){
					l21 = Y[1], r21 = Y[t << 1];
					l22 = Y[0], r22 = Y[t << 1 | 1];
				}
				else{
					l21 = Y[t << 1], r21 = n + n;
					l22 = Y[t << 1 | 1], r22 = Y[0];
				}
				
				K.emplace_back(l11 - 1, l21, r21, -Q[i].second);
				K.emplace_back(r11, l21, r21, Q[i].second);
				K.emplace_back(l12 - 1, l22, r22, -Q[i].second);
				K.emplace_back(r12, l22, r22, Q[i].second);
			}
		}
	}
	
	sort(K.begin(), K.end(), [&](query &qa, query &qb){
		return qa.x < qb.x;
	});
	
	i = 0;
	
	for(query &t: K){
		for(; i<G[p].size() && X[G[p][i] << 1] <= t.x; i++){
			T.addval(Y[G[p][i] << 1], 1);
		}
		if(t.i > 0) A[t.i] += T.getval(t.l, t.r);
		else A[-t.i] -= T.getval(t.l, t.r);
	}
		
	for(; i--; ){
		T.addval(Y[G[p][i] << 1], -1);
	}
	
	for(i=0; i<Q.size(); i=j){
		for(j=i; j<Q.size() && Q[i].first == Q[j].first; j++){
			A[Q[j].second] = A[Q[i].second];
		}
	}
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	
	ll i, a, b, c;
	
	cin >> n >> m; n += 2;
	
	for(i=2; i<n; i++){
		cin >> P[i].x >> P[i].y >> c;
		G[c].push_back(i);
	}
	
	cin >> P[0].x >> P[0].y >> P[1].x >> P[1].y;
	
	sort(0, X); sort(1, Y);
	
	cin >> q;
	
	for(i=1; i<=q; i++){
		cin >> a >> b;
		if(G[a].size() < G[b].size()) Q1[b].emplace_back(a, i);
		else Q2[a].emplace_back(b, i);
	}
	
	for(i=1; i<=m; i++){
		sort(G[i].begin(), G[i].end(), [&](ll &a, ll &b){
			return X[a << 1] < X[b << 1];
		});
		solve(0, i); solve(1, i);
	}
	
	for(i=1; i<=q; i++){
		cout << A[i] << endl;
	}
	
	return 0;
}

Compilation message

dragon2.cpp: In function 'void sort(ll, ll*)':
dragon2.cpp:66:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(i=0; i<V.size(); i++){
           ~^~~~~~~~~
dragon2.cpp: In function 'void solve(ll, ll)':
dragon2.cpp:80:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(i=0; i<Q.size(); i=j){
           ~^~~~~~~~~
dragon2.cpp:81:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(j=i; j<Q.size() && Q[i].first == Q[j].first; j++);
            ~^~~~~~~~~
dragon2.cpp:123:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(; i<G[p].size() && X[G[p][i] << 1] <= t.x; i++){
         ~^~~~~~~~~~~~
dragon2.cpp:134:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(i=0; i<Q.size(); i=j){
           ~^~~~~~~~~
dragon2.cpp:135:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(j=i; j<Q.size() && Q[i].first == Q[j].first; j++){
            ~^~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 9 ms 3192 KB Output is correct
2 Correct 13 ms 3480 KB Output is correct
3 Correct 61 ms 3320 KB Output is correct
4 Correct 368 ms 6416 KB Output is correct
5 Correct 318 ms 6400 KB Output is correct
6 Correct 12 ms 2936 KB Output is correct
7 Correct 13 ms 2936 KB Output is correct
8 Correct 9 ms 3064 KB Output is correct
9 Correct 7 ms 3064 KB Output is correct
10 Correct 7 ms 3196 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 61 ms 7504 KB Output is correct
2 Correct 114 ms 9456 KB Output is correct
3 Correct 65 ms 5612 KB Output is correct
4 Correct 55 ms 5364 KB Output is correct
5 Correct 57 ms 5360 KB Output is correct
6 Correct 56 ms 6924 KB Output is correct
7 Correct 61 ms 6928 KB Output is correct
8 Correct 56 ms 5872 KB Output is correct
9 Correct 41 ms 7276 KB Output is correct
10 Correct 40 ms 7276 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 3192 KB Output is correct
2 Correct 13 ms 3480 KB Output is correct
3 Correct 61 ms 3320 KB Output is correct
4 Correct 368 ms 6416 KB Output is correct
5 Correct 318 ms 6400 KB Output is correct
6 Correct 12 ms 2936 KB Output is correct
7 Correct 13 ms 2936 KB Output is correct
8 Correct 9 ms 3064 KB Output is correct
9 Correct 7 ms 3064 KB Output is correct
10 Correct 7 ms 3196 KB Output is correct
11 Correct 61 ms 7504 KB Output is correct
12 Correct 114 ms 9456 KB Output is correct
13 Correct 65 ms 5612 KB Output is correct
14 Correct 55 ms 5364 KB Output is correct
15 Correct 57 ms 5360 KB Output is correct
16 Correct 56 ms 6924 KB Output is correct
17 Correct 61 ms 6928 KB Output is correct
18 Correct 56 ms 5872 KB Output is correct
19 Correct 41 ms 7276 KB Output is correct
20 Correct 40 ms 7276 KB Output is correct
21 Correct 61 ms 7144 KB Output is correct
22 Correct 109 ms 7376 KB Output is correct
23 Correct 587 ms 7768 KB Output is correct
24 Correct 1242 ms 9824 KB Output is correct
25 Correct 421 ms 8996 KB Output is correct
26 Correct 384 ms 9264 KB Output is correct
27 Correct 94 ms 6260 KB Output is correct
28 Correct 95 ms 6384 KB Output is correct
29 Correct 380 ms 10480 KB Output is correct
30 Correct 369 ms 10480 KB Output is correct
31 Correct 373 ms 10836 KB Output is correct
32 Correct 392 ms 14056 KB Output is correct
33 Correct 830 ms 12272 KB Output is correct
34 Correct 379 ms 11048 KB Output is correct
35 Correct 385 ms 10876 KB Output is correct
36 Correct 382 ms 10868 KB Output is correct
37 Correct 385 ms 10996 KB Output is correct
38 Correct 831 ms 12444 KB Output is correct
39 Correct 902 ms 12412 KB Output is correct
40 Correct 843 ms 12396 KB Output is correct
41 Correct 413 ms 12240 KB Output is correct
42 Correct 400 ms 12192 KB Output is correct
43 Correct 430 ms 12436 KB Output is correct
44 Correct 109 ms 8936 KB Output is correct
45 Correct 107 ms 8304 KB Output is correct
46 Correct 108 ms 7788 KB Output is correct
47 Correct 105 ms 8060 KB Output is correct
48 Correct 107 ms 7220 KB Output is correct
49 Correct 104 ms 7536 KB Output is correct