愛悠閑 > hdu 1558 Segment set

hdu 1558 Segment set

分類: hdu  |  作者: xinag578 相關  |  發布日期 : 2015-06-16  |  熱度 : 464°
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;

int tot,bin[1024],cnt[1024];

void init()
{
    for(int i=0; i<1024; i++)
    {
        bin[i]=i;
        cnt[i]=1;
    }
    tot=1;
}

int _find(int x)
{
    while(bin[x]!=x)
        x=bin[x];

    return x;
}

int main()
{
    int T,m,i,j,t,k;
    double x1[2024],x2[2024],y1[2024],y2[2024];
    char cmd[10];
    scanf("%d",&T);
    while(T--)
    {
        init();
        scanf("%d",&m);
        for(k=0; k<m; k++)
        {
            scanf("%s",cmd);
            if(cmd[0]=='P')
            {
                scanf("%lf%lf%lf%lf",&x1[tot],&y1[tot],&x2[tot],&y2[tot]);
                //printf("%lf%lf%lf%lf",x1[tot],y1[tot],x2[tot],y2[tot]);
                i=tot;
                for(j=1; j<tot; j++)
                {
                    int a=_find(i),b=_find(j);
                    double t1,t2;
                    if(a==b) continue;
                    t1=((x1[i]-x1[j])*(y2[j]-y1[j])-(y1[i]-y1[j])*(x2[j]-x1[j]))*
                       ((x2[j]-x1[j])*(y2[i]-y1[j])-(y2[j]-y1[j])*(x2[i]-x1[j]));

                    t2=((x1[j]-x1[i])*(y2[i]-y1[i])-(y1[j]-y1[i])*(x2[i]-x1[i]))*
                       ((x2[i]-x1[i])*(y2[j]-y1[i])-(y2[i]-y1[i])*(x2[j]-x1[i]));
                    //printf("%.2f %.2f\n",t1,t2);
                    if(t1>=0&&t2>=0)
                    {
                        if(a>b)
                        {
                            bin[a]=b;
                            cnt[b]+=cnt[a];
                        }
                        else
                        {
                            bin[b]=a;
                            cnt[a]+=cnt[b];
                        }
                    }
                }
                tot++;
            }
            else if(cmd[0]=='Q')
            {
                scanf("%d",&t);
                printf("%d\n",cnt[_find(t)]);
            }
        }
        if(T) printf("\n");
    }
    return 0;
}


快乐彩中奖说明