QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#419434#8669. 正方形计数Hanghang0 0ms0kbC++144.6kb2024-05-23 22:06:482024-05-23 22:06:49

Judging History

你现在查看的是最新测评结果

  • [2024-05-23 22:06:49]
  • 评测
  • 测评结果:0
  • 用时:0ms
  • 内存:0kb
  • [2024-05-23 22:06:48]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef double db;
const db eps=1e-8,V=2003;
int dcmp(db x){return x>eps?1:x<-eps?-1:0;}
struct Point
{
    db x,y;
    void In(){cin>>x>>y;x++;y++;}
    void Out(int op=1){}
};
bool operator ==(Point A,Point B){return !dcmp(A.x-B.x)&&!dcmp(A.y-B.y);}
typedef Point Vec;
Vec operator +(Vec A,Vec B){return {A.x+B.x,A.y+B.y};}
Vec operator -(Vec A,Vec B){return {A.x-B.x,A.y-B.y};}
Vec operator *(Vec A,db d){return {A.x*d,A.y*d};}
db Cro(Vec A,Vec B){return A.x*B.y-A.y*B.x;}
db Dot(Vec A,Vec B){return A.x*B.x+A.y*B.y;}
Point Cross_LL(Point a,Point b,Point c,Point d)
{
    Vec x=b-a,y=d-c,z=a-c;
    return a+x*(Cro(y,z)/Cro(x,y));
}
bool Pan(Point a,Point b,Point c,Point d)
{
    db c1=Cro(b-a,c-a),c2=Cro(b-a,d-a);
    db d1=Cro(d-c,a-c),d2=Cro(d-c,b-c);
    return dcmp(c1)*dcmp(c2)<0&&dcmp(d1)*dcmp(d2)<0;
}
int OnSeg(Point p,Point a,Point b){return !dcmp(Cro(p-a,b-a))&&dcmp(Dot(p-a,p-b))<=0;}
int On(Point a,Point b,Point c,Point d)
{
    return OnSeg(a,c,d)||OnSeg(b,c,d)||OnSeg(c,a,b)||OnSeg(d,a,b);
}
struct Line
{
    Point a,b;
    void Out(){}
};
bool operator ==(Line A,Line B){return A.a==B.a&&A.b==B.b;}
db Atan(Vec a){return atan2(a.y,a.x);}
Point Cross(Line L1,Line L2){return Cross_LL(L1.a,L1.b,L2.a,L2.b);}
int Chk(Line L,Point a){return dcmp(Cro(a-L.a,L.b-L.a))>0;}
bool Cmp(Line L1,Line L2)
{
	db t1=Atan(L1.b-L1.a),t2=Atan(L2.b-L2.a);
	if(dcmp(t1-t2)!=0)return t1<t2;
	return dcmp(Cro(L2.a-L1.a,L1.b-L1.a))>0;
}
int Halfcut(Line *L,int n)
{
	sort(L+1,L+n+1,Cmp);int m=n,h=1,t=0;Line Q[n+1];n=0;
    for(int i=1;i<=m;i++)if(i==1||dcmp(Atan(L[i].b-L[i].a)-Atan(L[i-1].b-L[i-1].a)))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];
	cout<<t-h+1<<endl;
	return t-h+1;
}
ll F(ll a,ll b,ll c,ll n)
{
    ll ac=a/c,bc=b/c,m=(a*n+b)/c;
    if(!a)return bc*(n+1);
    if(a>=c||b>=c)return n*(n+1)/2*ac+(n+1)*bc+F(a%c,b%c,c,n);
    return n*m-F(c,c-b-1,a,m-1);
}
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; 
}
bool Ok(Point a,Point b,Point c,Point d,Point e,Point f)
{
	if(Pan(a,b,c,d)||Pan(a,b,e,f))return 1;
	return On(a,b,c,d)&&On(a,b,e,f); 
}
const int N=23;
int n,m;
ll ans=0;
Point p[N];
Line L[N],c[N],d[N];
bool In(Point v)
{
	for(int i=1;i<=n;i++)if(dcmp(Cro(p[i]-v,p[i+1]-v))<=0)return 0;
	return 1;
}
ll Calc(Point a,Point b,int l,int r,int op)
{
	
	if(!op)a.y-=0.5,b.y-=0.5;
	db dt=b.x-a.x,k=b.y-a.y,w=a.y*dt-a.x*k;
	dt*=4;k*=4;w*=4;
	return Sol(k,w,dt,r)-Sol(k,w,dt,l-1);
}
int 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]={p[i],p[i+1]};
	for(db x=0;x<=V;x+=0.5)for(db y=0;y<=V;y+=0.5)if(In({x,y}))
	{
		if(dcmp(x-floor(x))!=dcmp(y-floor(y)))continue;
		Point v={x,y};m=0;
		for(int i=1;i<=n;i++)if(Ok(p[i],p[i+1],v,{x,V},v,{V,y}))L[++m]={p[i],p[i+1]},L[m].Out();
		//cout<<endl;
		for(int i=1;i<=n;i++)if(Ok(p[i],p[i+1],v,{x,0},v,{0,y}))L[++m]={v*2-p[i],v*2-p[i+1]},L[m].Out();
		//cout<<endl;
		for(int i=1;i<=n;i++)if(Ok(p[i],p[i+1],v,{x,V},v,{0,y}))
		    L[++m]={x+p[i].y-y,y+x-p[i].x,x+p[i+1].y-y,y+x-p[i+1].x},L[m].Out();
		//cout<<endl;
		for(int i=1;i<=n;i++)if(Ok(p[i],p[i+1],v,{x,0},v,{V,y}))
		    L[++m]={x+y-p[i].y,y+p[i].x-x,x+y-p[i+1].y,y+p[i+1].x-x},L[m].Out();
		//cout<<endl;
		L[++m]={v,{V,y}};L[++m]={{x,V},v};Line las=L[m];
		//cout<<m<<endl;
		m=Halfcut(L,m);L[0]=L[m];L[m+1]=L[1];
		//cerr<<666<<endl;
		int C=0,D=0,st=0,dst=ceil(x+eps),ded=0,cst=0,ced=dst; 
		//for(int i=1;i<=m;i++)L[i].Out();
		//cout<<"Line: "<<endl;
		for(int i=1;i<=m;i++)if(L[i]==las)st=i;
		for(int i=st+1;i!=st;i=i%m+1)
		{
			if(L[i].b.x>L[i].a.x)d[++D]=L[i];
			if(L[i].b.x<L[i].a.x)c[++C]=L[i];
			if(C==1)cst=ded=floor(Cross(L[i],L[i-1]).x);
		}
		//for(int i=1;i<=D;i++)d[i].Out();
		//cout<<endl;
		//for(int i=1;i<=C;i++)c[i].Out(); 
		for(int i=1;i<=D;i++)
		{
			int l=dst,r=i==D?ded:floor(Cross(d[i],d[i+1]).x);
			//cout<<l<<" "<<r<<endl;d[i].Out();
			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);
			//cout<<l<<" "<<r<<endl;c[i].Out();
			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: 0
Time Limit Exceeded

Test #1:

score: 0
Time Limit Exceeded

input:

4
131 603
131 1828
1919 1828
1919 603

output:

4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
...

result:


Subtask #2:

score: 0
Time Limit Exceeded

Test #6:

score: 0
Time Limit Exceeded

input:

3
131 603
131 1828
1919 603

output:

4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
...

result:


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:

4
5
4
4
4
4
4
5
4
3

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%