HDU 4417.Super Mario-无修改区间小于等于H的数的个数-可持久化线段树

Super Mario

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 9618    Accepted Submission(s): 4074

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 line (the length is n), on every integer point i there is a brick on height hi. Now the question is how many bricks in [L, R] Mario can hit if the maximal height he can jump is H.

Input

The first line follows an integer T, the number of test data.
For each test data:
The first line contains two integers n, m (1 <= n <=10^5, 1 <= m <= 10^5), n is the length of the road, m is the number of queries.
Next line contains n integers, the height of each brick, the range is [0, 1000000000].
Next m lines, each line contains three integers L, R,H.( 0 <= L <= R < n 0 <= H <= 1000000000.)

Output

For each case, output "Case X: " (X is the case number starting from 1) followed by m lines, each line contains an integer. The ith integer is the number of bricks Mario can hit for the ith query.

Sample Input

1

10 10

0 5 2 7 5 4 3 8 7 7

2 8 6

3 5 0

1 3 1

1 9 4

0 1 0

3 5 5

5 5 1

4 6 3

1 5 7

5 7 3

Sample Output

Case 1:

4

0

0

3

1

2

0

1

5

1

Source

2012 ACM/ICPC Asia Regional Hangzhou Online

题意就是求区间小于等于H的数的个数。

这道题写的时间有点长,错在这几个地方:

(1)因为数是从0开始的,所以查询的时候,区间l[i]和r[i]应该+1,查询的时候就l[i]-1,r[i],或者区间l[i]和r[i]不变化,查询的时候,就l[i],r[i]+1。

(2)因为查询的是小于等于H的数,所以H也应该离散化,找对应的数,而不是直接查询,这里错了好久才发现。

(3)数组开小了,杭电这道题数组开小了报的是WA,把maxn=1e5+10改成2e5+10就过了。

还有一点,其实是自己脑子不好特意测了一下。

因为数据已经离散化处理过了,所以查询的时候不会有重复的数,所以查询的时候,lower_bound()和upper_bound()都可以。

//int cnt=lower_bound(b+1,b+1+d,h[i])-b;
  int cnt=upper_bound(b+1,b+1+d,h[i])-b-1;

以上两种都是对的。

代码:

  1 //无修改区间-可持久化线段树(权值线段树+可持久化)
  2 #include<iostream>
  3 #include<cstdio>
  4 #include<cstring>
  5 #include<algorithm>
  6 #include<bitset>
  7 #include<cassert>
  8 #include<cctype>
  9 #include<cmath>
 10 #include<cstdlib>
 11 #include<ctime>
 12 #include<deque>
 13 #include<iomanip>
 14 #include<list>
 15 #include<map>
 16 #include<queue>
 17 #include<set>
 18 #include<stack>
 19 #include<vector>
 20 using namespace std;
 21 typedef long long ll;
 22 typedef pair<int,int> pii;
 23
 24 const double PI=acos(-1.0);
 25 const double eps=1e-6;
 26 const ll mod=1e9+7;
 27 const int inf=0x3f3f3f3f;
 28 const int maxn=2e5+10;
 29 const int maxm=100+10;
 30 #define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
 31 #define lson l,m
 32 #define rson m+1,r
 33
 34 int a[maxn],b[maxn],sum[maxn<<5],ls[maxn<<5],rs[maxn<<5];//sum线段树里保存的值,L左儿子,R右儿子
 35 int n,m,sz=0;
 36
 37 void build(int &rt,int l,int r)//建棵空树
 38 {
 39     rt=++sz;sum[rt]=0;//动态开点,初始值为0,空树
 40     if(l==r){
 41         return ;
 42     }
 43
 44     int m=(l+r)>>1;
 45     build(ls[rt],lson);
 46     build(rs[rt],rson);
 47 }
 48
 49 void update(int pre,int &rt,int l,int r,int p,int c)
 50 {
 51     rt=++sz;sum[rt]=sum[pre]+c;//插入序列,首先继承以前的线段树 然后直接单点+1就可以
 52     ls[rt]=ls[pre];rs[rt]=rs[pre];
 53     if(l==r){
 54         return ;
 55     }
 56
 57     int m=(l+r)>>1;
 58     if(p<=m) update(ls[pre],ls[rt],lson,p,c);//因为右边不需要更新,所以覆盖掉左边
 59     else     update(rs[pre],rs[rt],rson,p,c);
 60     //sum[rt]=sum[ls[rt]]+sum[rs[rt]];
 61 }
 62
 63 int query(int pre,int rt,int L,int R,int l,int r)//查询l到r区间就是第r次插入减去第l-1次插入后的线段树的样子
 64 {
 65     if(L>R) return 0;
 66     if(L<=l&&r<=R){
 67         return sum[rt]-sum[pre];
 68     }
 69
 70     int ret=0;
 71     int m=(l+r)>>1;
 72     if(L<=m) ret+=query(ls[pre],ls[rt],L,R,lson);
 73     if(R> m) ret+=query(rs[pre],rs[rt],L,R,rson);
 74     return ret;
 75 }
 76
 77 int rt[maxn],l[maxn],r[maxn],h[maxn];
 78
 79 int main()
 80 {
 81     int t;
 82     scanf("%d",&t);
 83     for(int cas=1;cas<=t;cas++){
 84         scanf("%d%d",&n,&m);
 85         sz=0;
 86         for(int i=1;i<=n;i++)
 87         {
 88             scanf("%d",&a[i]);
 89             b[i]=a[i];
 90         }
 91         for(int i=1;i<=m;i++){
 92             scanf("%d%d%d",&l[i],&r[i],&h[i]);
 93             b[i+n]=h[i];
 94             l[i]++,r[i]++;
 95         }
 96         sort(b+1,b+1+n+m);//首先把值全部排序去重,用于建权值线段树,权值线段树保存的内容是值的数量。
 97         int d=unique(b+1,b+1+n+m)-(b+1);
 98         build(rt[0],1,d);
 99         for(int i=1;i<=n;i++) //按照序列顺序插入值
100         {
101             int p=lower_bound(b+1,b+1+d,a[i])-b;
102             update(rt[i-1],rt[i],1,d,p,1);
103         }
104         printf("Case %d:\n",cas);
105         for(int i=1;i<=m;i++)
106         {
107             //int L=1,R=upper_bound(b+1,b+1+d,h)-b-1;
108             //int cnt=lower_bound(b+1,b+1+d,h[i])-b;
109             int cnt=upper_bound(b+1,b+1+d,h[i])-b-1;
110             //printf("%d\n",query(rt[l[i]],rt[r[i]+1],1,cnt,1,d));
111             //printf("%d\n",query(rt[l],rt[r+1],1,cnt,1,d));
112             printf("%d\n",query(rt[l[i]-1],rt[r[i]],1,cnt,1,d));
113         }
114     }
115     return 0;
116 }

