BUAA 173 区间求和线段树 裸题

传送门 : 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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值