QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#418651 | #8669. 正方形计数 | zhicheng | 50 | 3106ms | 4268kb | C++14 | 6.0kb | 2024-05-23 15:00:06 | 2024-05-23 15:00:07 |
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))>=-eps;
}
return ((a.second-s)^(a.first-s))>eps;
}
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<<2],d[N<<2],tmp[N<<2];
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))>eps){
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-i;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--;
}
}
while(cntt!=1&&s[cntt]==s[1]){
cntt--;
}
s[cntt+1]=s[1];
if(cntt==2&&s[1]==s[2]){
cntt=1;
}
int fl=0;
for(int k=1;k<=cntt;k++){
if(isinf(s[k].x)||isnan(s[k].x)||!check1(s[k],p,cntp)){
fl=1;
}
}
if(fl){
for(int k=1;k<=cntt;k++){
if(!(isinf(s[k].x)||isnan(s[k].x)||!check1(s[k],p,cntp))&&isint(s[k].x)&&isint(s[k].y)){
ans++;
}
}
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;
q[0]=q[cnt];
q[cnt+1]=q[1];
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-eps),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-eps),floor(sy.x-eps));
if(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(q[k-1].first.x>q[k-1].second.x){
if(isint(s[k].x)){
ans+=floor(s[k].y-eps)+1+isint(s[k].y);
}
}
}
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+eps));
}
}
}
// cerr<<i<<" "<<j<<" "<<ans-las<<"\n";
}
}
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
Time Limit Exceeded
Test #6:
score: 25
Accepted
time: 3106ms
memory: 4196kb
input:
3 131 603 131 1828 1919 603
output:
63739309181
result:
ok 1 number(s): "63739309181"
Test #7:
score: 0
Accepted
time: 209ms
memory: 4056kb
input:
3 239 211 239 962 261 211
output:
353073
result:
ok 1 number(s): "353073"
Test #8:
score: -25
Time Limit Exceeded
input:
3 0 0 0 2000 2000 0
output:
result:
Subtask #3:
score: 15
Accepted
Test #11:
score: 15
Accepted
time: 1ms
memory: 4152kb
input:
8 0 13 4 15 15 15 15 6 13 1 12 0 5 0 0 6
output:
4047
result:
ok 1 number(s): "4047"
Test #12:
score: 0
Accepted
time: 1ms
memory: 4208kb
input:
8 0 4 1 15 2 15 15 14 15 4 14 0 1 0 0 2
output:
4200
result:
ok 1 number(s): "4200"
Test #13:
score: 0
Accepted
time: 0ms
memory: 4212kb
input:
5 7 15 15 13 15 0 3 0 0 15
output:
3635
result:
ok 1 number(s): "3635"
Test #14:
score: 0
Accepted
time: 0ms
memory: 4156kb
input:
8 0 12 2 14 7 15 13 15 15 10 15 1 8 0 0 0
output:
4511
result:
ok 1 number(s): "4511"
Test #15:
score: 0
Accepted
time: 1ms
memory: 4232kb
input:
6 0 11 3 15 7 15 15 12 10 0 0 0
output:
3006
result:
ok 1 number(s): "3006"
Test #16:
score: 0
Accepted
time: 0ms
memory: 4108kb
input:
5 0 0 0 2 1 2 2 1 2 0
output:
4
result:
ok 1 number(s): "4"
Subtask #4:
score: 20
Accepted
Dependency #3:
100%
Accepted
Test #17:
score: 20
Accepted
time: 156ms
memory: 4228kb
input:
8 49 299 144 300 300 260 250 15 115 0 30 0 23 19 0 85
output:
443602646
result:
ok 1 number(s): "443602646"
Test #18:
score: 0
Accepted
time: 156ms
memory: 4164kb
input:
8 0 133 103 300 130 300 257 294 297 227 300 150 277 40 161 4
output:
351466521
result:
ok 1 number(s): "351466521"
Test #19:
score: 0
Accepted
time: 163ms
memory: 4212kb
input:
8 76 286 114 300 300 300 300 205 291 0 47 0 4 57 2 235
output:
605026927
result:
ok 1 number(s): "605026927"
Test #20:
score: 0
Accepted
time: 157ms
memory: 4148kb
input:
8 0 102 40 274 282 300 300 234 267 0 34 0 6 57 0 86
output:
497330741
result:
ok 1 number(s): "497330741"
Test #21:
score: 0
Accepted
time: 135ms
memory: 4268kb
input:
7 0 288 156 300 212 300 265 176 300 86 278 0 0 36
output:
446722651
result:
ok 1 number(s): "446722651"
Subtask #5:
score: 15
Accepted
Dependency #4:
100%
Accepted
Test #22:
score: 15
Accepted
time: 954ms
memory: 4140kb
input:
5 257 800 766 800 800 353 667 0 42 0
output:
18881369614
result:
ok 1 number(s): "18881369614"
Test #23:
score: 0
Accepted
time: 1216ms
memory: 4216kb
input:
8 691 800 737 795 800 651 372 98 136 266 118 318 24 629 12 753
output:
8760058886
result:
ok 1 number(s): "8760058886"
Test #24:
score: 0
Accepted
time: 1050ms
memory: 4212kb
input:
8 718 800 740 800 800 726 800 670 711 367 595 150 86 0 57 136
output:
3064355626
result:
ok 1 number(s): "3064355626"
Test #25:
score: 0
Accepted
time: 1526ms
memory: 4164kb
input:
8 0 347 16 449 364 798 674 800 750 800 797 14 195 0 0 70
output:
23587042437
result:
ok 1 number(s): "23587042437"
Test #26:
score: 0
Accepted
time: 1622ms
memory: 4156kb
input:
8 322 800 596 800 686 777 800 280 764 69 396 0 46 179 0 660
output:
23185884331
result:
ok 1 number(s): "23185884331"
Subtask #6:
score: 0
Skipped
Dependency #1:
0%