HDU 4638 Group (offline algorithm | discrete line segment tree query), hdu4638

Source: Internet
Author: User
Tags acos

HDU 4638 Group (offline algorithm | discrete line segment tree query), hdu4638

Address: HDU 4638
First, I wrote a letter to the MO team. It's very simple.
The Code is as follows:

#include <iostream>#include <string.h>#include <math.h>#include <queue>#include <algorithm>#include <stdlib.h>#include <map>#include <set>#include <stdio.h>#include <time.h>using namespace std;#define LL long long#define pi acos(-1.0)#pragma comment(linker, "/STACK:1024000000")const int mod=1e9+7;const int INF=0x3f3f3f3f;const double eqs=1e-9;const int MAXN=100000+10;int a[MAXN];LL ha[MAXN], ans[MAXN], res;struct node {        int l, r, id, pos;} fei[MAXN];bool cmp(node x, node y){        return x.pos<y.pos||x.pos==y.pos&&x.r<y.r;}void reduce(int tmp){        if(ha[tmp-1]&&ha[tmp+1]) res++;        else if(!ha[tmp-1]&&!ha[tmp+1]) res--;        ha[tmp]=0;}void add(int tmp){        if(ha[tmp-1]&&ha[tmp+1]) res--;        else if(!ha[tmp-1]&&!ha[tmp+1]) res++;        ha[tmp]=1;}int main(){        int t, n, m, i, j, k, tmp, l, r;        scanf("%d",&t);        while(t--) {                scanf("%d%d",&n,&m);                for(i=1; i<=n; i++) {                        scanf("%d",&a[i]);                }                k=sqrt(n*1.0);                for(i=0; i<m; i++) {                        scanf("%d%d",&fei[i].l,&fei[i].r);                        fei[i].id=i;                        fei[i].pos=fei[i].l/k;                }                sort(fei,fei+m,cmp);                memset(ha,0,sizeof(ha));                l=1;                r=0;                res=0;                for(i=0; i<m; i++) {                        while(r>fei[i].r) {                                tmp=a[r];                                reduce(tmp);                                r--;                        }                        while(r<fei[i].r) {                                r++;                                tmp=a[r];                                add(tmp);                        }                        while(l<fei[i].l) {                                tmp=a[l];                                reduce(tmp);                                l++;                        }                        while(l>fei[i].l) {                                l--;                                tmp=a[l];                                add(tmp);                        }                        ans[fei[i].id]=res;                }                for(i=0; i<m; i++) {                        printf("%I64d\n",ans[i]);                }        }        return 0;}

Then I read the question report and found that the line segment tree and offline query can also be used. And it's amazing ..
First, save it offline. Maintenance is performed from left to right to maintain the relative number of the previous values for the current enumerated values. For the current number of I, first update the value of this number to 1, representing an independent string, find the numbers a [I]-1 and a [I] + 1 that do not exist. If yes, the numbers above cannot be independent strings, compared with a [I], a [I] string can exist in the total, so we need to separate a [I]-1 and a [I] + 1-1, respectively. then, in the query where the R value is I, You can directly sum the line tree. The Code is as follows:

#include <iostream>#include <string.h>#include <math.h>#include <queue>#include <algorithm>#include <stdlib.h>#include <map>#include <set>#include <stdio.h>#include <time.h>using namespace std;#define LL long long#define pi acos(-1.0)#pragma comment(linker, "/STACK:1024000000")#define root 1, n, 1#define lson l, mid, rt<<1#define rson mid+1, r, rt<<1|1const int mod=1e9+7;const int INF=0x3f3f3f3f;const double eqs=1e-9;const int MAXN=100000+10;int a[MAXN], pos[MAXN], sum[MAXN<<2], ans[MAXN];struct node{        int l, r, id;}fei[MAXN];bool cmp(node x, node y){        return x.r<y.r;}void PushUp(int rt){        sum[rt]=sum[rt<<1]+sum[rt<<1|1];}void Update(int p, int x, int l, int r, int rt){        if(l==r){                sum[rt]+=x;                return ;        }        int mid=l+r>>1;        if(p<=mid) Update(p,x,lson);        else Update(p,x,rson);        PushUp(rt);}int Query(int ll, int rr, int l, int r, int rt){        if(ll<=l&&rr>=r){                return sum[rt];        }        int mid=l+r>>1, ans=0;        if(ll<=mid) ans+=Query(ll,rr,lson);        if(rr>mid) ans+=Query(ll,rr,rson);        return ans;}int main(){        int t, n, m, i, j;        scanf("%d",&t);        while(t--){                scanf("%d%d",&n,&m);                for(i=1;i<=n;i++){                        scanf("%d",&a[i]);                        pos[a[i]]=i;                }                for(i=0;i<m;i++){                        scanf("%d%d",&fei[i].l,&fei[i].r);                        fei[i].id=i;                }                memset(sum,0,sizeof(sum));                sort(fei,fei+m,cmp);                j=0;                for(i=1;i<=n;i++){                        Update(i,1,root);                        if(a[i]>1&&pos[a[i]-1]<i) Update(pos[a[i]-1],-1,root);                        if(a[i]<n&&pos[a[i]+1]<i) Update(pos[a[i]+1],-1,root);                        while(j<m&&fei[j].r<=i){                                ans[fei[j].id]=Query(fei[j].l,i,root);                                j++;                        }                }                for(i=0;i<m;i++){                        printf("%d\n",ans[i]);                }        }        return 0;}

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.