답안 #132283

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
132283 2019-07-18T16:13:00 Z sebinkim Dragon 2 (JOI17_dragon2) C++14
60 / 100
4000 ms 11340 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]);
//				printf("%lld, %lld %lld, %lld %lld, %lld\n", t, l11, r11, l22, r22, Q[i].second);
				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[2], r11 = X[t << 1];
					l12 = X[3], r12 = X[t << 1 | 1];
				}
				else{
					l11 = X[t << 1 | 1], r11 = X[3];
					l11 = X[t << 1], r11 = n + n;
				}
				
				if(Y[t << 1] < Y[t << 1 | 1]){
					l21 = Y[0], r21 = Y[t << 1];
					l22 = Y[1], r22 = Y[t << 1 | 1];
				}
				else{
					l21 = Y[t << 1 | 1], r21 = Y[1];
					l21 = Y[t << 1], r21 = n + n;
				}
				
				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++){
//			printf("%lld, %lld, %lld\n", G[p][i], X[G[p][i] << 1], Y[G[p][i] << 1]);
			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);
	
//	for(i=0; i<n+n; i++) printf("%lld ", X[i]); printf("\n");
//	for(i=0; i<n+n; i++) printf("%lld ", Y[i]); printf("\n");
	
	cin >> q;
	
	for(i=1; i<=q; i++){
		cin >> a >> b;
		Q1[b].emplace_back(a, i);
//		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:124: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:136:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(i=0; i<Q.size(); i=j){
           ~^~~~~~~~~
dragon2.cpp:137: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:111:24: warning: 'l12' may be used uninitialized in this function [-Wmaybe-uninitialized]
     K.emplace_back(l12 - 1, l22, r22, -Q[i].second);
                    ~~~~^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 3064 KB Output is correct
2 Correct 12 ms 3320 KB Output is correct
3 Correct 55 ms 3320 KB Output is correct
4 Correct 357 ms 7020 KB Output is correct
5 Correct 321 ms 7544 KB Output is correct
6 Correct 13 ms 2936 KB Output is correct
7 Correct 12 ms 3064 KB Output is correct
8 Correct 8 ms 3064 KB Output is correct
9 Correct 7 ms 3192 KB Output is correct
10 Correct 7 ms 3064 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 58 ms 7276 KB Output is correct
2 Correct 95 ms 7940 KB Output is correct
3 Correct 63 ms 6256 KB Output is correct
4 Correct 55 ms 6232 KB Output is correct
5 Correct 56 ms 6116 KB Output is correct
6 Correct 53 ms 6632 KB Output is correct
7 Correct 53 ms 6636 KB Output is correct
8 Correct 57 ms 6508 KB Output is correct
9 Correct 36 ms 6728 KB Output is correct
10 Correct 36 ms 6748 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 3064 KB Output is correct
2 Correct 12 ms 3320 KB Output is correct
3 Correct 55 ms 3320 KB Output is correct
4 Correct 357 ms 7020 KB Output is correct
5 Correct 321 ms 7544 KB Output is correct
6 Correct 13 ms 2936 KB Output is correct
7 Correct 12 ms 3064 KB Output is correct
8 Correct 8 ms 3064 KB Output is correct
9 Correct 7 ms 3192 KB Output is correct
10 Correct 7 ms 3064 KB Output is correct
11 Correct 58 ms 7276 KB Output is correct
12 Correct 95 ms 7940 KB Output is correct
13 Correct 63 ms 6256 KB Output is correct
14 Correct 55 ms 6232 KB Output is correct
15 Correct 56 ms 6116 KB Output is correct
16 Correct 53 ms 6632 KB Output is correct
17 Correct 53 ms 6636 KB Output is correct
18 Correct 57 ms 6508 KB Output is correct
19 Correct 36 ms 6728 KB Output is correct
20 Correct 36 ms 6748 KB Output is correct
21 Correct 58 ms 6980 KB Output is correct
22 Correct 96 ms 7596 KB Output is correct
23 Correct 486 ms 8356 KB Output is correct
24 Correct 1125 ms 10924 KB Output is correct
25 Correct 415 ms 10776 KB Output is correct
26 Correct 380 ms 11108 KB Output is correct
27 Correct 93 ms 7284 KB Output is correct
28 Correct 93 ms 7284 KB Output is correct
29 Execution timed out 4005 ms 11340 KB Time limit exceeded
30 Halted 0 ms 0 KB -