菜的难受。。。

原文地址:https://www.cnblogs.com/ZERO-/p/9807491.html

时间: 2024-10-14 12:06:38

HDU 4417.Super Mario-无修改区间小于等于H的数的个数-可持久化线段树的相关文章

HDU 4417 Super Mario ( 超级马里奥 + 主席树 + 线段树/树状数组离线处理 + 划分树 )

HDU 4417 - Super Mario ( 主席树 + 线段树/树状数组离线处理 + 划分树 ) 这道题有很多种做法,我先学习的是主席树.后面陆续补上线段树离线和划分树 题目大意就是给定一个区间给定一个数列,每次要求你查询区间[L,R]内不超过K的数的数量 主席树做法: 最基本的是静态第k大,这里是求静态的 <= K,差不多,在Query操作里面需要修改修改 先建立size棵主席树,然后询问的时候统计的是 第R棵主席树中[1,K]的数量 - 第L-1棵主席树中[1,K]的数量 注意这里下标

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

Super Mario Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Description Mario is world-famous plumber. His “burly” figure and amazing jumping ability reminded in our memory. Now the poor princess is in trouble agai

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

hdu 4417 Super Mario/树套树

原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4417 题意很简单,给定一个序列求一个区间 [L, R,]中小于等于H的元素的个数. 好像函数式线段树可解吧,可弱弱的沙茶一直没弄懂其精髓,只好用树套树暴力碾压了 额树套树,线段树的每一个节点套一个sb树. 当查询[l,r]区间中的值小于等于H的个数,先用线段树找到相应的区间, 然后再查询该区间下对应的平衡树中小于等于H的个数,累加即可. 一直以为会超时,结果400+ms就过了,数据应该很弱吧(自己对

HDU 4417 Super Mario (划分树)(二分)

Super Mario Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 6077    Accepted Submission(s): 2645 Problem Description Mario is world-famous plumber. His "burly" figure and amazing jumping ab

HDU 4417 Super Mario 主席树查询区间小于某个值的个数

#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 #d

[HDU 4417] Super Mario (树状数组)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4417 题目大意:给你n个数,下标为0到n-1,m个查询,问查询区间[l,r]之间小于等于x的数有多少个. 写的时候逗比了...还是写的太少了.. 我们按照x从小到大排序来查询,然后找区间上的点,如果小于等于它就插入,然后看这个区间内插入了多少个点. 点也是可以排序的.. 详见代码: 1 #include <cstdio> 2 #include <cmath> 3 #include &l

hdu 4417 Super Mario(离线树状数组|划分树)

Super Mario Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2584    Accepted Submission(s): 1252 Problem Description Mario is world-famous plumber. His "burly" figure and amazing jumping a

HDU 4417 Super Mario(划分树问题求不大于k的数有多少)

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