题意:给定 n个数,查询 位置L R内 小于x的数有多少个。

对于某一次查询 把所有比x小的数 ”的位置“ 都加入到树状数组中,然后sum(R)-sum(L-1)就是答案,q次查询就要离线操作了,按高度排序。

#include <set>
#include <map>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const double eps = 1e-;
template <class T>
inline bool scan_d(T &ret)
{
char c;
int sgn;
if(c=getchar(),c==EOF)
return ;
while(c!='-'&&(c<''||c>'')) c=getchar();
sgn = (c=='-')?-:;
ret =(c=='-')?:(c-'');
while(c=getchar(),c>=''&&c<='') ret=ret*+(c-'');
ret*=sgn;
return ;
}
const int maxn = 1e5+;
int n,q,c[maxn];
int lowbit (int x)
{
return x & -x;
}
void add(int x,int d)
{
while (x <= n)
{
c[x] += d;
x += lowbit(x);
}
}
int sum(int x)
{
int ans = ;
while (x > )
{
ans += c[x];
x -= lowbit(x);
}
return ans;
}
struct Node1
{
int v,index;
}h[maxn];
struct Node2
{
int l,r,v,index,ans;
}H[maxn];
bool cmp1(const Node1 &n1,const Node1 &n2)
{
return n1.v < n2.v;
}
bool cmp2(const Node2 &n1,const Node2 &n2)
{
return n1.v < n2.v;
}
bool cmp3(const Node2 &n1,const Node2 &n2)
{
return n1.index < n2.index;
}
int main(void)
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
int t,cas = ;
scanf ("%d",&t);
while (t--)
{
memset(c,,sizeof(c));
scanf ("%d%d",&n,&q);
for (int i = ; i <= n; i++)
{
scanf ("%d",&h[i].v);
h[i].index = i;
}
sort(h+,h+n+,cmp1);
for (int i = ; i <= q; i++)
{
scanf ("%d%d%d",&H[i].l,&H[i].r,&H[i].v);
H[i].l++;
H[i].r++;
H[i].index = i;
}
sort(H+,H+q+,cmp2);
int j = ;
for (int i = ; i <= q; i++)
{
int tmp = H[i].v;
while (h[j].v <= tmp&&j<=n) //这里要加j<=n 不然会死循环
{
add(h[j].index,);
j++;
}
H[i].ans = sum(H[i].r) - sum(H[i].l-);
}
sort(H+,H+q+,cmp3);
printf("Case %d:\n",cas++);
for (int i = ; i <= q; i++)
{
printf("%d\n",H[i].ans);
}
}
return ;
}

HDU---4417Super Mario 树状数组 离线操作的更多相关文章

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

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

  2. HDU 4630 No Pain No Game 树状数组+离线操作

    题意:给一串数字,每次查询[L,R]中两个数的gcd的最大值. 解法:容易知道,要使取两个数让gcd最大,这两个数最好是倍数关系,所以处理出每个数的所有倍数,两两间根据倍数关系形成一条线段,值为该数. ...

  3. hdu 3333(树状数组 + 离线操作)

    Turing Tree Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  4. Super Mario 树状数组离线 || 线段树

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

  5. HDU 2838 (DP+树状数组维护带权排序)

    Reference: http://blog.csdn.net/me4546/article/details/6333225 题目链接: http://acm.hdu.edu.cn/showprobl ...

  6. HDU 2689Sort it 树状数组 逆序对

    Sort it Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

  7. hdu 4046 Panda 树状数组

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4046 When I wrote down this letter, you may have been ...

  8. HDU3874Necklace(树状数组+离线操作)

    此题的大意思说有一串珠子,每个珠子都有自己的欣赏值value,现在给你一串珠子每个的欣赏值,并给出一些询问,查询某个区间内部总欣赏值是多少,但是有一个约定就是如果这个区间内部有两个珠子的欣赏值是一样的 ...

  9. hdu 5497 Inversion 树状数组 逆序对,单点修改

    Inversion Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5497 ...

随机推荐

  1. 有关UITableViewCell的侧滑删除以及使用相关大神框架MGSwipeTableCell遇到的小问题

    提起笔,却不知道从何写起了,今天一整天都耗费在了这个可能根本不算是问题的小问题上,至今仍有一种蛋蛋的忧桑..(噢,不是提笔,是键盘手T_T) 表格视图在项目中就像是每日的家常便饭,在cell上添加侧滑 ...

  2. Gulp 简单的开发环境搭建

    //获取gulp //require()是 node (CommonJS)中获取模块的语法 var gulp=require('gulp'); //获取gulp-concat模块(用于合并文件):np ...

  3. windows服务程序

    首先创建一个myService的窗体程序作为服务安装卸载控制器(管理员身份运行vs,windows服务的安装卸载需要管理员权限)   在同一个解决方案里面添加一个windows服务程序,取名myWin ...

  4. Android下如何理解onMeasure,onLayout的过程

    在Android中view如何完成绘制这个过程介绍了很多,但是很多理论化的东西,最近重新整理一下,通俗的讲解一下. View绘制过程就好比你向银行贷款, 在执行onMeasure的时候,好比银行告诉你 ...

  5. 强制转https

    原文:http://blog.csdn.net/wzy_1988/article/details/8549290 需求简介 基于nginx搭建了一个https访问的虚拟主机,监听的域名是test.co ...

  6. VMware Virtual Machine安装报错解决1

    安装完VMware virtual machine 后,再进行 "create a new virtual machine"最后点击"Finish"时,报如下错 ...

  7. oracle 配置服务端

    oracle 配置服务端,类似我们配置java环境一样 防止乱码的配置: 变量名:NLS_LANG 变量值:SIMPLIFIED CHINESE_CHINA.ZHS16GBK 选择数据库的配置(重要) ...

  8. 通过Url传多个参数方法

    MVC3通过URL传值,一般情况下都会遇到[从客户端(&)中检测到有潜在危险的 Request.Path 值]的问题 这个问题的解决方法,我的其他博文已经有了说明,这里给出连接[从客户端(&a ...

  9. 如何读懂SQL Server的事务日志

    简介 本文将介绍SQL Server的事务日志中记录了哪一些信息,如何来读懂这些事务日志中信息.首先介绍一个微软没有公开的函数fn_dblog,在文章的接下来的部分主要用到这个函数来读取事务日志. f ...

  10. oracle服务开机自启动

    1.修改oracle系统配置文件::/etc/oratab vi /etc/oratab orcl:/opt/oracle/product/10.2.0/db_1:Y 2.在 /etc/init.d/ ...