Submission #27348

# Submission time Handle Problem Language Result Execution time Memory
27348 2017-07-12T10:20:42 Z khsoo01 Dragon 2 (JOI17_dragon2) C++11
100 / 100
1439 ms 245736 KB
#include<bits/stdc++.h>
#define X first
#define Y second
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;
typedef pair<int,int> pii;

ll n, m, q, s1, s2, ans[100005];
pll p1, p2, qry[100005];

map<pll, ll> mem;
vector<ll> z1, z2;
vector<pll> drg[30005], pt;
vector<pii> cp;

struct data {pii s[2], e[2]; int i;};
vector<data> rd[30005];

struct sweep {ll s, e, x, i;};
vector<sweep> swp[1111111];

struct segtree {
	ll v[2222222], lim;
	void init () {
		for(lim=1;lim<=2*s2;lim<<=1);
		for(ll i=2*lim;--i;) v[i] = 0;
	}
	void upd (ll P, ll V) {
		P += lim;
		while(P) {v[P] += V; P >>= 1;}
	}
	ll get (ll S, ll E) {
		S += lim; E += lim;
		ll ret = 0;
		while(S <= E) {
			if(S%2==1) ret += v[S++];
			if(E%2==0) ret += v[E--];
			S >>= 1; E >>= 1;
		}
		return ret;
	}
} seg;

ll ccw (const pll &A, const pll &B, const pll &C) {
	return (A.X*B.Y+B.X*C.Y+C.X*A.Y) - (A.Y*B.X+B.Y*C.X+C.Y*A.X);
}
ll ccw (const pii &A, const pii &B) {return 1ll*A.X*B.Y - 1ll*A.Y*B.X;}

bool isup (const pii &P) {
	return (0 < P.Y || (!P.Y && 0 < P.X));
}

bool cmp (const pii &A, const pii &B) {
	bool F1 = isup(A), F2 = isup(B);
	if(F1 != F2) return F1;
	return ccw(A, B) > 0;
}

void cpr (pll &P, vector<pll> &D, vector<data> &R, int O, vector<ll> &V, ll &S) {
	cp.clear(); V.clear();
	for(auto &T : D) cp.push_back(pii(T.X-P.X,T.Y-P.Y));
	sort(cp.begin(), cp.end(), cmp);
	cp.erase(unique(cp.begin(), cp.end()), cp.end());
	S = cp.size();
	for(auto &T : D) {
		V.push_back(lower_bound(cp.begin(), cp.end(), pii(T.X-P.X,T.Y-P.Y), cmp) - cp.begin() + 1);
	}
	for(auto &T : R) {
		bool flag = cmp(T.e[O], T.s[O]);
		T.s[O].X = lower_bound(cp.begin(), cp.end(), T.s[O], cmp) - cp.begin() + 1;
		T.e[O].X = upper_bound(cp.begin(), cp.end(), T.e[O], cmp) - cp.begin();
		if(flag != (T.e[O].X < T.s[O].X)) {T.s[O].X = -1; T.e[O].X = -1;}
	}
}

void add (ll XS, ll XE, ll YS, ll YE, ll I) {
	if(XS < 0 || YS < 0) return;
	swp[XE].push_back({YS, YE+(YS>YE)*s2, 1, I});
	if(XS > 1) swp[XS-1].push_back({YS, YE+(YS>YE)*s2, -1, I});
	if(XS > XE) swp[s1].push_back({YS, YE+(YS>YE)*s2, 1, I});
}

pll Flip (const pll &A, const pll &B) {
	return pll(2*A.X - B.X, 2*A.Y - B.Y);
}

data conv (pll S[], pll E[], ll I) {
	data C; C.i = I;
	C.s[0] = pii(S[0].X-p1.X, S[0].Y-p1.Y);
	C.e[0] = pii(E[0].X-p1.X, E[0].Y-p1.Y);
	C.s[1] = pii(S[1].X-p2.X, S[1].Y-p2.Y);
	C.e[1] = pii(E[1].X-p2.X, E[1].Y-p2.Y);
	return C;
}

