Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1966 Accepted Submission(s): 1012
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
Recommend
zhuyuanchen520 | We have carefully selected several similar problems for you:
5674
5673
5672
5671
5670
题解:莫队算法+分块
脑残错误害死人啊。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define N 100003
using namespace std;
int n,m,t;
int ans1[N],num[N],b[N],p[N],vis[N],belong[N],a[N];
struct data
{
int l,r,num;
}; data a1[N];
int cmp1(data a,data b)
{
if (belong[a.l]==belong[b.l])
return a.r<b.r;
return belong[a.l]<belong[b.l];
}
int main()
{
scanf("%d",&t);
for (int k=1;k<=t;k++)
{
scanf("%d%d",&n,&m);
for (int i=1;i<=n;i++)
scanf("%d",&a[i]);
for (int i=1;i<=m;i++)
scanf("%d%d",&a1[i].l,&a1[i].r),a1[i].num=i;
int size=ceil(sqrt(n));
for (int i=1;i<=n;i++)
belong[i]=(i-1)/size+1;
sort(a1+1,a1+m+1,cmp1);
memset(vis,0,sizeof(vis));
memset(ans1,0,sizeof(ans1));
int l=1; int r=0; int ans=0;
for (int i=1;i<=m;i++)
{
while (r<a1[i].r)
{
r++;
vis[a[r]]=1;
if (vis[a[r]-1]&&vis[a[r]+1])
ans--;
if (!vis[a[r]-1]&&!vis[a[r]+1])
ans++;
}
while (r>a1[i].r)
{
vis[a[r]]=0;
if (vis[a[r]-1]&&vis[a[r]+1])
ans++;
if (!vis[a[r]-1]&&!vis[a[r]+1])
ans--;
r--;
}
while (l>a1[i].l)
{
l--;
if (vis[a[l]-1]&&vis[a[l]+1])
ans--;
if (!vis[a[l]-1]&&!vis[a[l]+1])
ans++;
vis[a[l]]=1;
}
while (l<a1[i].l)
{
vis[a[l]]=0;
if (vis[a[l]-1]&&vis[a[l]+1])
ans++;
if (!vis[a[l]-1]&&!vis[a[l]+1])
ans--;
l++;
}
ans1[a1[i].num]=ans;
}
for (int i=1;i<=m;i++)
printf("%d\n",ans1[i]);
}
}

本文介绍了一道使用莫队算法与分块技术解决的编程竞赛题目。该问题涉及区间选择、最大化区间价值及组内连续标识的计算。通过案例分析展示了算法实现细节,并附带完整的代码示例。
978

被折叠的 条评论
为什么被折叠?



