Group

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

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
 
Source
 
Recommend
zhuyuanchen520
 

把查询区间按照左端点排序。

然后逐渐从左边删除数,看对后面的影响。

树状数组实现单点更新和求和

/*
* Author:kuangbin
* 1007.cpp
*/ #include <stdio.h>
#include <algorithm>
#include <string.h>
#include <iostream>
#include <map>
#include <vector>
#include <queue>
#include <set>
#include <string>
#include <math.h>
using namespace std;
const int MAXN = ;
int n; int lowbit(int x)
{
return x&(-x);
}
int c[MAXN];
void add(int i,int val)
{
while(i <= n)
{
c[i] += val;
i += lowbit(i);
}
}
int sum(int i)
{
int s = ;
while(i > )
{
s += c[i];
i -= lowbit(i);
}
return s;
}
int a[MAXN];
int num[MAXN]; int ans[MAXN];
struct Node
{
int l,r;
int index;
}node[MAXN];
bool cmp(Node a,Node b)
{
return a.l < b.l;
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int m;
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
memset(c,,sizeof(c));
for(int i = ;i <= n;i++)
{
scanf("%d",&a[i]);
num[a[i]]=i;
}
num[] = n+;
num[n+] = n+;
for(int i = ;i <= n;i++)
{
if(i < num[a[i]-] && i < num[a[i]+])
add(i,);
else if(i > num[a[i]-] && i > num[a[i]+])
add(i,-);
}
for(int i = ;i < m;i++)
{
scanf("%d%d",&node[i].l,&node[i].r);
node[i].index = i;
}
sort(node,node+m,cmp);
int i = ;
int j = ;
while(j < m)
{
while(i <= n && i < node[j].l)
{
if(i > num[a[i]-] && i > num[a[i]+])
add(i,-);
else if(i < num[a[i]-] && i < num[a[i]+])
{
int Min = min(num[a[i]-],num[a[i]+]);
int Max = max(num[a[i]-],num[a[i]+]);
add(i,-);
add(Min,);
add(Max,);
}
else if(i < num[a[i]-])
{
add(i,-);
add(num[a[i]-],);
}
else
{
add(i,-);
add(num[a[i]+],);
}
i++;
}
while( j < m && node[j].l <= i)
{
ans[node[j].index]= sum(node[j].r);
j++;
}
}
for(int i = ;i < m;i++)
printf("%d\n",ans[i]);
}
return ;
}

最新文章

  1. [LeetCode] Reverse Words in a String II 翻转字符串中的单词之二
  2. selenium RC+JAVA 运行所遇到的问题
  3. crond不执行原因分析
  4. 构建高性能web之路------mysql读写分离实战
  5. Java SpringMVC实现国际化整合案例分析(i18n)
  6. vs2010 和 svn的结合运用,svn的安装
  7. OpenLayers实现覆盖物选择信息提示
  8. printdir-deldir-bmp
  9. DxPackNet 2.视频截图和捕捉帧图片
  10. Beta 冲刺day2
  11. ioremap_nocache() 函数的使用【转】
  12. Djangoorm的多表建立与queryset对象的合并
  13. Window上安装—Docker 笔记
  14. ntpdate[31915]: the NTP socket is in use, exiting
  15. MJExtension代码解释
  16. ibus-libpinyin 无法选择除第一个外的候选词
  17. 小程序 波浪进度球 wave
  18. SpringMVC Controller配置方法有哪几种
  19. MYSQL中的CASE WHEN END AS
  20. Cocos2d-x学习笔记(三)main方法

热门文章

  1. exit() _exit() 函数区别
  2. java===java基础学习(15)---抽象,接口
  3. selenium===splinter模块和selenium异曲同工
  4. 【bzoj3224】普通平衡树
  5. 安装:python+webdriver环境
  6. cpu占用高 20180108
  7. ZOJ-3430
  8. django 类列表实例化
  9. Centos查找大文件的办法
  10. css - 字体图标的制作