zoukankan      html  css  js  c++  java
  • BZOJ 4216: Pig [分块]

    传送门

    询问区间和...内存3M


    分块前缀和...

    我连swap都是这么l=l^r , r=l^r , l=l^r写的....

    #include <cstdio>
    typedef long long ll;
    const int N=5e5+5,M=1e4+5;
    int n,Q,type,a[N],l,r,block=50,m;
    ll s[M],last; 
    int main(){
        freopen("in","r",stdin);
        scanf("%d%d%d",&n,&Q,&type);
        m=(n-1)/block+1;
        for(int i=1;i<=n;i++) scanf("%d",&a[i]),s[(i-1)/block+1]+=a[i];
        for(int i=1;i<=m;i++) s[i]+=s[i-1];
        while(Q--){
            scanf("%d%d",&l,&r);
            if(type){
                if(last<0) last=-last;
                l=(l^last)%n+1;r=(r^last)%n+1;
                if(l>r) l=l^r , r=l^r , l=l^r;
            }
            int pl=(l-1)/block+1,pr=(r-1)/block+1;
            last=0;
            if(pl==pr)
                for(int i=l;i<=r;i++) last+=a[i];
            else{
                last=s[pr-1]-s[pl];
                int _=pl==m ? n : pl*block;
                for(int i=l;i<=_;i++) last+=a[i];
                for(int i=(pr-1)*block+1;i<=r;i++) last+=a[i];
            }
            printf("%lld
    ",last);
        }
    }
  • 相关阅读:
    this指向
    作用域链
    入门
    一、servlet之初见
    jdbc之mysql
    第六章、树和二叉树
    第七章、暴力求解法
    机试
    第十三章、字符串
    栈和队列
  • 原文地址:https://www.cnblogs.com/candy99/p/6558271.html
Copyright © 2011-2022 走看看