hdu 4368 树状数组 离线维护
2015-08-29 12:42
225 查看
http://acm.hdu.edu.cn/showproblem.php?pid=4638
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
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 4368 树状数组 题目大意:给定1~n的一个排列,每次查询一个区间(l,r),将所求区间内的数进行分组,每一组内的数的值必须是连着的,该组的价值为元素个数的平方和 问在区间价值最大的情况下,要分成几组 解题思路:用树状数组离线维护,把询问按照右区间递增排序,然后从1~n维护,维护到i时注意找寻前i个是不是有a[i]-1,和a[i]+1,有的话直接去掉,因为 这两个数必定要和a[i]分到一组,留一个就好了 */ #include <stdio.h> #include <algorithm> #include <string.h> #include <iostream> using namespace std; const int maxn=100100; int n,m,a[maxn],p[maxn],ans[maxn]; struct note { int l,r,id; bool operator < (const note &other)const { return r<other.r; } }que[maxn]; int c[maxn]; int lowbit(int x) { return x&(-x); } int sum(int x) { int ret=0; while(x>0) { ret+=c[x];x-=lowbit(x); } return ret; } void add(int x,int d) { while(x<=n) { c[x]+=d; x+=lowbit(x); } } int main() { int T; scanf("%d",&T); while(T--) { scanf("%d%d",&n,&m); 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",&que[i].l,&que[i].r); que[i].id=i; } sort(que,que+m); memset(c,0,sizeof(c)); int j=0; for(int i=1;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(j<m&&que[j].r==i) { ans[que[j].id]=sum(que[j].r)-sum(que[j].l-1); j++; } } for(int i=0;i<m;i++) { printf("%d\n",ans[i]); } } return 0; }
相关文章推荐
- Linux文件系统损坏故障怎么办?故障修复方法
- hadoop做HA后,hbase修改
- linux运维实战练习-2015年8月27日课程作业
- linux运维之文件系统
- linux 学习笔记之权限管理命令
- tomcat-usrs.xml 配置示例
- Tomcat Context reload 与 OutOfMemory(PermSpace)
- Linux 正则表达式
- 五种开源协议的比较(BSD,Apache,GPL,LGPL,MIT) – 整理
- 分析linux系统下几种消息传递通信方式
- Linux 系统实时监控的瑞士军刀—Glances
- CentOS系统下的Hadoop集群(第9期)_MapReduce初级案例
- 详解VS2012发布网站步骤
- linux命令(4)curl命令详解
- Linux文件系统启动过程及login的实现 .
- ceph0.94安装部署(centos7.1)
- Linux命令(3)打Patch的方法
- 功放TAS5719耳机POP noise分析 对策
- linux ssh免登入
- CentOS系统下的Hadoop集群(第8期)_HDFS初探之旅