码迷,mamicode.com
首页 > 其他好文 > 详细

Bzoj3211: 花神游历各国

时间:2018-02-23 13:33:16      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:def   argc   c++   []   class   space   ons   return   odi   

题面

Bzoj

Sol

暴力开根,一个数开根到小于等于\(1\)就不用管了,维护区间\(max\)\(max<=1\)就不用管这个区间,线段树

# include <bits/stdc++.h>
# define RG register
# define IL inline
# define Fill(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long ll;
const int _(1e5 + 5);

IL int Input(){
    RG int x = 0, z = 1; RG char c = getchar();
    for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
    for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
    return x * z;
}

int n, m;
ll mx[_ << 2], sum[_ << 2];

IL void Build(RG int x, RG int l, RG int r){
    if(l == r){
        mx[x] = sum[x] = Input();
        return;
    }
    RG int mid = (l + r) >> 1, ls = x << 1, rs = x << 1 | 1;
    Build(ls, l, mid), Build(rs, mid + 1, r);
    mx[x] = max(mx[ls], mx[rs]), sum[x] = sum[ls] + sum[rs];
}

IL void Modify(RG int x, RG int l, RG int r, RG int L, RG int R){
    if(mx[x] <= 1) return;
    if(l == r){
        sum[x] = mx[x] = sqrt(mx[x]);
        return;
    }
    RG int mid = (l + r) >> 1, ls = x << 1, rs = x << 1 | 1;
    if(L <= mid) Modify(ls, l, mid, L, R);
    if(R > mid) Modify(rs, mid + 1, r, L, R);
    mx[x] = max(mx[ls], mx[rs]), sum[x] = sum[ls] + sum[rs];
}

IL ll Query(RG int x, RG int l, RG int r, RG int L, RG int R){
    if(L <= l && R >= r) return sum[x];
    RG int mid = (l + r) >> 1; RG ll ret = 0;
    if(L <= mid) ret = Query(x << 1, l, mid, L, R);
    if(R > mid) ret += Query(x << 1 | 1, mid + 1, r, L, R);
    return ret;
}

int main(RG int argc, RG char* argv[]){
    n = Input(), Build(1, 1, n), m = Input();
    for(RG int i = 1; i <= m; ++i){
        RG int k = Input(), l = Input(), r = Input();
        if(k == 1) printf("%lld\n", Query(1, 1, n, l, r));
        else Modify(1, 1, n, l, r);
    }
    return 0;
}

Bzoj3211: 花神游历各国

标签:def   argc   c++   []   class   space   ons   return   odi   

原文地址:https://www.cnblogs.com/cjoieryl/p/8461435.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!