当前位置:编程学习 > C/C++ >>

hdu 4638 Group(离线线段树)

搞了这么久,终于做了一个离线线段树。。。。个人觉得,如果询问间会相互影响到的话,就用所谓“离线”来搞。。。

这个题,考虑从左到右一个一个地加数,加到a[i]时,如果a[i]-1或a[i]+1有一个加过,总段数不变,都加过,合并两端,段数减一;如果都没加过,加入a[i]相当于新加了一段。

将所有询问按右端点从小到大处理(一个大的区间,增加的元素会对小的区间造成影响);边处理边加数。


 

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_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;
}

 

补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,