Submission #379809

# Submission time Handle Problem Language Result Execution time Memory
379809 2021-03-19T10:57:35 Z pit4h Dragon 2 (JOI17_dragon2) C++14
100 / 100
858 ms 36044 KB
#include<bits/stdc++.h>
#define st first
#define nd second
#define mp make_pair
using namespace std;
using ll = long long;
using pii = pair<int, int>;
const int MAXN = 3e4+1, MAXQ = 1e5+1;
int n, m, t[MAXN], q, ans[MAXQ];

int sgn(ll x) {
	return (x>=0)?x? 1: 0: -1;
}
struct Point {
	ll x, y;
	int id;
	Point() {}
	Point(ll _x, ll _y): x(_x), y(_y) {}
	void read() { cin>>x>>y; }
	Point operator-(const Point& o) const { return Point(x-o.x, y-o.y); }
	
	bool operator<(const Point& o) const { return make_pair(x, y) < make_pair(o.x, o.y); }
	ll cross(const Point& o) {
		return (ll)x*o.y - (ll)y*o.x;
	}
	ll cross(const Point& o1, const Point& o2) {
		return (o1 - *this).cross(o2 - *this);
	}
};
Point O1, O2;
bool cmp(Point p1, Point p2) { // clockwise
	if(sgn(O1.cross(O2, p1)) != sgn(O1.cross(O2, p2))) {
		return (O1.cross(O2, p1) < 0LL)? true : false;
	}
	return O1.cross(p1, p2) < 0LL;
}
Point p[MAXN], pp[MAXN], s[MAXN];
vector<int> tribe[MAXN];
vector<pair<int, int>> queries[MAXN];

int bit[MAXN];
void add(int idx, int delta) {
	assert(idx >= 0);
	for(; idx < n; idx = idx | (idx+1)) {
		bit[idx] += delta;
	}
}
int sum(int r) {
	assert(r >= 0);
	int res = 0;
	for(; r>=0; r = (r & (r+1)) - 1) {
		res += bit[r];
	}
	return res;
}
int sum(int l, int r) { 
	return sum(r) - ((l>0)?sum(l-1): 0);
}

void solve(vector<Point> pts, vector<pair<Point, int>> qs) {
	sort(pts.begin(), pts.end());
	sort(qs.begin(), qs.end());
	int j = 0;
	for(int i=0; i<(int)pts.size(); ++i) {
		while(j < (int)qs.size() && qs[j].st < pts[i]) {
			ans[qs[j].nd] += sum(0, qs[j].st.y-1);
			j++;
		}
		add(pts[i].y-1, 1);
	}
	while(j < (int)qs.size()) {
		ans[qs[j].nd] += sum(0, qs[j].st.y-1);
		j++;
	}
	for(int i=0; i<(int)pts.size(); ++i) {
		add(pts[i].y-1, -1);
	}
}

