蓝书4.1-4.4 树状数组、RMQ问题、线段树、倍增求LCA

这章的数据结构题很真实

T1 排队 bzoj 1699

题目大意:

求静态一些区间的最大值-最小值

思路:

ST表裸题

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstdlib>
 4 #include<cstring>
 5 #include<cmath>
 6 #include<algorithm>
 7 #include<queue>
 8 #include<vector>
 9 #define ll long long
10 #define inf 2139062143
11 #define MAXN 100100
12 using namespace std;
13 inline int read()
14 {
15     int x=0,f=1;char ch=getchar();
16     while(!isdigit(ch)) {if(ch==‘-‘) f=-1;ch=getchar();}
17     while(isdigit(ch)) {x=x*10+ch-‘0‘;ch=getchar();}
18     return x*f;
19 }
20 int n,q,g[MAXN],mn[MAXN][20],mx[MAXN][20];
21 int main()
22 {
23     n=read(),q=read();int a,b,t;
24     for(int i=1;i<=n;i++) mn[i][0]=mx[i][0]=g[i]=read();
25     for(int j=1;j<20;j++)
26         for(int i=1;i+(1<<j)-1<=n;i++)
27             mn[i][j]=min(mn[i][j-1],mn[i+(1<<(j-1))][j-1]),mx[i][j]=max(mx[i][j-1],mx[i+(1<<(j-1))][j-1]);
28     while(q--)
29     {
30         a=read(),b=read(),t=b-a+1,t=(int)log2(t);
31         printf("%d\n",max(mx[a][t],mx[b-(1<<t)+1][t])-min(mn[a][t],mn[b-(1<<t)+1][t]));
32     }
33 }

T2 选择客栈 luogu 1311

题目大意:

每个点有两个值 颜色和最小消费值 如果两个点对之间存在一个点最小消费值<=n

求这样的点对的个数

思路:

把每个颜色分别用链表存起来 记录上一个<=p的位置

对于每个颜色分别遍历

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstdlib>
 4 #include<cstring>
 5 #include<cmath>
 6 #include<algorithm>
 7 #include<queue>
 8 #include<vector>
 9 #define ll long long
10 #define inf 2139062143
11 #define MAXN 200100
12 using namespace std;
13 inline int read()
14 {
15     int x=0,f=1;char ch=getchar();
16     while(!isdigit(ch)) {if(ch==‘-‘) f=-1;ch=getchar();}
17     while(isdigit(ch)) {x=x*10+ch-‘0‘;ch=getchar();}
18     return x*f;
19 }
20 int n,k,p,g[MAXN],c[MAXN],tmp[60],fst[60],las,cnt,res,ans,l[MAXN],to[MAXN];
21 int main()
22 {
23     n=read(),k=read(),p=read();
24     for(int i=1;i<=n;i++)
25     {
26         c[i]=read(),g[i]=read();
27         if(g[i]<=p) las=i;l[i]=las;
28         if(!tmp[c[i]]) fst[c[i]]=i,tmp[c[i]]=i;
29         else to[tmp[c[i]]]=i,tmp[c[i]]=i;
30     }
31     for(int i=0,pos;i<k;i++)
32     {
33         pos=fst[i],cnt=1,res=0;
34         while(to[pos])
35         {
36             if(to[pos]>n) break;
37             if(l[to[pos]]>=pos) res=cnt;
38             ans+=res,cnt++,pos=to[pos];
39         }
40     }
41     printf("%d",ans);
42 }

T3 最大值  bzoj 1012

题解链接

T4 花神游历各国 bzoj 3211

题解链接

T5 维护序列 bzoj 1798

原文地址:https://www.cnblogs.com/yyc-jack-0920/p/9429765.html

时间: 2024-08-02 19:05:49

蓝书4.1-4.4 树状数组、RMQ问题、线段树、倍增求LCA的相关文章

【BZOJ3110】【整体二分+树状数组区间修改/线段树】K大数查询

Description 有N个位置,M个操作.操作有两种,每次操作如果是1 a b c的形式表示在第a个位置到第b个位置,每个位置加入一个数c 如果是2 a b c形式,表示询问从第a个位置到第b个位置,第C大的数是多少. Input 第一行N,M 接下来M行,每行形如1 a b c或2 a b c Output 输出每个询问的结果 Sample Input 2 5 1 1 2 1 1 1 2 2 2 1 1 2 2 1 1 1 2 1 2 3 Sample Output 1 2 1 HINT

