HDU - 4417 Super Mario 区间值域个数


传送门:HDU - 4417

思路分析

区间小于等于K的个数,直接主席树就行了

样例输入

1
10 10
0 5 2 7 5 4 3 8 7 7 
2 8 6
3 5 0
1 3 1
1 9 4
0 1 0
3 5 5
5 5 1
4 6 3
1 5 7
5 7 3

样例输出

Case 1:
4
0
0
3
1
2
0
1
5
1

AC代码

#include <bits/stdc++.h>
#define ls x<<1
#define rs x<<1|1
#define fi first
#define se second
#define ll long long
#define pb push_back
#define mp make_pair
#define fun function
#define sz(x) (x).size()
#define lowbit(x) (x)&(-x)
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))

namespace FastIO {
#define BUF_SIZE 100000
#define OUT_SIZE 100000
    bool IOerror=0;
    inline char nc() {
        static char buf[BUF_SIZE],*p1=buf+BUF_SIZE,*pend=buf+BUF_SIZE;
        if(p1==pend) {
            p1=buf;
            pend=buf+fread(buf,1,BUF_SIZE,stdin);
            if(pend==p1) {
                IOerror=1;
                return -1;
            }
        }
        return *p1++;
    }
    inline bool blank(char ch) {
        return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';
    }
    template<class T> inline bool read(T &x) {
        bool sign=0;
        char ch=nc();
        x=0;
        for(; blank(ch); ch=nc());
        if(IOerror)return false;
        if(ch=='-')sign=1,ch=nc();
        for(; ch>='0'&&ch<='9'; ch=nc())x=x*10+ch-'0';
        if(sign)x=-x;
        return true;
    }
    template<class T,class... U>bool read(T& h,U&... t) {
        return read(h)&&read(t...);
    }
#undef OUT_SIZE
#undef BUF_SIZE
};
using namespace std;
using namespace FastIO;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int INF = 0x3f3f3f3f;
const int N = 1e5+10;

struct node{
    int l,r,sum;
}tree[N * 40];

int cnt,root[N],a[N];

void ins(int l,int r,int pre,int &now,int pos){
    tree[++cnt]=tree[pre];
    now=cnt;
    tree[now].sum++;
    if(l==r) return ;
    int mid=l+r>>1;
    if(pos<=mid) ins(l,mid,tree[pre].l,tree[now].l,pos);
    else ins(mid+1,r,tree[pre].r,tree[now].r,pos);
}

int query(int l,int r,int L,int R,int x,int y){
    if(l>=x && r<=y) return tree[R].sum-tree[L].sum;
    int mid=l+r>>1;
    int ans=0;
    if(x<=mid) ans+=query(l,mid,tree[L].l,tree[R].l,x,y);
    if(y>mid) ans+=query(mid+1,r,tree[L].r,tree[R].r,x,y);
    return ans;
}

int main() {

#ifdef xiaofan
    freopen("1.in","r",stdin);
    freopen("1.out","w",stdout);
#endif

    int t;
    read(t);
    int time=0;
    while(t--){
        int n,m;
        read(n,m);
        cnt=0;
        vector<int>x;
        for(int i=1;i<=n;i++) read(a[i]),x.pb(a[i]);
        sort(all(x));
        x.erase(unique(all(x)),x.end());
        for(int i=1;i<=n;i++) a[i]=lower_bound(all(x),a[i])-x.begin()+1;
        int tot=sz(x);
        for(int i=1;i<=n;i++) ins(1,tot,root[i-1],root[i],a[i]);
        printf("Case %d:\n",++time);
        while(m--){
            int l,r,k;
            read(l,r,k);
            l++;
            r++;
            k=upper_bound(all(x),k)-x.begin();
            if(!k) cout<<0<<endl;
            else printf("%d\n",query(1,tot,root[l-1],root[r],1,k));
        }
    }




    return 0;
}


文章作者: 小凡
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 小凡 !
评论
  目录
隐藏
{% if theme.sakura.enable %}{% endif %}