题目链接: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. Python正则表达式-基础

    Python正则表达式-基础 本文转载自昔日暖阳,原文地址:http://www.osheep.cn/4806.html python使用正则,需要先引入re模块 import re 匹配符 单个字符 ...

  2. C语言程序设计·谭浩强(第四版)第二章课后习题的答案,算法——程序的灵魂

    C语言程序小练习 1.用C语言设计程序算出1-1/2+1/3-14+1/5...+1/99-1/100的值 #include<stdio.h> int main() { ; double ...

  3. Qt——菜单栏、工具栏、状态栏

    1.菜单栏 菜单栏的意义是将可点击触发最终事件的集中在一起,所以菜单栏中是QAction 添加菜单栏是QMainWindow的行为 QMenubar *menubar = this->addMe ...

  4. 多线程编程之Apue3rd_Chapter11之互斥锁_读写锁_自旋锁

    学习了apue3rd的第11章,主要讲的是多线程编程.因为线程共享进程的资源比如堆和全局变量,多线程编程最重要的是,使用各种锁进行线程同步. 线程编程首先要学习的三个函数如下: #include &l ...

  5. (数据科学学习手札32)Python中re模块的详细介绍

    一.简介 关于正则表达式,我在前一篇(数据科学学习手札31)中已经做了详细介绍,本篇将对Python中自带模块re的常用功能进行总结: re作为Python中专为正则表达式相关功能做出支持的模块,提供 ...

  6. 关于实现mybatis order by 排序传递参数实现 问题记录

    一    问题场景:本人项目纯纯的后端系统  并且项目前端采用纯纯的原生js 实现 1)表格  通过查询列表数据放入到域中  前段采用 for循环的方式实现遍历生成列表 2)分页实现本人是公司内部自定 ...

  7. Mysql双主操作

    MySQL双主(主主)架构方案   在企业中,数据库高可用一直是企业的重中之重,中小企业很多都是使用mysql主从方案,一主多从,读写分离等,但是单主存在单点故障,从库切换成主库需要作改动.因此,如果 ...

  8. AutoMapper.RegExtension 介绍

    AutoMapper.RegExtension 为一个特小特小特小的用来根据约定自动调用AutoMapper中的方法配置映射的扩展库.你可以引入该库也可以将源码中核心部分的代码文件夹整个拷贝至项目中. ...

  9. 《python核心编程第二版》第1章练习

    1–1. 安装 Python.请检查 Python 是否已经安装到你的系统上,如果没有,请下载并 安装它 略 1–2.  执行 Python.有多少种运行 Python 的不同方法?你喜欢哪一种?为什 ...

  10. 【SpringCloud】第二篇: 服务消费者(rest+ribbon)

    前言: 必需学会SpringBoot基础知识 简介: spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理.服务发现.断路器.路由.微代理.事件总线.全局锁.决策竞选. ...