Point reflect(Point pt, Point o) {
	ll dx = o.x - pt.x, dy = o.y - pt.y;
	pt.x += 2LL*dx, pt.y += 2LL*dy;
	return pt;
}
int main() {
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin>>n>>m;
	for(int i=1; i<=n; ++i) {
		p[i].read();	
		p[i].id = i;
		cin>>t[i];
		tribe[t[i]].push_back(i);
	}
	Point P1, P2;
	P1.read(), P2.read();
	if(P2 < P1) swap(P1, P2);

	for(int i=1; i<=n; ++i) {
		if(P1.cross(P2, p[i]) > 0LL) {
			pp[i] = reflect(p[i], P1);	
		}
		else {
			pp[i] = p[i];
		}
	}
	O1 = P1, O2 = P2;
	sort(pp+1, pp+n+1, cmp);
	for(int i=1; i<=n; ++i) {
		s[pp[i].id].x = i;
	}
	
	for(int i=1; i<=n; ++i) { 
		if(P2.cross(P1, p[i]) < 0LL) {
			pp[i] = reflect(p[i], P2);	
		}
		else {
			pp[i] = p[i];
		}
	}
	O1 = P2, O2 = P1;
	sort(pp+1, pp+n+1, cmp);
	for(int i=1; i<=n; ++i) {
		s[pp[i].id].y = i;
	}

	cin>>q;
	for(int i=1; i<=q; ++i) {
		int a, b; cin>>a>>b;
		if(tribe[a].size() > tribe[b].size()) {
			for(auto j: tribe[b]) {
				queries[a].push_back(mp(j, -i));
			}
		}
		else {
			for(auto j: tribe[a]) {
				queries[b].push_back(mp(j, i));
			}
		}
	}
	
	for(int i=1; i<=m; ++i) {
		vector<Point> up, down, up_r, down_r;
		vector<pair<Point, int>> upq, downq, upq_r, downq_r;
		for(auto j: tribe[i]) {
			if(P1.cross(P2, p[j]) < 0LL) {
				down.push_back(Point(s[j].x, n-s[j].y+1));
				down_r.push_back(Point(n-s[j].x+1, s[j].y));
			}
			else {
				up.push_back(Point(n-s[j].x+1, s[j].y));
				up_r.push_back(Point(s[j].x, n-s[j].y+1));
			}
		}
		for(auto j: queries[i]) {
			if(j.nd > 0) {
				upq.push_back(mp(Point(n-s[j.st].x+1, s[j.st].y), j.nd));
				downq.push_back(mp(Point(s[j.st].x, n-s[j.st].y+1), j.nd));
			}
			else {
				if(P1.cross(P2, p[j.st]) > 0LL) {
					upq_r.push_back(mp(Point(s[j.st].x, n-s[j.st].y+1), -j.nd));
					downq.push_back(mp(Point(s[j.st].x, n-s[j.st].y+1), -j.nd));
				}
				else {
					upq.push_back(mp(Point(n-s[j.st].x+1, s[j.st].y), -j.nd));
					downq_r.push_back(mp(Point(n-s[j.st].x+1, s[j.st].y), -j.nd));
				}
			}
		}
		solve(up, upq);
		solve(down, downq);
		solve(up_r, upq_r);
		solve(down_r, downq_r);
	}

	for(int i=1; i<=q; ++i) {
		cout<<ans[i]<<'\n';
	}
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 2408 KB Output is correct
2 Correct 8 ms 2660 KB Output is correct
3 Correct 40 ms 4124 KB Output is correct
4 Correct 74 ms 5484 KB Output is correct
5 Correct 48 ms 3436 KB Output is correct
6 Correct 5 ms 2412 KB Output is correct
7 Correct 7 ms 2300 KB Output is correct
8 Correct 5 ms 2508 KB Output is correct
9 Correct 5 ms 2408 KB Output is correct
10 Correct 5 ms 2392 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 41 ms 7600 KB Output is correct
2 Correct 90 ms 9740 KB Output is correct
3 Correct 42 ms 5048 KB Output is correct
4 Correct 33 ms 4460 KB Output is correct
5 Correct 39 ms 4844 KB Output is correct
6 Correct 37 ms 7380 KB Output is correct
7 Correct 38 ms 7380 KB Output is correct
8 Correct 42 ms 7272 KB Output is correct
9 Correct 41 ms 7268 KB Output is correct
10 Correct 44 ms 7292 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 2408 KB Output is correct
2 Correct 8 ms 2660 KB Output is correct
3 Correct 40 ms 4124 KB Output is correct
4 Correct 74 ms 5484 KB Output is correct
5 Correct 48 ms 3436 KB Output is correct
6 Correct 5 ms 2412 KB Output is correct
7 Correct 7 ms 2300 KB Output is correct
8 Correct 5 ms 2508 KB Output is correct
9 Correct 5 ms 2408 KB Output is correct
10 Correct 5 ms 2392 KB Output is correct
11 Correct 41 ms 7600 KB Output is correct
12 Correct 90 ms 9740 KB Output is correct
13 Correct 42 ms 5048 KB Output is correct
14 Correct 33 ms 4460 KB Output is correct
15 Correct 39 ms 4844 KB Output is correct
16 Correct 37 ms 7380 KB Output is correct
17 Correct 38 ms 7380 KB Output is correct
18 Correct 42 ms 7272 KB Output is correct
19 Correct 41 ms 7268 KB Output is correct
20 Correct 44 ms 7292 KB Output is correct
21 Correct 41 ms 7656 KB Output is correct
22 Correct 83 ms 10752 KB Output is correct
23 Correct 532 ms 23204 KB Output is correct
24 Correct 858 ms 36044 KB Output is correct
25 Correct 111 ms 7788 KB Output is correct
26 Correct 87 ms 6124 KB Output is correct
27 Correct 42 ms 5740 KB Output is correct
28 Correct 43 ms 5868 KB Output is correct
29 Correct 84 ms 10964 KB Output is correct
30 Correct 73 ms 8104 KB Output is correct
31 Correct 74 ms 7788 KB Output is correct
32 Correct 89 ms 11092 KB Output is correct
33 Correct 534 ms 23636 KB Output is correct
34 Correct 76 ms 7404 KB Output is correct
35 Correct 77 ms 7404 KB Output is correct
36 Correct 82 ms 7788 KB Output is correct
37 Correct 87 ms 7916 KB Output is correct
38 Correct 552 ms 22884 KB Output is correct
39 Correct 593 ms 24620 KB Output is correct
40 Correct 542 ms 23528 KB Output is correct
41 Correct 88 ms 10388 KB Output is correct
42 Correct 111 ms 12168 KB Output is correct
43 Correct 131 ms 11736 KB Output is correct
44 Correct 48 ms 8140 KB Output is correct
45 Correct 48 ms 6928 KB Output is correct
46 Correct 47 ms 6704 KB Output is correct
47 Correct 47 ms 8004 KB Output is correct
48 Correct 46 ms 7088 KB Output is correct
49 Correct 46 ms 6576 KB Output is correct