并查集+计算几何。

 /* 1558 */
#include <cstdio>
#include <cstring>
#include <cstdlib> #define MAXN 1005 typedef struct {
double x, y;
} Point_t; typedef struct {
Point_t b, e;
} Seg_t; Seg_t segs[MAXN];
int pre[MAXN];
int ans[MAXN]; int max(int a, int b) {
return a>b ? a:b;
} int min(int a, int b) {
return a<b ? a:b;
} double direction(Point_t p0, Point_t p1, Point_t p2) {
return (p1.x-p0.x)*(p2.y-p0.y) - (p1.y-p0.y)*(p2.x-p0.x);
} bool onSegment(Point_t p0, Point_t p1, Point_t p2) {
if ( (min(p0.x, p1.x)<=p2.x && p2.x<=max(p0.x, p1.x)) &&\
(min(p0.y, p1.y)<=p2.y && p2.y<=max(p0.y, p1.y)) )
return true;
return false;
} bool intersect(Seg_t x, Seg_t y) {
Point_t p1 = x.b, p2 = x.e, p3 = y.b, p4 = y.e;
double d1 = direction(p3, p4, p1);
double d2 = direction(p3, p4, p2);
double d3 = direction(p1, p2, p3);
double d4 = direction(p1, p2, p4);
if ( ((d1> && d2<) || (d1< && d2>)) &&\
((d3> && d4<) || (d3< && d4>)) )
return true;
else if (d1== && onSegment(p3, p4, p1))
return true;
else if (d2== && onSegment(p3, p4, p2))
return true;
else if (d3== && onSegment(p1, p2, p3))
return true;
else if (d4== && onSegment(p1, p2, p4))
return true;
else
return false;
} int find(int x) {
if (x == pre[x])
return x;
pre[x] = find(pre[x]);
return pre[x];
} void merge(int x, int y) {
int fx = find(x);
int fy = find(y);
if (fx != fy) {
pre[fy] = fx;
ans[fx] += ans[fy];
ans[fy] = ;
}
} int main() {
int t, n, m;
int i, j, k;
char cmd[]; #ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
#endif scanf("%d", &t);
while (t--) {
scanf("%d", &n);
for (i=; i<=n; ++i) {
pre[i] = i;
ans[i] = ;
}
m = ;
while (n--) {
scanf("%s", cmd);
if (cmd[] == 'P') {
scanf("%lf%lf%lf%lf", &segs[m].b.x, &segs[m].b.y, &segs[m].e.x, &segs[m].e.y);
for (i=; i<m; ++i) {
if (intersect(segs[i], segs[m]))
merge(i, m);
}
++m;
} else {
scanf("%d", &j);
k = find(j);
printf("%d\n", ans[k]);
}
}
if (t)
printf("\n");
} return ;
}

