zoukankan      html  css  js  c++  java
  • [CC-FNCS]Chef and Churu

    [CC-FNCS]Chef and Churu

    题目大意:

    一个长度为(n(nle10^5))的数列(A_{1sim n}),另有(n)个函数,第(i)个函数会返回数组中标号在(l_isim r_i)之间的元素的和。(q(qle10^5))次询问,询问包含以下两种:

    1. 将数组的第(x)个元素修改为(y);
    2. 询问标号在(m)(n)之间的函数的值的和。

    思路:

    对函数分块,树状数组维护(A)的前缀和。

    时间复杂度(mathcal O(nsqrt nlog n))

    源代码:

    #include<cmath>
    #include<cstdio>
    #include<cctype>
    #include<algorithm>
    inline int getint() {
    	register char ch;
    	while(!isdigit(ch=getchar()));
    	register int x=ch^'0';
    	while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
    	return x;
    }
    typedef unsigned long long uint64;
    const int N=1e5+1,B=317;
    int n,a[N],block,cnt[N][B],bel[N],beg[B],end[B];
    uint64 sum[B];
    struct Func {
    	int l,r;
    };
    Func f[N];
    class FenwickTree {
    	private:
    		uint64 val[N];
    		int lowbit(const int &x) const {
    			return x&-x;
    		}
    	public:
    		void modify(int p,const int &x) {
    			for(;p<=n;p+=lowbit(p)) {
    				val[p]+=x;
    			}
    		}
    		uint64 query(int p) const {
    			uint64 ret=0;
    			for(;p;p-=lowbit(p)) {
    				ret+=val[p];
    			}
    			return ret;
    		}
    		uint64 query(const int &l,const int &r) const {
    			return query(r)-query(l-1);
    		}
    };
    FenwickTree bit;
    class SegmentTree {
    	#define _left <<1
    	#define _right <<1|1
    	#define mid ((b+e)>>1)
    	private:
    		int val[N<<2];
    	public:
    		void build(const int &p,const int &b,const int &e) {
    			val[p]=0;
    			if(b==e) return;
    			build(p _left,b,mid);
    			build(p _right,mid+1,e);
    		}
    		void modify(const int &p,const int &b,const int &e,const int &l,const int &r) {
    			if(b==l&&e==r) {
    				val[p]++;
    				return;
    			}
    			if(l<=mid) modify(p _left,b,mid,l,std::min(mid,r));
    			if(r>mid) modify(p _right,mid+1,e,std::max(mid+1,l),r);
    		}
    		int query(const int &p,const int &b,const int &e,const int &x) const {
    			int ret=val[p];
    			if(b==e) return ret;
    			if(x<=mid) ret+=query(p _left,b,mid,x);
    			if(x>mid) ret+=query(p _right,mid+1,e,x);
    			return ret;
    		}
    	#undef _left
    	#undef _right
    	#undef mid
    };
    SegmentTree sgt;
    inline void init() {
    	for(register int i=bel[1];i<=bel[n];i++) {
    		sgt.build(1,1,n);
    		for(register int j=beg[i];j<=end[i];j++) {
    			sgt.modify(1,1,n,f[j].l,f[j].r);
    			sum[i]+=bit.query(f[j].l,f[j].r);
    		}
    		for(register int j=1;j<=n;j++) {
    			cnt[j][i]=sgt.query(1,1,n,j);
    		}
    	}
    }
    inline uint64 query(const int &l,const int &r) {
    	uint64 ret=0;
    	if(bel[l]==bel[r]) {
    		for(register int i=l;i<=r;i++) {
    			ret+=bit.query(f[i].l,f[i].r);
    		}
    		return ret;
    	}
    	for(register int i=l;i<=end[bel[l]];i++) {
    		ret+=bit.query(f[i].l,f[i].r);
    	}
    	for(register int i=bel[l]+1;i<bel[r];i++) {
    		ret+=sum[i];
    	}
    	for(register int i=beg[bel[r]];i<=r;i++) {
    		ret+=bit.query(f[i].l,f[i].r);
    	}
    	return ret;
    }
    int main() {
    	n=getint(),block=sqrt(n)*2;
    	for(register int i=1;i<=n;i++) {
    		a[i]=getint();
    		bit.modify(i,a[i]);
    		bel[i]=i/block;
    		if(!beg[bel[i]]) beg[bel[i]]=i;
    		end[bel[i]]=i;
    	}
    	for(register int i=1;i<=n;i++) {
    		f[i].l=getint();
    		f[i].r=getint();
    	}
    	init();
    	const int q=getint();
    	for(register int i=0;i<q;i++) {
    		const int opt=getint(),x=getint(),y=getint();
    		if(opt==1) {
    			bit.modify(x,y-a[x]);
    			for(register int i=bel[1];i<=bel[n];i++) {
    				sum[i]+=(uint64)(y-a[x])*cnt[x][i];
    			}
    			a[x]=y;
    		}
    		if(opt==2) {
    			printf("%llu
    ",query(x,y));
    		}
    	}
    	return 0;
    }
    
  • 相关阅读:
    sublime text 4 vim 插件配置
    ssh-keygen 的使用
    distribution transaction solution
    bilibili 大数据 视频下载 you-get
    Deepin 20.2.1 安装 MS SQL 2019 容器版本
    【转】使用Linux下Docker部署MSSQL并加载主机目录下的数据库
    【转】You Can Now Use OneDrive in Linux Natively Thanks to Insync
    dotnet 诊断工具安装命令
    Linux 使用 xrandr 设置屏幕分辨率
    【转】CentOS 7.9 2009 ISO 官方原版镜像下载
  • 原文地址:https://www.cnblogs.com/skylee03/p/9685240.html
Copyright © 2011-2022 走看看