您的位置:首页 > 其它

hdu-4638-Group-(树状数组,离线操作)

2017-10-07 22:08 411 查看
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

代码:

#include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<queue>
#include<cstring>
#include<map>
using namespace std;
#define M 100005
int n,m;
int tree[M],ans[M],a[M],pos[M];
struct node {
int l,r,id;
bool operator < (const node &obj) const
{
return r<obj.r;
}
}p[M];
inline int lowbit(int i)
{
return i&(-i);
}
void add(int pos,int v)
{
while(pos<=n)
{
tree[pos]+=v;
pos+=lowbit(pos);
}
}
int sum(int i)
{
int res=0;
while(i>0)
{
res+=tree[i];
i-=lowbit(i);
}
return res;
}
int main()
{
int T,i;
scanf("%d",&T);
while(T--)
{
memset(tree,0,sizeof(tree));
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
pos[a[i]]=i;
}
for(i=1;i<=m;i++)
{
scanf("%d%d",&p[i].l,&p[i].r);
p[i].id=i;
}
sort(p+1,p+1+m);
int it=1;
for(i=1;i<=n;i++)
{
add(i,1);
if(a[i]>1&&pos[a[i]-1]<i) add(pos[a[i]-1],-1);
if(a[i]<n&&pos[a[i]+1]<i) add(pos[a[i]+1],-1);
while(p[it].r==i&&it<=m)
{
ans[p[it].id]=sum(p[it].r)-sum(p[it].l-1);
it++;
}
}
for(i=1;i<=m;i++)
printf("%d\n",ans[i]);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: