hdu4638

题意

给定一个序列,序列由1-N个元素全排列而成,求任意区间可组成的连续的段数,比如[1,2,4]两段{[1,2],[4]},[1,2,4,3]一段{[1,2,3,4]}。

对于查询的区间询问的是可组成的连续的数的段数最小值。

分析

分块排好序,O(1)的维护就是每新添加一个元素 t 查看 t-1 与 t+1 是否都在区间内,如是则段数-1,如果都不在,则段数+1。

code

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN = 1e5 + 10;
int a[MAXN];
int vis[MAXN];
int ans[MAXN];
struct node
{
int l, r, bid, id;
bool operator < (const node &other) const
{
if(bid == other.bid) return r < other.r;
return bid < other.bid;
}
}q[MAXN]; int L, R, res; void query(int l, int r, int is)
{
if(is)
{
for(int i = l; i < L; i++)
{
if(vis[a[i] - 1] && vis[a[i] + 1]) res--;
else if(!vis[a[i] - 1] && !vis[a[i] + 1]) res++;
vis[a[i]] = 1;
}
for(int i = R + 1; i <= r; i++)
{
if(vis[a[i] - 1] && vis[a[i] + 1]) res--;
else if(!vis[a[i] - 1] && !vis[a[i] + 1]) res++;
vis[a[i]] = 1;
}
for(int i = L; i < l; i++)
{
if(vis[a[i] - 1] && vis[a[i] + 1]) res++;
else if(!vis[a[i] - 1] && !vis[a[i] + 1]) res--;
vis[a[i]] = 0;
}
for(int i = r + 1; i <= R; i++)
{
if(vis[a[i] - 1] && vis[a[i] + 1]) res++;
else if(!vis[a[i] - 1] && !vis[a[i] + 1]) res--;
vis[a[i]] = 0;
}
}
else
{
for(int i = l; i <= r; i++)
{
if(vis[a[i] - 1] && vis[a[i] + 1]) res--;
else if(!vis[a[i] - 1] && !vis[a[i] + 1]) res++;
vis[a[i]] = 1;
}
}
L = l; R = r;
ans[q[is].id] = res;
} int main()
{
int T;
scanf("%d", &T);
while(T--)
{
int n, m;
memset(vis, 0, sizeof vis);
res = 0;
scanf("%d%d", &n, &m);
int bsize = sqrt(n);
for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
for(int i = 0; i < m; i++)
{
scanf("%d%d", &q[i].l, &q[i].r);
q[i].id = i;
q[i].bid = q[i].l / bsize;
}
sort(q, q + m);
for(int i = 0; i < m; i++) query(q[i].l, q[i].r, i);
for(int i = 0; i < m; i++) printf("%d\n", ans[i]);
}
return 0;
}

hdu4638的更多相关文章

  1. hdu4638 group 树状数组

    连接:http://acm.hdu.edu.cn/showproblem.php?pid=4638 题意:就给给你n个数(大小在1-n里),然后给你连续的可以构成一个块,再给你N个询问,每个询问一个l ...

  2. HDU-4638 Group 树状数组+离线

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4638 个人认为比较不错的题目. 题意:给一个1-n的排列,询问区间[l,r]的数排序后连续区间的个数. ...

  3. hdu4638 莫队算法

    莫队算法基础题,题目看懂就能做出来 #include<iostream> #include<cstring> #include<cstdio> #include&l ...

  4. HDU4638:Group(线段树离线处理)

    Problem Description There are n men ,every man has an ID(1..n).their ID is unique. Whose ID is i and ...

  5. hdu-4638

    There are n men ,every man has an ID(1..n).their ID is unique. Whose ID is i and i-1 are friends, Wh ...

  6. 2013 Multi-University Training Contest 4

    HDU-4632 Palindrome subsequence 题意:给定一个字符串,长度最长为1000,问该串有多少个回文子串. 分析:设dp[i][j]表示从 i 到 j 有多少个回文子串,则有动 ...

随机推荐

  1. sql连接查询中on筛选与where筛选的区别

    sql查询这个东西, 要说它简单, 可以很简单, 通常情况下只需使用增删查改配合编程语言的逻辑表达能力,就能实现所有功能. 但是增删查改并不能代表sql语句的所有, 完整的sql功能会另人望而生畏. ...

  2. 栈实现getMin

    题目 实现一个特殊的栈,在实现栈的基本功能的基础上,在实现返回栈中最小元素的操作. 要求 pop.push.getMin操作的时间复杂度都是O(1). 设计的栈类型可以使用现成的栈结构. 解答 在设计 ...

  3. (function($){….})(jQuery)一种js插件写法

    我们先看第一个括号里边的内容:function($){….},这不就是一个匿名的函数吗?但是它的形参比较奇怪,是$,这里主要是为了不与其它的库冲突. 这样我们就比较容易理解第一个括号内的内容就是定义了 ...

  4. python scrapy 抓取脚本之家文章(scrapy 入门使用简介)

    老早之前就听说过python的scrapy.这是一个分布式爬虫的框架,可以让你轻松写出高性能的分布式异步爬虫.使用框架的最大好处当然就是不同重复造轮子了,因为有很多东西框架当中都有了,直接拿过来使用就 ...

  5. 《分布式Java应用之基础与实践》读书笔记一

    分布式Java应用的体系结构知识简单分为: 网络通信:包括协议和IO 消息方式的系统间通信:包括基于Java包.基于开源框架.性能角度 远程调用方式的系统间通信:包括基于Java包.基于开源框架.性能 ...

  6. Visual Studio(VS) F12 查看DLL源代码

    前言 我在VS中调试某个函数时,突发奇想"能不能使用VS的F12(转到定义)查看这个dll中当前函数的实现(源码),而不是像VS自带功能那样只能看到函数名和参数?" 回想起来在安装 ...

  7. java面试题—精选30道Java笔试题解答(一)

    下面都是我自己的答案非官方,仅供参考,如果有疑问或错误请一定要提出来,大家一起进步啦~~~ 1. 下面哪些是Thread类的方法() A start() B run() C exit() D getP ...

  8. qt中字符串转换

    11.各种数据类型的相互转换char * 与 const char *的转换char *ch1="hello11";const char *ch2="hello22&qu ...

  9. Oracle数据泵(上)

    导出 (以导出表空间为例) 1.给用户创建密码 alter user system identified by 00000000;   2.创建导出目录 create or replace  dire ...

  10. MySQL安装、输入密码闪退、workbench使用

    1.安装 安装就不细说了,网上一搜一大堆,但是教程推荐这个: wikihow 网站是wikiHOW,很有意思的网站,比百度经验强大很多. 2.输入密码闪退 安装完成后,在开始菜单,打开 开始程序 界面 ...