HDU4638-Group (莫队模板)

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
题目:HDU4638
题意:有n个人站成一队,每个人都有一个独一无二的序号x(1<= x <= n)。现给定n个人的顺序及序号,每个区间里,序号连续的人可以在一个组,现问给定区间中,能分成最小数量的组的个数。
思路:开始想用线段树做,多次询问区间的特性很适合线段树,但是这题中合并两个区间的复杂度太高了,实现困难,炸过内存,也T过。无果,于是学习了莫队算法。
莫队算法实现起来以及思路都很简单,类似一个暴力,但神奇的是它的复杂度。虽然我至今认为它是n^2,但是它就是过了。
莫队实现:
1.将询问q[],按照x=q.l/(q.l^1/2)为第一关键字,q.r为第二关键字排序
2.用两个指针currentl和currentr维护区间和答案,两个询问区间之间直接暴力跳转
实现起来非常容易,我个人认为,当题目很像线段树,但合并两个区间的代价很大的情况下就应该尝试莫队,当然,带修改也可以
AC代码:

#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#include<map>
#include<set>
#define met(s,k) memset(s,k,sizeof s)
#define scan(a) scanf("%d",&a)
#define scanl(a) scanf("%lld",&a)
#define scann(a,b) scanf("%d%d",&a,&b)
#define scannl(a,b) scanf("%lld%lld",&a,&b)
#define scannn(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define prin(a) printf("%d\n",a)
#define prinl(a) printf("%lld\n",a)
using namespace std;
typedef long long ll;
const  int maxn=1e5+10;
int n,m,cont,ans,ans1[maxn];
struct que
{
    int l,r,pos;
}q[maxn];
bool cmp(que a,que b)
{
    int x=a.l/cont;
    int y=b.l/cont;
    if(x!=y)return x<y;
    else return a.r<b.r;
}
int a[maxn],vis[maxn];
void removeposx(int pos)//暴力移动指针及维护答案
{
    if(vis[a[pos]-1]&&vis[a[pos]+1])ans+=1;
    else if(vis[a[pos]-1]==0&&vis[a[pos]+1]==0)ans-=1;
    vis[a[pos]]=0;
}
void addposx(int pos)
{
    if(vis[a[pos]-1]&&vis[a[pos]+1])ans-=1;
    else if(vis[a[pos]-1]==0&&vis[a[pos]+1]==0)ans+=1;
    vis[a[pos]]=1;
}
void modui(int m)
{
    int currenl=1,currenr=0;
    for (int i = 0; i <m; ++i)//注意自增和自减号的位置
    {
        while (currenr<q[i].r)addposx(++currenr);
        while (currenr>q[i].r)removeposx(currenr--);
        while (currenl<q[i].l)removeposx(currenl++);
        while (currenl>q[i].l)addposx(--currenl);
        ans1[q[i].pos]=ans;
    }
}
int main()
{
    int t;
    scan(t);
    while (t--)
    {
        scann(n,m);
        ans=0;
        met(vis,0);
        cont=(int)sqrt((double)n);
        for(int i=1;i<=n;i++)scan(a[i]);
        for(int i=0;i< m;i++)scann(q[i].l,q[i].r),q[i].pos=i;
        sort(q,q+m,cmp);
        modui(m);
        for (int j = 0; j <m; ++j)prin(ans1[j]);
    }
    return  0;
}

猜你喜欢

转载自blog.csdn.net/swust5120160705/article/details/79582248
今日推荐