HDU 4638 multi-school fourth case 1007 offline inquiry, tree array | Line Segment tree maintenance

Source: Internet
Author: User

In fact, this question is similar to the tree array and the number of line segments at the airport.

This offline processing is currently under visual testing, and many questions are maintained in the tree array.

Question: Give a sequence, and then ask, how many groups are there in a single interval? If the IDs of two people are continuous, they are a GROUP, this GROUP can have many people.

Idea: in fact, the previous questions are a bit similar. We first insert all the numbers into the line segment tree, so the value of each insertion is assumed to be at the pos, the inserted value is determined based on pos + 1 and pos-1. If both of them have already been identified, the pos inserted will be pos-1, pos, pos + 1 is connected to a GROUP, which is less than the pos-1 and pos + 1 respectively. Therefore, the value of pos is reduced by 1. Similarly, if pos-1 and pos + 1 are not inserted, the pos insertion GROUP will be + 1.

Then, when you ask offline, scan from the past to the next, and then sum the intervals.

Here I use the tree array and line segment tree methods to achieve this problem. In fact, the tree array is dominant because it is the sum of Single Point update intervals.

Of course, I am familiar with the line tree, so I changed my posture.

Line Segment Tree CODE:


 

#include <iostream>  #include <cstdio>  #include <algorithm>  #include <string>  #include <cmath>  #include <cstring>  #include <queue>  #include <set>  #include <vector>  #include <stack>  #include <map>  #include <iomanip>  #define PI acos(-1.0)  #define Max 2505  #define inf 1<<28  #define LL(x) ( x << 1 )  #define RR(x) ( x << 1 | 1 )  #define REP(i,s,t) for( int i = ( s ) ; i <= ( t ) ; ++ i )  #define ll long long  #define mem(a,b) memset(a,b,sizeof(a))  #define mp(a,b) make_pair(a,b)  #define PII pair<int,int>  using namespace std;   inline void RD(int &ret) {     char c;     do {         c = getchar();     } while(c < '0' || c > '9') ;     ret = c - '0';     while((c=getchar()) >= '0' && c <= '9')         ret = ret * 10 + ( c - '0' ); } #define N 300005  #define NN 100005  int L[N] , R[N] , val[N] ,M[N] ; int d[NN] ; int is[NN] ,a[NN] ; int pos[NN] ; int ans[NN] ; void init(){     mem(d, 0) ;     mem(is ,0) ; } struct kdq{     int s , e , id ; }Q[N] ; void push_up(int u){     val[u] = val[LL(u)] + val[RR(u)] ; } void build(int l ,int r ,int u){     L[u] = l ;     R[u] = r ;     if(l == r){         val[u] = d[l] ;         return ;     }     M[u] = l + r >> 1 ;     build(l , M[u] , LL(u)) ;     build(M[u] + 1 , r ,RR(u)) ;     push_up(u) ; }  void update(int l , int u , int num){     if(L[u] == l && R[u] == l){         val[u] += num ;         return ;     }     if(l <= M[u])update(l ,LL(u) ,num) ;     else update(l, RR(u) , num) ;     push_up(u) ; } int query(int l ,int r ,int u){     if(L[u] == l && R[u] == r){         return val[u] ;     }     if(r <= M[u])return query(l ,r ,LL(u)) ;     else if(l > M[u])return query(l , r, RR(u)) ;     else return (query(l ,M[u] , LL(u)) + query(M[u] + 1 , r ,RR(u))); } bool cmp(const kdq& a ,const kdq&b){     return a.s < b.s ; } int main() {     int T ;     cin >> T ;     while(T -- ){         int n , m ;         cin >> n >> m ;         init() ;         for (int i = 1 ; i <= n ; i ++ ){             RD(a[i]) ;             pos[a[i]] = i ;         }         for (int i = 0 ; i < m ; i ++ ){             RD(Q[i].s) ;             RD(Q[i].e) ;             Q[i].id = i ;         }         for (int i = 1 ; i <= n ; i ++ ){             int nn = 0 ;             if(is[a[i] + 1]) nn ++ ;             if(is[a[i] - 1]) nn ++ ;             if(nn == 2)d[i] -- ;             else if(nn == 0)d[i] ++ ;             is[a[i]] = 1 ;         }         build(1 , n , 1) ;         sort(Q , Q + m ,cmp) ;         int st = 1 ;         for (int i = 0 ; i < m ; i ++ ){             while(Q[i].s > st){                 if(pos[a[st] + 1] > st && a[st] < n){                     update(pos[a[st] + 1] ,1 , 1) ;                 }                 if(pos[a[st] - 1] > st && a[st] > 1){                     update(pos[a[st] - 1] , 1, 1) ;                 }                 st ++ ;             }             ans[Q[i].id] = query(Q[i].s ,Q[i].e , 1) ;         }         for (int i = 0 ; i < m ; i ++ ){             printf("%d\n",ans[i]) ;         }     }     return 0 ; } #include <iostream>#include <cstdio>#include <algorithm>#include <string>#include <cmath>#include <cstring>#include <queue>#include <set>#include <vector>#include <stack>#include <map>#include <iomanip>#define PI acos(-1.0)#define Max 2505#define inf 1<<28#define LL(x) ( x << 1 )#define RR(x) ( x << 1 | 1 )#define REP(i,s,t) for( int i = ( s ) ; i <= ( t ) ; ++ i )#define ll long long#define mem(a,b) memset(a,b,sizeof(a))#define mp(a,b) make_pair(a,b)#define PII pair<int,int>using namespace std;inline void RD(int &ret) {    char c;    do {        c = getchar();    } while(c < '0' || c > '9') ;    ret = c - '0';    while((c=getchar()) >= '0' && c <= '9')        ret = ret * 10 + ( c - '0' );}#define N 300005#define NN 100005int L[N] , R[N] , val[N] ,M[N] ;int d[NN] ;int is[NN] ,a[NN] ;int pos[NN] ;int ans[NN] ;void init(){    mem(d, 0) ;    mem(is ,0) ;}struct kdq{    int s , e , id ;}Q[N] ;void push_up(int u){    val[u] = val[LL(u)] + val[RR(u)] ;}void build(int l ,int r ,int u){    L[u] = l ;    R[u] = r ;    if(l == r){        val[u] = d[l] ;        return ;    }    M[u] = l + r >> 1 ;    build(l , M[u] , LL(u)) ;    build(M[u] + 1 , r ,RR(u)) ;    push_up(u) ;}void update(int l , int u , int num){    if(L[u] == l && R[u] == l){        val[u] += num ;        return ;    }    if(l <= M[u])update(l ,LL(u) ,num) ;    else update(l, RR(u) , num) ;    push_up(u) ;}int query(int l ,int r ,int u){    if(L[u] == l && R[u] == r){        return val[u] ;    }    if(r <= M[u])return query(l ,r ,LL(u)) ;    else if(l > M[u])return query(l , r, RR(u)) ;    else return (query(l ,M[u] , LL(u)) + query(M[u] + 1 , r ,RR(u)));}bool cmp(const kdq& a ,const kdq&b){    return a.s < b.s ;}int main() {    int T ;    cin >> T ;    while(T -- ){        int n , m ;        cin >> n >> m ;        init() ;        for (int i = 1 ; i <= n ; i ++ ){            RD(a[i]) ;            pos[a[i]] = i ;        }        for (int i = 0 ; i < m ; i ++ ){            RD(Q[i].s) ;            RD(Q[i].e) ;            Q[i].id = i ;        }        for (int i = 1 ; i <= n ; i ++ ){            int nn = 0 ;            if(is[a[i] + 1]) nn ++ ;            if(is[a[i] - 1]) nn ++ ;            if(nn == 2)d[i] -- ;            else if(nn == 0)d[i] ++ ;            is[a[i]] = 1 ;        }        build(1 , n , 1) ;        sort(Q , Q + m ,cmp) ;        int st = 1 ;        for (int i = 0 ; i < m ; i ++ ){            while(Q[i].s > st){                if(pos[a[st] + 1] > st && a[st] < n){                    update(pos[a[st] + 1] ,1 , 1) ;                }                if(pos[a[st] - 1] > st && a[st] > 1){                    update(pos[a[st] - 1] , 1, 1) ;                }                st ++ ;            }            ans[Q[i].id] = query(Q[i].s ,Q[i].e , 1) ;        }        for (int i = 0 ; i < m ; i ++ ){            printf("%d\n",ans[i]) ;        }    }    return 0 ;}


