题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4417

把数字离散化,一个查询拆成两个查询,每次查询一个前缀的和。主要问题是这个数组是静态的,如果带修改操作就不能离线了。

//http://acm.hdu.edu.cn/showproblem.php?pid=4417
#include<bits/stdc++.h>
using namespace std; const int maxn=;
int tree[maxn];
int N; int lowbit(int x)
{
return x&-x;
} void init(int n)
{
N=n;
for (int i=;i<=n;i++) tree[i]=;
} void add(int k,int x)
{
while (k<=N)
{
tree[k]+=x;
k+=lowbit(k);
}
} int sum(int k)
{
int res=;
while (k)
{
res+=tree[k];
k-=lowbit(k);
}
return res;
} vector<int> ls;
int a[maxn]; struct Query
{
int id;
int sgn;
int p,j;
int res;
bool operator < (const Query & q) const
{
return p<q.p;
}
}query[maxn*]; int ans[maxn]; int main()
{
int t;
scanf("%d",&t);
for (int cas=;cas<=t;cas++)
{
int n,m;
scanf("%d%d",&n,&m);
for (int i=;i<=n;i++) scanf("%d",&a[i]);
ls.clear();
for (int i=;i<=n;i++) ls.push_back(a[i]);
sort(ls.begin(),ls.end());
ls.erase(unique(ls.begin(),ls.end()),ls.end());
for (int i=;i<m;i++)
{
int l,r,h;
scanf("%d%d%d",&l,&r,&h);
l++;
r++;
int j=upper_bound(ls.begin(),ls.end(),h)-ls.begin();
query[i*].id=i;
query[i*].p=l-;
query[i*].j=j;
query[i*].sgn=-;
query[i*+].id=i;
query[i*+].p=r;
query[i*+].j=j;
query[i*+].sgn=;
}
sort(query,query+m*);
init(ls.size());
int now=;
for (int i=;i<=n;i++)
{
if (i)
{
int j=lower_bound(ls.begin(),ls.end(),a[i])-ls.begin()+;
add(j,);
}
while (now<m* && query[now].p==i)
{
query[now].res=sum(query[now].j);
now++;
}
}
memset(ans,,sizeof(ans));
for (int i=;i<m*;i++) ans[query[i].id]+=query[i].res*query[i].sgn;
printf("Case %d:\n",cas);
for (int i=;i<m;i++) printf("%d\n",ans[i]);
}
return ;
}

[hdu 4417]树状数组+离散化+离线处理的更多相关文章

  1. HDU 1394 树状数组+离散化求逆序数

    对于求逆序数问题,学会去利用树状数组进行转换求解方式,是很必要的. 一般来说我们求解逆序数,是在给定一串序列里,用循环的方式找到每一个数之前有多少个比它大的数,算法的时间复杂度为o(n2). 那么我们 ...

  2. hdu 5792 树状数组+离散化+思维

    题目大意: Given a sequence A with length n,count how many quadruple (a,b,c,d) satisfies: a≠b≠c≠d,1≤a< ...

  3. Disharmony Trees HDU - 3015 树状数组+离散化

    #include<cstdio> #include<cstring> #include<algorithm> #define ll long long using ...

  4. hdu 4325 树状数组+离散化

    思路:这题的思路很容易想到,把所有时间点离散化,然后按时间一步一步来,当到达时间i的时候处理所有在i处的查询. 这个代码怎一个挫字了得 #include<iostream> #includ ...

  5. Turing Tree HDU - 3333 (树状数组,离线求区间元素种类数)

    After inventing Turing Tree, 3xian always felt boring when solving problems about intervals, because ...

  6. Swaps and Inversions HDU - 6318 树状数组+离散化

    #include<iostream> #include<algorithm> #include<cstring> #include<cstdio> us ...

  7. C - The Battle of Chibi HDU - 5542 (树状数组+离散化)

    Cao Cao made up a big army and was going to invade the whole South China. Yu Zhou was worried about ...

  8. hdu 4638 树状数组 区间内连续区间的个数(尽可能长)

    Group Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Subm ...

  9. hdu 4777 树状数组+合数分解

    Rabbit Kingdom Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

随机推荐

  1. node获取URL数据

    req.method  -->GET req.hostname  -->127.0.0.1 req.originalUrl  -->/test/test/test?name=wang ...

  2. Linux 控制台

    shell shell命令分为两种:分别是内部命令和外部命令. 内部命令:在安装的时候嵌入系统内核. 外部命令:以文件的形式存在. 可以使用type命令查看是内部命令还是外部命令. Linux中,默认 ...

  3. HDU 5530:Pipes Selection

    题意: 给定长度为\(L\),元素总和为\(S\)的非负整数序列\(A\),对于每一个\(1 \leq i \leq S\),求出:所有满足\(\sum_{j=l}^rA_j=i\)的二元组\((l, ...

  4. HDU暑假多校第三场H.Monster Hunter

    一.题意 给定一个树状地图,每个树节点上有一只怪物,打死一只怪物的过程中将会消耗A点HP,打死之后将会获得B点HP.因为树状结构,所以每只怪物必须先打死父节点的怪兽之后在打死子节点的怪物.现在,给定每 ...

  5. css在线sprite

    大家知道网站图片多,浏览器下载多个图片要有多个请求.可是请求比较耗时,那怎么办呢? 对,方法就是css sprite. 今天我们来看看css在线sprite 百度搜索css-sprite 打开www. ...

  6. python2.7练习小例子(十六)

    16):题目:输入一行字符,分别统计出其中英文字母.空格.数字和其它字符的个数.     程序分析:利用 while 或 for 语句,条件为输入的字符不为 '\n'.     程序源代码: #!/u ...

  7. 从装机到配置-CentOS6.5

    L006课程结束后的总结 首先:系统(cat /etc/redhat-release):CentOS release 6.5 (Final) 版本(uname -r):2.6.32-431.el6.x ...

  8. CentOS7安装配置SonarQube

    一.SonarQubeServer 1.前提 安装好mysql5.7和jdk1.8. (1)安装Mysql create user 'sonar'@'localhost' identified by ...

  9. Python图像全屏显示

    需要在嵌入式设备上全屏显示图像,使用pil显示图像时,只能通过系统的图像浏览器显示.所以使用Python自带的tkinter import Tkinter as tk   这句在Python3中已经改 ...

  10. throw er; // Unhandled 'error' event&Error: ENOENT: no such file or directory,

    今天做一个文件上传的项目时, 用express-formidable往硬盘里面存文件时, 报  ENOENT:no such file or directory 原因就是程序不能像别的语言一样不存在就 ...