CSES - Aalto Competitive Programming 2024 - wk10 - Mon - Results
Submission details
Task:Polygon area
Sender:Niilo
Submission time:2024-11-11 16:23:18 +0200
Language:C++ (C++17)
Status:READY
Result:
Test results
testverdicttime
#1ACCEPTED0.00 sdetails
#20.01 sdetails
#30.00 sdetails

Code

#include <iostream>
#include <cmath>
#include <vector>
#include <cassert>
#include <algorithm>
using namespace std;
#define rep(i, a, b) for(int i=a;i<(b);++i)
#define all(x) begin(x),end(x)
#define sz(x) int((x).size())
using ll = long long;
using pii = pair<int,int>;
using vi = vector<int>;

constexpr double PI = 3.14159265358979323846;

/**
 * Author: Ulf Lundstrom
 * Date: 2009-02-26
 * License: CC0
 * Source: My head with inspiration from tinyKACTL
 * Description: Class to handle points in the plane.
 * 	T can be e.g. double or long long. (Avoid int.)
 * Status: Works fine, used a lot
 */
template <class T> int sgn(T x) { return (x > 0) - (x < 0); }
template<class T>
struct Point {
	typedef Point P;
	T x, y;
	explicit Point(T x=0, T y=0) : x(x), y(y) {}
	bool operator<(P p) const { return tie(x,y) < tie(p.x,p.y); }
	bool operator==(P p) const { return tie(x,y)==tie(p.x,p.y); }
	P operator+(P p) const { return P(x+p.x, y+p.y); }
	P operator-(P p) const { return P(x-p.x, y-p.y); }
	P operator*(T d) const { return P(x*d, y*d); }
	P operator/(T d) const { return P(x/d, y/d); }
	T dot(P p) const { return x*p.x + y*p.y; }
	T cross(P p) const { return x*p.y - y*p.x; }
	T cross(P a, P b) const { return (a-*this).cross(b-*this); }
	T dist2() const { return x*x + y*y; }
	double dist() const { return sqrt((double)dist2()); }
	// angle to x-axis in interval [-pi, pi]
	double angle() const { return atan2(y, x); }
	P unit() const { return *this/dist(); } // makes dist()=1
	P perp() const { return P(-y, x); } // rotates +90 degrees
	P normal() const { return perp().unit(); }
	// returns point rotated 'a' radians ccw around the origin
	P rotate(double a) const {
		return P(x*cos(a)-y*sin(a),x*sin(a)+y*cos(a)); }
  friend istream& operator>>(istream& os, P& p) {
		return os >> p.x >> p.y; }
	friend ostream& operator<<(ostream& os, P p) {
		return os << '(' << p.x << ',' << p.y << ')'; }
};
typedef Point<double> pd;

template<class P> bool onSegment(P s, P e, P p) {
	return p.cross(s, e) == 0 && (s - p).dot(e - p) <= 0;
}
/**
 * Author: Victor Lecomte, chilli
 * Date: 2019-04-27
 * License: CC0
 * Source: https://vlecomte.github.io/cp-geo.pdf
 * Description:\\
\begin{minipage}{75mm}
If a unique intersection point between the line segments going from s1 to e1 and from s2 to e2 exists then it is returned.
If no intersection point exists an empty vector is returned.
If infinitely many exist a vector with 2 elements is returned, containing the endpoints of the common line segment.
The wrong position will be returned if P is Point<ll> and the intersection point does not have integer coordinates.
Products of three coordinates are used in intermediate steps so watch out for overflow if using int or long long.
\end{minipage}
\begin{minipage}{15mm}
\includegraphics[width=\textwidth]{content/geometry/SegmentIntersection}
\end{minipage}
 * Usage:
 * vector<P> inter = segInter(s1,e1,s2,e2);
 * if (sz(inter)==1)
 *   cout << "segments intersect at " << inter[0] << endl;
 * Status: stress-tested, tested on kattis:intersection
 */
template<class P> vector<P> segInter(P a, P b, P c, P d) {
	auto oa = c.cross(d, a), ob = c.cross(d, b),
	     oc = a.cross(b, c), od = a.cross(b, d);
	// Checks if intersection is single non-endpoint point.
	if (sgn(oa) * sgn(ob) < 0 && sgn(oc) * sgn(od) < 0)
		return {(a * ob - b * oa) / (ob - oa)};
	vector<P> s;
	if (onSegment(c, d, a)) s.push_back(a);
	if (onSegment(c, d, b)) s.push_back(b);
	if (onSegment(a, b, c)) s.push_back(c);
	if (onSegment(a, b, d)) s.push_back(d);
	return s;
}

double polygonArea(const vector<pd>& p) {
  double sum = p.back().cross(p.front());
  rep(i,1,sz(p)) {
    sum += p[i-1].cross(p[i]);
  }
  return abs(sum);
}

int main() {
  int n;
	cin >> n;
	vector<pd> P(n);
	rep(i,0,n) {
		cin >> P[i];
	}
	cout << polygonArea(P) << '\n';
}

Test details

Test 1

Verdict: ACCEPTED

input
100
-7 -19
91 77
100 100
64 60
...

correct output
43582

user output
43582

Test 2

Verdict:

input
1000
365625896 -113418831
278762563 38777445
250367343 -96991975
175866909 -129766978
...

correct output
4053466653883387139

user output
4.05347e+18

Test 3

Verdict:

input
4
-1000000000 -1000000000
-1000000000 1000000000
1000000000 1000000000
1000000000 -1000000000

correct output
8000000000000000000

user output
8e+18