HDU 5862 Counting Intersections



Problem Description
Given some segments which are paralleled to the coordinate axis. You need to count the number of their intersection.

The input data guarantee that no two segments share the same endpoint, no covered segments, and no segments with length 0.
 

Input
The first line contains an integer T, indicates the number of test case.

The first line of each test case contains a number n(1<=n<=100000), the number of segments. Next n lines, each with for integers, x1, y1, x2, y2, means the two endpoints of a segment. The absolute value of the coordinate is no larger than 1e9.
 

Output
For each test case, output one line, the number of intersection.
 

Sample Input
  
  
2 4 1 0 1 3 2 0 2 3 0 1 3 1 0 2 3 2 4 0 0 2 0 3 0 3 2 3 3 1 3 0 3 0 2
 

Sample Output
  
  
4

0

给出一些平行于坐标轴的线段,问有几个交点。

把平行于x轴的和y轴的分开,交点是这两个之间相互的交点,对于其中一个建立线段树,

枚举另一个判断交点的数量即可。

#include<set>
#include<map>
#include<ctime>
#include<cmath>
#include<stack>
#include<queue>
#include<bitset>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
#define rep(i,j,k) for (int i = j; i <= k; i++)
#define per(i,j,k) for (int i = j; i >= k; i--)
#define loop(i,j,k) for (int i = j;i != -1; i = k[i])
#define lson x << 1, l, mid
#define rson x << 1 | 1, mid + 1, r
#define fi first
#define se second
#define mp(i,j) make_pair(i,j)
#define pii pair<int,int>
using namespace std;
typedef long long LL;
const int low(int x) { return x&-x; }
const double eps = 1e-8;
const int INF = 0x7FFFFFFF;
const int mod = 1e9 + 7;
const int N = 2e5 + 10;
const int read()
{
    char ch = getchar();
    while (ch<'0' || ch>'9') ch = getchar();
    int x = ch - '0';
    while ((ch = getchar()) >= '0'&&ch <= '9') x = x * 10 + ch - '0';
    return x;
}
int T, n, x, y, xx, yy, sz, t;
int f[20][N], g[20][N];

struct point
{
    int x, l, r;
    point(int x = 0, int l = 0, int r = 0) :x(x), l(l), r(r) {}
    bool operator<(const point&a)const
    {
        return x < a.x;
    }
}a[N], b[N];

void build(int d, int x, int l, int r)
{
    if (l == r)
    {
        f[d][l] = a[l].l, g[d][l] = a[l].r;
        return;
    }
    int mid = l + r >> 1;
    build(d + 1, lson);    build(d + 1, rson);
    for (int i = l, j = l, k = mid + 1; i <= r; i++)
    {
        if (k > r || (j <= mid && f[d + 1][j] <= f[d + 1][k])) f[d][i] = f[d + 1][j++];
        else f[d][i] = f[d + 1][k++];
    }
    for (int i = l, j = l, k = mid + 1; i <= r; i++)
    {
        if (k > r || (j <= mid && g[d + 1][j] <= g[d + 1][k])) g[d][i] = g[d + 1][j++];
        else g[d][i] = g[d + 1][k++];
    }
}

int get(int d, int x, int l, int r, int ll, int rr, int v)
{
    if (ll <= l&&r <= rr)
    {
        return upper_bound(f[d] + l, f[d] + r + 1, v) - f[d] - l - (lower_bound(g[d] + l, g[d] + r + 1, v) - g[d] - l);
    }
    else
    {
        int mid = l + r >> 1, res = 0;
        if (ll <= mid) res += get(d + 1, lson, ll, rr, v);
        if (rr > mid) res += get(d + 1, rson, ll, rr, v);
        return res;
    }
}

int main()
{
    T = read();
    while (T--)
    {
        t = sz = 0;
        scanf("%d", &n);
        rep(i, 1, n)
        {
            scanf("%d%d%d%d", &x, &y, &xx, &yy);
            if (x == xx) a[++sz] = point(x, min(y, yy), max(y, yy));
            else b[++t] = point(y, min(x, xx), max(x, xx));
        }
        sort(a + 1, a + sz + 1);
        build(1, 1, 1, sz);
        LL ans = 0;
        rep(i, 1, t)
        {
            int l = lower_bound(a + 1, a + sz + 1, point(b[i].l, 0, 0)) - a;
            int r = upper_bound(a + 1, a + sz + 1, point(b[i].r, 0, 0)) - a - 1;
            ans += get(1, 1, 1, sz, l, r, b[i].x);
        }
        printf("%lld\n", ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值