HDU4417(SummerTrainingDay08-N 主席树)
Super Mario
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7265 Accepted Submission(s): 3127
Problem Description
Input
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.)
Output
Sample Input
10 10
0 5 2 7 5 4 3 8 7 7
2 8 6
3 5 0
1 3 1
1 9 4
0 1 0
3 5 5
5 5 1
4 6 3
1 5 7
5 7 3
Sample Output
4
0
0
3
1
2
0
1
5
1
Source
//2017-09-07
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define ll long long
#define mid ((l+r)>>1) using namespace std; const int N = ;
const int M = N * ;
struct node{
int lson, rson, sum;//sum维护l到r的数有几个
}tree[M];
//第i棵线段树为插入前i个数字所构成的权值线段树。
int root[N], arr[N], arr2[N], tot;
int n, m, q; void init(){//将原数列排序并去重
tot = ;
for(int i = ; i <= n; i++)
arr2[i] = arr[i];
sort(arr2+, arr2++n);
m = unique(arr2+, arr2++n)-arr2-;
} //离散化
int getID(int x){
return lower_bound(arr2+, arr2++m, x) - arr2;
} int build(int l, int r){
int id = ++tot;
tree[id].sum = ;
if(l == r)return id;
if(l <= mid)
tree[id].lson = build(l, mid);
if(r > mid)
tree[id].rson = build(mid+, r);
return id;
} int update(int id, int pos, int value){
int newroot = ++tot, tmp = newroot;
tree[newroot].sum = tree[id].sum + value;
int l = , r = m;
while(l < r){
if(pos <= mid){
tree[newroot].lson = ++tot;
tree[newroot].rson = tree[id].rson;
newroot = tree[newroot].lson;
id = tree[id].lson;
r = mid;
}else{
tree[newroot].rson = ++tot;
tree[newroot].lson = tree[id].lson;
newroot = tree[newroot].rson;
id = tree[id].rson;
l = mid+;
}
tree[newroot].sum = tree[id].sum + value;
}
return tmp;
} int query(int ltree, int rtree, int k){
int l = , r = m, ans = ;
while(l < r){
if(k <= mid){
ltree = tree[ltree].lson;
rtree = tree[rtree].lson;
r = mid;
}else{
ans += tree[tree[rtree].lson].sum - tree[tree[ltree].lson].sum;
ltree = tree[ltree].rson;
rtree = tree[rtree].rson;
l = mid+;
}
}
if(l == r){
if(k < l)return ;
else return ans + tree[rtree].sum - tree[ltree].sum;
}
} int main()
{
//freopen("dataN.txt", "r", stdin);
int T, kase = ;
scanf("%d", &T);
while(T--){
scanf("%d%d", &n, &q);
for(int i = ; i <= n; i++)
scanf("%d", &arr[i]);
init();
root[] = build(, m);
for(int i = ; i <= n; i++){
int pos = getID(arr[i]);
root[i] = update(root[i-], pos, );
}
printf("Case %d:\n", ++kase);
while(q--){
int l, r, h;
scanf("%d%d%d", &l, &r, &h);
l++; r++;
int pos = getID(h);//找到第一个小于概数的位置
if(arr2[pos] > h)pos--;
printf("%d\n", query(root[l-], root[r], pos));
}
} return ;
}
HDU4417(SummerTrainingDay08-N 主席树)的更多相关文章
- HDU4417 - Super Mario(主席树)
题目大意 给定一个数列,每次要求你查询区间[L,R]内不超过K的数的数量 题解 和静态的区间第K大差不多,这题是<=K,先建立好n颗主席树,然后用第R颗主席树区间[1,K]内数的数量减去第L-1 ...
- [HDU4417]Super Mario(主席树+离散化)
传送门 又是一道主席树模板题,注意数组从0开始,还有主席树耗费空间很大,数组开大点,之前开小了莫名其妙TLE.QAQ ——代码 #include <cstdio> #include < ...
- hdu4417 Super Mario (树状数组/分块/主席树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4417 题目大意:给定一个长度为n的序列,有m个询问,每次询问包含l,r,h,即询问区间[l,r]小于等 ...
- hdu4417 主席树求区间小于等于K
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4417 Problem Description Mario is world-famous plum ...
- 【主席树】【bzoj2161】[hdu4348]
#include<cstdio> #include<algorithm> #include<cstring> #define N 400000 using name ...
- bzoj3207--Hash+主席树
题目大意: 给定一个n个数的序列和m个询问(n,m<=100000)和k,每个询问包含k+2个数字:l,r,b[1],b[2]...b[k],要求输出b[1]~b[k]在[l,r]中是否出现. ...
- bzoj1901--树状数组套主席树
树状数组套主席树模板题... 题目大意: 给定一个含有n个数的序列a[1],a[2],a[3]--a[n],程序必须回答这样的询问:对于给定的i,j,k,在a[i],a[i+1],a[i+2]--a[ ...
- BZOJ 3626: [LNOI2014]LCA [树链剖分 离线|主席树]
3626: [LNOI2014]LCA Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 2050 Solved: 817[Submit][Status ...
- BZOJ 1146: [CTSC2008]网络管理Network [树上带修改主席树]
1146: [CTSC2008]网络管理Network Time Limit: 50 Sec Memory Limit: 162 MBSubmit: 3522 Solved: 1041[Submi ...
- BZOJ 2588: Spoj 10628. Count on a tree [树上主席树]
2588: Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MBSubmit: 5217 Solved: 1233 ...
随机推荐
- java打包jar后,使之一直在linux上运行,不随终端退出而关闭
nohup java -jar xxx.jar&
- linux防火墙(三)—— iptables语法之匹配条件
一.iptables规则的匹配条件类型有三类 1.通用匹配:可直接使用,不依赖于其他条件或扩展,包括网络协议.IP地址.网络接口等条件 2.隐含匹配:要求以特定的协议匹配作为前提,包括端口.TCP标记 ...
- webApp在各大Android市场上的发布
本来打算每个月都写上一篇博客的,可是计划永远赶不上变化,不过这其中也有自己的懒惰,果然过年让整个人懈怠了不少.年后一直在赶项目以致于到今天才动手写这篇文章. 这一篇主要写点在公司的要求下发布的webA ...
- vue中封装公共方法,全局使用
1.以封装的合并单元格为例,首先建立一个util.js 2.在main.js中引用 3.直接在使用该方法的地方调用就可以了
- 如何进行bug总结
在项目过程中,测试同学会发现大量的bug,但同时也不可避免的会存在一些遗漏的bug.为了能够减少遗漏bug的现象,我们需要针对遗漏的问题进行总结,从教训中积累经验,总结方法,从而提高测试的覆盖度,提升 ...
- android开发学习——day5
活动跳转部分代码显式intent @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(saved ...
- 机器学习基础——模型参数评估与选择
当看过一些简单的机器学习算法或者模型后,对于具体问题该如何评估不同模型对具体问题的效果选择最优模型呢. 机器学习分类 1. 经验误差.泛化误差 假如m个样本中有a个样本分类错误 错误率:E = a / ...
- flex布局中transform出错
在flex布局下,若应用transform 的动画的子元素没有使用进行定位,则动画过程中,子元素将相对display:flex的元素进行static定位 动画结束后位置正常: 修复代码只需要posit ...
- Linux 部署 ASP.NET Core 的一些问题记录
异常错误: 关闭 IP6 #修改 vi /etc/sysctl.conf # 添加如下三条设置 net.ipv6.conf.all.disable_ipv6 = 1 net.ipv6.co ...
- Apple Pay 支付集成
Refer:https://open.unionpay.com/ajweb/product/detail?id=80 交易步骤: 1.浏览并选购商品:用户通过手机客户端与商户系统交互浏览选购商品,客户 ...