题意:给定 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. LeetCode 6. ZigZag Conversion Question

    题意:给你一个字符串和行数numRows,要求把该字符串变成一个"之"字形状后,按行数输出该字符串. 例子:"ABCDEFGHIJKLMNO", 4. 该字符串 ...

  2. WPF自定义下拉控件

    可以搜索的下拉条 using System; using System.Collections; using System.Collections.Generic; using System.Coll ...

  3. 添加链接服务器 SQL SERVER

    使用sql语句: exec sp_addlinkedserver @server='serverontest',@provider='sqloledb',@srvproduct='',@datasrc ...

  4. C++ 推断进程是否存在

    [cpp] view plaincopyprint? #include <windows.h> #include "psapi.h" #include"std ...

  5. 查看android进程信息

    打开adb shell.直接ps命令 假设查看某特定进程,比方<圣火英雄传>.用grep过滤 各列參数意义: USER        进程当前用户: PID             Pro ...

  6. Android应用程序中Activity的生命周期

    Android应用程序中Activity的生命周期 对于Android来说Activity的生命周期是非常的重要,尤其是对于新学者来说,只有充分了解了Activity的生命周期,才能写出优良用户体验的 ...

  7. UIView层次管理bringSubviewToFront,sendSubviewToBack

    将一个UIView显示在最前面只需要调用其父视图的 bringSubviewToFront()方法. 将一个UIView层推送到背后只需要调用其父视图的 sendSubviewToBack()方法. ...

  8. 将EXCEL中的列拼接成SQL insert插入语句

    工作中经常需要将EXCEL文件中的数据导入到各种数据库,但是对于不熟悉数据库的人来说,如果直接使用命令执行导入,这无疑是一个难题,也是一个风险.这里我们直接在EXCEL文件中拼接成标准的SQL ins ...

  9. Win7_x64下卸载Oracle11g

    Windows下Oracle11g卸载顺序1.计算机管理-->服务和应用程序-->停止所有Oracle相关服务2.运行D:\app\Administrator\product\11.2.0 ...

  10. css行高line-height的用法(转)

    本文导读: “行高“指一行文子的高度,具体来说是指两行文子间基线间的距离.在CSS,line-height被用来控制行与行之间的垂直距离.line- height 属性会影响行框的布局.在应用到一个块 ...