博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU-4417-Super Mario(线段树+离线处理)
阅读量:5010 次
发布时间:2019-06-12

本文共 2708 字,大约阅读时间需要 9 分钟。

Mario is world-famous plumber. His “burly” figure and amazing jumping ability reminded in our memory. Now the poor princess is in trouble again and Mario needs to save his lover. We regard the road to the boss’s castle as a line (the length is n), on every integer point i there is a brick on height hi. Now the question is how many bricks in [L, R] Mario can hit if the maximal height he can jump is H.

InputThe first line follows an integer T, the number of test data. 

For each test data: 
The first line contains two integers n, m (1 <= n <=10^5, 1 <= m <= 10^5), n is the length of the road, m is the number of queries. 
Next line contains n integers, the height of each brick, the range is [0, 1000000000]. 
Next m lines, each line contains three integers L, R,H.( 0 <= L <= R < n 0 <= H <= 1000000000.)OutputFor each case, output "Case X: " (X is the case number starting from 1) followed by m lines, each line contains an integer. The ith integer is the number of bricks Mario can hit for the ith query. 
Sample Input

110 100 5 2 7 5 4 3 8 7 7 2 8 63 5 01 3 11 9 40 1 03 5 55 5 14 6 31 5 75 7 3

Sample Output

Case 1:4003120151 代码:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
const int maxn=1e5+5;typedef long long ll;using namespace std;struct node{ int l,r; int num;}tree[maxn<<2];struct node1{ int pos,l,r,h; bool friend operator <(node1 x, node1 y) { return x.h
>1; build(m<<1,l,mid); build(m<<1|1,mid+1,r); return;}void update(int m,int index,int val){ if(tree[m].l==index&&tree[m].l==tree[m].r) { tree[m].num=val; return ; } int mid=(tree[m].l+tree[m].r)>>1; if(index<=mid) { update(m<<1,index,val); } else { update(m<<1|1,index,val); } pushup(m); return ;}int query(int m,int l,int r){ if(tree[m].l==l&&tree[m].r==r) { return tree[m].num; } int mid=(tree[m].l+tree[m].r)>>1; if(r<=mid) { return query(m<<1,l,r); } else if(l>mid) { return query(m<<1|1,l,r); } else { return query(m<<1,l,mid)+query(m<<1|1,mid+1,r); }}int a[maxn];int main(){ int T; cin>>T; for(int k=1;k<=T;k++) { int n,m; scanf("%d%d",&n,&m); for(int t=1;t<=n;t++) { scanf("%d",&p[t].h); p[t].pos=t; } for(int t=1;t<=m;t++) { scanf("%d%d%d",&h[t].l,&h[t].r,&h[t].h); h[t].l++; h[t].r++; h[t].pos=t; } printf("Case %d:\n",k); build(1,1,n); sort(p+1,p+n+1); sort(h+1,h+m+1); int j=1; int s=1; while(s<=n) { if(p[s].h>h[j].h) { // cout<
<<" "<
<
m) { break; } } else { update(1,p[s].pos,1); s++; } } while(j<=m) { a[h[j].pos]=query(1,h[j].l,h[j].r); j++; } for(int t=1;t<=m;t++) { printf("%d\n",a[t]); } } return 0;}

 

转载于:https://www.cnblogs.com/Staceyacm/p/11301116.html

你可能感兴趣的文章
nginx在Windows环境安装
查看>>
MVC案例——删除操作
查看>>
Timer和TimerTask的使用--2
查看>>
UWP 在 WebView 中执行 JavaScript 代码(用于模拟用户输入等)
查看>>
FileUpload1.PostedFile.FileName 获取的文件名
查看>>
Mock InjectMocks ( @Mock 和 @InjectMocks )区别
查看>>
如何获取免版权图片资源
查看>>
MySql避免全表扫描【转】
查看>>
Storm学习笔记二
查看>>
windows 中的类似于sudo的命令(在cmd中以另一个用户的身份运行命令)
查看>>
java===单类设计模式之饿汉式与懒汉式
查看>>
BZOJ 1083: [SCOI2005]繁忙的都市
查看>>
Maven 编译
查看>>
《学习之道》第十章学习方法29还记得散步的好处嘛
查看>>
Git常用命令总结
查看>>
iOS获取设备IP地址
查看>>
JavaSE| String常用方法
查看>>
NRF51822配对绑定要点
查看>>
C语言博客作业—数据类型
查看>>
Python封装与隐藏
查看>>