#include<stdio.h>
#include<cmath>
using namespace std;
struct Point {
double x, y;
Point(double X = 0, double Y = 0) { x = X, y = Y; }
Point operator + (Point B) { return Point(x + B.x, y + B.y); }
Point operator - (Point B) { return Point(x - B.x, y - B.y); }
Point operator * (double k) { return Point(x*k, y*k); }
Point operator / (double k) { return Point(x / k, y / k); }
};
struct Line {
Point p1, p2;
Line() {}
Line(Point P1, Point P2) { p1 = P1; p2 = P2; }
};
typedef Point Vector;
const double eps = 1e-8;
Line line[100];
int pre[100];
int sgn(double a) {
if (fabs(a) < eps)
return 0;
else
return a < 0 ? -1 : 1;
}
double Cross(Vector A, Vector B) { return B.y*A.x - B.x*A.y; }
bool Cross_segment(Point a, Point b, Point c, Point d) { //判断两线段是否相交
double c1 = Cross(b - a, c - a), c2 = Cross(b - a, d - a);
double d1 = Cross(d - c, a - c), d2 = Cross(d - c, b - c);
return sgn(c1)*sgn(c2) < 0 && sgn(d1)*sgn(d2) < 0; //1相交 0不相交
}
int find(int u) { //并查集搜索
return pre[u] == u ? u : pre[u] = find(pre[u]);
}
int main() {
int ccase;
scanf("%d", &ccase);
while (ccase--) {
for (int i = 0; i < 100; i++)
pre[i] = i;
int k = 1; //线段数
char t;
int n;
scanf("%d", &n); //指令数
while (n--) {
t = getchar();
while (t == ' ' || t == '\n')
t = getchar();
if (t == 'P') {
scanf("%lf%lf%lf%lf", &line[k].p1.x, &line[k].p1.y, &line[k].p2.x, &line[k].p2.y);
k++;
for (int i = 1; i < k - 1; i++) {
if (Cross_segment(line[k - 1].p1, line[k - 1].p2, line[i].p1, line[i].p2)) { //如果两线段相交
int x = pre[k - 1];
int y = pre[i];
if (x != y)
pre[x] = y;
}
}
}
else {
int x;
int num = 0;
scanf("%d", &x);
x = find(x);
for (int i = 1; i < k; i++)
if (find(i) == x)
num++;
printf("%d\n", num);
}
}
printf("\n");
}
return 0;
}
int find(int u) { //并查集搜索
return pre[u] == u ? u : pre[u] = find(pre[u]);
}
是不是这里无限递归了