HNOI2016礦區(計算幾何+對偶圖)

WAautomaton發表於2019-02-23

題目連結

題目大意

給出一個平面圖,每次詢問一塊區域內所有多邊形面積的平方和除以所有多邊形的面積和。
n2×105n\le 2\times 10^5

題解

對偶圖神仙題orz……
平面圖顯然可以想到轉對偶圖做,轉化的方法就是對於每條沒有visit過的邊,找到它的反向邊,按照順時針轉動直到有一條新邊出現,再沿著那條邊走,直到走回來。
會發現,這樣走的話只會走出兩種區域,一種是平面圖中的最小區域(也就是對偶圖中的點),一種是把整個平面圖框起來的區域。對於後者,我們在對偶圖中建一個根對應它,所有在邊界上的區域和它連邊。

考慮怎麼求值。對於一個詢問,我們把所有在詢問邊界上邊對應的對偶圖中邊刪掉,那麼就會形成若干連通塊,要麼整個連通塊一起統計進答案,要麼都不能被統計進答案。
不妨讓詢問邊界上的邊都從內向外連邊。我們考慮對偶圖的一棵dfs樹,如果邊界上的某條邊是非樹邊,則不考慮。如果它是樹邊,若它從下往上連,那麼答案加上其子樹中所有節點的貢獻;否則答案減去其子樹中所有節點的貢獻。
這是因為考慮從根開始往下搜尋,如果搜尋到了某條從下向上連的邊,那麼就說明其進入了某個連通塊;否則就說明其走出了某個連通塊。因此答案是對的。
注意對偶圖有重邊的情況!!!!(Debug了2小時,好菜)
複雜度O(mlogm+k)O(mlogm+k)

#include <bits/stdc++.h>
namespace IOStream {
	const int MAXR = 10000000;
	char _READ_[MAXR], _PRINT_[MAXR];
	int _READ_POS_, _PRINT_POS_, _READ_LEN_;
	inline char readc() {
	#ifndef ONLINE_JUDGE
		return getchar();
	#endif
		if (!_READ_POS_) _READ_LEN_ = fread(_READ_, 1, MAXR, stdin);
		char c = _READ_[_READ_POS_++];
		if (_READ_POS_ == MAXR) _READ_POS_ = 0;
		if (_READ_POS_ > _READ_LEN_) return 0;
		return c;
	}
	template<typename T> inline void read(T &x) {
		x = 0; register int flag = 1, c;
		while (((c = readc()) < '0' || c > '9') && c != '-');
		if (c == '-') flag = -1; else x = c - '0';
		while ((c = readc()) >= '0' && c <= '9') x = x * 10 - '0' + c;
		x *= flag;
	}
	template<typename T1, typename ...T2> inline void read(T1 &a, T2&... x) {
		read(a), read(x...);
	}
	inline int reads(char *s) {
		register int len = 0, c;
		while (isspace(c = readc()) || !c);
		s[len++] = c;
		while (!isspace(c = readc()) && c) s[len++] = c;
		s[len] = 0;
		return len;
	}
	inline void ioflush() { fwrite(_PRINT_, 1, _PRINT_POS_, stdout), _PRINT_POS_ = 0; fflush(stdout); }
	inline void printc(char c) {
		if (!c) return;
		_PRINT_[_PRINT_POS_++] = c;
		if (_PRINT_POS_ == MAXR) ioflush();
	}
	inline void prints(const char *s, char c) {
		for (int i = 0; s[i]; i++) printc(s[i]);
		printc(c);
	}
	template<typename T> inline void print(T x, char c = '\n') {
		if (x < 0) printc('-'), x = -x;
		if (x) {
			static char sta[20];
			register int tp = 0;
			for (; x; x /= 10) sta[tp++] = x % 10 + '0';
			while (tp > 0) printc(sta[--tp]);
		} else printc('0');
		printc(c);
	}
	template<typename T1, typename ...T2> inline void print(T1 x, T2... y) {
		print(x, ' '), print(y...);
	}
}
using namespace IOStream;
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

