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

给1~n的排列,m次询问,询问[L,R]区间内,有多少段连续的数

如3,1,2,4,5,有一段连续的数

6,3,1,2,5 有两段

用莫队算法,用排序和分块进行一些优化

#include<bits/stdc++.h>
using namespace std;
struct node
{
    int l,r,id,pos;
} p[100009];

int a[100009],cnt,ans[100009],vis[100009],m;

bool cmp(node a, node b)//如果在一个块内,则按照右端点排序,否则按左端点排序,目的是使L,R移动的次数尽量少
{
    if(a.pos == b.pos)
        return a.r < b.r;
    return a.l < b.l;
}
void del(int i)//删除操作
{
    vis[i] = 0;
    if(vis[i-1]&&vis[i+1])//如果和这个数左右相邻的数都存在,把这个数删了,就会多出来一个段
        cnt ++;
    else if(!vis[i-1]&&!vis[i+1])//如果和这个数左右相邻的数都不存在,把这个数删了,就会少一个段
        cnt --;
}
void add(int i)//添加操作
{
    vis[i] = 1;
    if(vis[i-1]&&vis[i+1])
        cnt --;
    else if(!vis[i-1]&&!vis[i+1])
        cnt ++;
}

void solve()
{
    memset(vis,0,sizeof(vis));//标记数组清零
    int L = 1,R = 0;
    cnt = 0;
    for(int i = 1; i <= m; i ++)
    {
        if(p[i].r<L||p[i].l>R)//如果当前要询问的区间的l在指针R的右侧,或r在L的左侧,即两者区间差距太大,直接移动耗时多,那就重新计算
        {
            cnt = 0;
            for(int j = L; j <= R; j ++)
            {
                vis[a[j]] = 0;
            }
            for(int j = p[i].l; j <= p[i].r; j ++)
            {
                add(a[j]);
            }
            L = p[i].l;
            R = p[i].r;
        }
        while(L < p[i].l)
        {
            del(a[L++]);
        }
        while(L > p[i].l)
        {
            add(a[--L]);
        }
        while(R > p[i].r)
        {
            del(a[R --]);
        }
        while(R< p[i].r)
        {
            add(a[++R]);
        }
        ans[p[i].id] = cnt;
    }

}

int main()
{
    int block, n,i,t;
    scanf("%d",&t);

    while(t --)
    {
        scanf("%d %d",&n, &m);
        for(i = 1; i <= n; i ++)
        {
            scanf("%d",&a[i]);
        }
        block = sqrt(n);
        for(i = 1; i <= m; i ++)
        {
            scanf("%d %d",&p[i].l, &p[i].r);
            p[i].id = i;
            p[i].pos = (p[i].l-1)/block+1;//按照左端点进行分块
        }

        sort(p+1,p+1+m,cmp);
        solve();

        for(i = 1; i <= m; i ++)
        {
            printf("%d\n",ans[i]);
        }
    }
    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值