您的位置:首页 > 其它

[POJ3067]Japan

2015-09-01 20:19 239 查看
题目链接:http://poj.org/problem?id=3067

线段树和树状数组都可以做。

线段树:

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <queue>
#include <map>
#include <stack>
#include <list>
#include <vector>

using namespace std;

const int maxn = 1000010;
typedef long long LL;
typedef struct Node {
LL x;
LL y;
};

Node node[maxn];
LL d[maxn<<1];
LL n, m, k;
LL ans;

inline bool cmp(Node a, Node b) {
if(a.x != b.x) {
return a.x > b.x;
}
return a.y > b.y;
}

//求某点管辖范围
LL lowbit(LL x) { //求x末尾最低位1的位置(末尾0的个数+1)
// return x & (x ^ (x - 1));
return x & (-x);
}

//区间更新树状数组(i到x)
void update(LL i, LL x, LL num) {
while(i <= x) {
d[i] += num;
i += lowbit(i);
}
}

//获取前x项和
LL getsum(LL x) {
LL sum = 0;
while(x > 0) {
sum += d[x];
x -= lowbit(x);
}
return sum;
}

int main() {
// freopen("in", "r", stdin);
LL kase = 1;
LL T;
scanf("%I64d", &T);
while(T--) {
memset(d, 0, sizeof(d));
scanf("%I64d %I64d %I64d", &n, &m, &k);
for(LL i = 1; i <= k; i++) {
scanf("%I64d %I64d", &node[i].x, &node[i].y);
}
sort(node+1, node+k+1, cmp);
ans = 0;
for(int i = 1; i <= k; i++) {
ans += getsum(node[i].y-1);
update(node[i].y, m, 1);
}
printf("Test case %I64d: %I64d\n", kase++, ans);
}
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: