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. Python 3.* print 出现SyntaxError: invalid syntax

    很简单,不知道为啥,据说是3.0以后的print都改为了print(); 例如 a=1 print a 上边出错 输入 a=1 print(a) 就正确了

  2. MySQL-压缩版-windows安装

    1.首先去dev.mysql.com/downloads/mysql/下载MySQL的压缩包,然后解压到任意盘符下. 2.打开系统变量在Path下追加mysql的路径(例如:C:\mysql-5.7. ...

  3. vue下拉列表

    最近在弄作品,做了个下拉列表.心想各位小哥哥.小姐姐可能会用到相同的需求,就把下拉列表封装一下,希望能对各位小哥哥,小姐姐有帮助 github地址:https://github.com/ClmPisc ...

  4. JAVA_SE基础——39.继承

    在面向对象程序设计中,可以从已有的类派生出新类. 这称做继承(inheritance). 白话解释: 例子1:继承一般是指晚辈从父辈那里继承财产,也可以说是子女拥有父母给予他们的东西. 例子2:猫和狗 ...

  5. python实现维吉尼亚解密

    # -*-coding:UTF-8-*- from sys import stdout miwen = "KCCPKBGUFDPHQTYAVINRRTMVGRKDNBVFDETDGILTXR ...

  6. 看到一个对CAP简单的解释

    一个分布式系统里面,节点组成的网络本来应该是连通的.然而可能因为一些故障,使得有些节点之间不连通了,整个网络就分成了几块区域.数据就散布在了这些不连通的区域中.这就叫分区.当你一个数据项只在一个节点中 ...

  7. Linux入门(1)_VMware和系统分区和系统安装和远程登陆管理

    1 VMware的安装和使用 注意有 快照 和 克隆 的功能. 快照相当于建立一个 系统还原点, 可以随时恢复到原来状态. 克隆功能可以复制一个和当前一样的系统,并可以选择链接安装,只使用很少的空间就 ...

  8. Windows中添加自己的程序到开机启动中(添加服务,添加注册表)

    在系统启动的时候启动自己想要启动的程序: 方法一:利用开机启动文件夹 将exe文件或exe文件的快捷方式复制到(启动)文件夹下 以win7为例:开始→所有程序→启动→鼠标右键打开 方法二:添加系统服务 ...

  9. vue组件详解(二)——使用props传递数据

    在 Vue 中,父子组件的关系可以总结为 props向下传递,事件向上传递.父组件通过 props 给子组件下发数据,子组件通过事件给父组件发送消息.看看它们是怎么工作的.  一.基本用法 组件不仅仅 ...

  10. Python内置函数(11)——complex

    英文文档: class complex([real[, imag]]) Return a complex number with the value real + imag*1j or convert ...