#include<iostream> #include<string.h> #include<algorithm> #include<stdio.h> #include<vector> #define LL long long #define rep(i,j,k) for(int i=j;i<=k;i++) #define per(i,j,k) for(int i=j;i>=k;i--) #define pb push_back #define pii pair<int,int> #define mp make_pair using namespace std; const int maxx = 1e5+6; vector<int>v; struct node{ int l,r,w; }tree[maxx*20]; int cnt; int root[maxx],a[maxx]; int l[maxx],r[maxx],h[maxx]; int getid(int x){ return lower_bound(v.begin(),v.end(),x)-v.begin()+1; } void inserts(int l,int r,int pre,int &now,int pos,int val){ ///新建节点编号 now=++cnt; ///拷贝并更新节点信息 tree[now]=tree[pre]; if (l==r){ tree[now].w+=val; return ; } int mid=(l+r)>>1; if (pos<=mid){ inserts(l,mid,tree[pre].l,tree[now].l,pos,val); }else { inserts(mid+1,r,tree[pre].r,tree[now].r,pos,val); } //维护区间数字的个数目 tree[now].w=tree[tree[now].l].w+tree[tree[now].r].w; } int query(int pre,int now,int l,int r,int pos){ if (l==r){ //查询某个区间数字的个数,就是两个版本权值线段树个数的差值 return tree[now].w-tree[pre].w; } int mid=(l+r)>>1; if (pos<=mid){ return query(tree[pre].l,tree[now].l,l,mid,pos); }else { return tree[tree[now].l].w-tree[tree[pre].l].w+query(tree[pre].r,tree[now].r,mid+1,r,pos); } } int main(){ int t; int n,m; scanf("%d",&t); int ca=0; while(t--){ ca++; printf("Case %d:\n",ca); cnt=0; scanf("%d%d",&n,&m); v.clear(); memset(root,0,sizeof(root)); memset(tree,0,sizeof(tree)); for(int i=1;i<=n;i++){ scanf("%d",&a[i]); v.pb(a[i]); } for (int i=1;i<=n;i++){ scanf("%d%d%d",&l[i],&r[i],&h[i]); v.push_back(h[i]);///注意为了防止出错,你需要同时离散高度,防止高度出现问题 } sort(v.begin(),v.end()); v.erase(unique(v.begin(),v.end()),v.end()); int sz=v.size(); for (int i=1;i<=n;i++){ int x=getid(a[i]); inserts(1,sz,root[i-1],root[i],x,1); } for (int i=1;i<=m;i++){ int posc=getid(h[i]); l[i]++; r[i]++; if (posc==0)printf("0\n"); else printf("%d\n",query(root[l[i]-1],root[r[i]],1,sz,posc)); } } return 0; }
原文地址:https://www.cnblogs.com/bluefly-hrbust/p/11360326.html
时间: 2024-11-16 22:26:40