当前位置:网站首页>【刷题笔记】线段树
【刷题笔记】线段树
2022-06-12 19:21:00 【Ctrl AC】
· 区间查询
query函数:
当线段树当前结点范围被要查询范围包围时直接返回。
否则判断左右子树的遍历方向。测试数据不会出现越界的情况,不需考虑。
配套题目:click
#include<bits/stdc++.h>
using namespace std;
int a[100010],treea[400010],treeb[400010],ma,mi;
void build(int p,int l,int r)
{
if(l==r)
{
treea[p]=treeb[p]=a[l];
return;
}
int mid=(l+r)/2;
build(2*p,l,mid);
build(2*p+1,mid+1,r);
treea[p]=max(treea[2*p],treea[2*p+1]);
treeb[p]=min(treeb[2*p],treeb[2*p+1]);
}
void search(int p,int l,int r,int x,int y)
{
if(x<=l&&r<=y)
{
ma=max(ma,treea[p]);
mi=min(mi,treeb[p]);
return;
}
int mid=(l+r)/2;
if(y<=mid) search(2*p,l,mid,x,y);
else if(x>mid) search(2*p+1,mid+1,r,x,y);
else{
search(2*p,l,mid,x,y);
search(2*p+1,mid+1,r,x,y);
}
}
int main()
{
int n,q; cin>>n>>q;
for(int i=1;i<=n;i++) cin>>a[i];
build(1,1,n);
while(q--)
{
ma=0, mi=0x3f3f3f3f;
int x,y; cin>>x>>y;
search(1,1,n,x,y);
cout<<ma-mi<<endl;
}
return 0;
}
· 分步建树
将数组长度作为分界线进行左右子树建立,当l==r时,就是最新元素的位置
配套题目:click
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN=2e5+5;
ll tree[4*MAXN],t;
void update(int node,int nodeleft,int noderight,int len,ll val){
if(nodeleft==noderight){
tree[node]=val; return;
}
int mid=(nodeleft+noderight)>>1;
if(mid>=len) update(node*2,nodeleft,mid,len,val);
else update(node*2+1,mid+1,noderight,len,val);
tree[node]=max(tree[node*2],tree[node*2+1]);
}
ll find(int node,int nodeleft,int noderight,int l,int r){
if(l<=nodeleft&&noderight<=r){
return tree[node];
}
int mid=(nodeleft+noderight)>>1;
if(r<=mid) return find(node*2,nodeleft,mid,l,r);
else if(l>mid) return find(node*2+1,mid+1,noderight,l,r);
else{
return max(find(node*2,nodeleft,mid,l,r),find(node*2+1,mid+1,noderight,l,r));
}
}
int main(){
int n,d; cin>>n>>d;
int len=0;
for(int i=0;i<n;i++){
char ch; int a;
cin>>ch>>a;
if(ch=='A'){
len++;
update(1,1,MAXN-1,len,(a+t)%d);
}
else{
t=find(1,1,MAXN-1,len-a+1,MAXN-1);
cout<<t<<endl;
}
}
return 0;
}
· 区间查询与更改模板
题目:click
#include<iostream>
#define int long long
using namespace std;
typedef long long ll;
const int N = 100005;
int w[N];
struct node {
int l, r;
ll sum;
ll add;
}tree[N<<2];
void build(int node,int nodeleft,int noderight) {
if (nodeleft == noderight) tree[node] = { nodeleft,noderight,w[nodeleft],0 };
else {
tree[node] = { nodeleft,noderight };
int mid = (nodeleft + noderight) / 2;
build(node * 2, nodeleft, mid);
build(node * 2 + 1, mid + 1, noderight);
tree[node].sum = tree[node * 2].sum + tree[node * 2 + 1].sum;
}
}
void pushdown(int node) {
struct node& root = tree[node], & left = tree[node * 2], & right = tree[node * 2 + 1];
if (root.add) {
left.add += root.add; left.sum += (left.r - left.l + 1) * root.add;
right.add += root.add; right.sum += (right.r - right.l + 1) * root.add;
root.add = 0;
}
}
void update(int node, int left, int right, int val) {
if (left <= tree[node].l && tree[node].r <= right) {
tree[node].sum += (tree[node].r - tree[node].l + 1) * val;
tree[node].add += val;
}
else {
pushdown(node);
int mid = (tree[node].l + tree[node].r) / 2;
if (left <= mid) update(node * 2, left, right, val);
if (right > mid) update(node * 2 + 1, left, right, val);
tree[node].sum = tree[node * 2].sum + tree[node * 2 + 1].sum;
}
}
int query(int node, int left, int right) {
if (left <= tree[node].l && tree[node].r <= right) return tree[node].sum;
pushdown(node);
int mid = (tree[node].l + tree[node].r) / 2;
if (right <= mid) return query(node * 2, left, right);
else if (left > mid) return query(node * 2 + 1, left, right);
else {
return query(node * 2, left, right) + query(node * 2 + 1, left, right);
}
}
signed main() {
int n, m; cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> w[i];
build(1, 1, n);
while (m--) {
char c; cin >> c;
if (c == 'C') {
int l, r, val; cin >> l >> r >> val;
update(1, l, r, val);
}
else {
int l, r; cin >> l >> r;
cout << query(1, l, r) << endl;
}
}
return 0;
}
· 单点修改区间查询模板
题目:click
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int num[N];
struct node {
int l, r;
int MAX;
}tree[N<<2];
void build(int node, int nodeleft, int noderight) {
if (nodeleft == noderight) tree[node] = { nodeleft,noderight,num[nodeleft] };
else {
tree[node] = { nodeleft,noderight };
int mid = (nodeleft + noderight) / 2;
build(node * 2, nodeleft, mid);
build(node * 2 + 1, mid + 1, noderight);
tree[node].MAX = max(tree[node * 2].MAX, tree[node * 2 + 1].MAX);
}
}
void update(int node, int id, int val) {
if (tree[node].l == id && tree[node].r == id) {
tree[node].MAX = val;
}
else {
int mid = (tree[node].l + tree[node].r) / 2;
if (id <= mid) update(node * 2, id, val);
else update(node * 2 + 1, id, val);
tree[node].MAX = max(tree[node * 2].MAX, tree[node * 2 + 1].MAX);
}
}
int query(int node, int left, int right) {
if (left <= tree[node].l && tree[node].r <= right) return tree[node].MAX;
else {
int mid = (tree[node].l + tree[node].r) / 2;
if (right <= mid) return query(node * 2, left, right);
else if (left > mid) return query(node * 2 + 1, left, right);
else {
return max(query(node * 2, left, right), query(node * 2 + 1, left, right));
}
}
}
int main() {
int n, m;
while (cin >> n >> m) {
for (int i = 1; i <= n; i++) cin >> num[i];
build(1, 1, n);
while (m--) {
char c; cin >> c;
int a, b; cin >> a >> b;
if (c == 'Q') {
cout << query(1, a, b) << endl;
}
else {
update(1, a, b);
}
}
}
return 0;
}
边栏推荐
- asp. Net using JSON to interact with API data
- YOLOX网络结构详解
- 攻防世界(web篇)---supersqli
- Super heavy! Apache Hudi multimode index optimizes queries up to 30 times
- review.js ppt数学公式无法显示
- Native servlet - upload & download of files
- The practice of machine learning in meituan distribution system: restoring the real world with technology - Notes
- 被八股文害惨了。。。
- VC Hacon Joint Programming genimage3extern writeimage
- Redis (XXXII) - using redis as a distributed lock
猜你喜欢
ISCC2022
Méthode de sauvegarde programmée basée sur la base de données distribuée elle - même
no available service ‘null‘ found, please make sure registry config correct
Wincc7.5 SP1 method for adjusting picture size to display resolution
嵌入式开发:固件工程师的6项必备技能
Rhca memoirs -- Introduction to cl280
Start with no place to live
io.seata.common.exception.FrameworkException: can not connect to services-server.
How to download Vega in China
超级重磅!Apache Hudi多模索引对查询优化高达30倍
随机推荐
leetcode:6095. Strong password verifier II [simple simulation + direct false]
Implementation of VGA protocol based on FPGA
tarfile解压嵌套tar
How to download proxystrike in China
Daily blog - micro service permission 12 matters
Cookie & session & kaptcha verification code
Dacom G150双模耳机,为爱发声,呵护孩子听力健康成长
Can't understand kotlin source code? Starting with the contracts function~
Leetcode 474. 一和零
Shell 数组和函数
What did 3GPP ran do in the first F2F meeting?
no available service ‘null‘ found, please make sure registry config correct
Analysis report on market demand and investment strategy of China's re guarantee industry 2022-2028
[0008] unordered list
Leetcodesql: count the number of students in each major
Details of thansmitablethreadlocal
Wincc7.5 SP1 method for adjusting picture size to display resolution
io. seata. common. exception. FrameworkException: can not connect to services-server.
超级重磅!Apache Hudi多模索引对查询优化高达30倍
asp. Net using JSON to interact with API data