Submission #447843

# Submission time Handle Problem Language Result Execution time Memory
447843 2021-07-27T17:39:27 Z koioi.org-koosaga Hexagonal Territory (APIO21_hexagon) C++17
60 / 100
75 ms 24140 KB
#include "hexagon.h"
#include <bits/stdc++.h>
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(), (v).end()
using namespace std;
typedef long long lint;
typedef pair<int, int> pi;
const int MAXN = 300005;
const int dx[6] = {0, 1, 1, 0, -1, -1};
const int dy[6] = {1, 1, 0, -1, -1, 0};
const int mod = 1e9 + 7;

struct mint {
    int val;
    mint() { val = 0; }
    mint(const lint& v) {
        val = (-mod <= v && v < mod) ? v : v % mod;
        if (val < 0) val += mod;
    }

    friend ostream& operator<<(ostream& os, const mint& a) { return os << a.val; }
    friend bool operator==(const mint& a, const mint& b) { return a.val == b.val; }
    friend bool operator!=(const mint& a, const mint& b) { return !(a == b); }
    friend bool operator<(const mint& a, const mint& b) { return a.val < b.val; }

    mint operator-() const { return mint(-val); }
    mint& operator+=(const mint& m) { if ((val += m.val) >= mod) val -= mod; return *this; }
    mint& operator-=(const mint& m) { if ((val -= m.val) < 0) val += mod; return *this; }
    mint& operator*=(const mint& m) { val = (lint)val*m.val%mod; return *this; }
    friend mint ipow(mint a, lint p) {
        mint ans = 1; for (; p; p /= 2, a *= a) if (p&1) ans *= a;
        return ans;
    }
    friend mint inv(const mint& a) { assert(a.val); return ipow(a, mod - 2); }
    mint& operator/=(const mint& m) { return (*this) *= inv(m); }

    friend mint operator+(mint a, const mint& b) { return a += b; }
    friend mint operator-(mint a, const mint& b) { return a -= b; }
    friend mint operator*(mint a, const mint& b) { return a *= b; }
    friend mint operator/(mint a, const mint& b) { return a /= b; }
    operator int64_t() const {return val; }
};

lint ccw(pi a, pi b, pi c){
	int dx1 = b.first - a.first;
	int dy1 = b.second - a.second;
	int dx2 = c.first - a.first;
	int dy2 = c.second - a.second;
	return 1ll * dx1 * dy2 - 1ll * dy1 * dx2;
}

bool turn(vector<pi> &a){
	lint A = 0;
	for(int i = 2; i < sz(a); i++) A += ccw(a[0], a[i-1], a[i]);
	return A > 0;
}

struct node{
	int p, s, e;
	bool operator<(const node &n)const{
		return pi(p, s) < pi(n.p, n.s);
	}
};

vector<int> len[200005];
vector<node> vect;
vector<int> gph[MAXN];

mint solve(int x, int p, int d){
	mint ret = mint(d) * mint(vect[x].e - vect[x].s + 1);
	for(auto &i : gph[x]){
		if(i != p) ret += solve(i, x, d + 1);
	}
	return ret;
}