[模板]树状数组1/ZKW线段树

https://www.luogu.org/problemnew/show/P3374 1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4 #define rson (o<<1|1) 5 #define lson (o<<1) 6 const int N = 530000<<1;//zkw线段树只能查询(0,bit),所以只有bit-2个叶节点,bit-2<n

【刷题】【数据结构】【树状数组】【线段树】

1>数星星 (复制自他人博客) 由于题目中给的数据是按y轴排序,我们只需构建x轴的树状数组,也就是说我们只需统计星星i之前一共有多少个x坐标小于或等于Xi的星星,这个数值也就是星星i的等级 又因为树状数组无法处理下标为0的元素(会死循环),所以要把每个x坐标+1 #include<cstdio> #include<cstdlib> #include<algorithm> using namespace std; int max(int a,int b) { ret

[树状数组][权值线段树] Codeforces 1093E Intersection of Permutations

题目描述 You are given two permutations aa and bb , both consisting of nn elements. Permutation of nn elements is such a integer sequence that each value from 11 to nn appears exactly once in it. You are asked to perform two types of queries with them: 1

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

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

poj 1195 二维树状数组 及二维树状数组模板

http://poj.org/problem?id=1195 求矩阵和的时候,下标弄错WA了一次... 求矩形(x1,y1) (x2,y2)的sum |sum=sum(x2,y2)-sum(x1-1,y2)-sum(x2,y1-1)+sum(x1-1,y1-1) 二维树状数组讲解:http://blog.csdn.net/u011026968/article/details/38532117 二维树状数组模板: /*========================================

HDU 4325 离散化+树状数组 或者 不使用树状数组

题意:给出一些花的开放时间段,然后询问某个时间点有几朵花正在开放. 由于ti<1e9,我们需要先将时间离散化,然后将时间点抽象为一个数组中的点,显然,我们需要进行区间更新和单点查询,可以考虑线段树与树状数组两种做法,一般的,树状数组是用来维护区间和与单点修改的,那么,如何通过树状数组进行区间更新和单点查询呢,联想到差分数组,差分数组可以在o(1)的时间内进行区间的更新,但是单点查询的效率为o(n),显然不能用于处理此题,这时,考虑树状数组维护差分数组,就可以o(logn)地进行区间更新(更新差分

【算法#3】树状数组&amp;二叉索引树

其实是数据结构. 智推连续几天给我推树状数组的模板,还放在第一位-- 对着蓝书的图看了好几天才看懂,树状数组的另外一个名字是二叉索引树,指通过把一个数组抽象的变形成树状的以求得到树形数据结构的效果.有人说是线段树的阉割版,我不太清楚,树状数组应该是不支持区间修改加速的. 首先我们需要理解lowbit的概念,它指的是一个数转成二进制后位数最低的那个1表示的值.它具有一个特殊的性质但是为什么具有这个性质是无须证明也不用了解的. 然后我们画一个图,在一定范围内按lowbit的大小从上向下逐层分布,一层

未解决的树状数组差分(改变区间,求单点值)

// luogu-judger-enable-o2 #include <bits/stdc++.h> #define inf 500001 #define lll long long int using namespace std; long c[inf],a,n,m,k,xx,nn; int lowbit(int x){ return x&(-x);//求X二进制下从右到左第一个1的所对应的十进制下的值 } void update(int x,int y){//update添加修改之

信奥一本通-树状数组模版题目-修改数列元素+求子数列元素和

给定n个数列,规定有两种操作,一是修改某个元素,二是求子数列[a,b]的连续和.数列的元素个数最多10万个,询问操作最多10万次. 输入 第一行2个整数n,m(n表示输入n个数,m表示有m个操作) 第二行输入n个数列. 接下来m行,每行有三个数k,a,b(k=0表示求子数列[a,b]的和:k=1表示第a个数加b). 输出 输出若干行数字,表示k=0时,对应的子数列[a,b]的连续和. 样例输入 10 5 1 2 3 4 5 6 7 8 9 10 1 1 5 0 1 3 0 4 8 1 7 5 0