bdfz_2024_summer/day7/SegmentTree/SegmentTree.cpp.old
2024-08-09 14:26:48 +08:00

68 lines
1.5 KiB
C++

#include<bits/stdc++.h>
using namespace std;
// #define ASSERTM(c,m){if(!(c)){cerr<<"assert failed :"<<#c<<" "<<m<<endl;return -1;}}
#define ASSERT(c){if(!(c)){cerr<<"assert failed :"<<#c<<endl;return -1;}}
// #define PRINT_VALUE(v){cout<<#v<<" :"<<(v)<<endl;}
#define PRINT_VALUE(v){(v);}
const int MAX_N = 1e4+5;
const int LEN = 4*MAX_N;
int tree[LEN];
void build(int t[],int a[],int s,int e,int n){
if(s==e){
// PRINT_VALUE(n)
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 query(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),sum=0;
sum+=query(t, s, m, n*2, l, r);
sum+=query(t, m+1, e, n*2+1, l, r);
return sum;
}
int main(int argc,char *argv[]){
ASSERT(argc==3);
ifstream ifile(argv[1]);
ifstream afile(argv[2]);
ASSERT(ifile.is_open()==true)
ASSERT(afile.is_open()==true)
int n;
ifile>>n;
int a[n+1];
int t[n*4+1];
for(int i=1;i<=n;i++){
ifile>>a[i];
}
build(t,a,1,n,1);
// for(int i=1;i<=n*4;i++){
// PRINT_VALUE(i);
// PRINT_VALUE(t[i]);
// }
int m;
ifile>>m;
for(int i=1;i<=m;i++){
int cans;
afile>>cans;
int a,b;
ifile>>a>>b;
int myans;
// myans=query(t,1,n,1,a,b);
PRINT_VALUE((myans=query(t,1,n,1,a,b),myans));
ASSERT(cans==myans)
}
}