struct HashTable {
	static const int BASE = 1 << 21, MOD = BASE - 1;
	ll key[BASE]; int val[BASE];
	inline int get_hash(ll x) {
		return (x >> 16 ^ x << 5 ^ x >> 1) & MOD;
	}
	inline int get_step(ll x) {
		return (x >> 15 ^ x << 6 ^ x >> 2) & MOD;
	}
	inline void ins(ll x, int v) {
		int h = get_hash(x), s = get_step(x) | 1;
		while (key[h]) (h += s) &= MOD;
		key[h] = x, val[h] = v;
	}
	inline int get(ll x) {
		int h = get_hash(x), s = get_step(x) | 1;
		while (key[h] != x) (h += s) &= MOD;
		return val[h];
	}
} hs;
const int MAXP = 200005, MAXN = 400005, MAXM = 1200005;
struct Linker { int u, v; double ang; } lnk[MAXM];
struct Edge { int fr, to, next, tp; } edge[MAXM];
int bel[MAXM], xx[MAXP], yy[MAXP], vis[MAXM], nxt[MAXM];
int arr[MAXP], head[MAXN], rt, n, m, K, tot, bcnt;
ll area[MAXN], sqr[MAXN], sum[MAXN];
vector<int> vec[MAXP];
inline bool cmp(int a, int b) { return lnk[a].ang < lnk[b].ang; }
inline void link(int u, int v) {
	lnk[tot] = (Linker) { u, v, atan2(yy[v] - yy[u], xx[v] - xx[u]) };
	vec[u].push_back(tot++);
}
void dfs(int u, int fa) {
	vis[u] = 1;
	sum[u] = area[u], sqr[u] = area[u] * area[u];
	for (int i = head[u]; ~i; i = edge[i].next) {
		int v = edge[i].to;
		if (vis[v]) continue;
		edge[i].tp = 1, edge[i ^ 1].tp = 2, dfs(v, u);
		sum[u] += sum[v], sqr[u] += sqr[v];
	}
}
inline void addedge(int u, int v) {
	edge[tot] = (Edge) { u, v, head[u], 0 };
	head[u] = tot++;
}
int main() {
	read(n, m, K);
	for (int i = 1; i <= n; i++) read(xx[i], yy[i]);
	for (int i = 1; i <= m; i++) {
		int u, v; read(u, v);
		link(u, v), link(v, u);
	}
	for (int i = 1; i <= n; i++) {
		sort(vec[i].begin(), vec[i].end(), cmp);
		for (int j = 1; j < (int)vec[i].size(); j++)
			nxt[vec[i][j]] = vec[i][j - 1];
		nxt[vec[i][0]] = vec[i].back();
	}
	int mm = tot; ll lstans = 0;
	for (int i = 0; i < mm; i++) if (!vis[i]) {
		++bcnt;
		for (int j = i; !vis[j]; j = nxt[j ^ 1]) {
			vis[j] = 1;
			bel[j] = bcnt;
			ll xa = xx[lnk[j].u], ya = yy[lnk[j].u];
			ll xb = xx[lnk[j].v], yb = yy[lnk[j].v];
			area[bcnt] += xa * yb - ya * xb;
		}
		if (area[bcnt] < 0) rt = bcnt;
	}
	tot = 0;
	memset(vis, 0, sizeof(vis));
	memset(head, -1, sizeof(head));
	for (int i = 0; i < mm; i++) if (bel[i] != bel[i ^ 1]) {
		addedge(bel[i], bel[i ^ 1]);
		hs.ins((ll)lnk[i].u * n + lnk[i].v, tot - 1);
	}
	dfs(rt, 0);
	while (K--) {
		int c; read(c); c = (c + lstans) % n + 1;
		ll fz = 0, fm = 0;
		for (int i = 0; i < c; i++) {
			read(arr[i]); arr[i] = (arr[i] + lstans) % n + 1;
			if (i > 0) {
				int p = hs.get((ll)arr[i - 1] * n + arr[i]);
				if (edge[p].tp == 1) fz -= sqr[edge[p].to], fm -= sum[edge[p].to];
				else if (edge[p].tp == 2) fz += sqr[edge[p].fr], fm += sum[edge[p].fr];
			}
		}
		int p = hs.get((ll)arr[c - 1] * n + arr[0]);
		if (edge[p].tp == 1) fz -= sqr[edge[p].to], fm -= sum[edge[p].to];
		else if (edge[p].tp == 2) fz += sqr[edge[p].fr], fm += sum[edge[p].fr];
		fm *= 2;
		ll g = __gcd(fz, fm);
		print(lstans = fz / g, fm / g);
	}
	ioflush();
	return 0;
}

相關文章