UPC 2224 Boring Counting (离线线段树,统计区间[l,r]之间大小在[A,B]中的数的个数)

题目链接: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]中的数的个数)

时间: 2024-07-30 23:00:24

UPC 2224 Boring Counting (离线线段树,统计区间[l,r]之间大小在[A,B]中的数的个数)的相关文章

Codeforces Round #271 (Div. 2) F.Ant colony(线段树 + 统计区间内某数的个数)

F. Ant colony Mole is hungry again. He found one ant colony, consisting of n ants, ordered in a row. Each ant i (1 ≤ i ≤ n) has a strength si. In order to make his dinner more interesting, Mole organizes a version of «Hunger Games» for the ants. He c

2016 Multi-University Training Contest 10 [HDU 5861] Road (线段树:区间覆盖+单点最大小)

HDU 5861 题意 在n个村庄之间存在n-1段路,令某段路开放一天需要交纳wi的费用,但是每段路只能开放一次,一旦关闭将不再开放.现在给你接下来m天内的计划,在第i天,需要对村庄ai到村庄bi的道路进行开放.在满足m天内花费最小的情况下,求出每天的花销. 分析: 我们可以想到用线段树想到记录每一段路的开始时间与结束时间,开始时间很简单,就是一开始的时间,结束的时间求法可以参考区间覆盖,这是类似的: 然后我们在转化哪一天开哪些,哪一天关哪些,那这天的贡献sum = 开-关 ; 这很关键,我在比

poj3667,线段树,区间合并,维护左,右,中区间

题目大意:Hotel有N(1 ≤ N ≤ 50,000)间rooms,并且所有的rooms都是连续排列在同一边,groups需要check in 房间,要求房间的编号为连续的r..r+Di-1并且r是最小的:visitors同样可能check out,并且他们每次check out都是编号为Xi ..Xi +Di-1 (1 ≤ Xi ≤ N-Di+1)的房间,题目的输入有两种样式: 1  a     :  groups需要check in  a间编号连续的房间,然后这些房间住进去 2  a  

【SDUT OJ 2610】 Boring Counting(主席树)

[SDUT OJ 2610] Boring Counting(主席树) Boring Counting Time Limit: 3000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 In this problem you are given a number sequence P consisting of N integer and Pi is the ith element in the sequence. Now you task is to ans

hdu 4638 Group(莫队算法|离线线段树)

Group Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1323    Accepted Submission(s): 703 Problem Description There are n men ,every man has an ID(1..n).their ID is unique. Whose ID is i and i-

HDU 4417 Super Mario(离线线段树or树状数组)

Problem Description Mario is world-famous plumber. His "burly" figure and amazing jumping ability reminded in our memory. Now the poor princess is in trouble again and Mario needs to save his lover. We regard the road to the boss's castle as a l

27号的十道离线线段树

27号的十道离线线段树 hdu4288: (2012成都网络赛&&cf) #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<algorithm> #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 using namespace std; const

spoj gss2 : Can you answer these queries II 离线&amp;&amp;线段树

1557. Can you answer these queries II Problem code: GSS2 Being a completist and a simplist, kid Yang Zhe cannot solve but get Wrong Answer from most of the OI problems. And he refuse to write two program of same kind at all. So he always failes in co

HDU ACM 4417 Super Mario 离线线段树

分析:离线线段树,把所有询问离线读入,然后按H从小到大排序.对于所有结点也按从小到大排序,然后根据查询的H,将比H小的点加入到线段树,最后就是一个区间求和.这题貌似也可以用划分树,树状数组等方法做. #include<iostream> #include<algorithm> using namespace std; #define N 100005 struct Tree { int left,right,cnt; } TREE[N<<2]; struct Query