HDU4417 (Super Mario)

题目链接:传送门

题目大意:一个大小为 n 的数组,m组询问,每组询问[x,y]内<=v的数的数量。

题目思路:主席树(注意询问时数组下标越界问题)

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <cstring>
 7 #include <stack>
 8 #include <cctype>
 9 #include <queue>
10 #include <string>
11 #include <vector>
12 #include <set>
13 #include <map>
14 #include <climits>
15 #define lson rt<<1,l,mid
16 #define rson rt<<1|1,mid+1,r
17 #define fi first
18 #define se second
19 #define ping(x,y) ((x-y)*(x-y))
20 #define mst(x,y) memset(x,y,sizeof(x))
21 #define mcp(x,y) memcpy(x,y,sizeof(y))
22 using namespace std;
23 #define gamma 0.5772156649015328606065120
24 #define MOD 1000000007
25 #define inf 0x3f3f3f3f
26 #define N 100005
27 #define maxn 30010
28 typedef pair<int,int> PII;
29 typedef long long LL;
30 LL read(){
31     LL x=0,f=1;char ch=getchar();
32     while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
33     while(ch>=‘0‘&&ch<=‘9‘){x=(x<<3)+(x<<1)+ch-‘0‘;ch=getchar();}
34     return x*f;
35 }
36
37 int n,m,flag,L,R,sz;
38 struct Seg{int l,r,v;}seg[N*25];
39 int a[N],b[N],root[N];
40 void init(){
41     root[0]=sz=0;
42     mst(seg,0);
43 }
44 void update(int &rot,int rt,int l,int r){
45     seg[++sz]=seg[rot],rot=sz;
46     ++seg[rot].v;
47     if(l==r)return;
48     int mid=l+r>>1;
49     if(L<=mid)update(seg[rot].l,lson);
50     else update(seg[rot].r,rson);
51 }
52 int query(int L,int R,int rt,int l,int r,int v){
53     if(l==r)return seg[R].v-seg[L].v;
54     int mid=l+r>>1,temp=0;
55     if(v<=mid)temp+=query(seg[L].l,seg[R].l,lson,v);
56     else{
57         temp+=seg[seg[R].l].v-seg[seg[L].l].v;
58         temp+=query(seg[L].r,seg[R].r,rson,v);
59     }
60     return temp;
61 }
62 int main(){
63     int i,j,group,x,y,v,Case=0;
64     group=read();
65     while(group--){
66         init();
67         n=read(),m=read();
68         for(i=1;i<=n;++i)a[i]=read(),b[i]=a[i];
69         sort(b+1,b+n+1);int _n=unique(b+1,b+n+1)-b-1;
70         for(i=1;i<=n;++i){
71             L=lower_bound(b+1,b+_n+1,a[i])-b;
72             update(root[i]=root[i-1],1,1,_n);
73         }
74         printf("Case %d:\n",++Case);
75         while(m--){
76             x=read(),y=read(),v=read();
77             ++x,++y;
78             L=root[x-1],R=root[y];
79             v=upper_bound(b+1,b+_n+1,v)-b-1;
80             if(v) printf("%d\n",query(L,R,1,1,_n,v));
81             else printf("0\n");
82         }
83     }
84     return 0;
85 }
时间: 2025-01-12 05:26:35

HDU4417 (Super Mario)的相关文章

超级搜索(Super search)

插件介绍: 现在的搜索引擎会极大的帮助用户搜索到想要的搜索的内容,我们常用的搜索引擎包括百度.搜狗.360搜索等等,今天就为大家推荐一个超级搜索的插件.超级搜索基于浏览器的全面搜索.智能识别搜索关键字,集成收藏夹(书签)搜索,历史记录搜索等功能.支持自定义扩展搜索,支持打开搜索结果列表等功能. 使用说明: 在谷歌应用商店里安装超级搜索(Super search),并在扩展器里启动它,点击chromeos右上角的超级搜索插件,该插件会弹出一个搜索的下拉框,在下拉框的文本框中输入想要搜索的内容,并在

文件相似性判断 -- 超级特征值(super feature)