【HDOJ】1558 Segment set的更多相关文章

  1. 【BZOJ3165】[HEOI2013]Segment(李超线段树)

    [BZOJ3165][HEOI2013]Segment(李超线段树) 题面 BZOJ 洛谷 题解 似乎还是模板题QwQ #include<iostream> #include<cst ...

  2. 【HDOJ】4729 An Easy Problem for Elfness

    其实是求树上的路径间的数据第K大的题目.果断主席树 + LCA.初始流量是这条路径上的最小值.若a<=b,显然直接为s->t建立pipe可以使流量最优:否则,对[0, 10**4]二分得到 ...

  3. 【HDOJ】4305 Lightning

    1. 题目描述当一个结点lightning后,可以向其周围距离小于等于R的结点传播lightning.然后以该结点为中心继续传播.以此类推,问最终形成的树形结构有多少个. 2. 基本思路生成树级数模板 ...

  4. 【HDOJ】【3506】Monkey Party

    DP/四边形不等式 裸题环形石子合并…… 拆环为链即可 //HDOJ 3506 #include<cmath> #include<vector> #include<cst ...

  5. 【HDOJ】【3516】Tree Construction

    DP/四边形不等式 这题跟石子合并有点像…… dp[i][j]为将第 i 个点开始的 j 个点合并的最小代价. 易知有 dp[i][j]=min{dp[i][j] , dp[i][k-i+1]+dp[ ...

  6. 【HDOJ】【3480】Division

    DP/四边形不等式 要求将一个可重集S分成M个子集,求子集的极差的平方和最小是多少…… 首先我们先将这N个数排序,容易想到每个自己都对应着这个有序数组中的一段……而不会是互相穿插着= =因为交换一下明 ...

  7. 【HDOJ】【2829】Lawrence

    DP/四边形不等式 做过POJ 1739 邮局那道题后就很容易写出动规方程: dp[i][j]=min{dp[i-1][k]+w[k+1][j]}(表示前 j 个点分成 i 块的最小代价) $w(l, ...

  8. 【HDOJ】【3415】Max Sum of Max-K-sub-sequence

    DP/单调队列优化 呃……环形链求最大k子段和. 首先拆环为链求前缀和…… 然后单调队列吧<_<,裸题没啥好说的…… WA:为毛手写队列就会挂,必须用STL的deque?(写挂自己弱……s ...

  9. 【HDOJ】【3530】Subsequence

    DP/单调队列优化 题解:http://www.cnblogs.com/yymore/archive/2011/06/22/2087553.html 引用: 首先我们要明确几件事情 1.假设我们现在知 ...

随机推荐

  1. POJ 2418 ,ZOJ 1899 Hardwood Species - from lanshui_Yang

    Description Hardwoods are the botanical group of trees that have broad leaves, produce a fruit or nu ...

  2. [Unity3d][NGUI]两种思路解决AssetBundle的依赖关系.

    接上文. 使用上文中的AssetBundle打包方式生成的文件包括了依赖关系中的文件. 一般的使用中并不会发现什么问题. 可是当配合NGUI的时候,使用dynamicFont时打包AssetBundl ...

  3. Struts2自己定义拦截器实例—登陆权限验证

    版本号:struts2.1.6 此实例实现功能:用户须要指定username登陆,登陆成功进入对应页面运行操作,否则返回到登陆页面进行登陆,当直接訪问操作页面(登陆后才干訪问的页面)时则不同意,须返回 ...

  4. [转] Are You Making a Big Mistake in This Volatile Market?

    Stock market volatility continues unabated. It may be too early to tell, but I’m marking the top of ...

  5. Camera2Raw

    This sample demonstrates how to use the Camera2 API to capture RAW camera buffers and save them as D ...

  6. 500 OOPS: vsftpd: refusing to run with writable root inside chroot()解决方法

    vsftpd.conf配置文件如下: [root@rusky ~]# cat /etc/vsftpd/vsftpd.conf | grep -v "#" anonymous_ena ...

  7. HDU 5120 Intersection(几何模板题)

    题意:给定两个圆环,求两个圆环相交的面积. 思路:由于圆心和半径不一样,分了好多种情况,后来发现只要把两个圆相交的函数写好之后就不需要那么复杂了.两个圆相交的面积的模板如下: double area_ ...

  8. POJ 3162 Walking Race(树的直径+单调队列)

    题目大意:对一棵树,求出从每个结点出发能到走的最长距离(每个结点最多只能经过一次),将这些距离按排成一个数组得到dis[1],dis[2],dis[3]……dis[n] ,在数列的dis中求一个最长的 ...

  9. vim中选择匹配文本删除技巧

    试举几例如下: 如何只保留匹配内容行而删除其他行? :v/pattern/d :help :v 如何对每行只保留匹配内容而删除这一行中的其它内容 :%s/^.pattern.$/\1/g 删除包含特定 ...

  10. shell脚本中echo显示内容带颜色

    转自:http://www.cnblogs.com/lr-ting/archive/2013/02/28/2936792.html shell脚本中echo显示内容带颜色显示,echo显示带颜色,需要 ...