Guest User

Untitled

a guest
Feb 4th, 2012
168
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <vector>
  2. #include <list>
  3. #include <map>
  4. #include <set>
  5. #include <deque>
  6. #include <stack>
  7. #include <bitset>
  8. #include <algorithm>
  9. #include <functional>
  10. #include <numeric>
  11. #include <utility>
  12. #include <sstream>
  13. #include <iomanip>
  14. #include <cstdio>
  15. #include <cmath>
  16. #include <cstdlib>
  17. #include <ctime>
  18. #include <cstring>
  19. #include <iostream>
  20. #include <cstdio>
  21. using namespace std;
  22.  
  23. typedef long long ll;
  24. typedef unsigned long long ull;
  25. typedef unsigned ul;
  26. typedef long double ld;
  27.  
  28. const ul MAXN=200000;
  29.  
  30. struct point
  31. {
  32.     ll x,y;
  33.     friend istream& operator>>(istream& in, point &rhs)
  34.     {
  35.         return in>>rhs.x>>rhs.y;
  36.     }
  37.     bool operator<(const point& rhs)const
  38.     {
  39.         return x<rhs.x||(x==rhs.x&&y<rhs.y);
  40.     }
  41.     bool operator==(const point& rhs)const
  42.     {
  43.         return x==rhs.x&&y==rhs.y;
  44.     }
  45.     bool operator !=(const point& rhs)const
  46.     {
  47.         return !(*this==rhs);
  48.     }
  49. };
  50.  
  51. bool cw(const point &a, const point &b, const point &c)
  52. {
  53.     return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x)<0;
  54. }
  55.  
  56. bool ccw(const point &a, const  point &b, const  point &c)
  57. {
  58.     return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x)>0;
  59. }
  60.  
  61. void convex_hull(point* a, int &a_size)
  62. {
  63.     if(a_size==1)return;
  64.     sort(a,a+a_size);
  65.     point p1=a[0],p2=a[a_size-1];
  66.     static point up[MAXN],down[MAXN];
  67.     int up_size,down_size;
  68.     up_size=down_size=1;
  69.     up[0]=down[0]=p1;
  70.     for(int i=1;i<a_size;++i)
  71.     {
  72.         if(i==a_size-1||cw(p1,a[i],p2))
  73.         {
  74.             while(up_size>=2&&!cw(up[up_size-2],up[up_size-1],a[i]))
  75.                 up_size--;
  76.             up[up_size++]=a[i];
  77.         }
  78.         if(i==a_size-1||ccw(p1,a[i],p2))
  79.         {
  80.             while(down_size>=2&&!ccw(down[down_size-2],down[down_size-1],a[i]))
  81.                 down_size--;
  82.             down[down_size++]=a[i];
  83.         }
  84.     }
  85.     a_size=0;
  86.     for(int i=0;i<up_size;++i)
  87.         a[a_size++]=up[i];
  88.     for(int i=down_size-2;i>0;--i)
  89.         a[a_size++]=down[i];
  90. }
  91.  
  92. bool point_on_segment(point pt, point seg1,point seg2)
  93. {
  94.     if(!(seg1<seg2))swap(seg1,seg2);
  95.     return (!cw(seg1,pt,seg2)&&!ccw(seg1,pt,seg2)&&!(pt<seg1)&&(pt<seg2||pt==seg2));
  96. }
  97.  
  98. int normalize(int a, int max_a)
  99. {
  100.     while(a<0)a+=max_a;
  101.     return a%max_a;
  102. }
  103.  
  104. int main()
  105. {
  106.     int K;
  107.     cin>>K;
  108.     while(K--)
  109.     {
  110.         int n,hull_size;
  111.         bool f=true;
  112.         bool onOneLine=true;
  113.         cin>>n;
  114.         static point a[MAXN],b[MAXN];
  115.         int simple_check=0;
  116.         bool fast_res=false;
  117.         for(int i=0;i<n;i++)
  118.         {
  119.             cin>>a[i];
  120.             if(i&&a[i]==a[i-1]){--i;--n;}
  121.             b[i]=a[i];
  122.         }
  123.         hull_size=n;
  124.         for(int i=0;i<n+5&&!fast_res;i++)
  125.         {
  126.             if(!simple_check)
  127.             {
  128.                 simple_check=cw(a[normalize(i,n)],a[normalize(i+1,n)],a[normalize(i+2,n)])-ccw(a[normalize(i,n)],a[normalize(i+1,n)],a[normalize(i+2,n)]);
  129.             }
  130.             else
  131.             {
  132.                 fast_res=simple_check*(cw(a[normalize(i,n)],a[normalize(i+1,n)],a[normalize(i+2,n)])-ccw(a[normalize(i,n)],a[normalize(i+1,n)],a[normalize(i+2,n)]))<0;
  133.             }
  134.             onOneLine=onOneLine&&(!cw(a[normalize(i,n)],a[normalize(i+1,n)],a[normalize(i+2,n)])&&!ccw(a[normalize(i,n)],a[normalize(i+1,n)],a[normalize(i+2,n)]));
  135.         }
  136.         if(fast_res){cout<<"0 ";continue;}
  137.         convex_hull(b,hull_size);
  138.         int direction;
  139.         int i_start=0;
  140.         int current=find(a,a+n,b[i_start])-a;
  141.         int n_same=0;
  142.         while(a[current]==a[normalize(current+1,n)]&&n_same<=n)current=normalize(current+1,n),n_same++;
  143.         if(n_same>n)
  144.         {cout<<1<<" ";continue;}
  145.         if(point_on_segment(a[normalize(current+1,n)],b[i_start],b[normalize(i_start+1,hull_size)]))direction=1;
  146.         else if(point_on_segment(a[normalize(current+1,n)],b[i_start],b[normalize(i_start-1,hull_size)]))direction=-1;
  147.         int i,next_hull,next;
  148.         for(i=i_start;f&&i<hull_size;current+=direction,++i)
  149.         {
  150.             while(a[current]==a[normalize(current+direction,n)])
  151.                 current=normalize(current+direction,n);
  152.             i=normalize(i,hull_size);
  153.             next=normalize(i+1,hull_size);
  154.             next_hull=normalize(current+direction,n);
  155.             current=normalize(current,n);
  156.             if(point_on_segment(a[next_hull],a[current],b[next]))
  157.             {
  158.                 if(a[next_hull]!=b[next])i--;
  159.             }else {cout<<0<<" ";f=false;}
  160.         }
  161.         if(f)
  162.             cout<<"1 ";
  163.     }
  164. }
RAW Paste Data