基于内容的变长分块(CDC)技术,可以用来对文件进行变长分块,而后用来进行重复性检测,广泛用于去重系统中.后来又出现了对相似数据块进行delta压缩,进一步节省存储开销.所以就需要一种高效的相似性检测算法,在论文 WAN Optimized Replication of Backup Datasets Using Stream-Informed Delta Compression 提出的super-features 算法具有很好的效果.主要思想是在滑动窗口进行分块的过程中,通过一个窗口的rabi

《OpenGL 超级宝典(Super Bible)第五版》 有关 PBO 的 Example

代码即关键注释如下: static GLfloat vGreen[] = { 0.0f, 1.0f, 0.0f, 1.0f }; static GLfloat vWhite[] = { 1.0f, 1.0f, 1.0f, 1.0f }; static GLfloat vLightPos[] = { 0.0f, 3.0f, 0.0f, 1.0f }; GLsizei screenWidth; // Desired window or desktop width GLsizei screenHeig

【hdu4417】Super Mario——主席树

题目链接 题目大意为给定一个长度为n的区间,同时给出m个询问,每次询问在区间[l,r]中有多少个数小于或等于k. 同样考虑用主席树来维护,每次只需要找到序列b中第一个等于k的数,那么要求的数必定在b[1]~b[upper_bound(k)]这个范围内,接下来就像线段树统计区间个数那样,若完全包含则直接加上e[rr].sum-e[ll].sum,否则就分两边递归统计.而建树什么的就直接套模板即可. 还要注意一点,原题的区间默认从0开始,因此若像我一样写了区间从1开始的记得在query之前将l和r加

权限管理与切换(二十一)

权限管理与切换:sudo,id,chmod,chown,chgrp,setfacl,getfacl,umask 21.1.su 功能:变更用户身份(super user) 切换用户时: 切换用户时: (1) 不读取目标用户的配置文件(非登录式切换,半切换): su  USERNAME (2) 读取目标用户的配置文件(登录式切换,完全切换): su  -l  USERNAME su  -  USERNAME 注意:root切换至任何其它用户无须认证密码:普通用户切换至其它用户,都需要密码: 21.

清晰架构(Clean Architecture)的Go微服务: 依赖注入(Dependency Injection)

在清晰架构(Clean Architecture)中,应用程序的每一层(用例,数据服务和域模型)仅依赖于其他层的接口而不是具体类型. 在运行时,程序容器1负责创建具体类型并将它们注入到每个函数中,它使用的技术称为依赖注入2. 以下是要求. 容器包的依赖关系: 容器包是唯一依赖于具体类型和许多外部库的包,因为它需要创建具体类型. 本程序中的所有其他软件包主要仅依赖于接口. 外部库可以包括DB和DB连接,gRPC连接,HTTP连接,SMTP服务器,MQ等. #2中提到的具体类型的资源链接只需要创建一

Super Mario(线段树离线区间k值)

以前见过这题,没做出来,知道是离线处理,这次仔细想了下, 首先把出现的高度都map离散化一下,以离散化出来的数目g建树,把每个位置都开俩个vector,一个存以这个位置为L的询问,一个存以这个位置为R的询问. 然后从1-g 进行更新,假如当前i是以第j个区间的开始位置,那么这时就可以询问一下<=p[j].h的个数s,显然这时第J个区间多加的,需要减掉,p[j].sum-=s; 然后更新第i个数,update(a[i],1,g,1);再找到某第k区间是以i结尾的,那么依旧询问一下,得出s,p[k]

HDU 4417 Super Mario(主席树求区间内的区间查询+离散化)

Super Mario Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5101    Accepted Submission(s): 2339 Problem Description Mario is world-famous plumber. His “burly” figure and amazing jumping abilit

zoj 1232 Adventure of Super Mario (Floyd+dp)

Adventure of Super Mario Time Limit: 2 Seconds      Memory Limit: 65536 KB After rescuing the beautiful princess, Super Mario needs to find a way home -- with the princess of course :-) He's very familiar with the 'Super Mario World', so he doesn't n