[POJ][描] 2489 - Line Segments@Morris' Blog|PChome Online 人新台
2013-04-30 11:35:26| 人713| 回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;
  &nbs; 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
人(713) | 回(0)| 推 (0)| 收藏 (0)|
全站分: 不分 | 人分: 其他目 |
此分下一篇:[深度先] 迷生成
此分上一篇:[算] 期中考古

是 (若未登入"人新台"看不到回覆唷!)
* 入:
入片中算式的果(可能0) 
(有*必填)
TOP
全文
ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86