24h購物| | PChome| 登入
2013-04-30 11:35:26| 人氣675| 回應0 | 上一篇 | 下一篇

[POJ][掃描線] 2489 - Line Segments

推薦 0 收藏 0 轉貼0 訂閱站台

Description

Background
Line segments are a very common element in computational geometry. A line segment is the set of points forming the shortest path between two points (including those points). Although they are a very basic concept it can be hard to work with them if they appear in huge numbers unless you have an efficient algorithm.
Problem
Given a set of line segments, count how many distinct pairs of line segments are overlapping. Two line segments are said to be overlapping if they intersect in an infinite number of points.

Input

The first line contains the number of scenarios.
Each scenario starts with the number n of line segments (1 <= n <= 100000). Then follow n lines consisting of four integers x1, y1, x2, y2 in the range [0, 1000000] each, representing a line segment that connects the points (x1, y1) and (x2, y2). It is guaranteed that a line segment does not degenerate to a single point.

Output

The output for every scenario begins with a line containing "Scenario #i:", where i is the number of the scenario starting at 1. Then print a single line containing the number of distinct pairs of overlapping line segments followed by an empty line.
Hint: The number of overlapping pairs may not fit into a 32-bit integer.

Sample Input

2
8
1 1 2 2
2 2 3 3
1 3 3 1
10 0 20 0
20 0 30 0
15 0 25 0
50 0 100 0
70 0 80 0
1
0 0 1 1

Sample Output

Scenario #1:
3

Scenario #2:
0

Hint

Huge input,scanf is recommended.

Source

TUD Programming Contest 2005, Darmstadt, Germany

記住:交一點不算
算出每個線段的方程 ax+by = c
將同一方程分堆,在同一堆中使用掃描線算法,
即等價於計算一維空間的區間的 overlap pair. O(nlogn)
// 860 ms


#include <stdio.h>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
struct Pt {
    int x, y;
    bool operator<(const Pt &a) const {
        if(a.x != x)
            return x < a.x;
        return y < a.y;
    }
    bool operator>(const Pt &a) const {
        if(a.x != x)
            return x > a.x;
        return y > a.y;
    }
};
struct cmp {
    bool operator() (const Pt &a, const Pt &b) const {
        return a > b;
    }
};
struct Seg {
    int a, b;
    int c; // ax + by = c maybe use long long
    Pt s, e;
    bool operator<(const Seg &A) const {
        if(A.a != a)    return a < A.a;
        if(A.b != b)    return b < A.b;
        if(A.c != c)    return c < A.c;
        if(s < A.s)     return true;
        else            return false;
    }
};
int gcd(int x, int y) {
    if(x == 0)  return y;
    if(y == 0)  return x;
    if(x < 0)   x = -x;
    if(y < 0)   y = -y;
    long long t;
    while(x%y)
        t = x, x = y, y = t%y;
    return y;
}
Seg D[100000];
int main() {
    int testcase, cases = 0;
    int n;
    int i, j, k;
    scanf("%d", &testcase);
    while(testcase--) {
        scanf("%d", &n);
        int g;
        for(i = 0; i < n; i++) {
            scanf("%d %d", &D[i].s.x, &D[i].s.y);
            scanf("%d %d", &D[i].e.x, &D[i].e.y);
            if(D[i].e < D[i].s)
                swap(D[i].e, D[i].s);
            D[i].a = D[i].s.y-D[i].e.y;
            D[i].b = -D[i].s.x+D[i].e.x;
            g = gcd(D[i].a, D[i].b);
            if(g == 0) {D[i].a = D[i].b = 0;}
            else    D[i].a /= g, D[i].b /= g;
            if(D[i].a < 0)  D[i].a *= -1, D[i].b *= -1;
            D[i].c = D[i].a*D[i].s.x + D[i].b*D[i].s.y;
        }
        sort(D, D+n);
        priority_queue<Pt, vector<Pt>, cmp> pQ;
        long long ret = 0;
        for(i = 0; i < n; ) {
            j = i;
            while(i < n && D[i].a == D[j].a && D[i].b == D[j].b && D[i].c == D[j].c)
                i++;
            while(!pQ.empty()) //<pQ.clear()>
                pQ.pop();
            for(k = j; k < i; k++) {
                while(!pQ.empty()) {
                    if(D[k].s.x == D[k].e.x) {
                        if(pQ.top().y <= D[k].s.y)
                            pQ.pop();
                        else    break;
                    } else {
                        if(pQ.top().x <= D[k].s.x)
                            pQ.pop();
                        else    break;
                    }
                }
                ret += pQ.size();
                pQ.push(D[k].e);
            }
        }
        printf("Scenario #%d:\n%I64d\n", ++cases, ret);
        if(testcase)
            puts("");
    }
    return 0;
}

台長: Morris
人氣(675) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: 其他題目 |
此分類下一篇:[深度優先] 隨機迷宮生成
此分類上一篇:[計算機組織] 期中考古題

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文