Skip to content

Instantly share code, notes, and snippets.

@asi1024
Created October 11, 2016 04:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asi1024/5a1d9e382b0cf5778c6b358ac6a16158 to your computer and use it in GitHub Desktop.
Save asi1024/5a1d9e382b0cf5778c6b358ac6a16158 to your computer and use it in GitHub Desktop.
#include <bits/stdc++.h>
#define REP(i,n) for(int i=0;i<(int)(n);i++)
#define ALL(x) (x).begin(),(x).end()
using namespace std;
using ll = long long;
template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }
int x[128], y[128];
int main() {
int N;
while (cin >> N, N) {
vector<tuple<int,int,int>> edge;
REP(i,N) cin >> x[i] >> y[i];
REP(i,N)
if (x[i] == x[(i+1)%N])
edge.emplace_back(x[i], y[i], y[(i+1)%N]);
int left = 1e9, right = -1e9, top = 1e9, bot = -1e9;
int xx, yy;
REP(i,4) {
cin >> xx >> yy;
chmin(left, xx); chmax(right, xx);
chmin(top, yy); chmax(bot, yy);
}
ll res = 0;
for (auto t: edge) {
int x1, x2, from, to;
tie(x1, from, to) = t;
ll overlap = max(0, min(bot, max(from, to)) - max(top, min(from, to)));
if (to < from) overlap = -overlap;
ll not_overlap = to - from - overlap;
x2 = x1;
if (x1 > right) x2 -= right - left;
else if (x1 > left) x2 = left;
res += not_overlap * x1 + overlap * x2;
}
cout << res << endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment