D - Group(莫队1)

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

题目大意:在不同的查询区间里排序找出连续序列的个数

 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<deque>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
using namespace std;
typedef long long ll;
const double pi = acos(-1.0);
const ll mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const int maxn = 1e5 + 5;
int n, m;
int a[maxn], belong[maxn], num[maxn], re[maxn];
int ans;
struct node
{
    int l, r, id;
}q[maxn];
void build()
{
    int block = sqrt(n);
    for(int i=1;i<=n;i++)
    {
        belong[i] = (i-1)/block + 1;
    }
}
bool cmp(node a, node b)
{
    if(belong[a.l] == belong[b.l]) return a.r < b.r;
    else return belong[a.l] < belong[b.l];
}
void add(int x)
{
    int flag = num[x-1] + num[x+1];
    if(!flag) ++ans;
    else if(flag == 2) --ans;
    ++num[x];
}
void del(int x)
{
    int flag = num[x-1] + num[x + 1];
    if(!flag) --ans;
    else if(flag == 2) ++ans;
    --num[x];
}
int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
        ans = 0;
        memset(re, 0, sizeof(re));
        memset(a, 0, sizeof(a));
        memset(num, 0, sizeof(num));
        memset(belong, 0, sizeof(belong));
        scanf("%d %d", &n, &m);
        for(int i=1;i<=n;i++)
        {
            scanf("%d", &a[i]);
        }
        build();
        for(int i=1;i<=m;i++)
        {
            q[i].id = i;
            scanf("%d %d", &q[i].l, &q[i].r);
        }
        sort(q+1, q+m+1, cmp);
        int l = 1, r = 0;
        for(int i=1;i<=m;i++)
        {
            int ql = q[i].l;
            int qr = q[i].r;
            while(r < qr)
            {
                add(a[++r]);
            }
            while(r > qr)
            {
                del(a[r--]);
            }
            while(l < ql)
            {
                del(a[l++]);
            }
            while(l > ql)
            {
                add(a[--l]);
            }
            re[q[i].id] = ans;
        }
        for(int 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、付费专栏及课程。

余额充值