[POJ3067]Japan
2015-09-01 20:19
239 查看
题目链接:http://poj.org/problem?id=3067
线段树和树状数组都可以做。
线段树:
View Code
线段树和树状数组都可以做。
线段树:
#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
相关文章推荐
- 【java基础】面向过程~面向对象
- 【java基础】面向过程~面向对象
- [ZJOI2008]树的统计Count(点权树链剖分(模板已完善))
- Git工作流使用过程学习【工具:Git+SourceTree】
- Mac终端Shell分类以及配置文件等级区别
- 实现屏幕切换、滑动-ViewPager详解(二)之--------PagerTitleStrip与PagerTabStrip添加标题栏
- html中DIV+CSS与TABLE布局方式的区别及HTML5新加入的结构标签(转)
- C# Eval在asp.net中的用法及作用
- NOIP2012 借教室
- ISA指针
- 浮点数
- C# Eval在asp.net中的用法及作用
- 初步swift语言学习笔记2(可选类型?和隐式可选类型!)
- XMPP开发环境配置,纯新手/小白/看不懂英文适用,绝对详细(修复版)
- 项目实战No1 基本骨架
- auto_ptr、shared_ptr、weak_ptr、scoped_ptr用法小结
- 承受
- checkbox标签的使用
- Android:简单实现ViewPager+TabHost+TabWidget实现导航栏导航和滑动切换
- hibernate的二级缓存和查询缓存