QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#418135 | #8669. 正方形计数 | zhicheng | 0 | 3125ms | 4164kb | C++14 | 5.6kb | 2024-05-23 11:05:18 | 2024-05-23 11:05:18 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int N=15;
const double eps=1e-9;
int ss[N<<2];
double ang[N<<2];
struct point{
double x,y;
}a[N],s[N<<2],sx,sy;
pair<point,point>p[N<<2],q[N<<2],pp[N<<2];
inline bool operator==(point a,point b){
return fabs(a.x-b.x)<eps&&fabs(a.y-b.y)<eps;
}
inline point operator+(point a,point b){
return {a.x+b.x,a.y+b.y};
}
inline point operator-(point a,point b){
return {a.x-b.x,a.y-b.y};
}
inline point operator*(point a,double b){
return {a.x*b,a.y*b};
}
inline point operator/(point a,double b){
return {a.x/b,a.y/b};
}
inline double operator^(point a,point b){
return a.x*b.y-a.y*b.x;
}
inline double angle(pair<point,point>a){
return atan2(a.second.y-a.first.y,a.second.x-a.first.x);
}
inline point intersect(pair<point,point>a,pair<point,point>b){
return a.first+(a.second-a.first)/(((a.second-b.first)^(b.second-b.first))+((b.second-b.first)^(a.first-b.first)))*((b.second-b.first)^(a.first-b.first));
}
inline double area(point *q,int n){
double ans=0;
for(int i=2;i<=n-1;i++){
ans+=((q[i]-q[1])^(q[i+1]-q[1]));
}
return ans/2;
}
int op;
inline bool check(pair<point,point>a,pair<point,point>b,pair<point,point>c){
point s=intersect(b,c);
if(op){
return ((a.second-s)^(a.first-s))>=0;
}
return ((a.second-s)^(a.first-s))>0;
}
inline int Half_Plane_Intersection(pair<point,point> *p,pair<point,point> *q,int n){
int cnt=0,front=1,back=2;
static pair<point,point>s[N],d[N],tmp[N];
for(int i=1;i<=n;i++){
tmp[i]=p[i];
}
s[++cnt]=p[1];
for(int i=2;i<=n;i++){
if(fabs(angle(p[i])-angle(p[i-1]))>eps){
s[++cnt]=p[i];
}
}
d[1]=s[1];
d[2]=s[2];
for(int i=3;i<=cnt;i++){
while(front<back&&check(s[i],d[back],d[back-1])){
back--;
}
while(front<back&&check(s[i],d[front],d[front+1])){
front++;
}
d[++back]=s[i];
}
while(front<back&&check(d[front],d[back],d[back-1])){
back--;
}
while(front<back&&check(d[back],d[front],d[front+1])){
front++;
}
cnt=0;
for(int i=front;i<=back;i++){
q[++cnt]=d[i];
}
for(int i=1;i<=n;i++){
p[i]=tmp[i];
}
return cnt;
}
inline bool check1(point a,pair<point,point> *p,int n){
for(int i=1;i<=n;i++){
if(((p[i].second-a)^(p[i].first-a))>0){
return 0;
}
}
return 1;
}
inline int f(int a,int b,int c,int n){
if(a>=c||b>=c){
return f(a%c,b%c,c,n)+a/c*n*(n+1)/2+(n+1)*(b/c);
}
int m=(a*n+b)/c;
if(m==0){
return 0;
}
return n*m-f(c,c-b-1,a,m-1);
}
inline int solve(point p,int dx,int dy,int l,int r){
int del,d=abs(__gcd(dx,dy)),ans=0;
dx/=d;
dy/=d;
for(int i=l;i<=r;i++){
ans+=p.y+(i-p.x)*dy/dx+1;
}
return ans;
// return f(dy,dx*p.y-p.x*dy,dx,r)-f(dy,dx*p.y-p.x*dy,dx,l-1)-del*(r-l+1)+(r-l+1);
}
inline int get(point p,int dx,int dy,int l,int r){
dx/=__gcd(dx,dy);
dx=abs(dx);
return floor((r-p.x)/dx)-floor((l-1-p.x)/dx);
}
inline bool isint(double x){
return fabs(x-floor(x))<eps||fabs(x-ceil(x))<eps;
}
int main(){
int n,maxx=0,cnt,cntt,cntp;
long long ans=0;
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%lf%lf",&a[i].x,&a[i].y);
maxx=max({(double)maxx,a[i].x,a[i].y});
}
a[n+1]=a[1];
reverse(a+1,a+n+2);
for(int i=0;i<=maxx;i++){
for(int j=1;j<=maxx;j++){
cntp=0;
for(int k=1;k<=n;k++){
p[++cntp]={a[k],a[k+1]};
p[++cntp]={a[k]+(point){-i,-j},a[k+1]+(point){-i,-j}};
p[++cntp]={a[k]+(point){-i-j,i-j},a[k+1]+(point){-i-j,i-j}};
p[++cntp]={a[k]+(point){-j,i},a[k+1]+(point){-j,i}};
}
for(int i=1;i<=cntp;i++){
ang[i]=angle(p[i]);
ss[i]=i;
pp[i]=p[i];
}
sort(ss+1,ss+cntp+1,[](int a,int b){
if(fabs(ang[a]-ang[b])>eps){
return ang[a]<ang[b];
}
return ((p[a].second-p[a].first)^(p[b].first-p[a].first))<0;
});
for(int i=1;i<=cntp;i++){
p[i]=pp[ss[i]];
}
op=0;
cnt=Half_Plane_Intersection(p,q,cntp);
cntt=1;
s[1]=intersect(q[cnt],q[1]);
for(int k=1;k<=cnt-1;k++){
s[++cntt]=intersect(q[k],q[k+1]);
if(s[cntt]==s[cntt-1]){
cntt--;
}
}
s[cntt+1]=s[1];
if(cntt==2&&s[1]==s[2]){
cntt=1;
}
if(isinf(s[1].x)||isnan(s[1].x)||!check1(s[1],p,cntp)){
continue;
}
if(cntt==1){
if(isint(s[1].x)&&isint(s[1].y)){
ans++;
}
continue;
}
else if(cntt==2){
cnt=Half_Plane_Intersection(p,q,cntp);
cntt=1;
for(int k=1;k<=cnt-1;k++){
s[++cntt]=intersect(q[k],q[k+1]);
}
s[cntt+1]=s[1]=intersect(q[cnt],q[1]);
goto lass;
}
op=1;
cnt=Half_Plane_Intersection(p,q,cntp);
cntt=1;
for(int k=1;k<=cnt-1;k++){
s[++cntt]=intersect(q[k],q[k+1]);
}
s[cntt+1]=s[1]=intersect(q[cnt],q[1]);
if(cntt==2&&s[1]==s[2]){
cntt=1;
}
lass:;
int las=ans;
for(int k=1;k<=cnt;k++){
sx=s[k];
sy=s[k+1];
if(q[k].first.x==q[k].second.x){
if(q[k].first.y>q[k].second.y){
ans+=floor(sx.y+eps)+1;
}
else{
ans-=floor(sx.y-eps)+1;
}
}
else{
if(q[k].first.x<q[k].second.x){
ans-=solve(q[k].first,q[k].second.x-q[k].first.x,q[k].second.y-q[k].first.y,ceil(sx.x),floor(sy.x-eps));
ans+=get(q[k].first,q[k].second.x-q[k].first.x,q[k].second.y-q[k].first.y,ceil(sx.x),floor(sy.x-eps));
if(k!=cnt&&q[k+1].first.x>q[k+1].second.x){
if(isint(s[k+1].x)){
ans-=floor(s[k+1].y-eps)+1;
}
}
if(k!=1&&q[k-1].first.x>q[k-1].second.x){
if(isint(s[k].x)){
ans+=floor(s[k].y-eps)+2;
}
}
}
else{
ans+=solve(q[k].first,q[k].first.x-q[k].second.x,q[k].first.y-q[k].second.y,ceil(sy.x+eps),floor(sx.x));
}
}
}
}
}
printf("%lld",ans);
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Time Limit Exceeded
Test #1:
score: 0
Time Limit Exceeded
input:
4 131 603 131 1828 1919 1828 1919 603
output:
result:
Subtask #2:
score: 0
Wrong Answer
Test #6:
score: 0
Wrong Answer
time: 3125ms
memory: 4164kb
input:
3 131 603 131 1828 1919 603
output:
41846818771
result:
wrong answer 1st numbers differ - expected: '63739309181', found: '41846818771'
Subtask #3:
score: 0
Time Limit Exceeded
Test #11:
score: 0
Time Limit Exceeded
input:
8 0 13 4 15 15 15 15 6 13 1 12 0 5 0 0 6
output:
result:
Subtask #4:
score: 0
Skipped
Dependency #3:
0%
Subtask #5:
score: 0
Skipped
Dependency #4:
0%
Subtask #6:
score: 0
Skipped
Dependency #1:
0%