zoukankan      html  css  js  c++  java
  • Poj3468 A Simple Problem with Integers (分块)

    题面

    Poj

    题解

    区间求和$+$区间修改板子,这里用分块写的

    #include <cmath>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using std::min; using std::max;
    using std::swap; using std::sort;
    typedef long long ll;
    #define int ll
    
    const int N = 1e5 + 10 , SN = 340;
    int n, siz, q, bel[N], val[N];
    int sum[SN], add[SN], L[SN], R[SN];
    
    template<typename T>
    void read(T &x) {
    	int flag = 1; x = 0; char ch = getchar();
    	while(ch < '0' || ch > '9') { if(ch == '-') flag = -flag; ch = getchar(); }
    	while(ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); x *= flag;
    }
    
    void modify (int l, int r, int c) {
    	int fl = bel[l], fr = bel[r];
    	if(fl == fr) {
    		for(int i = l; i <= r; ++i)
    			val[i] += c, sum[fl] += c;
    	} else {
    		for(int i = l; i <= R[fl]; ++i)
    			val[i] += c, sum[fl] += c;
    		for(int i = fl + 1; i < fr; ++i) add[i] += c;
    		for(int i = L[fr]; i <= r; ++i)
    			val[i] += c, sum[fr] += c;
    	}
    }
    
    int query(int l, int r) {
    	int fl = bel[l], fr = bel[r], ret = 0;
    	if(fl == fr) {
    		for(int i = l; i <= r; ++i)
    			ret += val[i] + add[fl];
    	} else {
    		for(int i = l; i <= R[fl]; ++i)
    			ret += val[i] + add[fl];
    		for(int i = fl + 1; i < fr; ++i) ret += sum[i] + add[i] * (R[i] - L[i] + 1);
    		for(int i = L[fr]; i <= r; ++i)
    			ret += val[i] + add[fr];
    	} return ret;
    }
    
    signed main () {
    	read(n), read(q), siz = sqrt(n);
    	for(int i = 1; i <= n; ++i)
    		read(val[i]), bel[i] = (i - 1) / siz + 1, sum[bel[i]] += val[i];
    	for(int i = 1; i <= bel[n]; ++i)
    		L[i] = R[i - 1] + 1, R[i] = i * siz;
    	R[bel[n]] = n; int l, r, k;
    	while(q--) {
    		char opt; scanf("
    %c", &opt);
    		read(l), read(r);
    		if(opt == 'Q') printf("%lld
    ", query(l, r));
    		else read(k), modify(l, r, k);
    	}
    	return 0;
    }
    
  • 相关阅读:
    5.线性回归算法
    作业14 15 手写数字识别-小数据集
    作业13 14 深度学习-卷积
    作业12 13-垃圾邮件分类2
    作业11 12.朴素贝叶斯-垃圾邮件分类
    作业10:11.分类与监督学习,朴素贝叶斯分类算法
    作业9、主成分分析
    作业8、特征选择
    作业7.逻辑回归实践
    作业6.逻辑归回
  • 原文地址:https://www.cnblogs.com/water-mi/p/10303502.html
Copyright © 2011-2022 走看看