int solve(vector<pi> v){
	vect.clear();
	for(int i = 0; i < MAXN; i++){
		gph[i].clear();
	}
	int xmin = 1e9;
	if(!turn(v)) reverse(v.begin() + 1, v.end());
	for(auto &[x, y] : v){
		xmin = min(xmin, x);
	}
	int xmax = 0;
	for(auto &[x, y] : v){
		x -= xmin;
		xmax = max(xmax, x);
	}
	for(int i = 0; i < sz(v); i++){
		auto s = v[i], e = v[(i + 1) % sz(v)];
		auto p = v[(i + sz(v) - 1) % sz(v)];
		if(s.first == e.first){
			if(s.second < e.second){
				if(p.first < s.first) len[s.first].push_back(s.second);
			}
			else{
				if(p.first > s.first) len[s.first].push_back(s.second);
			}
			continue;
		}
		int delta = (e.second - s.second) / (e.first - s.first);
		if(s.first < e.first){
			if(p.first < s.first || (p.first == s.first && p.second > s.second)) len[s.first].push_back(s.second);
			if(p.first > s.first){
				len[s.first].push_back(s.second);
				len[s.first].push_back(s.second);
			}
			for(int j = s.first + 1; j < e.first; j++) len[j].push_back(s.second + delta * (j - s.first));
		}
		else if(s.first > e.first){
			if(p.first > s.first || (p.first == s.first && p.second < s.second)) len[s.first].push_back(s.second);
			if(p.first < s.first){
				len[s.first].push_back(s.second);
				len[s.first].push_back(s.second);
			}
			for(int j = s.first - 1; j > e.first; j--) len[j].push_back(s.second + delta * (j - s.first));
		}
	}
	for(int i = 0; i <= xmax; i++){
		assert(sz(len[i]) % 2 == 0);
		sort(all(len[i]));
		vector<int> nlen = {len[i][0]};
		for(int j = 1; j < sz(len[i]) / 2; j++){
			if(len[i][2*j-1] + 1 == len[i][2*j]) continue;
			nlen.push_back(len[i][2*j-1]);
			nlen.push_back(len[i][2*j]);
		}
		nlen.push_back(len[i].back());
		len[i].clear();
		for(int j = 0; j < sz(nlen) / 2; j++){
			vect.push_back({i, nlen[2*j], nlen[2*j+1]});
		}
	}
		auto insec = [&](int j, int k){
			return max(vect[j].s, vect[k].s) <= min(vect[j].e + 1, vect[k].e);
		};
	for(int i = 0; i < xmax; i++){
		int p = lower_bound(all(vect), (node){i, -int(1e9), -1}) - vect.begin();
		int q = lower_bound(all(vect), (node){i+1, -int(1e9), -1}) - vect.begin();
		int r = lower_bound(all(vect), (node){i+2, -int(1e9), -1}) - vect.begin();
		int k = q;
		for(int j = p; j < q; j++){
			while(k < r && vect[k].e < vect[j].s) k++;
			if(insec(j, k)){
				while(k < r && insec(j, k)){
					gph[j].push_back(k);
					gph[k].push_back(j);
					k++;
				}
				k--;
			}
		}
	}
//	cout << "sz(vect) = " << sz(vect) << endl;
	for(int i = 0; i < sz(vect); i++){
//		printf("vect[%d] =(%d %d %d)\n", i, vect[i].p, vect[i].s, vect[i].e);
	}
	mint ans = 0;
	for(int i = 0; i < sz(vect); i++){
		if(vect[i].p == -xmin && vect[i].s <= 0 && 0 <= vect[i].e){
//			cout << "init " << i << endl;
			return solve(i, -1, 0);
		}
	}
	assert(0);
}

int draw_territory(int N, int _A, int _B,
		std::vector<int> D, std::vector<int> L) {
	for(auto &i : D) i--;
	vector<pi> a(N);
	for(int i = 1; i < N; i++){
		a[i].first = a[i-1].first + dx[D[i-1]] * L[i-1];
		a[i].second = a[i-1].second + dy[D[i-1]] * L[i-1];
	}
	lint A = 0;
	lint B = accumulate(all(L), 0ll);
	for(int i = 2; i < N; i++) A += ccw(a[0], a[i-1], a[i]);
	A = abs(A);
	lint I = (A - B + 2) / 2;
	mint sumA = mint(I + B) * mint(_A);
	mint sumB = 0;
	for(int _ = 0; _ < 3; _++){
		if(_B == 0) continue;
		vector<pi> a(N);
		for(int i = 1; i < N; i++){
			a[i].first = a[i-1].first + dx[D[i-1]] * L[i-1];
			a[i].second = a[i-1].second + dy[D[i-1]] * L[i-1];
		}
		sumB += solve(a);
		for(auto &j : D) j = (j + 1) % 6;
	}
	return sumA + mint(_B) * sumB / mint(2);
}

Compilation message

hexagon.cpp: In function 'int solve(std::vector<std::pair<int, int> >)':
hexagon.cpp:162:7: warning: variable 'ans' set but not used [-Wunused-but-set-variable]
  162 |  mint ans = 0;
      |       ^~~
