QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#418779 | #8669. 正方形计数 | zhicheng | 100 ✓ | 3873ms | 4316kb | C++14 | 6.1kb | 2024-05-23 15:41:28 | 2024-05-23 15:41:29 |
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(ang[ss[i]]-ang[ss[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;
del=max(double(0),-floor(p.y-p.x*dy/dx));
p.y+=del;
if(dy<0){
dy=-dy;
p.x=-p.x;
swap(l,r);
l=-l;
r=-r;
p.x+=-l;
r+=-l;
l=0;
}
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));
}
}
}
}
}
printf("%lld",ans);
}
详细
Subtask #1:
score: 10
Accepted
Test #1:
score: 10
Accepted
time: 754ms
memory: 4132kb
input:
4 131 603 131 1828 1919 1828 1919 603
output:
361182910200
result:
ok 1 number(s): "361182910200"
Test #2:
score: 0
Accepted
time: 142ms
memory: 4088kb
input:
4 239 211 239 962 261 962 261 211
output:
1498772
result:
ok 1 number(s): "1498772"
Test #3:
score: 0
Accepted
time: 1077ms
memory: 4080kb
input:
4 0 0 0 2000 2000 2000 2000 0
output:
1336001667000
result:
ok 1 number(s): "1336001667000"
Test #4:
score: 0
Accepted
time: 94ms
memory: 4112kb
input:
4 36 771 36 786 672 786 672 771
output:
427720
result:
ok 1 number(s): "427720"
Test #5:
score: 0
Accepted
time: 13ms
memory: 4208kb
input:
4 0 100 100 200 200 100 100 0
output:
34001650
result:
ok 1 number(s): "34001650"
Subtask #2:
score: 25
Accepted
Test #6:
score: 25
Accepted
time: 570ms
memory: 4272kb
input:
3 131 603 131 1828 1919 603
output:
63739309181
result:
ok 1 number(s): "63739309181"
Test #7:
score: 0
Accepted
time: 124ms
memory: 4148kb
input:
3 239 211 239 962 261 211
output:
353073
result:
ok 1 number(s): "353073"
Test #8:
score: 0
Accepted
time: 651ms
memory: 4216kb
input:
3 0 0 0 2000 2000 0
output:
222889277611
result:
ok 1 number(s): "222889277611"
Test #9:
score: 0
Accepted
time: 81ms
memory: 4308kb
input:
3 36 771 36 786 672 771
output:
98847
result:
ok 1 number(s): "98847"
Test #10:
score: 0
Accepted
time: 2ms
memory: 4236kb
input:
3 0 0 0 100 100 0
output:
1473186
result:
ok 1 number(s): "1473186"
Subtask #3:
score: 15
Accepted
Test #11:
score: 15
Accepted
time: 1ms
memory: 4212kb
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: 4192kb
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: 4312kb
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: 1ms
memory: 4316kb
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: 0ms
memory: 4160kb
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: 4240kb
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: 72ms
memory: 4268kb
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: 73ms
memory: 4192kb
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: 73ms
memory: 4100kb
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: 67ms
memory: 4248kb
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: 58ms
memory: 4300kb
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: 281ms
memory: 4204kb
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: 408ms
memory: 4268kb
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: 397ms
memory: 4188kb
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: 454ms
memory: 4228kb
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: 504ms
memory: 4272kb
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: 15
Accepted
Dependency #1:
100%
Accepted
Dependency #2:
100%
Accepted
Dependency #3:
100%
Accepted
Dependency #4:
100%
Accepted
Dependency #5:
100%
Accepted
Test #27:
score: 15
Accepted
time: 3060ms
memory: 4252kb
input:
8 0 1150 314 2000 1101 2000 1617 1607 1778 551 738 0 607 10 0 1011
output:
577130875850
result:
ok 1 number(s): "577130875850"
Test #28:
score: 0
Accepted
time: 3384ms
memory: 4192kb
input:
8 0 1841 1526 2000 1981 1680 1968 678 1893 26 973 0 616 315 524 434
output:
735496008519
result:
ok 1 number(s): "735496008519"
Test #29:
score: 0
Accepted
time: 2428ms
memory: 4204kb
input:
6 0 258 10 2000 1730 2000 2000 1510 1973 0 0 129
output:
1203935109430
result:
ok 1 number(s): "1203935109430"
Test #30:
score: 0
Accepted
time: 2624ms
memory: 4300kb
input:
7 200 2000 1686 2000 1951 1878 2000 863 1422 0 21 0 0 1015
output:
1100462975231
result:
ok 1 number(s): "1100462975231"
Test #31:
score: 0
Accepted
time: 3873ms
memory: 4264kb
input:
8 701 2000 1449 2000 1847 1928 2000 1496 1987 668 1588 108 263 0 0 1985
output:
997591862206
result:
ok 1 number(s): "997591862206"
Test #32:
score: 0
Accepted
time: 3211ms
memory: 4224kb
input:
8 15 2000 1235 2000 1545 1886 1970 1526 1828 427 1238 97 372 0 0 1786
output:
816089046494
result:
ok 1 number(s): "816089046494"
Test #33:
score: 0
Accepted
time: 2404ms
memory: 4100kb
input:
7 0 1685 1331 2000 2000 1941 2000 1310 1757 631 21 113 0 575
output:
633230324466
result:
ok 1 number(s): "633230324466"
Test #34:
score: 0
Accepted
time: 2629ms
memory: 4236kb
input:
8 0 650 0 1350 650 2000 1350 2000 2000 1350 2000 650 1350 0 650 0
output:
900037062925
result:
ok 1 number(s): "900037062925"