bzoj 3211 分类: bzoj 2015-08-07 18:00 8人阅读 评论(0) 收藏


分析一下,每个数最多只会做5次开根操作就会小于或等于1,在这之后它的值就不会改变。。。

用线段树记录区间的最大值,然后只对max > 1 的区间做开根操作即可。

时间复杂度:O(MlogN+N)


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <vector>
#include <utility>
#include <stack>
#include <queue>
#include <iostream>
#include <algorithm>

template<class Num>void read(Num &x)
{
    char c; int flag = 1;
    while((c = getchar()) < '0' || c > '9')
        if(c == '-') flag *= -1;
    x = c - '0';
    while((c = getchar()) >= '0' && c <= '9')
        x = (x<<3) + (x<<1) + (c-'0');
    x *= flag;
    return;
}
template<class Num>void write(Num x)
{
    if(x < 0) putchar('-'), x = -x;
    static char s[20];int sl = 0;
    while(x) s[sl++] = x%10 + '0',x /= 10;
    if(!sl) {putchar('0');return;}
    while(sl) putchar(s[--sl]);
}
const int maxn = 1e5 + 5;

int n , m, a[maxn];

int max[maxn<<2];
long long sum[maxn<<2];

#define L(x)  (x<<1)
#define R(x)  ((x<<1)|1)
long long query(int l,int r,int ll,int rr,int si)
{
    if(l == ll && r == rr)
        return sum[si];
    else
    {
        int mid = (ll+rr)>>1;

        if(r <= mid)    
            return query(l,r,ll,mid,L(si));
        else if(l > mid)
            return query(l,r,mid+1,rr,R(si));
        else
            return query(l, mid,ll,mid,L(si)) + query(mid+1,r,mid+1,rr,R(si));
    }
}
void update(int x)
{
    sum[x] = sum[L(x)] + sum[R(x)];
    max[x] = std::max(max[L(x)], max[R(x)]);
}
void build(int ll,int rr,int si)
{
    if(ll == rr)
        sum[si] = max[si] = a[ll];
    else
    {
        int mid = (ll+rr)>>1;

        build(ll,mid,L(si));

        build(mid+1,rr,R(si));

        update(si);
    }
}
void change(int l,int r,int ll,int rr,int si)
{   
    if(max[si] <= 1)return;

    if(ll == rr)
    {
        max[si] = sqrt(max[si]);
        sum[si] = max[si];
    }
    else
    { 
        int mid = (ll+rr)>>1;
        if(r <= mid)
            change(l,r,ll,mid,L(si));
        else if(l > mid)
            change(l,r,mid+1,rr,R(si));
        else
        {
            change(l,mid,ll,mid,L(si));
            change(mid+1,r,mid+1,rr,R(si));
        }
        update(si);
    }
}
#undef L
#undef R

int main()
{
#ifndef ONLINE_JUDGE
    freopen("3211.in","r",stdin);
    freopen("3211.out","w",stdout);
#endif  

    read(n);

    for(int i = 1; i <= n; i++) read(a[i]);

    build(1, n, 1);

    read(m);
    while(m--)
    {
        int op, l, r;

        read(op),read(l),read(r);

        switch(op)
        {
            case 1:
                write(query(l, r, 1, n, 1)), puts("");
                break;
            case 2:
                change(l, r, 1, n, 1);
                break;    
        }
    }

#ifndef ONLINE_JUDGE
    fclose(stdin);
    fclose(stdout);
#endif
    return 0;       
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

posted @ 2015-08-07 18:00  <Dash>  阅读(114)  评论(0编辑  收藏  举报