hdu4638 Group(莫队)

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个数(编号为1~n且各不相同),m个查询区间,求出每个查询区间中不连续的段的个数,5 4 3、3 5 4这样的段都称为连续的一段;
思路:上莫队,考虑如何从已知答案的区间(l,r)求出(ql,qr)的答案,对于一个加入的数x,我们就要考虑x-1,x+1是否在我们当前维护的区间中,分情况讨论答案的变化;用vis数组记录一下数是否存在于当前维护的区间里面就。还有就是我先跑l指针就会错,这样会导致不在[l,r]中的数的vis值仍为1;

//#include<bits/stdc++.h>
#include<queue>
#include <cmath>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#define mod (1000000007)
#define middle (l+r)>>1
#define SIZE 1000000+5
#define lowbit(x) (x&(-x))
#define lson (rt<<1)
#define rson (rt<<1|1)
#define MP(x,y) make_pair(x,y)
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const int inf_max = 0x3f3f3f3f;
const ll Linf = 9e18;
const int maxn = 1e5 + 10;
const long double E = 2.7182818;
const double eps=0.0001;
using namespace std;
inline int read()
{
    int f=1,res=0;
    char ch = getchar();
    while(ch<'0'||ch>'9') { if(ch=='-') f=-1; ch=getchar(); }
    while(ch>='0'&&ch<='9') { res=res*10+ch-'0' ; ch=getchar(); }
    return f*res;
}
struct QRY {
    int l,r,id,ans;
}q[maxn];
int vis[maxn],ret,n,m,a[maxn],block;
bool cmp1(QRY a,QRY b) {
    return a.id < b.id;
}
bool cmp2(QRY a,QRY b) {
    int bla = (a.l - 1) / block + 1,blb = (b.l - 1) / block + 1;
    if(bla == blb) return a.r < b.r;
    return a.l < b.l;
}
void rmv(int x) {
    if(x + 1 <= n && x - 1 >= 1) {
        if(vis[x + 1] && vis[x - 1]) ret++;
        else if(!vis[x + 1] && !vis[x - 1]) ret--;
    }else if(x + 1 <= n) {
        if(!vis[x + 1]) ret--;
    }else if(x - 1 >= 1) {
        if(!vis[x - 1]) ret--;
    }
    vis[x] = 0;
}
void add(int x) {
    if(x + 1 <= n && x - 1 >= 1) {
        if(vis[x + 1] && vis[x - 1]) ret--;
        else if(!vis[x + 1] && !vis[x - 1]) ret++;
    }
    else if(x + 1 <= n) {
        if(!vis[x + 1]) ret++;
    }else if(x - 1 >= 1) {
        if(!vis[x - 1]) ret++;
    }
    vis[x] = 1;
}
int main()
{
    int t;
    t = read();
    while(t--) {
        memset(vis,0,sizeof(vis));ret = 0;
        n = read(),m = read();
        block = sqrt(n);
        for(int i = 1;i <= n; ++i) {
            a[i] = read();
        }
        for(int i = 1;i <= m; ++i) {
            q[i].l = read(),q[i].r = read();
            q[i].id = i;
        }
        sort(q + 1,q + 1 + m,cmp2);
        int l = 1,r = 0;
        for(int i = 1;i <= m; ++i) {
            while(r > q[i].r) rmv(a[r]),r--;
            while(r < q[i].r) r++,add(a[r]);
            while(l < q[i].l) rmv(a[l]),l++;
            while(l > q[i].l) l--,add(a[l]);
            q[i].ans = ret;
        }
        sort(q + 1,q + 1 + m,cmp1);
        for(int i = 1;i <= m; ++i) printf("%d\n",q[i].ans);
    }
    return 0;
}
发布了37 篇原创文章 · 获赞 19 · 访问量 6245

猜你喜欢

转载自blog.csdn.net/qq_44077455/article/details/104091655