题意:给定 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. 【KMP】Period

    KMP算法 Next[]函数深入理解,Next[]当前字符前匹配字符数,串长n-Next[i]=串内循环子串的长度p. 本题求子循环串内循环节数. Problem Description For ea ...

  2. 根据请求头跳转判断Android&iOS

    if(navigator.userAgent.match(/Android/i)) { window.location = 'http://apk.hiapk.com/m/downloads?id=c ...

  3. android后台截屏实现(2)--screencap源码修改

    首先找到screencap类在Android源码中的位置,/442/frameworks/base/cmds/screencap/screencap.cpp 源码如下: /* * Copyright ...

  4. MySQL添加外键的方法

    为book表添加外键: <1>明确指定外键的名称: 语法:alter table 表名 add constraint 外键的名称 foreign key(你的外键字段名) REFERENC ...

  5. somethings about QSplitter

    m_splitter = new QSplitter(Qt::Horizontal);    m_splitter->addWidget(this->m_leftWidget);    m ...

  6. springmvc中项目启动直接调用方法

    1. <servlet> <servlet-name> AutoServlet </servlet-name> <servlet-class> com. ...

  7. Combo( 自定义下拉框) 组件

    本节课重点了解 EasyUI 中 Combo(自定义下拉框)组件的使用方法,这个组件依赖于ValidateBox(验证框)组件 一. 加载方式自定义下拉框不能通过标签的方式进行创建.<input ...

  8. NUnit单元测试初试

    创建项目,创建几个方法 创建测试类 开启NUnit测试工具,新建一个测试项目 打开测试的程序集 选择节点,点击测试,绿色通过,红色说明有错误

  9. (转)chm格式的电子书打开是空白的解决办法

    当我们从网络上下载chm格式的电子书到本地后,打开就发现chm格式的电子书右边的目录是正常的,但是chm格式的电子书内容部分却是空白的情况,很多时候我们都以为是中毒了,但是用杀毒软件却杀不出来,其实很 ...

  10. 后台的Activity被系统回收怎么办?

    onSaveIntanceState,当程序中的某个Activity A在运行中,主动或者被动的运行另外一个新的Activity B,这个时候 A就会执行onSaveIntanceState(Bund ...