题意:给你一些数,有多次询问,问你在l,r区间内小于k的数有多少个

思路:主席树大发好,虽然树状数组和线段树离线也可以做

代码:

#include <set>
#include <map>
#include <queue>
#include <stack>
#include <math.h>
#include <vector>
#include <string>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#define zero(a) fabs(a)<eps
#define max( x, y )  ( ((x) > (y)) ? (x) : (y) )
#define min( x, y )  ( ((x) < (y)) ? (x) : (y) )
#define lowbit(x) (x&(-x))
#define debug(a) cerr<<#a<<"=="<<a<<endl
typedef long long LL;
const double pi=acos(-1.0);
const double eps=1e-8;
const int inf=0x3f3f3f3f;
const LL linf=0x3f3f3f3f3f3f3f3f;
using namespace std;const int maxn=100007;
int n,m,a[maxn],root[maxn],cnt,x,y,k;
struct node
{int l,r,sum;
}T[maxn*40];
vector<int>v;
int getid(int x)
{return lower_bound(v.begin(),v.end(),x)-v.begin();
}
void update(int l,int r,int &x,int y,int pos)
{T[++cnt]=T[y],T[cnt].sum++,x=cnt;if(l==r)return ;int mid=(l+r)>>1;if(pos<=mid)update(l,mid,T[x].l,T[y].l,pos);else update(mid+1,r,T[x].r,T[y].r,pos);
}int query(int l,int r,int x,int y,int L,int R)
{if(L>R) return 0;if(L<=l && r<=R){return T[y].sum-T[x].sum;}int mid=(l+r)>>1,ans=0;if(L<=mid) ans+=query(l,mid,T[x].l,T[y].l,L,R);if(R>mid) ans+=query(mid+1,r,T[x].r,T[y].r,L,R);return ans;
}
int main()
{int tt;scanf("%d",&tt);int cas=1;while(tt--){scanf("%d%d",&n,&m);v.clear();for(int i=1;i<=n;i++){scanf("%d",&a[i]);v.push_back(a[i]);}sort(v.begin(),v.end());v.erase(unique(v.begin(),v.end()),v.end());for(int i=1;i<=n;i++){update(0,v.size()-1,root[i],root[i-1],getid(a[i]));}printf("Case %d:\n",cas++);int t1,t2,t3;while(m--){scanf("%d%d%d",&t1,&t2,&t3);int pos=upper_bound(v.begin(),v.end(),t3)-v.begin()-1;printf("%d\n",query(0,v.size()-1,root[t1],root[t2+1],0,pos));}}return 0;
}

转载于:https://www.cnblogs.com/lalalatianlalu/p/8855114.html

hdu 4417 Super Mario(可持久化线段树)相关推荐

  1. HDU 4417 Super Mario(莫队 + 树状数组 + 离散化)

    Super Mario 思路 区间查找问题,容易想到离线莫队,确实这题就是莫队,接下来我们考虑如何维护区间高度值问题. 既然是离线嘛,我们容易想到离散化和他挂钩,想想这题是否需要离散化,高度的最大值是 ...

  2. HDU 4417 Super Mario(线段树)

    Super Mario Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  3. HDU - 4417 Super Mario(主席树/线段树+离线)

    题目链接:点击查看 题目大意:给出由 n 个数的数列,再给出 m 次查询,每次查询需要输出 [ l , r ] 内小于等于 h 的数有多少个 题目分析:大晚上睡不着觉随便做做题,发现这个题目原来可以用 ...

  4. HDU 4417 Super Mario(线段树离线处理/主席树)

    Mario is world-famous plumber. His "burly" figure and amazing jumping ability reminded in ...

  5. HDU 4417 Super Mario(线段树||树状数组+离线操作 之线段树篇)

    Mario is world-famous plumber. His "burly" figure and amazing jumping ability reminded in ...

  6. HDU 4417 Super Mario(离线线段树or树状数组)

    Problem Description Mario is world-famous plumber. His "burly" figure and amazing jumping ...

  7. hdu 4417 Super Mario 树状数组||主席树

    Super Mario Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Prob ...

  8. HDU 4417 Super Mario(划分树)

    Super Mario Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  9. #HDU 4417 Super Mario (主席树 + 二分)

    Super Mario Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

最新文章

  1. java 行为模式_java设计模式--行为模式
  2. 【数据结构作业心得】4-0 二叉树
  3. ubuntu安装curl报错或get update失败
  4. Visifire Silverlight Charts (基于SilverLight的Chart组件)
  5. 问答机器人接口python_设计用于机器学习工程的Python接口
  6. 浅谈OpenCL之Platform API(2)
  7. ArcGIS支持的常用数据类型
  8. linux命令大全(持续更新)
  9. 【服务器】创建docker、运行jupyter相关命令
  10. 关于idea中运行maven项目报错显示找不到包或符号的问题——终极方案
  11. SCRT804安装教程
  12. Linux 创建并且运行Django项目
  13. DXP2004/Altium Desinger 自己画元器件和封装,及注意事项
  14. html qq在线状态,qq各种在线状态意思
  15. 2018 Google 开发者大会.md
  16. HIGEN海坚驱动器维修FDA7045伺服变频器维修
  17. android webview 加载过程,实战:七步完成Android Webview图片加载
  18. linux usb回环程序,在Linux中创建回环设备(loopback device)的方法
  19. 目前主流微型计算机显卡总线接口是,江苏省2011年普通高校对口单招文化统考计算机专业综合理论试卷B...
  20. split().reverse().join()放一起 好记很多

热门文章

  1. 摘自ubantuer-Linux防火墙iptables学习笔记(三)iptables命令详解和举例
  2. Maven系列三Maven内置变量
  3. java_ant详解
  4. asp.net中page对象生命周期和各事件执行顺序
  5. 79. Word Search
  6. Linux常用命令行
  7. 牛客网 2018年全国多校算法寒假训练营练习比赛(第三场)D.小牛vs小客-博弈
  8. 使3D空间中物体朝向和其速度方向一致的旋转矩阵计算方案
  9. public void DeleteT(ListT EntityList) where T : class, new() 这是什么意思
  10. CentOS 6.0图解网络安装全过程