HDU 4417.Super Mario-可持久化线段树(无修改区间小于等于H的数的个数)
Super Mario
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 9618 Accepted Submission(s): 4074
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.)
//int cnt=lower_bound(b+1,b+1+d,h[i])-b;
int cnt=upper_bound(b+,b++d,h[i])-b-;
以上两种都是对的。
代码:
//无修改区间-可持久化线段树(权值线段树+可持久化)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<deque>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii; const double PI=acos(-1.0);
const double eps=1e-;
const ll mod=1e9+;
const int inf=0x3f3f3f3f;
const int maxn=2e5+;
const int maxm=+;
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define lson l,m
#define rson m+1,r int a[maxn],b[maxn],sum[maxn<<],ls[maxn<<],rs[maxn<<];//sum线段树里保存的值,L左儿子,R右儿子
int n,m,sz=; void build(int &rt,int l,int r)//建棵空树
{
rt=++sz;sum[rt]=;//动态开点,初始值为0,空树
if(l==r){
return ;
} int m=(l+r)>>;
build(ls[rt],lson);
build(rs[rt],rson);
} void update(int pre,int &rt,int l,int r,int p,int c)
{
rt=++sz;sum[rt]=sum[pre]+c;//插入序列,首先继承以前的线段树 然后直接单点+1就可以
ls[rt]=ls[pre];rs[rt]=rs[pre];
if(l==r){
return ;
} int m=(l+r)>>;
if(p<=m) update(ls[pre],ls[rt],lson,p,c);//因为右边不需要更新,所以覆盖掉左边
else update(rs[pre],rs[rt],rson,p,c);
//sum[rt]=sum[ls[rt]]+sum[rs[rt]];
} int query(int pre,int rt,int L,int R,int l,int r)//查询l到r区间就是第r次插入减去第l-1次插入后的线段树的样子
{
if(L>R) return ;
if(L<=l&&r<=R){
return sum[rt]-sum[pre];
} int ret=;
int m=(l+r)>>;
if(L<=m) ret+=query(ls[pre],ls[rt],L,R,lson);
if(R> m) ret+=query(rs[pre],rs[rt],L,R,rson);
return ret;
} int rt[maxn],l[maxn],r[maxn],h[maxn]; int main()
{
int t;
scanf("%d",&t);
for(int cas=;cas<=t;cas++){
scanf("%d%d",&n,&m);
sz=;
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
b[i]=a[i];
}
for(int i=;i<=m;i++){
scanf("%d%d%d",&l[i],&r[i],&h[i]);
b[i+n]=h[i];
l[i]++,r[i]++;
}
sort(b+,b++n+m);//首先把值全部排序去重,用于建权值线段树,权值线段树保存的内容是值的数量。
int d=unique(b+,b++n+m)-(b+);
build(rt[],,d);
for(int i=;i<=n;i++) //按照序列顺序插入值
{
int p=lower_bound(b+,b++d,a[i])-b;
update(rt[i-],rt[i],,d,p,);
}
printf("Case %d:\n",cas);
for(int i=;i<=m;i++)
{
//int L=1,R=upper_bound(b+1,b+1+d,h)-b-1;
//int cnt=lower_bound(b+1,b+1+d,h[i])-b;
int cnt=upper_bound(b+,b++d,h[i])-b-;
//printf("%d\n",query(rt[l[i]],rt[r[i]+1],1,cnt,1,d));
//printf("%d\n",query(rt[l],rt[r+1],1,cnt,1,d));
printf("%d\n",query(rt[l[i]-],rt[r[i]],,cnt,,d));
}
}
return ;
}
菜的难受。。。
HDU 4417.Super Mario-可持久化线段树(无修改区间小于等于H的数的个数)的更多相关文章
- HDU 2665.Kth number-可持久化线段树(无修改区间第K小)模板 (POJ 2104.K-th Number 、洛谷 P3834 【模板】可持久化线段树 1(主席树)只是输入格式不一样,其他几乎都一样的)
Kth number Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDU 4348.To the moon SPOJ - TTM To the moon -可持久化线段树(带修改在线区间更新(增减)、区间求和、查询历史版本、回退到历史版本、延时标记不下放(空间优化))
To the moon Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
- 计蒜客 38229.Distance on the tree-1.树链剖分(边权)+可持久化线段树(区间小于等于k的数的个数)+离散化+离线处理 or 2.树上第k大(主席树)+二分+离散化+在线查询 (The Preliminary Contest for ICPC China Nanchang National Invitational 南昌邀请赛网络赛)
Distance on the tree DSM(Data Structure Master) once learned about tree when he was preparing for NO ...
- HDU 4417 Super Mario(线段树)
Super Mario Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- HDU 4417 Super Mario(主席树求区间内的区间查询+离散化)
Super Mario Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- hdu 4417 Super Mario/树套树
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4417 题意很简单,给定一个序列求一个区间 [L, R,]中小于等于H的元素的个数. 好像函数式线段树可 ...
- HDU 4417 Super Mario (划分树)(二分)
Super Mario Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDU 4417 Super Mario
题解:函数式线段树求区间小于等于k的数有几个,离线做法,首先将所有询问和序列一起离散,然后用函数式线段树处理. #include <map> #include <cstdio> ...
- HDU - 1754 I Hate It (线段树点修改求最大值)
题意:有N个学生M条操作,0<N<=200000,0<M<5000,要么查询某区间内学生的最高分,要么更改某学生的成绩. 分析:原理和线段树点修改求和类似. #include& ...
随机推荐
- Makefile的简单使用
led.bin: led.o arm-linux-ld -Ttext 0x0 -o led.elf $^ arm-linux-objcopy -O binary led.elf led.bin arm ...
- LightOJ 1340 - Story of Tomisu Ghost 阶乘分解素因子
http://www.lightoj.com/volume_showproblem.php?problem=1340 题意:问n!在b进制下至少有t个后缀零,求最大的b. 思路:很容易想到一个数通过分 ...
- Eclipse中安装Tomcat
1. 下载Tomcat并安装: http://tomcat.apache.org/download-60.cgi 2. 下载最新Eclipse的Tomacat插件: http://www.eclips ...
- John's trip(POJ1041+欧拉回路+打印路径)
题目链接:http://poj.org/problem?id=1041 题目: 题意:给你n条街道,m个路口,每次输入以0 0结束,给你的u v t分别表示路口u和v由t这条街道连接,要输出从起点出发 ...
- python模块中requests参数stream
PS:这个参数真没用过 当下载大的文件的时候,建议使用strea模式. 默认情况下是false,他会立即开始下载文件并存放到内存当中,倘若文件过大就会导致内存不足的情况. 当把get函数的stream ...
- (转)LSI SAS 1068E Raid CentOS 5.5 安装实例浪潮NF5220系列 分类: linux
新来了一批服务器,全都是清一色的国产服务器,相同的阵列卡,令人头疼的是Linux标准内核不包含该raid驱动,需要单独安装,如果是新升级内核,肯定需要编译进去该raid驱动.一.先把主板自带的驱动光盘 ...
- 浅谈linux的死锁检测 【转】
转自:http://www.blog.chinaunix.net/uid-25942458-id-3823545.html 死锁:就是多个进程(≥2)因为争夺资源而相互等待的一种现象,若无外力推动,将 ...
- 【LabVIEW技巧】LabVIEW中的错误1
前言 前几日,小黑充电学习意外的看到了下面的这个东东. 编程许久竟然没有见过这样子的错误枚举,甚为好奇,问刘大后才知道是Error Ring,为此恶补一下LabVIEW中与错误处理相关的内容. 错误的 ...
- node.js2
同步是指:同步阻塞操作,异步是指:异步非阻塞操作. 第一部分:fs模块 1.引入fs模块 require('fs'); 2.写文件 01.异步写:writeFile fs.writeFile(path ...
- [ 总结 ] Linux系统启动流程
Linux系统启动过程分析: 按下电源 --> BIOS自检 --> 系统引导(lilo/grub) --> 启动内核 --> 初始化系统 --> 用户登录 1. BIO ...