HDU 4638 Group 离线莫队

版权声明:存在错误或者不清楚的地方还望指出 https://blog.csdn.net/hypHuangYanPing/article/details/82947140
/**
HDU 4638 Group 莫队
链接:http://acm.hdu.edu.cn/showproblem.php?pid=4638
题意:区间连续数字的块数;
离线莫队;判断当前数字移去或添加时对当前区间该数字左右端点的影响;
***********tricks***********
手写排序差评;
*/

#include<bits/stdc++.h>
#define ll long long
using namespace std;

const int maxn=1e5+7;
int n,q,blo,s[maxn],pos[maxn];

struct node{
    int l,r,id;
    bool operator <(const node &a)const{
        return pos[l]<pos[a.l]||(pos[l]==pos[a.l]&&r<a.r);
    }
}a[maxn];

int ans[maxn];
int num[maxn];

int main(){
    int t;scanf("%d",&t);
    while(t--){
        scanf("%d %d",&n,&q);
        blo=(int)sqrt(n*1.0);
        for(int i=1;i<=n;i++) pos[i]=(i-1)/blo+1;
        for(int i=1;i<=n;i++) scanf("%d",&s[i]);
        for(int i=1;i<=q;i++){
            scanf("%d %d",&a[i].l,&a[i].r);
            a[i].id=i;
        }
        sort(a+1,a+1+q);
        int l=1,r=0,ret=0;
        memset(num,0,sizeof(num));
        for(int i=1;i<=q;i++){
             while(r<a[i].r){
                r++;
                num[s[r]]=1;
                if(num[s[r]-1]==1&&num[s[r]+1]==1) ret--;
                else if(num[s[r]-1]==0&&num[s[r]+1]==0) ret++;
            }
            while(r>a[i].r){
                num[s[r]]=0;
                if(num[s[r]-1]==1&&num[s[r]+1]==1) ret++;
                else if(num[s[r]-1]==0&&num[s[r]+1]==0) ret--;
                r--;
            }
            while(l<a[i].l){
                num[s[l]]=0;
                if(num[s[l]-1]==1&&num[s[l]+1]==1) ret++;
                else if(num[s[l]-1]==0&&num[s[l]+1]==0) ret--;
                l++;
            }
            while(l>a[i].l){
                l--;
                num[s[l]]=1;
                if(num[s[l]-1]==1&&num[s[l]+1]==1) ret--;
                else if(num[s[l]-1]==0&&num[s[l]+1]==0) ret++;
            }
            ans[a[i].id]=ret;
        }
        for(int i=1;i<=q;i++) printf("%d\n",ans[i]);
    }
    return 0;
}


     /*
2
5 4
3 1 2 5 4
1 5
2 4
2 5
3 4

7 6
2 7 3 6 5 1 4
2 4
2 7
3 6
1 5
3 7
2 5

*/

猜你喜欢

转载自blog.csdn.net/hypHuangYanPing/article/details/82947140