Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 25023 | Accepted: 6738 |
Description
Input
Output
Sample Input
1 3 4 4 1 4 2 3 3 2 3 1
Sample Output
Test case 1: 5求所有线的交点
思路:
按左右两侧从小到大排序,加入当前直线产生的交点数=已处理的直线中比当前右端点大的直线条数
树状数组维护
因为不知道K的范围,所以要开的大一点
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int MAX = 1005; struct ss { int x, y; }line[MAX*MAX]; bool cmp(ss a, ss b) { if(a.x < b.x) return true; else if(a.x == b.x && a.y < b.y) return true; else return false; } int T, N, M, K, kase = 1; long long ans, tree[MAX]; void add(int pos) { while(pos <= M) { tree[pos] ++; pos += pos&(-pos); } } long long query(int pos) { long long sum = 0; while(pos > 0) { sum += tree[pos]; pos -= pos&(-pos); } return sum; } int main() { scanf("%d", &T); while(T--) { ans = 0; memset(tree, 0, sizeof(tree)); scanf("%d %d %d", &N, &M, &K); for(int i = 0;i < K; ++i) { scanf("%d %d", &line[i].x, &line[i].y); } sort(line, line+K, cmp); for(int i = 0;i < K; ++i) { add(line[i].y); ans += query(M)-query(line[i].y);//比当前节点右端点大的端点数 } printf("Test case %d: %lld\n", kase++, ans); } return 0; }