Group

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

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
 题意:t组数据 给你一个长度为n的序列 m个询问[l,r] 问 l到r 的值可以组成多少个连续的段 
 题解:例如序列3 1 2 5 4  
查询 [1,5]  值为{1,2,3,4,5}  只有一个连续的段
查询 [2,4] 值为{1,2} {5} 有两个连续的段
莫队处理;
 
 #include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<map>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#define ll __int64
using namespace std;
int n,m;
struct node
{
int l,r,id;
} N[];
int p[];
int block;
int a[];
int x[];
int mp[];
ll ans=;
ll re[];
int cmp(struct node aa,struct node bb)
{
if(p[aa.l]==p[bb.l])
return aa.r<bb.r;
else
return p[aa.l]<p[bb.l];
}
void update(int w,int h)
{
if(h==)
{
mp[a[w]]=;
if(mp[a[w]-]==&&mp[a[w]+]==)
ans++;
if(mp[a[w]-]==&&mp[a[w]+]==)
ans--;
}
else
{
mp[a[w]]=;
if(mp[a[w]-]==&&mp[a[w]+]==)
ans++;
if(mp[a[w]-]==&&mp[a[w]+]==)
ans--;
}
}
int t;
int main()
{
scanf("%d",&t);
for(int o=;o<=t;o++)
{
for(int i=;i<;i++)
mp[i]=;
scanf("%d %d",&n,&m);
for(int i=; i<=n; i++)
scanf("%d",&a[i]);
for(int i=; i<=m; i++)
{
scanf("%d %d",&N[i].l,&N[i].r);
N[i].id=i;
}
block=(int)sqrt((double)n);
for(int i=; i<=n; i++)
p[i]=(i-)/block+;
sort(N+,N++m,cmp);
ans=;
for(int i=,l=,r=; i<=m; i++)
{
for(; r<N[i].r; r++) update(r+,);
for(; l>N[i].l; l--) update(l-,);
for(; r>N[i].r; r--) update(r,-);
for(; l<N[i].l; l++) update(l,-);
re[N[i].id]=ans;
}
for(int i=; i<=m; i++)
printf("%I64d\n",re[i]);
}
return ;
}

最新文章

  1. spring的BeanFactory加载过程
  2. ssh密钥私钥不能登陆问题处理
  3. python实现概率分布
  4. 夺命雷公狗—angularjs—7—多条数据的遍历
  5. 关于华为C8812救砖教程
  6. HDU 3949 XOR(高斯消元)
  7. 写个 Hello World 而已,要不要这么震撼?!
  8. C#字符串string的常用使用方法
  9. css选择器基本属性
  10. java基础程序题
  11. 2016 Vultr VPS最新优惠码,赠送新用户70美元,亲测有效
  12. (6.1)linux操作系统基础
  13. chrome浏览器被reimage pair 劫持怎么处理
  14. 714-Card Trick
  15. 08-部署node节点
  16. express koa koa2 优缺点分析
  17. PTA 堆栈操作合法性(20 分)
  18. python sshtunnel 简单介绍
  19. Android Studio 2.3.3上引入3.0上开发的项目遇到的问题
  20. python 正则表达式 提取网页中标签的中文

热门文章

  1. leetcode个人题解——#40 Combination Sum2
  2. 实现属于自己的TensorFlow(一) - 计算图与前向传播
  3. centos上搭建git服务--2
  4. ffmpe安装
  5. 单源最短路——Bellman-Ford算法
  6. PSP Daily——团队项目Alpha发布
  7. Python:文件操作总结1——文件基本操作
  8. mininet实验 脚本实现控制交换机行为
  9. 寒假作业2:简化电梯设计elevator
  10. 学习c++ofstream和ifstream