社区讨论
求大佬帮忙看看,线段树这样建可以吗
学术版参与者 3已保存回复 6
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 6 条
- 当前快照
- 1 份
- 快照标识符
- @loba4ynw
- 此快照首次捕获于
- 2023/10/29 17:39 2 年前
- 此快照最后确认于
- 2023/11/03 23:36 2 年前
CPP
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<queue>
#include<cmath>
#include<algorithm>
using namespace std;
struct node{
int s;
int l,r;
}tree[100000];
void bulit(int l,int r,int n){
tree[n].l=l;
tree[n].r=r;
if(l==r){
cin >>tree[n].s;
return;
}
bulit(l,(l+r)/2,n*2);
bulit((l+r)/2+1,r,n*2+1);
tree[n].s=tree[n*2].s+tree[n*2+1].s;
}
int n,m;
int main(){
cin >>n>>m;
bulit(1,n,1);
for(int i=1;i<=n*4;i++){
cout <<i<<":["<<tree[i].l<<" "<<tree[i].r<<"]"<<tree[i].s<<endl;
}
return 0;
}
回复
共 6 条回复,欢迎继续交流。
正在加载回复...