hdoj 4417
来源:互联网 发布:互换身体的网络电影 编辑:程序博客网 时间:2024/06/10 23:38
题目:
hdoj : http://acm.hdu.edu.cn/showproblem.php?pid=4417
思路:
将砖块与问题都按高度排序,并用变量标记其位置,之后利用树状数组的特点,在每个问题询问前将这个高度前的砖块更新入树状数组中,之后查询并将结果记录在ans数组中即可。
AC代码:
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;#define lowbit(x) x & (-x)const int M = 100000 + 10;struct Height{int l, r, h, dx;bool operator <(const Height &i)const{return h < i.h;}}q[M], h[M];int ans[M], n, tree[M];void add(int a){while(a <= n){tree[a]++;a += lowbit(a);}}int sum(int a){int res = 0;while(a > 0){res += tree[a];a -= lowbit(a);}return res;}main(){int T;scanf("%d", &T);for(int p = 1; p <= T; p++){memset(tree, 0, sizeof(tree));int qs;scanf("%d %d", &n, &qs);for(int i = 1; i <= n; i++){scanf("%d", &h[i].h);h[i].dx = i;}for(int i = 1; i <= qs; i++){scanf("%d %d %d", &q[i].l, &q[i].r, &q[i].h);q[i].l++;q[i].r++;q[i].dx = i;}sort(h + 1, h + 1 + n);sort(q + 1, q + qs + 1);for(int i = 1, j = 1; i <= qs; i++)//i = query j = nums{while(j <= n && h[j].h <= q[i].h)//更新砖块高度至询问高度{add(h[j].dx);j++;}ans[q[i].dx] = sum(q[i].r) - sum(q[i].l - 1);}printf("Case %d:\n", p);for(int i = 1; i <= qs; i++)printf("%d\n", ans[i]);}}
0 0
- hdoj 4417
- HDOJ 4417: Super Mario
- HDOJ 4417 Super Mario
- HDOJ
- hdoj
- hdoj
- HDOJ
- 【线段树】 HDOJ 4417 Super Mario
- hdoj 4417 Super Mario 【树状数组 + 思维】
- hdoj 1568 && hdoj 5344 && hdoj 5444
- HDOJ 2176
- Hdoj--1272
- hdoj 1003
- hdoj 1005
- hdoj 1030
- hdoj 2154
- hdoj 2100
- HDOJ 1106
- VBS操作Excel常见方法
- 独立成分分析
- 'nasm.exe' is not recognized as an internal or external command
- Java 设计模式详解
- <httpHandlers>的配置和使用
- hdoj 4417
- web.xml中load-on-startup的作用
- SQL中OBJECT_ID,OBJECT_NAME,OBJECT_DEFINITION的用法
- 大数据排序,理论上支持个100亿条没问题吧,o(∩_∩)o 哈哈 --给爱吃大肉包补充了注释
- Cocos2d-X 3.0 创建项目
- 利用闪回技术单行恢复
- StageFright框架流程解读
- eclipse自动提示功能没了的解决办法
- 定时监测程序运行的情况以及自动重启程序