传送门 : BUAA 173
期末了, 要复习, 所以刷水题防手生
ps :BUAA自动生成的头注释不错, 打算写个脚本
AC code
/*
Author: YY & 8023
Result: AC Submission_id: 228165
Created at: Tue Dec 20 2016 22:59:18 GMT+0800 (CST)
Problem_id: 173 Time: 1734 Memory: 18240
*/
#include<iostream>
#include<cstring>
using namespace std;
const int N = 1000000 + 5;
int sum[N << 2];
int n, M, m;
void pushUp(int x){
sum[x] = sum[x << 1] + sum[x << 1 | 1];
}
void build(){
memset(sum, 0, sizeof(sum));
for(M = 1; M <= n + 1; M <<= 1);
for(int i = 1; i <= n; ++i) cin >> sum[i + M];
for(int i = M - 1; i; --i){
pushUp(i);
}
}
int query(){
int l, r;
cin >> l >> r;
l += M - 1;
r += M + 1;
int res = 0;
while(r ^ l ^ 1){
if(~l & 1) res += sum[l + 1];
if(r & 1) res += sum[r - 1];
r >>= 1;
l >>= 1;
}
return res;
}
int main(){
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif
while(cin >> n >> m){
build();
while(m--) cout << query() << endl;
}
return 0;
}