SPOJ AMR10A Playground
O(N)
/*
* Author: NICK WONG
* Created Time: 7/30/2014 14:14:06
* File Name:
*/
#include<iostream>
#include<sstream>
#include<fstream>
#include<vector>
#include<list>
#include<deque>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cctype>
#include<cmath>
#include<ctime>
#include<iomanip>
using namespace std;
#define out(x) cout<<#x<<": "<<x<<endl
const double eps(1e-8);
const int maxn=50100;
const long long maxint=-1u>>1;
const long long maxlong=maxint*maxint;
typedef long long lint;
int n,q,ask[maxn][2],x,y;
double sum,ans,f[maxn];
struct Point
{
double x,y;
Point(double x=0,double y=0):x(x),y(y){}
};
typedef struct Point Vector;
Vector operator - (Point a,Point b) { return Vector(a.x-b.x,a.y-b.y);}
double Cross(Vector a, Vector b) { return a.x*b.y-a.y*b.x;}
double area(Point a, Point b, Point c)
{
Vector u,v;
u=a-b; v=a-c;
double area=abs(Cross(u,v))/2;
return area;
}
Point a[maxn];
void init()
{
cin >> n >> q;
for (int i=0; i<=n-1; i++)
scanf("%lf%lf",&a[i].x,&a[i].y);
for (int i=1; i<=q; i++)
scanf("%d%d",&ask[i][0],&ask[i][1]);
}
void work()
{
double one,two,three,tmp;
memset(f,0,sizeof(f));
for (int i=2; i<=n-1; i++)
{
tmp=area(a[0],a[i-1],a[i]);
f[i]=f[i-1]+tmp;
}
sum=f[n-1];
for (int i=1; i<=q; i++)
{
x=ask[i][0]; y=ask[i][1];
one=f[x];
two=area(a[0],a[x],a[y]);
three=sum-f[y];
ans=one+two+three;
ans=min(ans,sum-ans);
printf("%.1lf\n",ans);
}
}
int main()
{
init();
work();
return 0;
}
<table width="100%" style="font-family: Verdana, Arial, Helvetica, sans-serif; background-color: rgb(246, 249, 224); margin-top: 10px;"><tbody><tr><td style="font-size: 13px;"><h1 style="font-size: 20px; font-weight: normal; text-align: center;">8055. Playground</h1><h2 style="font-size: 16px; font-weight: normal; text-align: center;">Problem code: AMR10A</h2></td></tr></tbody></table><br style="color: rgb(0, 0, 32); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px; text-align: center; background-color: rgb(246, 249, 224);" /><a target=_blank rel="nofollow" href="https://www.digitalocean.com/?refcode=c59674ea4847" class="freelancer_728" style="text-decoration: none; color: rgb(0, 0, 160); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px; text-align: center; background-color: rgb(246, 249, 224);"><img src="http://www.spoj.com/gfx/digitalocean-horizontal-eps.png" width="410" height="109" border="0" alt="" /></a><span style="color: rgb(0, 0, 32); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px; text-align: center; background-color: rgb(246, 249, 224);"></span><p align="justify" style="font-size: 13px; text-align: justify; color: rgb(0, 0, 32); font-family: Verdana, Arial, Helvetica, sans-serif; background-color: rgb(246, 249, 224);"></p><p style="font-size: 13px; text-align: justify; color: rgb(0, 0, 32); font-family: Verdana, Arial, Helvetica, sans-serif; background-color: rgb(246, 249, 224);">My kid's school cleared a large field on their property recently to convert it into a playing area. The field is polygonal. The school administration decided to separate the field into two areas by building a straight picket fence between the area for the older kids and the area for the younger kids. The fence would go between two non-adjacent vertices of the polygonal field, and given the shape of the field, all such possible fences would lie strictly and entirely within the field.
Naturally, the smaller of the two areas would go to the younger kids. So can you help the school determine what the area of the smaller play-area would be for different fence positions?
<strong>INPUT</strong>
The first line contains 2 numbers N denoting the number of points in the convex polygon and Q denoting the number of possible locations of straight line fences.
The next N lines contain 2 integers each. The ith line contains the integers xi yi denoting the coordinates of the ith point of the polygon. The points are given in clockwise order.
The next Q lines contain 2 integers a b denoting that a straight line fence is to be drawn connecting a and b.
<strong>OUTPUT</strong>
Output Q lines one corresponding to each query. For each query, output the area of the smaller region for the corresponding query truncated to 1 decimal place. Always have 1 digit after the decimal place, so if the answer is 1, output it as 1.0 instead.
<strong>CONSTRAINTS</strong>
4 <= N <= 50000
1 <= Q <= 50000
-20,000,000 <= x,y <= 20,000,000
0 <= a < b-1
b < N
<strong>SAMPLE INPUT</strong>
4 2
0 0
0 1
1 2
1 0
1 3
0 2
<strong>SAMPLE OUTPUT</strong>
0.5
0.5
<strong>EXPLANATION</strong>
The polygon is given by the points (0,0) (0,1) (1,2) (1,0).
In the first query, we join the points (0,1) and (1,0) which leads to the 2 areas given by (0,0) (0,1) (1,0) and (0,1) (1,2) (1,0). The first triangle has an area of 0.5 and the second triangle has an area of 1. The minimum of these 2 is 0.5.
In the second query, we join the points (0,0) and (1,2) which leads to the 2 areas given by (0,0) (0,1) (1,2) and (0,0) (1,2) (1,0). The first triangle has an area of 0.5 and the second triangle has an area of 1. The minimum of these 2 is 0.5.</p>