To find the total number of intersections in a straight line tree array or segment tree POJ 3067 Japan

Source: Internet
Author: User
Tags cas

http://poj.org/problem?id=3067

Japan
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 23602 Accepted: 6369

Description

Japan plans to welcome the ACM ICPC World Finals and a lot of roads must is built for the venue. Japan is Tall island with N cities on the East coast and M cities on the West coast (M <=, N <= 1000). K superhighways would be build. Cities on each coast is numbered 1, 2, ... Each superhighway are straight line and connects city in the East coast with city of the West coast. The funding for the construction are guaranteed by ACM. A major portion of the sum is determined by the number of crossings between superhighways. At the most of the superhighways cross at the one location. Write A program that calculates the number of the crossings between superhighways.

Input

The input file starts with t-the number of test cases. Each test case is starts with three numbers–n, M, K. Each of the next K lines contains, numbers–the numbers of cities connected by the superhighway. The first one is the number of the city on the East coast and second one are the number of the city of the West coast.

Output

For each test case, write one line in the standard output:
Test Case Number: (Number of crossings)

Sample Input

13 4 41 42 33 23 1

Sample Output

Test Case 1:5

Source

Southeastern Europe 2006

Idea: Tree-like array
Analysis:
1 The topic required is to find the total number of intersections of all straight lines, and the topic clearly indicates that there is a maximum of only one intersection between the two lines at superhighways
2 We should first sort these lines: according to the left number from small to large, the left number is at the same time according to the right number from small to large. So assuming there is now a line 1-3, then the line with the intersection of the number is certainly greater than 3, then the process can be used to add the tree-like array, the sum is complete and update the tree array.
3 because K of the topic is not given, then the worst case 1+2+...k

4 data will exceed int, so we should choose long long


Code:

#include <cstdio> #include <cstring> #include <iostream> #include <algorithm>using namespace    std;const int N = 1010;const int MAXN = 1000010;struct node{int x;    int y;         BOOL operator< (const node& s) const{if (x < s.x) return true;         else if (x = = s.x && y < S.y) return true;    return false; }}; Node node[maxn];int N, M, K;long long treenum[n];int lowbit (int x) {return x& (-X);}    Long long getsum (int x) {long Long sum = 0;        while (x) {sum + = treenum[x];    X-= Lowbit (x); } return sum;}        void Add (int x, int val) {while (X < N) {Treenum[x] + = val;    x + = Lowbit (x);    }}long long Solve () {long long ans = 0;    memset (treenum, 0, sizeof (treenum));    Sort (node, node+k);        for (int i = 0; i < K; i++) {ans + = i-getsum (NODE[I].Y);    Add (node[i].y, 1); } return ans;    int main () {int cas = 1;    int case; scanf ("%d", &case);         while (case--) {scanf ("%d%d%d", &n, &m, &k);          for (int i = 0; i < K; i++) scanf ("%d%d", &node[i].x, &AMP;NODE[I].Y);    printf ("Test Case%d:%lld\n", cas++, Solve ()); } return 0;}

idea: line segment tree
Analysis:
1 The topic requires finding the total number of intersections of all straight lines, and the topic clearly indicates that there is at most one intersection between two lines
2 It is obvious that we should sort these lines first: According to the left number from small to large, the left number is at the same time according to the right number from small to large. So assuming that there is now a line 1-3, then the line with the intersection of the number is certainly greater than 3, then this process can be used to find the segment tree, find finished and update the segment tree.
3 because the title of n Maximum 5*10^5, then the worst case 1+2+...N, will exceed int, so we should choose long long.

Code:

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm>using namespace   STD; #define MAXN 1000010int N, m, k;struct point{int x; int y;};   Point p[maxn];struct node{int left;   int right; int sum;};   Node Node[4*maxn];bool CMP (point A, point B) {if (a.x < b.x) return true;   else if (a.x = = b.x && a.y < B.Y) return true; return false;}   void Buildtree (int left, int. right, int pos) {node[pos].left = left;   Node[pos].right = right;   node[pos].sum = 0;   if (left = = right) return;   int mid = (left+right) >>1;   Buildtree (left, Mid, pos<<1); Buildtree (Mid+1, right, (pos<<1) +1);} int query (int left, int right, int pos) {if (Node[pos].left = = Left && Node[pos].right = = right) return nod   E[pos].sum;   int mid = (node[pos].left + node[pos].right) >>1;   if (right <= mid) return to query (left, right, pos<<1); else if (Left > Mid) return query (left, right, (pos<<1) +1); else return query (left, Mid, pos<<1) +query (Mid+1, right, (pos<<1) +1);}      void Update (int index, int pos) {if (Node[pos].left = = node[pos].right) {node[pos].sum++;   Return   } int mid = (node[pos].left+node[pos].right) >>1;   if (index <= mid) update (index, pos<<1);   else Update (index, (pos<<1) +1); Node[pos].sum = Node[pos<<1].sum + node[(pos<<1) +1].sum;}  int main () {int T, case = 1;  Long long ans;//Select Long Long scanf ("%d", &t);    while (t--) {scanf ("%d%d%d", &n, &m, &k);    for (int i = 0; i < K; i++) scanf ("%d%d", &p[i].x, &AMP;P[I].Y);    Sort (p, p+k, CMP);    Buildtree (1, M, 1);    Ans = 0;       for (int i = 0; i < K; i++) {if (P[i].y < m) ans + = query (p[i].y+1, M, 1);    Update (P[I].Y, 1);  } printf ("Test Case%d:%lld\n", case++, ans); } return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

To find the total number of intersections in a straight line tree array or segment tree POJ 3067 Japan

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.