HDU 4638 Group 人生第一次莫队!!!

优雅的暴力————莫队算法

原来莫队如此暴力,在看完BZOJ小Z的袜子之后,拿这道题莫队练练手,想不到莫队如此简单!!!

HDU 4638 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

简单说下题意  5  个人 两次询问   相邻的两个数是朋友  比如说1 和2   2  和3  

问2次查询的朋友圈的大小

看样例

1--5  就是1

2--4 就是2  两只队伍     1  2   和    5

直接上代码   真的开心

#include"bits/stdc++.h"
#define LL long long
using namespace std;
struct node
{
    int l,r,id,ans;
};
node p[100005];
int block[100005];
int now[100005];
int que[100005];
int last_l,last_r;
bool cmp1(node a,node b)
{
    if(block[a.l]<block[b.l])return true;
    if(block[a.l]>block[b.l])return false;
    return a.r<b.r;
}
bool cmp2(node a,node b)
{
    return a.id<b.id;
}
int main()
{
    int T;
    scanf("%d",&T);
    for(int cas=1; cas<=T; cas++)
    {
        last_r=0;
        last_l=1;
        memset(now,0,sizeof(now));
        int people,query;
        scanf("%d%d",&people,&query);
        for(int i=1; i<=people; i++)
        {
            scanf("%d",&que[i]);
        }
        for(int i=1; i<=query; i++)
        {
            scanf("%d%d",&p[i].l,&p[i].r);
            p[i].id=i;

        }
        block[0]=(int)sqrt(people);
        for(int i=1; i<=people; ++i)
        {
            block[i]=i/block[0]+1;
        }
        int sum=0;
        sort(p+1,p+query+1,cmp1);
       for(int i=1; i<=query; i++)
        {
            if(last_r<p[i].r)
            {
                for(int j=last_r+1; j<=p[i].r; j++)
                {
                    now[que[j]]=1;
                    sum++;
                    if(now[que[j]-1])
                        sum--;
                    if(now[que[j]+1])
                        sum--;
                }
            }
            if(last_r>p[i].r)
            {
                for(int j=p[i].r+1; j<=last_r; j++)
                {
                    now[que[j]]=0;
                    sum--;
                    if(now[que[j]-1])
                        sum++;
                    if(now[que[j]+1])
                        sum++;
                }
            }
            //))))))))
            if(last_l<p[i].l)
            {
                for(int j=last_l; j<=p[i].l-1; j++)
                {
                    now[que[j]]=0;
                    sum--;
                    if(now[que[j]-1])
                        sum++;
                    if(now[que[j]+1])
                        sum++;
                }
            }
            if(last_l>p[i].l)
            {
                for(int j=p[i].l; j<=last_l-1; j++)
                {
                    now[que[j]]=1;
                    sum++;
                    if(now[que[j]-1])
                        sum--;
                    if(now[que[j]+1])
                        sum--;
                }
            }
            last_l=p[i].l;
            last_r=p[i].r;
            p[i].ans=sum;
        }
        sort(p+1,p+query+1,cmp2);
        for(int i=1; i<=query; i++)
        {
            printf("%d\n",p[i].ans);
        }

    }
}

猜你喜欢

转载自blog.csdn.net/swustzhaoxingda/article/details/86591922