Tree array CODE:

 #include <iostream>  #include <cstdio>  #include <algorithm>  #include <string>  #include <cmath>  #include <cstring>  #include <queue>  #include <set>  #include <vector>  #include <stack>  #include <map>  #include <iomanip>  #define PI acos(-1.0)  #define Max 2505  #define inf 1<<28  #define LL(x) ( x << 1 )  #define RR(x) ( x << 1 | 1 )  #define REP(i,s,t) for( int i = ( s ) ; i <= ( t ) ; ++ i )  #define ll long long  #define mem(a,b) memset(a,b,sizeof(a))  #define mp(a,b) make_pair(a,b)  #define PII pair<int,int>  using namespace std;   inline void RD(int &ret) {     char c;     do {         c = getchar();     } while(c < '0' || c > '9') ;     ret = c - '0';     while((c=getchar()) >= '0' && c <= '9')         ret = ret * 10 + ( c - '0' ); } inline void OT(int a){     if(a >= 10)OT(a / 10) ;     putchar(a % 10 + '0') ; } #define N 100005  int a[N] ; int n , m ; struct kdq{     int s , e , id ; }ed[N] ; bool cmp(const kdq& a ,const kdq& b){     return a.s < b.s ; } int c[N] ; inline void update(int pos ,int num){     for (int i = pos ; i <= n ; i += i & (-i) ){         c[i] += num ;     } } inline int sum(int pos){     int ans = 0 ;     for (int i = pos ; i >= 1 ; i -= i & (-i)){         ans += c[i] ;     }     return ans ; } int is[N] ; int ans[N] ; int pos[N] ; int main() {     int T ;     cin >> T ;     while( T -- ){         cin >> n >> m ;         mem(c ,0) ;         mem(is , 0) ;         mem(pos , 0) ;         REP(i , 1 , n ){             RD(a[i]) ;             pos[a[i]] = i ;         }         REP(i , 0 , m - 1){             RD(ed[i].s) ;             RD(ed[i].e) ;             ed[i].id = i ;         }         REP(i , 1 , n){             int nn = 0 ;             if(is[a[i] + 1])nn ++ ;             if(is[a[i] - 1])nn ++ ;             if(nn == 2)update(i , -1) ;             else if(nn == 0)update(i , 1) ;             is[a[i]] = 1 ;         }         sort(ed , ed + m ,cmp) ;         int st = 1 ;         for (int i = 0 ; i < m ; i ++ ){             while(ed[i].s > st){                 if(pos[a[st] + 1] > st && a[st] < n){                     update(pos[a[st] + 1] , 1) ;                 }                 if(pos[a[st] - 1] > st && a[st] > 1){                     update(pos[a[st] - 1] , 1) ;                 }                 st ++ ;             }             ans[ed[i].id] = sum(ed[i].e) - sum(ed[i].s - 1) ;         }         for (int i = 0 ; i < m ; i ++ ){             printf("%d\n",ans[i]) ;         }     }     return 0 ; } #include <iostream>#include <cstdio>#include <algorithm>#include <string>#include <cmath>#include <cstring>#include <queue>#include <set>#include <vector>#include <stack>#include <map>#include <iomanip>#define PI acos(-1.0)#define Max 2505#define inf 1<<28#define LL(x) ( x << 1 )#define RR(x) ( x << 1 | 1 )#define REP(i,s,t) for( int i = ( s ) ; i <= ( t ) ; ++ i )#define ll long long#define mem(a,b) memset(a,b,sizeof(a))#define mp(a,b) make_pair(a,b)#define PII pair<int,int>using namespace std;inline void RD(int &ret) {    char c;    do {        c = getchar();    } while(c < '0' || c > '9') ;    ret = c - '0';    while((c=getchar()) >= '0' && c <= '9')        ret = ret * 10 + ( c - '0' );}inline void OT(int a){    if(a >= 10)OT(a / 10) ;    putchar(a % 10 + '0') ;}#define N 100005int a[N] ;int n , m ;struct kdq{    int s , e , id ;}ed[N] ;bool cmp(const kdq& a ,const kdq& b){    return a.s < b.s ;}int c[N] ;inline void update(int pos ,int num){    for (int i = pos ; i <= n ; i += i & (-i) ){        c[i] += num ;    }}inline int sum(int pos){    int ans = 0 ;    for (int i = pos ; i >= 1 ; i -= i & (-i)){        ans += c[i] ;    }    return ans ;}int is[N] ;int ans[N] ;int pos[N] ;int main() {    int T ;    cin >> T ;    while( T -- ){        cin >> n >> m ;        mem(c ,0) ;        mem(is , 0) ;        mem(pos , 0) ;        REP(i , 1 , n ){            RD(a[i]) ;            pos[a[i]] = i ;        }        REP(i , 0 , m - 1){            RD(ed[i].s) ;            RD(ed[i].e) ;            ed[i].id = i ;        }        REP(i , 1 , n){            int nn = 0 ;            if(is[a[i] + 1])nn ++ ;            if(is[a[i] - 1])nn ++ ;            if(nn == 2)update(i , -1) ;            else if(nn == 0)update(i , 1) ;            is[a[i]] = 1 ;        }        sort(ed , ed + m ,cmp) ;        int st = 1 ;        for (int i = 0 ; i < m ; i ++ ){            while(ed[i].s > st){                if(pos[a[st] + 1] > st && a[st] < n){                    update(pos[a[st] + 1] , 1) ;                }                if(pos[a[st] - 1] > st && a[st] > 1){                    update(pos[a[st] - 1] , 1) ;                }                st ++ ;            }            ans[ed[i].id] = sum(ed[i].e) - sum(ed[i].s - 1) ;        }        for (int i = 0 ; i < m ; i ++ ){            printf("%d\n",ans[i]) ;        }    }    return 0 ;} 

 

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.