Poj3067 Japan (tree array)

Source: Internet
Author: User

Reprinted please indicate the source: http://blog.csdn.net/u012860063

Question link: http://poj.org/problem? Id = 3067


Description

Japan plans to welcome the acm icpc world finals and a lot of roads must be built for the venue. japan is tall island with N cities on the East Coast and M cities on the West Coast (M <= 1000, n <= 1000 ). k superhighways will be build. cities on each coast are numbered 1, 2 ,... from north to south. each superhighway is straight line and connects city on the East Coast with city of the West Coast. the funding for the construction is guaranteed by ACM. A major portion of the sum is determined by the number of crossings between superhighways. at most two superhighways cross at 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 starts with three numbers-n, m, K. each of the next K lines contains two 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 is the number of the City of the West Coast.

Output

For each test case write one line on the standard output:
Test Case (case number): (number of crossings)

Sample Input

13 4 41 42 33 23 1

Sample output

Test case 1: 5

Source

Southeastern Europe 2006


There are N and M cities on the east and west coast of Japan. Now, the highway is built to connect the east and west coast cities, and the number of intersections is calculated.

Practice: (the interpretation of the practice is from: http://blog.csdn.net/weiguang_123/article/details/7895848) Each tells the road to (x, y), that is, build a road between the X City on the East Coast and the Y city on the West Coast. When two roads have intersection, meet (x1-x2) * (y1-y2) <0. Therefore, sort each route by X from small to large. If X is the same, sort by Y from small to large. Then, the sorted highway is updated online using a tree array, and the sum of the values of Y in the descending order is the number of intersections.

The above may be a bit difficult to understand. The details are as follows.

Note that the endpoints of edge I are Xi and Yi.

Since X is sorted from small to large, suppose we are processing the K edge, then 1st ~ K-1 side X must be less than (equal to the time and not discussed) K side of X, then in the front K-1 side, the Y value of the edge that intersection the K edge must be greater than the YK, so we only need to find the number of y values of the edge in the front K-1, m], that is, the reverse order of the YK, M is the number of cities in the west coast, that is, the maximum value of Y. Therefore, the problem is converted into the problem of range summation, Which is solved by a tree array. When the X of the two edges is the same, we will remember that the y values of these two edges are ya, Yb (ya <Yb), and we will first process (x, ya ), the reason for re-processing (x, Yb) is obvious, because when X is the same, the two sides are considered to have no intersection. If we first process (x, Yb ), then the next time (x, ya) is processed, (x, ya) will add a reverse order to (x, Yb), that is, the intersection of the two edges.


The Code is as follows:

# Include <cstdio> # include <cstring> # include <iostream> # include <algorithm> using namespace STD; int n, m, K; # define Max 2047 struct node {int L, R; // left and right endpoints respectively} line [Max * max]; // expressway structure int C [Max]; // tree array bool CMP (node A, Node B) {if (. L = B. l) {return. r <B. r;} return. L <B. l;} int lowbit (int x) // 2 ^ K {return X & (-x);} void Update (int I, int X) // I point increment is X {While (I <= m) {C [I] + = x; I + = lowbit (I );}} __int64 sum (int x) {__ int64 sum = 0; while (x> 0) {sum + = C [X]; X-= lowbit (x );} return sum;} int main () {int T, I; scanf ("% d", & T); int TT = 1; while (t --) {scanf ("% d", & N, & M, & K); for (I = 1; I <= K; I ++) // I must start from 1 {scanf ("% d", & line [I]. l, & line [I]. r); // input} Sort (LINE + 1, line + k + 1, CMP); // sort by L in ascending order, L is sorted in ascending order of R at the same time. // This forms the r one-dimensional tree array memset (C, 0, sizeof (c); __int64 ret = 0; // The final result for (I = 1; I <= K; I ++) // I must start from 1 {Update (line [I]. r, 1); // insert RET + = I-sum (line [I] In the tree array. r); // I indicates the number of inserted elements. getsum returns the number of elements less than or equal to the current R value, // Subtraction is the number of elements meeting the condition} printf ("Test Case % d: % LLD \ n", TT ++, RET);} return 0 ;}



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.