hdu 4638 树状数组 区间内连续区间的个数(尽可能长)
Group
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1959 Accepted Submission(s): 1006
of an interval is sum of these value of groups. The people of same group's id must be continuous. Now we chose an interval of men and want to know there should be how many groups so the value of interval is max.
For each case first line is n, m(1<=n ,m<=100000) indicate there are n men and m query.
Then a line have n number indicate the ID of men from left to right.
Next m line each line has two number L,R(1<=L<=R<=n),mean we want to know the answer of [L,R].
Sample Input
1
5 2
3 1 2 5 4
1 5
2 4
Sample Output
1
2
/*
hdu 4638 树状数组 区间内连续区间的个数(尽可能长) 给你n个数,让你查询区间[l,r]内最长连续区间的个数。求的是区间长度平方的和,
所以区间长度越长越好
3 1 2 5 4 在[1,5]上:1,2,3,4,5 1个
在[2,4]上:1,2 和 4 2个
首先我们把每个数看成独立的,即每个数赋值为1
对于当前出现的a[i],如果前面出现了a[i-1],a[i+1]那么就能组成一队,但是我们
是从左往右递推出来的,所以保存的值应尽可能在右边以方便查询,所以如果前面出现了
a[i-1],a[i+1].则在它们的位置上-1,删除它们的独立值. hhh-2016-04-04 17:05:44
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <functional>
using namespace std;
#define lson (i<<1)
#define rson ((i<<1)|1)
typedef long long ll;
const int mod = 10007;
const int maxn = 100050; int s[maxn];
int a[maxn];
int p[maxn];
int ans[maxn];
struct node
{
int l,r;
int id;
} op[maxn];
int n,m;
bool cmp(node a,node b)
{
return a.r < b.r;
} int lowbit(int x)
{
return x&(-x);
} void add(int x,int val)
{
while(x <= n)
{
s[x] += val;
x += lowbit(x);
}
} int sum(int x)
{
int cnt = 0;
while(x)
{
cnt += s[x];
x -= lowbit(x);
}
return cnt;
} int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
memset(s,0,sizeof(s));
for(int i = 1; i <= n; i++)
{
scanf("%d",&a[i]);
p[a[i]] = i;
}
for(int i =0; i< m; i++)
{
scanf("%d%d",&op[i].l,&op[i].r);
op[i].id = i;
}
sort(op,op+m,cmp);
for(int i = 1,cur = 0; i <= n; i++)
{
add(i,1);
if(a[i] > 1 && p[a[i]-1] < i)
add(p[a[i]-1],-1);
if(a[i] < n && p[a[i]+1] < i)
add(p[a[i]+1],-1); while(i == op[cur].r && cur < m)
{
ans[op[cur].id] = sum(op[cur].r)-sum(op[cur].l-1);
cur++;
}
}
for(int i = 0; i < m; i++)
printf("%d\n",ans[i]);
}
return 0;
}
hdu 4638 树状数组 区间内连续区间的个数(尽可能长)的更多相关文章
- HDU 5654 xiaoxin and his watermelon candy 离线树状数组 区间不同数的个数
xiaoxin and his watermelon candy 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5654 Description Du ...
- hdu 4638 树状数组
思路:将查询区间按右节点的升序排列,然后插入第i个数的时候,若nun[i]+1已经插入,那么就update(pre[num[i]+1],-1):pre[]表示的是该数的位置.同样若 num[i]-1存 ...
- HDU 4638 树状数组 想法题
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4638 解题思路: 题意为询问一段区间里的数能组成多少段连续的数.先考虑从左往右一个数一个数添加,考虑当 ...
- hdu 1754 I Hate It(树状数组区间求最值)2007省赛集训队练习赛(6)_linle专场
题意: 输入一行数字,查询第i个数到第j个数之间的最大值.可以修改其中的某个数的值. 输入: 包含多组输入数据. 每组输入首行两个整数n,m.表示共有n个数,m次操作. 接下来一行包含n个整数. 接下 ...
- hdu 1116 敌兵布阵(树状数组区间求和)
题意: 给出一行数字,然后可以修改其中第i个数字,并且可以询问第i至第j个数字的和(i <= j). 输入: 首行输入一个t,表示共有t组数据. 接下来每行首行输入一个整数n,表示共有n个数字. ...
- hdu 4777 树状数组+合数分解
Rabbit Kingdom Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- HDU 2852 (树状数组+无序第K小)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2852 题目大意:操作①:往盒子里放一个数.操作②:从盒子里扔掉一个数.操作③:查询盒子里大于a的第K小 ...
- 【bzoj5173】[Jsoi2014]矩形并 扫描线+二维树状数组区间修改区间查询
题目描述 JYY有N个平面坐标系中的矩形.每一个矩形的底边都平行于X轴,侧边平行于Y轴.第i个矩形的左下角坐标为(Xi,Yi),底边长为Ai,侧边长为Bi.现在JYY打算从这N个矩形中,随机选出两个不 ...
- 【bzoj3110】[Zjoi2013]K大数查询 整体二分+树状数组区间修改
题目描述 有N个位置,M个操作.操作有两种,每次操作如果是1 a b c的形式表示在第a个位置到第b个位置,每个位置加入一个数c.如果是2 a b c形式,表示询问从第a个位置到第b个位置,第C大的数 ...
随机推荐
- Python 二分查找
(非递归实现) def binary_search(alist, item): first = 0 last = len(alist)-1 while first<=last: midpoint ...
- continue和break的特殊用法。
break在程序中一般来说的作用就是跳出当前循环,然后再据需执行循环外的语句.continue也是对当前循环来说直接进入到下一次循环.其实我们在程序中有时候循环体嵌套太多,进行到某一步是希望直接bre ...
- DML数据操作语言之复杂查询
1.视图(View) 我们知道,在关系型数据库中,用来保存实际数据记录的是数据表.和表同等概念也是用来保存东西是:视图. 但是数据表是用来保存实际数据记录的,而视图是用来保存常用select语句的. ...
- Android webview Mixed Content无法显示图片解决
转自:http://blog.csdn.net/crazy_zihao/article/details/51557425 前言 在使用WebView加载https资源文件时,如果认证证书不被Andro ...
- ruby:TypeError: 对象不支持此属性或方法
解决办法. 1.下载对应版本 下载node.js,根据ruby版本决定下载32还是x64,我的ruby版本x64 https://npm.taobao.org/mirrors/node/v8.9.3/ ...
- 关于搭建MyBatis框架(二)
由于在[关于使用Mybatis的使用说明(一)http://www.cnblogs.com/zdb292034/p/8675766.html]中存在不太完善地方,通过此片文档进行修订: 阅读指南:(1 ...
- JAVA_SE基础——48.多态
面向对象程序设计的三个特点是封装.继承和多态.前面已经学习了前两个特点.本章节将介绍多态性. 多态:一个对象具备多种形态.(父类的引用类型变量指向了子类的对象)或者是接口 的引用类型变量指向了接口实现 ...
- Column Addition~DP(脑子抽了,当时没有想到)
Description A multi-digit column addition is a formula on adding two integers written like this:
- cookieUtil
public class CookieUtil { /** * 设置cookie * @param name cookie名字 * @param value cookie值 * @param maxA ...
- Mosquito集群模式
参考链接: http://blog.csdn.net/z729685731/article/details/70142182 http://blog.csdn.net/yuhaiyang457288/ ...