Submission #447842

# Submission time Handle Problem Language Result Execution time Memory
447842 2021-07-27T17:37:06 Z koioi.org-koosaga Hexagonal Territory (APIO21_hexagon) C++17
41 / 100
2000 ms 24264 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]});
		}
	}
	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();
		for(int j = p; j < q; j++){
			for(int k = q; k < r; k++){
				if(max(vect[j].s, vect[k].s) <= min(vect[j].e + 1, vect[k].e)){
		//			printf("add edge %d %d\n", j, k);
					gph[j].push_back(k);
					gph[k].push_back(j);
				}
			}
		}
	}
//	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:156:7: warning: variable 'ans' set but not used [-Wunused-but-set-variable]
  156 |  mint ans = 0;
      |       ^~~
# Verdict Execution time Memory Grader output
1 Correct 7 ms 11996 KB Output is correct
2 Correct 7 ms 11980 KB Output is correct
3 Correct 7 ms 11980 KB Output is correct
4 Correct 9 ms 11980 KB Output is correct
5 Correct 7 ms 12032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 12044 KB Output is correct
2 Correct 7 ms 12036 KB Output is correct
3 Runtime error 18 ms 24264 KB Execution killed with signal 11
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 9 ms 12032 KB Output is correct
2 Correct 9 ms 12076 KB Output is correct
3 Correct 9 ms 12044 KB Output is correct
4 Correct 9 ms 12040 KB Output is correct
5 Correct 9 ms 12036 KB Output is correct
6 Correct 10 ms 12036 KB Output is correct
7 Correct 9 ms 11980 KB Output is correct
8 Correct 9 ms 11980 KB Output is correct
9 Correct 9 ms 12108 KB Output is correct
10 Correct 9 ms 12108 KB Output is correct
11 Correct 9 ms 12108 KB Output is correct
12 Correct 11 ms 12108 KB Output is correct
13 Correct 9 ms 12108 KB Output is correct
14 Correct 10 ms 12108 KB Output is correct
15 Correct 9 ms 12108 KB Output is correct
16 Correct 10 ms 12160 KB Output is correct
17 Correct 9 ms 12108 KB Output is correct
18 Correct 10 ms 12108 KB Output is correct
19 Correct 10 ms 12032 KB Output is correct
20 Correct 9 ms 12012 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 12036 KB Output is correct
2 Correct 9 ms 12176 KB Output is correct
3 Correct 7 ms 12008 KB Output is correct
4 Correct 7 ms 11980 KB Output is correct
5 Correct 8 ms 12048 KB Output is correct
6 Correct 9 ms 12236 KB Output is correct
7 Correct 13 ms 12876 KB Output is correct
8 Correct 7 ms 11980 KB Output is correct
9 Correct 7 ms 12044 KB Output is correct
10 Correct 7 ms 11980 KB Output is correct
11 Correct 17 ms 13312 KB Output is correct
12 Correct 17 ms 13388 KB Output is correct
13 Correct 16 ms 13240 KB Output is correct
14 Correct 17 ms 13388 KB Output is correct
15 Correct 7 ms 11980 KB Output is correct
16 Correct 8 ms 11988 KB Output is correct
17 Correct 7 ms 11980 KB Output is correct
18 Correct 7 ms 11980 KB Output is correct
19 Correct 9 ms 11980 KB Output is correct
# 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 11980 KB Output is correct
4 Correct 8 ms 12236 KB Output is correct
5 Correct 8 ms 11980 KB Output is correct
6 Correct 8 ms 12024 KB Output is correct
7 Correct 9 ms 12108 KB Output is correct
8 Correct 10 ms 12236 KB Output is correct
9 Correct 14 ms 12876 KB Output is correct
10 Correct 7 ms 11980 KB Output is correct
11 Correct 8 ms 11980 KB Output is correct
12 Correct 7 ms 12028 KB Output is correct
13 Correct 16 ms 13260 KB Output is correct
14 Correct 16 ms 13388 KB Output is correct
15 Correct 16 ms 13260 KB Output is correct
16 Correct 17 ms 13464 KB Output is correct
17 Correct 8 ms 12040 KB Output is correct
18 Correct 7 ms 12040 KB Output is correct
19 Correct 7 ms 11980 KB Output is correct
20 Correct 22 ms 14476 KB Output is correct
21 Correct 9 ms 12284 KB Output is correct
22 Correct 8 ms 12108 KB Output is correct
23 Correct 28 ms 15300 KB Output is correct
24 Correct 49 ms 17196 KB Output is correct
25 Correct 47 ms 17552 KB Output is correct
26 Correct 32 ms 14776 KB Output is correct
27 Correct 23 ms 13916 KB Output is correct
28 Correct 20 ms 13352 KB Output is correct
29 Correct 45 ms 17952 KB Output is correct
30 Correct 44 ms 17988 KB Output is correct
31 Correct 47 ms 17940 KB Output is correct
32 Correct 60 ms 17688 KB Output is correct
33 Correct 25 ms 14692 KB Output is correct
34 Correct 19 ms 13304 KB Output is correct
35 Correct 7 ms 11980 KB Output is correct
36 Correct 7 ms 11936 KB Output is correct
37 Correct 7 ms 12036 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 12040 KB Output is correct
2 Correct 9 ms 11980 KB Output is correct
3 Correct 9 ms 12108 KB Output is correct
4 Correct 9 ms 11988 KB Output is correct
5 Correct 9 ms 12108 KB Output is correct
6 Correct 9 ms 12108 KB Output is correct
7 Correct 9 ms 12036 KB Output is correct
8 Correct 9 ms 11980 KB Output is correct
9 Correct 9 ms 12032 KB Output is correct
10 Correct 9 ms 12108 KB Output is correct
11 Correct 11 ms 12052 KB Output is correct
12 Correct 10 ms 12104 KB Output is correct
13 Correct 9 ms 12108 KB Output is correct
14 Correct 11 ms 12108 KB Output is correct
15 Correct 9 ms 12156 KB Output is correct
16 Correct 10 ms 12108 KB Output is correct
17 Correct 9 ms 12108 KB Output is correct
18 Correct 7 ms 11980 KB Output is correct
19 Correct 9 ms 12236 KB Output is correct
20 Correct 7 ms 11980 KB Output is correct
21 Correct 8 ms 12044 KB Output is correct
22 Correct 8 ms 12044 KB Output is correct
23 Correct 9 ms 12236 KB Output is correct
24 Correct 13 ms 12816 KB Output is correct
25 Correct 7 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 20 ms 13260 KB Output is correct
29 Correct 16 ms 13324 KB Output is correct
30 Correct 16 ms 13308 KB Output is correct
31 Correct 20 ms 13388 KB Output is correct
32 Correct 7 ms 11980 KB Output is correct
33 Correct 7 ms 12040 KB Output is correct
34 Correct 44 ms 16580 KB Output is correct
35 Correct 35 ms 15112 KB Output is correct
36 Correct 30 ms 14768 KB Output is correct
37 Correct 43 ms 16508 KB Output is correct
38 Correct 61 ms 16880 KB Output is correct
39 Correct 53 ms 17652 KB Output is correct
40 Correct 60 ms 18308 KB Output is correct
41 Correct 147 ms 19740 KB Output is correct
42 Correct 123 ms 19256 KB Output is correct
43 Correct 94 ms 18084 KB Output is correct
44 Correct 82 ms 22320 KB Output is correct
45 Correct 73 ms 21620 KB Output is correct
46 Correct 74 ms 21748 KB Output is correct
47 Execution timed out 2059 ms 23336 KB Time limit exceeded
48 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 7 ms 11980 KB Output is correct
2 Correct 7 ms 11960 KB Output is correct
3 Runtime error 18 ms 24196 KB Execution killed with signal 11
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 10 ms 11980 KB Output is correct
2 Correct 7 ms 12036 KB Output is correct
3 Correct 8 ms 11980 KB Output is correct
4 Runtime error 19 ms 24112 KB Execution killed with signal 11
5 Halted 0 ms 0 KB -