HDU---4417Super Mario 树状数组 离线操作
题意:给定 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 树状数组 离线操作的更多相关文章
- hdu 4417 Super Mario 树状数组||主席树
Super Mario Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Prob ...
- HDU 4630 No Pain No Game 树状数组+离线操作
题意:给一串数字,每次查询[L,R]中两个数的gcd的最大值. 解法:容易知道,要使取两个数让gcd最大,这两个数最好是倍数关系,所以处理出每个数的所有倍数,两两间根据倍数关系形成一条线段,值为该数. ...
- hdu 3333(树状数组 + 离线操作)
Turing Tree Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- Super Mario 树状数组离线 || 线段树
Super Mario Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDU 2838 (DP+树状数组维护带权排序)
Reference: http://blog.csdn.net/me4546/article/details/6333225 题目链接: http://acm.hdu.edu.cn/showprobl ...
- HDU 2689Sort it 树状数组 逆序对
Sort it Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- hdu 4046 Panda 树状数组
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4046 When I wrote down this letter, you may have been ...
- HDU3874Necklace(树状数组+离线操作)
此题的大意思说有一串珠子,每个珠子都有自己的欣赏值value,现在给你一串珠子每个的欣赏值,并给出一些询问,查询某个区间内部总欣赏值是多少,但是有一个约定就是如果这个区间内部有两个珠子的欣赏值是一样的 ...
- hdu 5497 Inversion 树状数组 逆序对,单点修改
Inversion Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5497 ...
随机推荐
- Web Service那点事
出现 如今基于浏览器的client应用程序越来越流行,而从开发角度来看,一方面是client浏览器的安装配置不再须要我们再去花费非常大的精力.还有一方林则是由于client和server之间通信的问题 ...
- Android 之 SharedPreferences
1 简介 SharedPreferences是一种轻量级的数据存储方式,它可以用键值对的方式把简单数据类型(boolean.int.float.long和String)存储在应用程序的私有目录下(da ...
- QLCDNumber设置背景色和显示数字颜色
只看楼主 倒序阅读楼主 发表于: 2013-10-22 //LCD时间显示 QLCDNumber *m_pLcdTime = new QLCDNumber(thi ...
- MySQL 加密/压缩函数
这些问题可能导致数据值的改变.一般而言,上述问题可能在你使用非二进制串数据类型(如char,varchar,text等数据类型)的情况下发生. AES_ENCRYPT()和AES_DECRYPT() ...
- DOS环境下MySQL使用及不同字符集之间的转换
mysql -uroot -p; show databses; 创建数据库\c; create database webclass; use webclass; 创建表并设置好各字段的属性\c cre ...
- DateTimeBox( 日期时间输入框)
本节课重点了解 EasyUI 中 DateTimeBox(日期时间输入框)组件的使用方法,这个组件依赖于 DateBox(日期输入框)组件和 TimeSpinner(时间微调)组件. 一. 加载方式/ ...
- NHibernate+NUnit (VS2012+SQL Server2008) (转)
源博客:http://blog.csdn.net/jiajiayouba/article/details/9877875 首先,你要明白,NHibernate在.NET中的作用和Hibernate在J ...
- js中substring/substr和C#中Substring的用法
一:在js中截取字符串的方法有两个:substring和substr 1.方法: substring(int stringIndex,[int endIndex]) 截取从索引为stringIndex ...
- displaytag 动态列实现
这种动态列的实现方法来自displaytag-examples-1.2.war提供的示例中,实际上下载下来的zip文件中不仅有各种jar包,还有这个包含各种例子的war包,是学习displaytag的 ...
- root用户安装的软件在普通用户不生效
root安装完成后,有时候需要进入普通用户,修改以下文件才能生效: .在这个文件中添加环境变量 vim ~/.bash_profile .生效 source ~/.bash_profile