SPOJ-GSS系列

数据结构好题啊。。。

GSS1 - Can you answer these queries I

链接

题意:维护区间内最大子段和

直接线段树。。。

GSS4 - Can you answer these queries IV

链接

题意:维护区间开方,区间求和。区间内的数不超过1e18。

用计算器可知,1e18开方6次后即可变成1。维护线段树,每个数最多改6次,每次时间复杂度为\(O(log n)\),总时间复杂度为\(O(n log n)\)

三倍经验:luogu bzoj

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;

typedef long long ll;
const int Maxn=410000;

int tl[Maxn],tr[Maxn],n,opt,l,r;
int bj[Maxn];
ll tn[Maxn],a[Maxn];

inline void update(int root) {
    tn[root]=tn[root<<1]+tn[(root<<1)|1];
    bj[root]=bj[root<<1]&bj[(root<<1)|1];
}

void build(int root,int l,int r) {
    tl[root]=l;
    tr[root]=r;
    if(l==r) {
        tn[root]=a[l];
        if(tn[root]<=1) bj[root]=1;
        return ;
    }
    int mid=l+r>>1;
    build(root<<1,l,mid);
    build((root<<1)|1,mid+1,r);
    update(root);
}

ll query(int root,int l,int r) {
    int lc=tl[root],rc=tr[root];
    int mid=lc+rc>>1;
    if(l<=lc&&r>=rc) return tn[root];
    ll ans=0;
    if(l<=mid) ans+=query(root<<1,l,r);
    if(r>mid) ans+=query((root<<1)|1,l,r);
    return ans;
}

void change(int root,int l,int r) {
    if(bj[root]) return ;
    int lc=tl[root],rc=tr[root];
    if(lc==rc) {
        tn[root]=sqrt(tn[root]);
        if(tn[root]<=1) bj[root]=1;
        return ;
    }
    int mid=lc+rc>>1;
    if(l<=mid) change(root<<1,l,r);
    if(r>mid) change((root<<1)|1,l,r);
    update(root);
}

int main() {
    int t=0;
    while(scanf("%d",&n)!=EOF) {
        t++;
        memset(bj,0,sizeof(bj));
        printf("Case #%d:\n",t);
        for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
        build(1,1,n);
        scanf("%d",&n);
        for(int i=1;i<=n;i++) {
            scanf("%d%d%d",&opt,&l,&r);
            if(l>r) swap(l,r);
            if(opt==1) printf("%lld\n",query(1,l,r));
            else change(1,l,r);
        }
        putchar('\n');
    }
    return 0;
}

转载于:https://www.cnblogs.com/shanxieng/p/9922284.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值