zoukankan      html  css  js  c++  java
  • HDU 4638 group(离线+树状数组)

    此题实质上是询问每段区间[l,r]内的数字能组成多少段连续的数字。

    不大好用线段树合并区间来写。

    考虑离线,类似于HH的项链的话,对询问按右端点排序,对于右端点的每一次右移,则对于询问,实际上是求的后缀区间。

    如果前面已经出现了a[r]-1的话,那么它们能组成连续的区间,a[r]+1同理。

    剩下的模仿HH的项链即可。

    # include <cstdio>
    # include <cstring>
    # include <cstdlib>
    # include <iostream>
    # include <vector>
    # include <queue>
    # include <stack>
    # include <map>
    # include <set>
    # include <cmath>
    # include <algorithm>
    using namespace std;
    # define lowbit(x) ((x)&(-x))
    # define pi acos(-1.0)
    # define eps 1e-8
    # define MOD 1000000007
    # define INF 1000000000
    # define mem(a,b) memset(a,b,sizeof(a))
    # define FOR(i,a,n) for(int i=a; i<=n; ++i)
    # define FO(i,a,n) for(int i=a; i<n; ++i)
    # define bug puts("H");
    # define lch p<<1,l,mid
    # define rch p<<1|1,mid+1,r
    # define mp make_pair
    # define pb push_back
    typedef pair<int,int> PII;
    typedef vector<int> VI;
    # pragma comment(linker, "/STACK:1024000000,1024000000")
    typedef long long LL;
    int Scan() {
        int res=0, flag=0;
        char ch;
        if((ch=getchar())=='-') flag=1;
        else if(ch>='0'&&ch<='9') res=ch-'0';
        while((ch=getchar())>='0'&&ch<='9')  res=res*10+(ch-'0');
        return flag?-res:res;
    }
    void Out(int a) {
        if(a<0) {putchar('-'); a=-a;}
        if(a>=10) Out(a/10);
        putchar(a%10+'0');
    }
    const int N=100005;
    //Code begin...
    
    struct Q{int l, r, id;}q[N];
    int a[N], to[N][2], vis[N], tree[N], ans[N], n;
    
    bool comp(Q a, Q b){return a.r<b.r;}
    void add(int x, int val){while (x<=n) tree[x]+=val, x+=lowbit(x);}
    int sum(int x)
    {
        int res=0;
        while (x) res+=tree[x], x-=lowbit(x);
        return res;
    }
    int main ()
    {
        int T, m;
        T=Scan();
        while (T--) {
            mem(to,0); mem(vis,0); mem(tree,0);
            n=Scan(); m=Scan();
            FOR(i,1,n) {
                a[i]=Scan();
                if (vis[a[i]-1]) to[i][0]=vis[a[i]-1];
                if (vis[a[i]+1]) to[i][1]=vis[a[i]+1];
                vis[a[i]]=i;
            }
            FOR(i,1,m) q[i].l=Scan(), q[i].r=Scan(), q[i].id=i;
            sort(q+1,q+m+1,comp);
            int now=0;
            FOR(i,1,m) {
                while (now<q[i].r) {
                    ++now;
                    add(now,1);
                    if (to[now][0]) add(to[now][0],-1);
                    if (to[now][1]) add(to[now][1],-1);
                }
                ans[q[i].id]=sum(q[i].r)-sum(q[i].l-1);
            }
            FOR(i,1,m) Out(ans[i]), putchar('
    ');
        }
        return 0;
    }
    View Code
  • 相关阅读:
    Java初始化顺序
    生产者与消费者
    Java线程
    思科QoS知识全面了解
    思科3750交换机堆叠配置指南
    Cisco VRRP Config Case1
    思科交换机快速定位流量异常端口
    QoS笔记
    Cisco BGP团体配置要领
    Cisco BGP Local_Pref配置要领
  • 原文地址:https://www.cnblogs.com/lishiyao/p/6525405.html
Copyright © 2011-2022 走看看