void sadi (ll S, ll M, ll I) {
	for(auto &T : drg[S]) {
		pll L[2], R[2];
		L[0] = T; R[0] = Flip(p1, T);
		if(ccw(p1, L[0], p2) < 0) swap(L[0], R[0]);
		L[1] = T; R[1] = Flip(p2, T);
		if(ccw(p2, L[1], p1) < 0) swap(L[1], R[1]);
		rd[M].push_back(conv(L, R, I));
	}
}

void majo (ll S, ll M, ll I) {
	for(auto &T : drg[M]) {
		pll L[2], R[2];
		L[0] = p2; R[0] = Flip(p1, T);
		if(ccw(p1, L[0], R[0]) < 0) swap(L[0], R[0]);
		L[1] = p1; R[1] = Flip(p2, T);
		if(ccw(p2, L[1], R[1]) < 0) swap(L[1], R[1]);
		rd[S].push_back(conv(L, R, I));
	}
	for(auto &T : drg[M]) {
		pll L[2], R[2];
		L[0] = T; R[0] = Flip(p1, T);
		if(ccw(p1, L[0], p2) > 0) swap(L[0], R[0]);
		L[1] = T; R[1] = Flip(p2, T);
		if(ccw(p2, L[1], p1) > 0) swap(L[1], R[1]);
		rd[S].push_back(conv(L, R, I));
	}
}

int main()
{
	scanf("%lld%lld",&n,&m);
	for(ll i=1;i<=n;i++) {
		ll A, B, C;
		scanf("%lld%lld%lld",&A,&B,&C);
		drg[C].push_back(pll(A, B));
	}
	scanf("%lld%lld%lld%lld%lld",&p1.X,&p1.Y,&p2.X,&p2.Y,&q);
	for(ll i=1;i<=q;i++) {
		ll A, B;
		scanf("%lld%lld",&A,&B);
		qry[i] = pll(A, B);
		if(!mem[qry[i]]) {
			mem[qry[i]] = 1;
			if(drg[A].size() > 2*drg[B].size()) majo(A, B, i);
			else sadi(A, B, i);
		}
	}
	for(ll i=1;i<=m;i++) {
		cpr(p1, drg[i], rd[i], 0, z1, s1);
		cpr(p2, drg[i], rd[i], 1, z2, s2);
		for(auto &T : rd[i]) {
			add(T.s[0].X, T.e[0].X, T.s[1].X, T.e[1].X, T.i);
		}
		seg.init(); pt.clear();
		for(ll j=0;j<drg[i].size();j++) {
			pt.push_back(pll(z1[j], z2[j]));
		}
		sort(pt.begin(), pt.end());
		for(ll j=1,k=0;j<=s1;j++) {
			while(k < pt.size() && pt[k].X <= j) {
				seg.upd(pt[k].Y, 1);
				seg.upd(pt[k].Y+s2, 1);
				k++;
			}
			for(auto &T : swp[j]) {
				ans[T.i] += seg.get(T.s, T.e) * T.x;
			}
			swp[j].clear();
		}
	}
	for(ll i=1;i<=q;i++) {
		mem[qry[i]] += ans[i];
		printf("%lld\n", mem[qry[i]]-1);
	}
}

Compilation message

