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

题意:
问所给区间的连续的序列个数,如:3 1 2,6 7, 9都算连续的序列

思路:
n最大1e5,m最大1e5,若纯暴力,1e10,会爆,很显然普通分块解决不了,
因此用了莫队,先分块,再把所给区间排序
利用l,r的移动,数组走一遍求出所有离线区间的结果,最后一起输出

离线就是预先已经知道了已知和所有要求的情况,这是莫队必不可少的使用条件,不然拿啥排序呀

关于区间的排序:
区间的排序,先看left是否在同一块,
若在一块,即left距离近,那么左边移来移去没啥大不了,就按right从小到大排
若不在一块,即left距离远,左边移来移去有风险,就按left从小到大排

#include <bits/stdc++.h>

using namespace std;

int a[100005], belong[100005], coun[100005];
struct node
{
    int left, right, num;
}str[100005];

bool cmp(struct node x, struct node y)
{
    if(belong[x.left]==belong[y.left]) return x.right < y.right;
    else return x.left < y.left;
}

int add(int n, int sum)
{
    coun[n] = 1;
    if(coun[n-1]&&coun[n+1]) sum--;          //连接两序列成为一个序列
    else if(!coun[n-1]&&!coun[n+1]) sum++;     //前无古人后无来者,本身是一个序列
    return sum;
}

int subtract(int n, int sum)
{
    coun[n] = 0;
    if(coun[n-1]&&coun[n+1]) sum++;
    else if(!coun[n-1]&&!coun[n+1]) sum--;
    return sum;
}

int main()
{
    int n, m, t, re[100005], i, temp, l, r, sum;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%d %d", &n, &m);
        temp = sqrt(n);
        for(i=1; i<=n; i++)
        {
            scanf("%d", &a[i]);
            belong[i] = (i-1) / temp + 1;
        }
        for(i=1; i<=m; i++)
        {
            str[i].num = i;
            scanf("%d %d", &str[i].left, &str[i].right);
        }
        sort(str+1, str+m+1, cmp);
        l = 1;      //先走右,再走左
        r = 0;
        sum = 0;
        memset(coun, 0, sizeof(coun));
        for(i=1; i<=m; i++)
        {
            while(r < str[i].right)
            {
                r++;                  //最终的右端点应该包括所求区间的右端点
                sum = add(a[r], sum);
            }
            while(r > str[i].right)
            {
                sum = subtract(a[r], sum);
                r--;
            }
            while(l < str[i].left)
            {
                sum = subtract(a[l], sum);
                l++;
            }
            while(l > str[i].left)
            {
                l--;
                sum = add(a[l], sum);
            }
            re[str[i].num] = sum;
        }
        for(i=1; i<=m; i++)
        {
            printf("%d\n", re[i]);
        }
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值