HDU 4638 Group

Group

Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Problem Description

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

题意:

有n个人,每个人有不同的编号,选择一个区间,问这个区间如果区间的值达到最大的情况需要分成几组,区间的值是每组人数的平方总和。

思路:

因为是平方的总和,所以人数越多总和越大,所以要尽量把连续的分在一组中,所以就可以用莫队算法,增加一个点的时候,如果两边都没有数字的话,那么这个数字一定自成一组,如果一个数组两边都有数字的话,那么就可以连接这两个数字成一组,这样原来的两组就变成了一组,同理减少也是这样的,就可以算出最后的组数的是多少了。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
template <class T>
inline void read(T &ret) {
    char c;
    int sgn;
    if (c = getchar(), c == EOF) return ;
    while (c != '-' && (c < '0' || c > '9')) c = getchar();
    sgn = (c == '-') ? -1:1;
    ret = (c == '-') ? 0:(c - '0');
    while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0');
    ret *= sgn;
    return ;
}
inline void out(int x) {
    if (x > 9) out(x / 10);
    putchar(x % 10 + '0');
}
const int maxn = 100010;
int unit = 0, now = 0;
int cnt[maxn] = {0};
struct NODE {
    int l, r, id;
    bool friend operator < (NODE a, NODE b) {
        if (a.l / unit == b.l / unit) return a.r < b.r;
        else return a.l / unit < b.l / unit;
    }
};
NODE node[maxn];
void add(int val) {
    if (!cnt[val - 1] && !cnt[val + 1]) now++;
    if (cnt[val - 1] && cnt[val + 1]) now--;
    cnt[val]++;
}
void del(int val) {
    if (cnt[val - 1] && cnt[val + 1]) now++;
    if (!cnt[val - 1] && !cnt[val + 1]) now--;
    cnt[val]--;
}
int main() {
    int t, n, m;
    int a[maxn];
    read(t);
    while (t--) {
        memset(cnt, 0, sizeof(cnt));
        now = 0;
        read(n), read(m);
        for (int i = 1; i <= n; i++) read(a[i]);
        for (int i = 1; i <= m; i++) {
            read(node[i].l), read(node[i].r);
            node[i].id = i;
        }
        unit = ceil(sqrt(n));
        sort(node + 1, node + m + 1);
        int l = 1, r = 0;
        int ans[maxn] = {0};
        for (int i = 1; i <= m; i++) {
            int left = node[i].l, right = node[i].r;
            while (r < right) add(a[++r]);
            while (r > right) del(a[r--]);
            while (l > left) add(a[--l]);
            while (l < left) del(a[l++]);
            ans[node[i].id] = now;
        }
        for (int i = 1; i <= m; i++) {
            out(ans[i]);
            putchar('\n');
        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值