题目链接: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=+;
const int INF=0x3f3f3f3f;
int n,m;
int tree[maxn<<];
int ans[maxn][];
/*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]=;
if(L==R){
return;
}
int mid=(L+R)>>;
build(lson);
build(rson);
} void update(int rt,int L,int R,int x){
if(L==R){
tree[rt]++;
return;
}
int mid=(L+R)>>;
if(x<=mid)
update(lson,x);
else
update(rson,x);
tree[rt]=tree[rt<<]+tree[rt<<|];
} int query(int rt,int L,int R,int l,int r){
if(l<=L&&R<=r){
return tree[rt];
}
int mid=(L+R)>>;
int ret=;
if(l<=mid)
ret+=query(lson,l,r);
if(r>mid)
ret+=query(rson,l,r);
return ret;
} void solve(){
sort(num+,num+n+);
sort(q+,q+m+,cmp1);
build(,,n);
int d=;
for(int j=;j<=m;j++){
while(d<=n && num[d].value<q[j].a){
update(,,n,num[d].idx);
d++;
}
ans[q[j].idx][]=query(,,n,q[j].l,q[j].r);
}
sort(q+,q+m+,cmp2);
build(,,n);
d=;
for(int j=;j<=m;j++){
while(d<=n && num[d].value<=q[j].b){
update(,,n,num[d].idx);
d++;
}
ans[q[j].idx][]=query(,,n,q[j].l,q[j].r);
} }
int main()
{
int t,cases=;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++){
scanf("%d",&num[i].value);
num[i].idx=i;
}
for(int i=;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=;i<=m;i++){
printf("%d\n",ans[i][]-ans[i][]);
}
}
return ;
}

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

  1. UPC 2224 Boring Counting ★(山东省第四届ACM程序设计竞赛 tag:线段树)

    [题意]给定一个长度为N的数列,M个询问区间[L,R]内大于等于A小于等于B的数的个数. [题目链接]http://acm.upc.edu.cn/problem.php?id=2224 省赛的时候脑抽 ...

  2. 主席树——求区间[l,r]不同数字个数的模板(向左密集 D-query)

    主席树的另一种用途,,(还有一种是求区间第k大,区间<=k的个数) 事实上:每个版本的主席树维护了每个值最后出现的位置 这种主席树不是以权值线段树为基础,而是以普通的线段树为下标的 /* 无修改 ...

  3. Codeforces VK Cup 2015 - Qualification Round 1 D. Closest Equals 离线线段树 求区间相同数的最小距离

    D. Closest Equals Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/prob ...

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

    HDU 5861 题意 在n个村庄之间存在n-1段路,令某段路开放一天需要交纳wi的费用,但是每段路只能开放一次,一旦关闭将不再开放.现在给你接下来m天内的计划,在第i天,需要对村庄ai到村庄bi的道 ...

  5. hdu 1754 I Hate It (线段树求区间最值)

    HDU1754 I Hate It Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u D ...

  6. BZOJ 3626 [LNOI2014]LCA 树剖+(离线+线段树 // 在线+主席树)

    BZOJ 4012 [HNOI2015]开店 的弱化版,离线了,而且没有边权(长度). 两种做法 1 树剖+离线+线段树 这道题求的是一个点zzz与[l,r][l,r][l,r]内所有点的lcalca ...

  7. hdu 4288 离线线段树+间隔求和

    Coder Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Su ...

  8. bzoj2333 离线 + 线段树

    https://www.lydsy.com/JudgeOnline/problem.php?id=2333 有N个节点,标号从1到N,这N个节点一开始相互不连通.第i个节点的初始权值为a[i],接下来 ...

  9. HDU 5700 区间交 离线线段树

    区间交 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5700 Description 小A有一个含有n个非负整数的数列与m个区间.每个区间可以表示为 ...

随机推荐

  1. 编译mgiza的准备

    cmake之前需要首先设置环境变量: export BOOST_LIBRARYDIR=$BOOST_ROOT/lib64export BOOST_ROOT=/home/noah/boost_1_57_ ...

  2. C++ json库jsoncpp 吐槽

    Explain 最近在做游戏接入SDK时用到C++的json库jsoncpp,jsoncpp 是一款优秀的json库,但恶心的一点是它采用Assert作为错误处理方法,而assert在linux下通过 ...

  3. 5 给我们的c#程序添加注释

    注释是你的程序中的一个重要部分.在程序中添加注释是用来告诉你和其他人你的程序是做什么用的,你的思路是怎样的.注释可以用你熟悉的中文进行添加. 当你想暂时把你程序中的某些语句去掉的时候,不需要把他们删除 ...

  4. EF6 在原有数据库中使用 CodeFirst 总复习(四、新建实体对象)

    在原有数据库中使用 CodeFirst ,除了第一次添加实体后要立即执行一次 Enable-Migrations add-migration Initial  -IgnoreChanges updat ...

  5. 005--VS C++ 加载位图

    //全局变量 HDC mdc; //--------------------------------------------InitInstance() 函数--------------------- ...

  6. 001--VS2013 c++ 游戏框架

    头文件:MainClass.h 内容: #include <Windows.h> //全局函数声明LRESULT CALLBACK WndProc(HWND hwnd, UINT mess ...

  7. .net 常用正则表达式

    Net中正则表达式的简单使用方法及常见验证判断 判断字符串是只是数字我们可以这样写:return new System.Text.RegularExpressions.Regex(@"^([ ...

  8. android讯飞语音开发常遇到的问题

    场景:android项目中共使用了3个语音组件:在线语音听写.离线语音合成.离线语音识别 11208:遇到这个错误,授权应用失败,先检查装机量(3台测试权限),以及appid的申请时间(35天期限), ...

  9. 【Reorder List】cpp

    题目: Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do ...

  10. Nagios全方位告警接入-电话/微信/短信都支持

    百度告警平台地址: http://gaojing.baidu.com 联系我们: 邮箱:gaojing@baidu.com 电话:13924600771 QQ群:183806029 Nagios接入 ...