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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章