Submission #852340

# Submission time Handle Problem Language Result Execution time Memory
852340 2023-09-21T15:54:59 Z MilosMilutinovic Tri (CEOI09_tri) C++14
70 / 100
1789 ms 6292 KB
#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef basic_string<int> B;
typedef long long ll;
typedef pair<int,int> PII;
typedef double db;
mt19937 mrand(random_device{}()); 
const ll mod=1000000007;
int rnd(int x) { return mrand() % x;}
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
// head

const int N=100100;
const int L=20;
const int S=sqrt(N);
int n,m,st[N][L],logs[N];
ll d[N];

mt19937 rng(time(0));

struct pt {
	int x,y;
	pt(int _x=0,int _y=0) : x(_x),y(_y) {}
}p[N];
vector<pt> ch[N];
int orient(pt a,pt b,pt c) {
	ll v=(b.y-a.y)*1ll*(c.x-b.x)-(b.x-a.x)*1ll*(c.y-b.y);
	return v==0?0:(v<0?-1:1);
}
bool const operator<(const pt& a,const pt& b) {
	if (orient(pt(0,0),a,b)==0) {
		return (a.x==b.x?a.y<b.y:a.x<b.x);
	}
	return orient(pt(0,0),a,b)>0;
}
ll dist(pt a,pt b) {
	return (a.x-b.x)*1ll*(a.x-b.x)+(a.y-b.y)*1ll*(a.y-b.y);
}
ll dist(pt a,pt b,pt c) {
	//return dist(a,c)+dist(b,c);
	/* https://stackoverflow.com/questions/849211/shortest-distance-between-a-point-and-a-line-segment */
	ll A = c.x - a.x;
  ll B = c.y - a.y;
  ll C = b.x - a.x;
  ll D = b.y - a.y;

  ll dot = A * C + B * D;
  ll len_sq = C * C + D * D;
  ll param = -1;
  if (len_sq != 0) //in case of 0 length line
      param = dot / len_sq;

  ll xx, yy;

  if (param < 0) {
    xx = a.x;
    yy = a.y;
  }
  else if (param > 1) {
    xx = b.x;
    yy = b.y;
  }
  else {
    xx = a.x + param * C;
    yy = a.y + param * D;
  }

  ll dx = c.x - xx;
  ll dy = c.y - yy;
  return dx*dx+dy*dy;
}
bool in_tri(pt a,pt b,pt c,pt p) {
	return orient(a,b,p)!=-1&&orient(b,c,p)!=-1&&orient(c,a,p)!=-1;
}
int cmp(int i,int j) {
	return d[i]<d[j]?i:j;
}
int query(int l,int r) {
	int k=logs[r-l+1];
	return cmp(st[l][k],st[r-(1<<k)+1][k]);
}
int main() {
	scanf("%d%d",&n,&m);
	rep(i,0,n) {
		scanf("%d%d",&p[i].x,&p[i].y);
	}
	sort(p,p+n);
	rep(i,0,n) {
		ch[i/S].pb(p[i]);
	}
	rep(i,0,n+1) {
		if (ch[i].empty()) continue;
		vector<pt> hull;
		sort(all(ch[i]),[&](pt a,pt b) { return a.x<b.x||(a.x==b.x&&a.y<b.y); });
		for (auto& p:ch[i]) {
			while (SZ(hull)>=2&&orient(hull[SZ(hull)-2],hull[SZ(hull)-1],p)>=0) {
				hull.pop_back();
			}
			hull.pb(p);
		}
		ch[i]=hull;
	}
	while (m--) {
		pt a,b;
		scanf("%d%d%d%d",&a.x,&a.y,&b.x,&b.y);
		if (orient(b,pt(0,0),a)<=0) swap(a,b);
		int L,R;
		{
			L=n-1;
			int low=0,high=n-1;
			while (low<=high) {
				int mid=low+high>>1;
				if (orient(p[mid],pt(0,0),a)>=0) {
					L=mid;
					high=mid-1;
				} else {
					low=mid+1;
				}
			}
		}
		{
			R=0;
			int low=0,high=n-1;
			while (low<=high) {
				int mid=low+high>>1;
				if (orient(p[mid],pt(0,0),b)<=0) {
					R=mid;
					low=mid+1;
				} else {
					high=mid-1;
				}
			}
		}
		if (L>R) {
			printf("N\n");
			continue;
		}
		if (R-L<=S) {
			bool ok=false;
			rep(i,L,R+1) if (in_tri(a,b,pt(0,0),p[i])) ok=true;
			puts(ok?"Y":"N");
			continue;
		}
		bool ok=false;
		while (L%S!=0) {
			if (in_tri(a,b,pt(0,0),p[L])) ok=true;
			L++;
		}
		while (R>=L&&R%S!=S-1) {
			if (in_tri(a,b,pt(0,0),p[R])) ok=true;
			R--;
		}
		VI ord;
		rep(i,L/S,R/S+1) ord.pb(i);
		shuffle(ord.begin(),ord.end(),rng);
		rep(i,L/S,R/S+1) {
			int sz=SZ(ch[i]);
			int low=0,high=sz-1;
			while (low<=high) {
				int mid=low+high>>1;
				if (in_tri(a,b,pt(0,0),ch[i][mid])) {
					ok=true;
					break;
				}
				//if (mid+1!=sz) assert(orient(ch[i][mid],ch[i][mid+1],b)!=0);
				if (mid+1==sz||(!in_tri(a,b,pt(0,0),ch[i][mid+1])&&orient(ch[i][mid],ch[i][mid+1],b)==1)) {
					high=mid-1;
				} else {
					low=mid+1;
				}
			}
			if (ok) break;
		}
		puts(ok?"Y":"N");
	}
}

Compilation message

tri.cpp: In function 'int main()':
tri.cpp:123:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  123 |     int mid=low+high>>1;
      |             ~~~^~~~~
tri.cpp:136:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  136 |     int mid=low+high>>1;
      |             ~~~^~~~~
tri.cpp:171:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  171 |     int mid=low+high>>1;
      |             ~~~^~~~~
tri.cpp:94:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   94 |  scanf("%d%d",&n,&m);
      |  ~~~~~^~~~~~~~~~~~~~
tri.cpp:96:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   96 |   scanf("%d%d",&p[i].x,&p[i].y);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
tri.cpp:116:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  116 |   scanf("%d%d%d%d",&a.x,&a.y,&b.x,&b.y);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4696 KB Output is correct
2 Correct 2 ms 4700 KB Output is correct
3 Correct 127 ms 5096 KB Output is correct
4 Correct 472 ms 5668 KB Output is correct
5 Correct 1789 ms 6292 KB Output is correct
6 Correct 379 ms 5724 KB Output is correct
7 Correct 470 ms 6176 KB Output is correct
8 Incorrect 167 ms 5720 KB Output isn't correct
9 Incorrect 192 ms 5856 KB Output isn't correct
10 Incorrect 217 ms 6236 KB Output isn't correct