题目链接: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. web学习第二天

    今天是学习web的第二天,早上用css3做了个会动的小熊,border-radius为圆的半径, .smallxiong {    width: 400px;    height: 400px;    ...

  2. 分布式系统的CAP(Redis)

    CAP理论就是说在分布式存储系统中,最多只能实现上面的两点.而由于当前的网络硬件肯定会出现延迟丢包等问题,所以 分区容忍性是我们必须需要实现的. 所以我们只能在一致性和可用性之间进行权衡,没有NoSQ ...

  3. node 动态页面渲染

    代码: 'use strict' const express = require('express'); const consoldiate = require('consolidate'); con ...

  4. R语言绘图:ROC曲线图

    使用pROC包绘制ROC曲线 #####***绘制ROC曲线***##### library("pROC") N <- dim(data2)[1] #数据长度 set.see ...

  5. linux io 学习笔记(02)---条件变量,管道,信号

    条件变量的工作原理:对当前不访问共享资源的任务,直接执行睡眠处理,如果此时需要某个任务访问资源,直接将该任务唤醒.条件变量类似异步通信,操作的核心:睡眠.唤醒. 1.pthread_cond_t  定 ...

  6. git的关于测试的相关的内容

    今天,我们来讲一下git的分支的一些内容,在以前的时候,我一直都以为,对于一个项目,这个时候,我们把这个项目叫做项目a项目,这个a项目有master,staging,以及我自己的分支xxx,当我想上测 ...

  7. 对C语言连等式的学习

    例子如下 [pgsql@localhost soft]$ cat test1.c #include <stdlib.h> #include <stdio.h> int main ...

  8. CentOS7安装Oracle 11gR2 图文详解

    注:Oracle11gR2 X64安装 一.环境准备 安装包: 1.VMware-workstation-full-11.1.0-2496824.exe 2.CentOS-7-x86_64-DVD-1 ...

  9. 在Go语言里检测内存泄漏

    我们先来设定一下数据库,建立一个MySQL数据库表,名为users,里面有login_name.nickname.uid.password.forbidden几个字段,其中uid与forbidden为 ...

  10. MySQL查询所有库中表名

    select table_name from information_schema.tables where table_schema='contract_ggpt' and table_type=' ...