Hdu 4638 Group (offline line segment tree)

Source: Internet
Author: User
Tags add numbers bitset

After so long, I finally made an offline line segment tree .... I personally think that if the inquiry will affect each other, I will use the so-called "offline...

This question is considered to add numbers one by one from left to right. If a [I]-1 or a [I] + 1 is added, the total number of segments remains unchanged. If both ends are merged, the number of segments is reduced by one. If none are added, adding a [I] is equivalent to adding a new segment.

Process all requests from the right endpoint from small to large (a large range, the added elements will affect the small range); process the Edge Addition.


 

include<algorithm>  #include<iostream>  #include<cstring>  #include<cstdlib>  #include<fstream>  #include<sstream>  #include<bitset>  #include<vector>  #include<string>  #include<cstdio>  #include<cmath>  #include<stack>  #include<queue>  #include<stack>  #include<map>  #include<set>  #define FF(i, a, b) for(int i=a; i<b; i++)  #define FD(i, a, b) for(int i=a; i>=b; i--)  #define REP(i, n) for(int i=0; i<n; i++)  #define CLR(a, b) memset(a, b, sizeof(a))  #define debug puts("**debug**")  #define LL long long  #define PB push_back  using namespace std;  const int maxn = 100001; int a[maxn], c[maxn], pos[maxn], ans[maxn]; int n, m; struct Q {     int l, r, id;     bool operator < (const Q rhs) const     {         return r < rhs.r;     } }q[maxn];  int low(int x) {return x & (-x);} void add(int x, int v) {     while(x <= n)     {         c[x] += v;         x += low(x);     } } int getsum(int x) {     int ret = 0;     while(x > 0)     {         ret += c[x];         x -= low(x);     }     return ret; }  int main() {     int T; scanf("%d", &T);     while(T--)     {         scanf("%d%d", &n, &m);         FF(i, 1, n+1) scanf("%d", &a[i]), pos[a[i]] = i, c[i] = 0;         REP(i, m) scanf("%d%d", &q[i].l, &q[i].r), q[i].id = i;         sort(q, q+m);         int j = 1;         for(int i=0; i<m; )         {             if(q[i].r < j)             {                 ans[q[i].id] = getsum(q[i].r) - getsum(q[i].l-1);                 i++;             }             else             {                 add(j, 1);                 if(a[j] > 1 && pos[a[j]-1] < j) add(pos[a[j]-1], -1);                 if(a[j] < n && pos[a[j]+1] < j) add(pos[a[j]+1], -1);                 j++;             }         }         REP(i, m) printf("%d\n", ans[i]);     }     return 0; } #include<algorithm>#include<iostream>#include<cstring>#include<cstdlib>#include<fstream>#include<sstream>#include<bitset>#include<vector>#include<string>#include<cstdio>#include<cmath>#include<stack>#include<queue>#include<stack>#include<map>#include<set>#define FF(i, a, b) for(int i=a; i<b; i++)#define FD(i, a, b) for(int i=a; i>=b; i--)#define REP(i, n) for(int i=0; i<n; i++)#define CLR(a, b) memset(a, b, sizeof(a))#define debug puts("**debug**")#define LL long long#define PB push_backusing namespace std;const int maxn = 100001;int a[maxn], c[maxn], pos[maxn], ans[maxn];int n, m;struct Q{    int l, r, id;    bool operator < (const Q rhs) const    {        return r < rhs.r;    }}q[maxn];int low(int x) {return x & (-x);}void add(int x, int v){    while(x <= n)    {        c[x] += v;        x += low(x);    }}int getsum(int x){    int ret = 0;    while(x > 0)    {        ret += c[x];        x -= low(x);    }    return ret;}int main(){    int T; scanf("%d", &T);    while(T--)    {        scanf("%d%d", &n, &m);        FF(i, 1, n+1) scanf("%d", &a[i]), pos[a[i]] = i, c[i] = 0;        REP(i, m) scanf("%d%d", &q[i].l, &q[i].r), q[i].id = i;        sort(q, q+m);        int j = 1;        for(int i=0; i<m; )        {            if(q[i].r < j)            {                ans[q[i].id] = getsum(q[i].r) - getsum(q[i].l-1);                i++;            }            else            {                add(j, 1);                if(a[j] > 1 && pos[a[j]-1] < j) add(pos[a[j]-1], -1);                if(a[j] < n && pos[a[j]+1] < j) add(pos[a[j]+1], -1);                j++;            }        }        REP(i, m) printf("%d\n", ans[i]);    }    return 0;}

 

Related Article

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.