Title Link: http://poj.org/problem?id=3067
Test instructions is: Japan has N cities in the east, from north to south numbered 1 2 3,,,n,m cities in the west, from north to south numbered 1 2,,,, M,k Highway will be built, one end of the highway in the west, one end in the east; ask how many intersections of highways, not including the city's endpoints.
First, the x is sorted from small to large, if X is the same and then y from small to large. Simulation can be found that the number of intersection points is the number of reverse order of each Y, the problem becomes the reverse order of how many, the tree array to solve on it.
Pay attention to the answer with Longlong.
1 //#pragma COMMENT (linker, "/stack:102400000,102400000")2#include <iostream>3#include <cstdio>4#include <cstring>5#include <algorithm>6 using namespacestd;7 Const intMAXN = 1e3 +5;8 typedef __int64 LL;9 intBIT[MAXN], m;Ten structData { One intx, y; A BOOL operator< (ConstData &cmp)Const{ - if(x = =cmp.x) - returnY <cmp.y; the returnX <cmp.x; - } -}A[MAXN *MAXN]; - +InlinevoidAddinti) { - for(; I <= m; i + = (I &-i)) +bit[i]++; A } at - intSuminti) { - intres =0; - for(; I >=1; I-= (I &-i)) -Res + =Bit[i]; - returnRes; in } - to intMain () + { - intT, N, K; thescanf"%d", &t); * for(intCA =1; CA <= T; ca++) { $scanf" %d%d%d", &n, &m, &k);Panax Notoginsengmemset (bit,0,sizeof(bit)); - for(inti =0; I < K; i++) { thescanf"%d%d", &a[i].x, &a[i].y); + } ASort (A, A +k); theLL res =0; + for(inti =0; I < K; i++) { -Res + = (LL) (I-sum (A[I].Y)); $ Add (a[i].y); $ } -printf"Test Case%d:", CA); -printf"%i64d\n", res); the } -}
POJ 3067 Japan (tree-like array)