dragon2.cpp: In function 'int main()':
dragon2.cpp:153:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(ll j=0;j<drg[i].size();j++) {
               ^
dragon2.cpp:158:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    while(k < pt.size() && pt[k].X <= j) {
            ^
dragon2.cpp:129:25: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%lld%lld",&n,&m);
                         ^
dragon2.cpp:132:33: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%lld%lld%lld",&A,&B,&C);
                                 ^
dragon2.cpp:135:58: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%lld%lld%lld%lld%lld",&p1.X,&p1.Y,&p2.X,&p2.Y,&q);
                                                          ^
dragon2.cpp:138:26: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%lld%lld",&A,&B);
                          ^
# Verdict Execution time Memory Grader output
1 Correct 13 ms 49612 KB Output is correct
2 Correct 26 ms 50664 KB Output is correct
3 Correct 66 ms 62228 KB Output is correct
4 Correct 389 ms 75656 KB Output is correct
5 Correct 309 ms 59564 KB Output is correct
6 Correct 16 ms 49452 KB Output is correct
7 Correct 9 ms 49588 KB Output is correct
8 Correct 9 ms 49672 KB Output is correct
9 Correct 16 ms 49632 KB Output is correct
10 Correct 13 ms 49660 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 66 ms 52792 KB Output is correct
2 Correct 166 ms 67584 KB Output is correct
3 Correct 56 ms 52588 KB Output is correct
4 Correct 46 ms 50252 KB Output is correct
5 Correct 23 ms 49980 KB Output is correct
6 Correct 59 ms 53188 KB Output is correct
7 Correct 46 ms 52160 KB Output is correct
8 Correct 46 ms 52932 KB Output is correct
9 Correct 43 ms 53060 KB Output is correct
10 Correct 36 ms 52932 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 49612 KB Output is correct
2 Correct 26 ms 50664 KB Output is correct
3 Correct 66 ms 62228 KB Output is correct
4 Correct 389 ms 75656 KB Output is correct
5 Correct 309 ms 59564 KB Output is correct
6 Correct 16 ms 49452 KB Output is correct
7 Correct 9 ms 49588 KB Output is correct
8 Correct 9 ms 49672 KB Output is correct
9 Correct 16 ms 49632 KB Output is correct
10 Correct 13 ms 49660 KB Output is correct
11 Correct 66 ms 52792 KB Output is correct
12 Correct 166 ms 67584 KB Output is correct
13 Correct 56 ms 52588 KB Output is correct
14 Correct 46 ms 50252 KB Output is correct
15 Correct 23 ms 49980 KB Output is correct
16 Correct 59 ms 53188 KB Output is correct
17 Correct 46 ms 52160 KB Output is correct
18 Correct 46 ms 52932 KB Output is correct
19 Correct 43 ms 53060 KB Output is correct
20 Correct 36 ms 52932 KB Output is correct
21 Correct 63 ms 52808 KB Output is correct
22 Correct 176 ms 67924 KB Output is correct
23 Correct 973 ms 146876 KB Output is correct
24 Correct 1439 ms 245736 KB Output is correct
25 Correct 486 ms 75912 KB Output is correct
26 Correct 389 ms 60932 KB Output is correct
27 Correct 43 ms 51828 KB Output is correct
28 Correct 49 ms 52232 KB Output is correct
29 Correct 363 ms 63996 KB Output is correct
30 Correct 373 ms 57904 KB Output is correct
31 Correct 319 ms 58336 KB Output is correct
32 Correct 339 ms 66296 KB Output is correct
33 Correct 1002 ms 142076 KB Output is correct
34 Correct 186 ms 57708 KB Output is correct
35 Correct 276 ms 58544 KB Output is correct
36 Correct 329 ms 60200 KB Output is correct
37 Correct 356 ms 60964 KB Output is correct
38 Correct 1193 ms 146344 KB Output is correct
39 Correct 1209 ms 165608 KB Output is correct
40 Correct 1056 ms 135920 KB Output is correct
41 Correct 376 ms 64628 KB Output is correct
42 Correct 413 ms 69192 KB Output is correct
43 Correct 443 ms 80712 KB Output is correct
44 Correct 89 ms 55992 KB Output is correct
45 Correct 86 ms 54960 KB Output is correct
46 Correct 83 ms 56168 KB Output is correct
47 Correct 76 ms 53712 KB Output is correct
48 Correct 69 ms 53060 KB Output is correct
49 Correct 73 ms 53492 KB Output is correct