hdu 4638 Group(莫队)

题目链接:hdu 4638 Group

Group

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


 

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

2013 Multi-University Training Contest 4

 

 

Recommend

zhuyuanchen520

 

题意:T组样例,n个人,m个询问,n个人的编号从1到n,随机排列,问在区间内,分为几组,每组价值的和最大。分组要求是每组内编号要连续,价值计算方式是,每组人k个,价值为k*k。

解题思路:首先要明确,能够连续,就不要分开。然后用莫队离线,用vis【i】记录i这个数字是否存在,当增加编号i时,若i的左右编号都存在,那么增加后相当于把两段不连续的变成一段连续的,也就是说组数减少1,若i的左右编号都不存在,那么相当于增加了一个新的分组,组数增加1,下面是代码,尤其要注意,莫队的点,要先增加再减少。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<set>
using namespace std;
typedef long long ll;

struct node{
	ll l,r;
	ll id;
}q[100005];

ll block;
ll num[100005];

bool cmp(const node &a,const node &b){
	if((a.l/block)!=(b.l/block))return a.l<b.l;
	return a.r<b.r;
}

int main(){
	int t;
	scanf("%d",&t);
	while(t--){
		ll n,m;
		scanf("%lld%lld",&n,&m);
		block=sqrt(1.0*n);
		ll i,j;
		for(i=1;i<=n;i++){
			scanf("%lld",&num[i]);
		}
		for(i=1;i<=m;i++){ 
			scanf("%lld%lld",&q[i].l,&q[i].r);
			q[i].id=i;
		}
		sort(q+1,q+1+m,cmp);
		ll vis[100005];
		memset(vis,0,sizeof(vis));
		ll ans[100005];
		memset(ans,0,sizeof(ans));
		for(i=q[1].l;i<=q[1].r;i++){
			vis[num[i]]=1;
			if((vis[num[i]-1]==0)&&(vis[num[i]+1]==0)){
				ans[q[1].id]++;
			}
			if((vis[num[i]-1]!=0)&&(vis[num[i]+1]!=0)){
				ans[q[1].id]--;
			}
		}
		
		ll temp=ans[q[1].id];
		ll L=q[1].l,R=q[1].r;
		ll x;
		for(i=2;i<=m;i++){
			while(q[i].l<L){
				L--;
				x=num[L];
				if((vis[x+1]!=0)&&(vis[x-1]!=0))temp--;
				if((vis[x+1]==0)&&(vis[x-1]==0))temp++;
				vis[x]=1;
			}
			while(q[i].r>R){
				R++;
				x=num[R];
				if((vis[x+1]!=0)&&(vis[x-1]!=0))temp--;
				if((vis[x+1]==0)&&(vis[x-1]==0))temp++;
				vis[x]=1;
			}
			while(q[i].r<R){
				x=num[R];
				if((vis[x+1]!=0)&&(vis[x-1]!=0))temp++;
				if((vis[x+1]==0)&&(vis[x-1]==0))temp--;
				vis[x]=0;
				R--;
			}
			while(q[i].l>L){
				x=num[L];
				if((vis[x+1]!=0)&&(vis[x-1]!=0))temp++;
				if((vis[x+1]==0)&&(vis[x-1]==0))temp--;
				vis[x]=0;
				L++;
			}
			ans[q[i].id]=temp;
		}
		for(i=1;i<=m;i++){
			printf("%lld\n",ans[i]);
		}
	}
	return 0;
} 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值