您的位置:首页 > 运维架构

hdu 4638 Group(线段树,离线维护左边界,4级)

2013-08-02 15:36 603 查看

Group

Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 369 Accepted Submission(s): 207



[align=left]Problem Description[/align]
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.

[align=left]Input[/align]
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].

[align=left]Output[/align]
For every query output a number indicate there should be how many group so that the sum of value is max.

[align=left]Sample Input[/align]

1
5 2
3 1 2 5 4
1 5
2 4


[align=left]Sample Output[/align]

1
2


[align=left]Source[/align]
2013 Multi-University Training Contest 4

[align=left]Recommend[/align]
zhuyuanchen520

思路:离线处理,维护左边界,树状数组记录,从左界到开始的不连续段个数。
如何找不连续段呢? 判断比i大1小1的再不在区间就可以了。接下来就水了。

失误:多校时怎么没想到这么找区间呢。其实感觉不难。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define FOR(i,a,b) for(int i=a;i<=b;++i)
#define clr(f,z) memset(f,z,sizeof(f))
using namespace std;
const int mm=1e5+9;
int c[mm],id[mm],zf[mm],ans[mm];
class Edge
{
public:
int l,r,id;
bool operator <(const Edge&x)const
{
return l<x.l;
}
} f[mm];
int lowbit(int x)
{
return x&(-x);
}
void add(int pos,int val)
{
for(int i=pos; i<mm; i+=lowbit(i))
c[i]+=val;
}
int get_sum(int pos)
{
int ret=0;
for(int i=pos; i>0; i-=lowbit(i))
ret+=c[i];
return ret;
}
int cas,n,m;
int main()
{
while(~scanf("%d",&cas))
{
while(cas--)
{
scanf("%d%d",&n,&m);
FOR(i,1,n)
{
scanf("%d",&zf[i]);
id[zf[i]]=i;
}
id[0]=id[n+1]=n+3;
FOR(i,1,m)
{
scanf("%d%d",&f[i].l,&f[i].r);f[i].id=i;
}
clr(c,0);
FOR(i,1,n)
{
if( id[ zf[i]+1 ]>i && id[ zf[i]-1 ]>i )///两个都在后面是孤立点
{
add(i,1);
}
else if( id[ zf[i]+1 ]<i && id[ zf[i]-1 ]<i )///两个都在前面,加入就合并
{
add(i,-1);
}
}
sort(f+1,f+m+1);
// FOR(i,1,n)//cout<<c[i]<<endl;
//cout<<get_sum(6)<<endl;
int i=1,j=1;
while(j<=m)
{
while(i<=n&&i<f[j].l)//去除小于左界影响
{
if( id[zf[i]+1 ]>i && id[zf[i]-1 ]>i )
{
add(i,-1);
add(id[zf[i]+1 ],1);
add(id[zf[i]-1 ],1);
}
else if( id[zf[i]+1 ]<i && id[zf[i]-1 ]<i )
{
add(i,-1);
}
else if( id[zf[i]+1 ]>i )
{
add(i,-1);
add(id[zf[i]+1],1);
}
else if( id[zf[i]-1 ]>i )
{
add(i,-1);
add(id[zf[i]-1 ],1);
}
++i;
}
while(j<=m&&f[j].l<=i)
{
ans[f[j].id]=get_sum(f[j].r);
++j;
}
}
FOR(i,1,m)
printf("%d\n",ans[i]);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: