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