标签:style blog class code tar ext
题目链接:http://acm.upc.edu.cn/problem.php?id=2224
题意:给出n个数pi,和m个查询,每个查询给出l,r,a,b,让你求在区间l~r之间的pi的个数(A<=pi<=B,l<=i<=r)。
参考链接:http://www.cnblogs.com/zj62/p/3558967.html
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <set> #define lson rt<<1,L,mid #define rson rt<<1|1,mid+1,R /* http://acm.upc.edu.cn/problem.php?id=2224 */ using namespace std; const int maxn=50000+5; const int INF=0x3f3f3f3f; int n,m; int tree[maxn<<2]; int ans[maxn][2]; /*ans[i][0]记录第i个查询区间中比a小的数的个数,ans[i][1]记录第i个查询中比b小的数的个数,答案为ans[i][1]-ans[i][0]*/ struct Num{ int value; int idx; bool operator<(const Num tmp)const{ return value<tmp.value; } }num[maxn]; struct Query{ int l,r,a,b; int idx; }q[maxn]; bool cmp1(const Query tmp1,const Query tmp2){ return tmp1.a<tmp2.a; } bool cmp2(const Query tmp1,const Query tmp2){ return tmp1.b<tmp2.b; } void build(int rt,int L,int R){ tree[rt]=0; if(L==R){ return; } int mid=(L+R)>>1; build(lson); build(rson); } void update(int rt,int L,int R,int x){ if(L==R){ tree[rt]++; return; } int mid=(L+R)>>1; if(x<=mid) update(lson,x); else update(rson,x); tree[rt]=tree[rt<<1]+tree[rt<<1|1]; } int query(int rt,int L,int R,int l,int r){ if(l<=L&&R<=r){ return tree[rt]; } int mid=(L+R)>>1; int ret=0; if(l<=mid) ret+=query(lson,l,r); if(r>mid) ret+=query(rson,l,r); return ret; } void solve(){ sort(num+1,num+n+1); sort(q+1,q+m+1,cmp1); build(1,1,n); int d=1; for(int j=1;j<=m;j++){ while(d<=n && num[d].value<q[j].a){ update(1,1,n,num[d].idx); d++; } ans[q[j].idx][0]=query(1,1,n,q[j].l,q[j].r); } sort(q+1,q+m+1,cmp2); build(1,1,n); d=1; for(int j=1;j<=m;j++){ while(d<=n && num[d].value<=q[j].b){ update(1,1,n,num[d].idx); d++; } ans[q[j].idx][1]=query(1,1,n,q[j].l,q[j].r); } } int main() { int t,cases=0; scanf("%d",&t); while(t--){ scanf("%d%d",&n,&m); for(int i=1;i<=n;i++){ scanf("%d",&num[i].value); num[i].idx=i; } for(int i=1;i<=m;i++){ scanf("%d%d%d%d",&q[i].l,&q[i].r,&q[i].a,&q[i].b); q[i].idx=i; } solve(); printf("Case #%d:\n",++cases); for(int i=1;i<=m;i++){ printf("%d\n",ans[i][1]-ans[i][0]); } } return 0; }
UPC 2224 Boring Counting (离线线段树,统计区间[l,r]之间大小在[A,B]中的数的个数),布布扣,bubuko.com
UPC 2224 Boring Counting (离线线段树,统计区间[l,r]之间大小在[A,B]中的数的个数)
标签:style blog class code tar ext
原文地址:http://www.cnblogs.com/chenxiwenruo/p/3704201.html