Group

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1959    Accepted Submission(s): 1006

Problem Description
There are n men ,every man has an ID(1..n).their ID is unique. Whose ID is i and i-1 are friends, Whose ID is i and i+1 are friends. These n men stand in line. Now we select an interval of men to make some group. K men in a group can create K*K value. The value
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.
 
Input
First line is T indicate the case number.
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].
 
Output
For every query output a number indicate there should be how many group so that the sum of value is max.

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 树状数组 区间内连续区间的个数(尽可能长)的更多相关文章

  1. HDU 5654 xiaoxin and his watermelon candy 离线树状数组 区间不同数的个数

    xiaoxin and his watermelon candy 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5654 Description Du ...

  2. hdu 4638 树状数组

    思路:将查询区间按右节点的升序排列,然后插入第i个数的时候,若nun[i]+1已经插入,那么就update(pre[num[i]+1],-1):pre[]表示的是该数的位置.同样若 num[i]-1存 ...

  3. HDU 4638 树状数组 想法题

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4638 解题思路: 题意为询问一段区间里的数能组成多少段连续的数.先考虑从左往右一个数一个数添加,考虑当 ...

  4. hdu 1754 I Hate It(树状数组区间求最值)2007省赛集训队练习赛(6)_linle专场

    题意: 输入一行数字,查询第i个数到第j个数之间的最大值.可以修改其中的某个数的值. 输入: 包含多组输入数据. 每组输入首行两个整数n,m.表示共有n个数,m次操作. 接下来一行包含n个整数. 接下 ...

  5. hdu 1116 敌兵布阵(树状数组区间求和)

    题意: 给出一行数字,然后可以修改其中第i个数字,并且可以询问第i至第j个数字的和(i <= j). 输入: 首行输入一个t,表示共有t组数据. 接下来每行首行输入一个整数n,表示共有n个数字. ...

  6. hdu 4777 树状数组+合数分解

    Rabbit Kingdom Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  7. HDU 2852 (树状数组+无序第K小)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2852 题目大意:操作①:往盒子里放一个数.操作②:从盒子里扔掉一个数.操作③:查询盒子里大于a的第K小 ...

  8. 【bzoj5173】[Jsoi2014]矩形并 扫描线+二维树状数组区间修改区间查询

    题目描述 JYY有N个平面坐标系中的矩形.每一个矩形的底边都平行于X轴,侧边平行于Y轴.第i个矩形的左下角坐标为(Xi,Yi),底边长为Ai,侧边长为Bi.现在JYY打算从这N个矩形中,随机选出两个不 ...

  9. 【bzoj3110】[Zjoi2013]K大数查询 整体二分+树状数组区间修改

    题目描述 有N个位置,M个操作.操作有两种,每次操作如果是1 a b c的形式表示在第a个位置到第b个位置,每个位置加入一个数c.如果是2 a b c形式,表示询问从第a个位置到第b个位置,第C大的数 ...

随机推荐

  1. Linux下vim上编辑实现进度条

    1.效果展示: 进度条,先来看一个效果: 这是进度结果,模拟实现了进度条的前进.百分比的现实.以及稍微的动画特效. 2.原理描述: 因为Linux系统下的输出有缓存,如果及时刷新显示,就可以得到我们想 ...

  2. ord在python是什么意思?

    >>> help(ord)Help on built-in function ord in module builtins:ord(...) #这是一个函数 ord(c) -> ...

  3. scrapy 修改URL爬取起始位置

    import scrapy from Autopjt.items import myItem from scrapy.http import Request class AutospdSpider(s ...

  4. mvc架构模式概念

    MVC模式是"Model-View-Controller"的缩写,中文翻译为"模式-视图-控制器".MVC应用程序总是由这三个部分组成.Event(事件)导致C ...

  5. java8-Stream之数值流

    在Stream里元素都是对象,那么,当我们操作一个数字流的时候就不得不考虑一个问题,拆箱和装箱.虽然自动拆箱不需要我们处理,但依旧有隐含的成本在里面.Java8引入了3个原始类型特化流接口来解决这个问 ...

  6. EasyUI中, datagrid用loadData方法绑定数据。

    $("#dg").datagrid("loadData", { , " }, { "ck": "1", &qu ...

  7. JAVA_SE基础——12.运算符的优先级

    优先级 操作符 含义 关联性 用法 ---------------------------------------------------------------- 1 [ ] 数组下标 左 arra ...

  8. 记一次向maven中央仓库提交依赖包

    Maven是Java中最常用的依赖管理工具,Maven的中央仓库保罗万象,涵盖了各个领域的框架.工具和文档,也是Java生态强大生命力的体现.我们自己开发的一些有用有趣的代码也可以通过打包上传到mav ...

  9. rsync 自动创建目录的坑点

    rsync同步文件有三种模式: 1.把源站路径下某个文件,同步到目标路径.例如rsync -aR /data/1/2/3/a.txt 1.1.1.1:/data/ ,目标机器将自动创建多层目录存放a. ...

  10. 微信小程序tab(swiper)切换

    <- wxml -> <view class="youhui"> <view ' bindtap='toggle'> 未使用 </view ...