QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#420241 | #8669. 正方形计数 | Hanghang | 85 | 3808ms | 4260kb | C++14 | 4.5kb | 2024-05-24 15:44:35 | 2024-05-24 15:44:42 |
Judging History
answer
#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define il inline
#define cn const
typedef double db;
const db eps=1e-8,V=3003;
il int dcmp(cn db &x){return x>eps?1:x<-eps?-1:0;}
struct Point
{
db x,y;
void In(){cin>>x>>y;x+=1001;y+=1001;}
void Out(){cout<<x<<" "<<y<<endl;}
};
il bool operator ==(Point A,Point B){return !dcmp(A.x-B.x)&&!dcmp(A.y-B.y);}
typedef Point Vec;
il Vec operator +(cn Vec &A,cn Vec &B){return {A.x+B.x,A.y+B.y};}
il Vec operator -(cn Vec &A,cn Vec &B){return {A.x-B.x,A.y-B.y};}
il Vec operator *(cn Vec &A,cn db &d){return {A.x*d,A.y*d};}
il db Cro(cn Vec &A,cn Vec &B){return A.x*B.y-A.y*B.x;}
il db Dot(cn Vec &A,cn Vec &B){return A.x*B.x+A.y*B.y;}
il Point Cross_LL(cn Point &a,cn Point &b,cn Point &c,cn Point &d)
{
Vec x=b-a,y=d-c,z=a-c;
return a+x*(Cro(y,z)/Cro(x,y));
}
il bool Pan(cn Point &a,cn Point &b,cn Point &c,cn Point &d)
{
return dcmp(Cro(b-a,c-a))*dcmp(Cro(b-a,d-a))<0&&dcmp(Cro(d-c,a-c))*dcmp(Cro(d-c,b-c))<0;
}
il bool On(cn Point &p,cn Point &a,cn Point &b){return a.x<=p.x&&p.x<=b.x&&a.y<=p.y&&p.y<=b.y;}
il db Atan(Vec a){return atan2(a.y,a.x);}
struct Line
{
Point a,b;db arg;
il void Init(cn Point &_a,cn Point &_b){a=_a;b=_b;arg=Atan(b-a);}
il void Init(db _a,db _b,db _c,db _d){a={_a,_b};b={_c,_d};arg=Atan(b-a);}
void Out(){cout<<a.x<<" "<<a.y<<" "<<b.x<<" "<<b.y<<endl;}
}Q[23];
il bool operator ==(cn Line &A,cn Line &B){return A.a==B.a&&A.b==B.b;}
il Point Cross(cn Line &L1,cn Line &L2){return Cross_LL(L1.a,L1.b,L2.a,L2.b);}
il bool Chk(cn Line &L,cn Point &a){return dcmp(Cro(a-L.a,L.b-L.a))>0;}
il bool Cmp(cn Line &L1,cn Line &L2)
{
if(dcmp(L1.arg-L2.arg)!=0)return L1.arg<L2.arg;
return dcmp(Cro(L2.a-L1.a,L1.b-L1.a))>0;
}
il int Halfcut(Line *L,int n)
{
sort(L+1,L+n+1,Cmp);int m=n,h=1,t=0;n=0;
for(int i=1;i<=m;i++)if(i==1||dcmp(L[i].arg-L[i-1].arg))L[++n]=L[i];
for(int i=1;i<=n;i++)
{
while(h<t&&Chk(L[i],Cross(Q[t],Q[t-1])))t--;
while(h<t&&Chk(L[i],Cross(Q[h],Q[h+1])))h++;
Q[++t]=L[i];
}
while(h<t&&Chk(Q[h],Cross(Q[t],Q[t-1])))t--;
while(h<t&&Chk(Q[t],Cross(Q[h],Q[h+1])))h++;
for(int i=h;i<=t;i++)L[i-h+1]=Q[i];
return t-h+1;
}
il ll F(ll a,ll b,ll c,ll n)
{
if(!a)return b/c*(n+1);
if(a>=c||b>=c)return a/c*n*(n+1)/2+b/c*(n+1)+F(a%c,b%c,c,n);
return (a*n+b)/c*n-F(c,c-b-1,a,(a*n+b)/c-1);
}
il ll Sol(ll a,ll b,ll c,ll n)
{
ll s=0;
if(b<0)s+=(-b+c-1)/c*(n+1),b=(b%c+c)%c;
if(a<0)s+=(-a+c-1)/c*n*(n+1)/2,a=(a%c+c)%c;
return F(a,b,c,n)-s;
}
il bool Ok(cn Point &a,cn Point &b,cn Point &c,cn Point &d)
{
return On(a,c,d)||On(b,c,d);
}
const int N=23;
int n,m;
ll ans=0;
Point p[N];
Line L[N],c[N],d[N];
il bool In(cn Point &v)
{
for(int i=1;i<=n;i++)if(dcmp(Cro(p[i]-v,p[i+1]-v))<=0)return 0;
return 1;
}
il ll Calc(Point a,Point b,int l,int r,int op)
{
if(op==0)a.y-=0.0001,b.y-=0.0001;
if(a.x>b.x)swap(a,b);
db dt=b.x-a.x,k=b.y-a.y,w=a.y*dt-a.x*k;
dt*=2000,k*=2000,w*=2000;
return Sol(k,w+k*l,dt,r-l);
}
signed main()
{
cin>>n;
for(int i=1;i<=n;i++)p[i].In();
reverse(p+2,p+n+1);p[n+1]=p[1];
for(int i=1;i<=n;i++)L[i].Init(p[i],p[i+1]);
for(db x=1000;x<=V;x+=0.5)for(db y=dcmp(x-floor(x))*0.5+1000;y<=V;y+=1.0)if(In({x,y}))
{
Point v={x,y};m=0;
for(int i=1;i<=n;i++)
{
Point kx=p[i],ky=p[i+1];
if(Ok(kx,ky,v,{V,V})||Pan(kx,ky,{V,V},v))
L[++m].Init(kx,ky);
if(Ok(kx,ky,{0,0},v)||Pan(kx,ky,{0,0},v))
L[++m].Init(v*2-kx,v*2-ky);
if(Ok(kx,ky,{0,y},{x,V})||Pan(kx,ky,{0,V},v))
L[++m].Init(x+kx.y-y,y+x-kx.x,x+ky.y-y,y+x-ky.x);
if(Ok(kx,ky,{x,0},{V,y})||Pan(kx,ky,{V,0},v))
L[++m].Init(x+y-kx.y,y+kx.x-x,x+y-ky.y,y+ky.x-x);
}
L[++m].Init(v,{V,y});L[++m].Init({x,V},v);Line las=L[m];
m=Halfcut(L,m);L[0]=L[m];L[m+1]=L[1];
int C=0,D=0,st=0,dst=ceil(x+eps),ded=0,cst=0,ced=dst;
for(int i=1;i<=m;i++)if(L[i]==las){st=i;break;}
for(int i=st==m?1:st+1;i!=st;i=i==m?1:i+1)if(L[i].b.x!=L[i].a.x)
{
if(L[i].b.x>L[i].a.x)d[++D]=L[i];
else
{
c[++C]=L[i];
if(C==1)cst=ded=floor(Cross(L[i],L[i-1]).x);
}
}
for(int i=1;i<=D;i++)
{
int l=dst,r=i==D?ded:floor(Cross(d[i],d[i+1]).x);
if(l<=r)ans-=Calc(d[i].a,d[i].b,l,r,0),dst=r+1;
}
for(int i=1;i<=C;i++)
{
int r=cst,l=i==C?ced:ceil(Cross(c[i],c[i+1]).x+eps);
if(l<=r)ans+=Calc(c[i].a,c[i].b,l,r,1),cst=l-1;
}
}
cout<<ans;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 10
Accepted
Test #1:
score: 10
Accepted
time: 1015ms
memory: 4048kb
input:
4 131 603 131 1828 1919 1828 1919 603
output:
361182910200
result:
ok 1 number(s): "361182910200"
Test #2:
score: 0
Accepted
time: 32ms
memory: 4044kb
input:
4 239 211 239 962 261 962 261 211
output:
1498772
result:
ok 1 number(s): "1498772"
Test #3:
score: 0
Accepted
time: 1834ms
memory: 4104kb
input:
4 0 0 0 2000 2000 2000 2000 0
output:
1336001667000
result:
ok 1 number(s): "1336001667000"
Test #4:
score: 0
Accepted
time: 27ms
memory: 3988kb
input:
4 36 771 36 786 672 786 672 771
output:
427720
result:
ok 1 number(s): "427720"
Test #5:
score: 0
Accepted
time: 43ms
memory: 4088kb
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: 824ms
memory: 4176kb
input:
3 131 603 131 1828 1919 603
output:
63739309181
result:
ok 1 number(s): "63739309181"
Test #7:
score: 0
Accepted
time: 29ms
memory: 4020kb
input:
3 239 211 239 962 261 211
output:
353073
result:
ok 1 number(s): "353073"
Test #8:
score: 0
Accepted
time: 1174ms
memory: 4176kb
input:
3 0 0 0 2000 2000 0
output:
222889277611
result:
ok 1 number(s): "222889277611"
Test #9:
score: 0
Accepted
time: 23ms
memory: 4084kb
input:
3 36 771 36 786 672 771
output:
98847
result:
ok 1 number(s): "98847"
Test #10:
score: 0
Accepted
time: 23ms
memory: 4192kb
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: 48ms
memory: 4260kb
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: 45ms
memory: 4128kb
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: 15ms
memory: 4140kb
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: 43ms
memory: 4152kb
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: 36ms
memory: 4088kb
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: 24ms
memory: 4044kb
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: 165ms
memory: 4184kb
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: 129ms
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: 130ms
memory: 4240kb
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: 158ms
memory: 4256kb
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: 121ms
memory: 4156kb
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: 517ms
memory: 4152kb
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: 545ms
memory: 4132kb
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: 450ms
memory: 4240kb
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: 760ms
memory: 4236kb
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: 787ms
memory: 4172kb
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
Time Limit Exceeded
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: 3808ms
memory: 4144kb
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: -15
Time Limit Exceeded
input:
8 0 1841 1526 2000 1981 1680 1968 678 1893 26 973 0 616 315 524 434