HDU 4638-Group(线段树+离线处理)
题意:
给n个编号,m个查询每个查询l,r,求下标区间[l,r]中能分成标号连续的组数(一组内的标号是连续的)
分析:
我们认为初始,每个标号为一个组(线段树维护区间组数),从左向右扫序列,当前标号,要考虑和他相邻的标号的位置,若前面位置出现了和它相邻的标号,
则前面位置组数减一(因为可以合并成一组),查询区间离线处理,保证了查询的正确。
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <string>
#include <cctype>
#include <complex>
#include <cassert>
#include <utility>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
typedef pair<int,int> PII;
typedef long long ll;
#define lson l,m,rt<<1
#define pi acos(-1.0)
#define rson m+1,r,rt<<1|1
#define All 1,N,1
#define N 100010
#define read freopen("in.txt", "r", stdin)
const ll INFll = 0x3f3f3f3f3f3f3f3fLL;
const int INF= 0x7ffffff;
const int mod = ;
//pos 相邻标号在前面出现的位置
int sum[N*],n,pos[N],e,a[N],tmp[N];
struct que{
int l,r,id;
}q[N];
bool cmp(que x,que y){
return x.r<y.r;
}
void pushup(int rt){
sum[rt]=sum[rt<<]+sum[rt<<|];
}
void build(int l,int r,int rt){
if(l==r){
sum[rt]=;
return;
}
int m=(l+r)>>;
build(lson);
build(rson);
pushup(rt);
}
void update(int p,int v,int l,int r,int rt){
if(l==r){
sum[rt]+=v;
return;
}
int m=(l+r)>>;
if(p<=m)update(p,v,lson);
else update(p,v,rson);
pushup(rt);
}
int query(int L,int R,int l,int r,int rt){
if(L<=l&&R>=r){
return sum[rt];
}
int m=(l+r)>>;
int num=;
if(L<=m)num+=query(L,R,lson);
if(R>m)num+=query(L,R,rson);
return num;
}
void solve(){
sort(q+,q+e+,cmp);
memset(pos,,sizeof(pos));
build(,n,);
int b=;
for(int i=;i<=n;++i){
if(pos[a[i]-])
update(pos[a[i]-],-,,n,);
if(pos[a[i]+])
update(pos[a[i]+],-,,n,);
pos[a[i]]=i;
while(b<=e&&q[b].r==i){
tmp[q[b].id]=query(q[b].l,q[b].r,,n,);
b++;
}
}
for(int i=;i<=e;++i)
printf("%d\n",tmp[i]);
}
int main()
{
int t;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&e);
for(int i=;i<=n;++i)
scanf("%d",&a[i]);
for(int i=;i<=e;++i){
scanf("%d%d",&q[i].l,&q[i].r);
q[i].id=i;
}
solve();
}
return ;
}
HDU 4638-Group(线段树+离线处理)的更多相关文章
- HDU 4417 【线段树+离线处理】
http://acm.hdu.edu.cn/showproblem.php?pid=4417 题意:找出给定区间内,有多少个数小于等于给定的数.用线段树维护的话会超时,要用到线段树的离线操作,对询问与 ...
- hdu 4288 Coder (线段树+离线)
题意: 刚开始有一个空集合.有三种操作: 1.往集合中加入一个集合中不存在的数 x 2.从集合中删除一个已经存在的数 x 3.计算集合的digest sum并输出. digest sum求 ...
- HDU 4638 Group 【树状数组,分块乱搞(莫队算法?)】
根据题目意思,很容易得出,一个区间里面连续的段数即为最少的group数. 题解上面给的是用树状数组维护的. 询问一个区间的时候,可以一个一个的向里面添加,只需要判断a[i]-1 和 a[i]+1是否已 ...
- HDU4638:Group(线段树离线处理)
Problem Description There are n men ,every man has an ID(1..n).their ID is unique. Whose ID is i and ...
- HDU - 3874 Necklace (线段树 + 离线处理)
欢迎參加--每周六晚的BestCoder(有米! ) Necklace Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 65536/3 ...
- HDU 3333 & 3874 (线段树+离线询问)
两个题目都是求区间之内,不重复的数字之和,3333需要离散化处理................. 调试了一下午........说多了都是泪........... #include <iostr ...
- Necklace HDU - 3874 (线段树/树状数组 + 离线处理)
Necklace HDU - 3874 Mery has a beautiful necklace. The necklace is made up of N magic balls. Each b ...
- hdu 4031 attack 线段树区间更新
Attack Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)Total Subm ...
- hdu 3016 dp+线段树
Man Down Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...
随机推荐
- Happy Number
https://leetcode.com/problems/happy-number/ Write an algorithm to determine if a number is "hap ...
- hibernate.cfg.xml hibernate 配置文件模板
<?xml version='1.0' encoding='UTF-8'?> <!--表明解析本XML文件的DTD文档位置,DTD是Document Type Definition ...
- *[codility]MissingInteger
今天开始刷刷codility上respectable的题目,难度适中. https://codility.com/demo/take-sample-test/missing_integer 本题是找出 ...
- linux上tomcat安装
转载:http://my.oschina.net/zmf/blog/309484 tomcat安装 下载后解压:tar -zxvf apache-tomcat-7.0.28 移动到/usr/local ...
- OpenCV源码阅读(1)---matx.h---mat类与vec类
matx.h matx类是opencv中的一个基础类,其位于core模块中,所执行的操作时opencv矩阵和向量的运算.如果熟悉基于matlab的图像处理,那么很容易想到,所有对图像的操作归根结底都是 ...
- linux用VSFTP搭建FTP服务器
一般在各种linux的发行版中,默认带有的ftp软件是vsftp,从各个linux发行版对vsftp的认可可以看出,vsftp应该是一款不错的ftp软件. sudo apt-get install v ...
- makefile使用
linux make手册:http://www.gnu.org/software/make/manual/make.html 一篇文章: 假设我们有一个程序由5个文件组成,源代码如下:/*main.c ...
- distinct用法
distinct可以列出不重复的记录,对于单个字段来说distinct使用比较简单,但是对于多个字段来说,distinct使用起来会使人发狂.而且貌似也没有见到微软对distinct使用多字段的任何说 ...
- [POJ3177]Redundant Paths(双连通图,割边,桥,重边)
题目链接:http://poj.org/problem?id=3177 和上一题一样,只是有重边. 如何解决重边的问题? 1. 构造图G时把重边也考虑进来,然后在划分边双连通分量时先把桥删去,再划分 ...
- bzoj3955
首先,最短路不同的两辆车一定不会发生堵塞 对于最短路相同的点,我们把属于最短路径上的边拎出来建图跑最大流即可 然后我TLE了…… 因为很明显建出来图很大,而真正流的流量很小 普通的初始标号都是0的sa ...