update segtree done

This commit is contained in:
Zengtudor 2024-08-09 15:36:41 +08:00
parent 67559577b7
commit e3ca9c7a0d
4 changed files with 79 additions and 1 deletions

6
day7/SegmentTree/2.in Normal file
View File

@ -0,0 +1,6 @@
5
10 11 12 13 14
3
1 5
5 5
4 5

3
day7/SegmentTree/2.out Normal file
View File

@ -0,0 +1,3 @@
60
14
27

View File

@ -0,0 +1,69 @@
#include<bits/stdc++.h>
using namespace std;
#define AS(c){if(!(c)){cerr<<"assert failed: "<<#c<<endl;return -1;}}
void build(int t[],int a[],int s,int e,int n){
if(s==e){
t[n]=a[s];
return;
}
int m=s+((e-s)>>1);
build(t,a,s,m,n*2);
build(t,a,m+1,e,n*2+1);
t[n]=t[n*2]+t[n*2+1];
}
int search(int t[],int s,int e,int n,int l,int r){
if(l<=s&&e<=r){
return t[n];
}
if(e<l||r<s){
return 0;
}
int m = s+((e-s)>>1);
return search(t, s, m, n*2,l, r)+search(t, m+1, e, n*2+1, l, r);
}
#ifdef OITEST
#endif
#ifndef OITEST
#endif
int main(int argc,char *argv[]){
#ifdef OITEST
AS(argc==3)
ifstream ifile(argv[1]);
ifstream afile(argv[2]);
AS(ifile.is_open()==true);
AS(afile.is_open()==true);
stringstream ss;
#define cout ss
#define cin ifile
#endif
#ifndef OITEST
#endif
int n;
cin>>n;
int k=n*4+1;
int t[k];
int arr[n+1];
for(int i=1;i<=n;i++){
cin>>arr[i];
}
build(t, arr, 1, n, 1);
int m;
cin>>m;
for(int i=1;i<=m;i++){
int l,r;
cin>>l>>r;
cout<<search(t, 1, n, 1, l, r)<<endl;
#ifdef OITEST
int myans,cans;
ss>>myans;
afile>>cans;
AS(myans==cans)
#endif
}
}

View File

@ -59,7 +59,7 @@ target("P5431")
target("segtree") target("segtree")
set_rundir("./day7/SegmentTree") set_rundir("./day7/SegmentTree")
add_files("./day7/SegmentTree/*.cpp") add_files("./day7/SegmentTree/*.cpp")
for v=1,1 do for v=1,2 do
local s=tostring(v) local s=tostring(v)
add_tests(s,{files="./day7/SegmentTree/*.cpp",runargs={s..".in",s..".out"},defines="OITEST"}) add_tests(s,{files="./day7/SegmentTree/*.cpp",runargs={s..".in",s..".out"},defines="OITEST"})
end end