# Verdict Execution time Memory Grader output
1 Correct 8 ms 11980 KB Output is correct
2 Correct 7 ms 11992 KB Output is correct
3 Correct 7 ms 11992 KB Output is correct
4 Correct 7 ms 11980 KB Output is correct
5 Correct 7 ms 11980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 11980 KB Output is correct
2 Correct 7 ms 11980 KB Output is correct
3 Runtime error 18 ms 24128 KB Execution killed with signal 11
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 8 ms 11980 KB Output is correct
2 Correct 9 ms 11996 KB Output is correct
3 Correct 9 ms 12088 KB Output is correct
4 Correct 9 ms 12032 KB Output is correct
5 Correct 10 ms 12044 KB Output is correct
6 Correct 8 ms 11980 KB Output is correct
7 Correct 9 ms 11980 KB Output is correct
8 Correct 9 ms 12080 KB Output is correct
9 Correct 9 ms 12108 KB Output is correct
10 Correct 9 ms 12108 KB Output is correct
11 Correct 8 ms 12108 KB Output is correct
12 Correct 9 ms 12000 KB Output is correct
13 Correct 10 ms 12108 KB Output is correct
14 Correct 9 ms 12108 KB Output is correct
15 Correct 8 ms 12108 KB Output is correct
16 Correct 9 ms 12108 KB Output is correct
17 Correct 9 ms 12108 KB Output is correct
18 Correct 10 ms 12108 KB Output is correct
19 Correct 9 ms 12108 KB Output is correct
20 Correct 8 ms 11980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 11980 KB Output is correct
2 Correct 8 ms 12108 KB Output is correct
3 Correct 7 ms 11980 KB Output is correct
4 Correct 7 ms 11980 KB Output is correct
5 Correct 8 ms 12108 KB Output is correct
6 Correct 9 ms 12264 KB Output is correct
7 Correct 13 ms 12748 KB Output is correct
8 Correct 7 ms 11980 KB Output is correct
9 Correct 7 ms 11980 KB Output is correct
10 Correct 7 ms 12104 KB Output is correct
11 Correct 16 ms 13144 KB Output is correct
12 Correct 16 ms 13200 KB Output is correct
13 Correct 15 ms 13052 KB Output is correct
14 Correct 17 ms 13260 KB Output is correct
15 Correct 7 ms 11980 KB Output is correct
16 Correct 7 ms 11980 KB Output is correct
17 Correct 7 ms 11980 KB Output is correct
18 Correct 7 ms 11980 KB Output is correct
19 Correct 6 ms 11980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 11980 KB Output is correct
2 Correct 7 ms 11980 KB Output is correct
3 Correct 7 ms 12032 KB Output is correct
4 Correct 9 ms 12108 KB Output is correct
5 Correct 7 ms 12056 KB Output is correct
6 Correct 7 ms 11980 KB Output is correct
7 Correct 8 ms 12108 KB Output is correct
8 Correct 9 ms 12236 KB Output is correct
9 Correct 13 ms 12676 KB Output is correct
10 Correct 7 ms 11980 KB Output is correct
11 Correct 7 ms 12016 KB Output is correct
12 Correct 7 ms 11980 KB Output is correct
13 Correct 17 ms 13196 KB Output is correct
14 Correct 16 ms 13208 KB Output is correct
15 Correct 15 ms 13132 KB Output is correct
16 Correct 17 ms 13236 KB Output is correct
17 Correct 7 ms 12068 KB Output is correct
18 Correct 7 ms 11964 KB Output is correct
19 Correct 7 ms 11980 KB Output is correct
20 Correct 22 ms 13820 KB Output is correct
21 Correct 9 ms 12228 KB Output is correct
22 Correct 8 ms 12108 KB Output is correct
23 Correct 28 ms 14536 KB Output is correct
24 Correct 48 ms 16076 KB Output is correct
25 Correct 41 ms 16324 KB Output is correct
26 Correct 24 ms 14016 KB Output is correct
27 Correct 19 ms 13516 KB Output is correct
28 Correct 15 ms 13016 KB Output is correct
29 Correct 47 ms 16672 KB Output is correct
30 Correct 44 ms 16684 KB Output is correct
31 Correct 45 ms 16680 KB Output is correct
32 Correct 47 ms 16680 KB Output is correct
33 Correct 24 ms 14196 KB Output is correct
34 Correct 16 ms 13056 KB Output is correct
35 Correct 7 ms 11980 KB Output is correct
36 Correct 7 ms 11980 KB Output is correct
37 Correct 7 ms 11980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 11980 KB Output is correct
2 Correct 9 ms 11980 KB Output is correct
3 Correct 8 ms 12100 KB Output is correct
4 Correct 9 ms 12092 KB Output is correct
5 Correct 9 ms 11980 KB Output is correct
6 Correct 9 ms 11980 KB Output is correct
7 Correct 9 ms 11980 KB Output is correct
8 Correct 9 ms 11988 KB Output is correct
9 Correct 14 ms 12108 KB Output is correct
10 Correct 9 ms 12108 KB Output is correct
11 Correct 9 ms 12112 KB Output is correct
12 Correct 9 ms 12108 KB Output is correct
13 Correct 9 ms 12112 KB Output is correct
14 Correct 9 ms 12052 KB Output is correct
15 Correct 9 ms 12108 KB Output is correct
16 Correct 9 ms 12108 KB Output is correct
17 Correct 9 ms 12108 KB Output is correct
18 Correct 7 ms 12040 KB Output is correct
19 Correct 8 ms 12108 KB Output is correct
20 Correct 7 ms 11980 KB Output is correct
21 Correct 7 ms 11980 KB Output is correct
22 Correct 8 ms 12108 KB Output is correct
23 Correct 8 ms 12236 KB Output is correct
24 Correct 13 ms 12796 KB Output is correct
25 Correct 8 ms 11980 KB Output is correct
26 Correct 7 ms 11980 KB Output is correct
27 Correct 7 ms 11980 KB Output is correct
28 Correct 16 ms 13068 KB Output is correct
29 Correct 16 ms 13208 KB Output is correct
30 Correct 15 ms 13132 KB Output is correct
31 Correct 16 ms 13260 KB Output is correct
32 Correct 7 ms 11980 KB Output is correct
33 Correct 7 ms 11980 KB Output is correct
34 Correct 41 ms 16688 KB Output is correct
35 Correct 32 ms 15048 KB Output is correct
36 Correct 27 ms 14656 KB Output is correct
37 Correct 40 ms 16452 KB Output is correct
38 Correct 44 ms 16828 KB Output is correct
39 Correct 49 ms 17600 KB Output is correct
40 Correct 54 ms 18200 KB Output is correct
41 Correct 42 ms 19780 KB Output is correct
42 Correct 45 ms 19220 KB Output is correct
43 Correct 41 ms 18064 KB Output is correct
44 Correct 73 ms 22068 KB Output is correct
45 Correct 74 ms 21276 KB Output is correct
46 Correct 73 ms 21468 KB Output is correct
47 Correct 75 ms 23104 KB Output is correct
48 Correct 48 ms 22892 KB Output is correct
49 Correct 48 ms 23484 KB Output is correct
50 Correct 7 ms 11980 KB Output is correct
51 Correct 7 ms 11980 KB Output is correct
52 Correct 7 ms 11972 KB Output is correct
53 Correct 75 ms 21060 KB Output is correct
54 Correct 75 ms 21016 KB Output is correct
55 Correct 9 ms 12108 KB Output is correct
56 Correct 9 ms 12108 KB Output is correct
57 Correct 9 ms 12032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 11980 KB Output is correct
2 Correct 7 ms 11980 KB Output is correct
3 Runtime error 18 ms 24116 KB Execution killed with signal 11
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 8 ms 11980 KB Output is correct
2 Correct 7 ms 11980 KB Output is correct
3 Correct 7 ms 11920 KB Output is correct
4 Runtime error 18 ms 24140 KB Execution killed with signal 11
5 Halted 0 ms 0 KB -