• poj3468 A Simple Problem with Integers(zkw区间修改模板)


    此题是一道线段树的裸题,这里只是为了保存我的zkw线段树模板

    #include <cstdio>
    #include <cstring>
    #include <iostream>
    using namespace std;
    
    typedef long long LL;
    
    inline int geti() {
    	static int Ina; static char Inc; static bool InSign;
    	InSign = false;
    	while ((Inc = getchar()) < '0' || Inc >'9') InSign |= Inc == '-';
    	Ina = Inc - '0';
    	while ((Inc = getchar()) >= '0' && Inc <= '9') Ina = (Ina << 3) + (Ina << 1) + Inc - '0';
    	return InSign ? -Ina : Ina;
    }
    
    inline void Outi(LL x) {
    	if (x < 0) putchar('-'), x = -x;
    	static char buf[20]; static int Len;
    	Len = 0; while (x) buf[++Len] = x % 10 + '0', x /= 10; 
    	while (Len) putchar(buf[Len--]);
    }
    
    const int N = 200005;
    int pre, dl[N << 1], dr[N << 1];
    LL C[N << 1], ly[N << 1];
    #define ls u<<1
    #define rs u<<1|1
    void Down(const int &u) {
    	if (ly[u] && u < pre) {
    		C[ls] += (dr[ls] - dl[ls] + 1) * ly[u];
    		C[rs] += (dr[rs] - dl[rs] + 1) * ly[u];
    		ly[ls] += ly[u]; ly[rs] += ly[u];
    		ly[u] = 0;
    	}
    }
    
    int Stack[30], top;
    void Up(int u) {
    	for (top = 0; u; u >>= 1) Stack[++top] = u;
    	while (top) Down(Stack[top--]);
    }
    
    LL Query(int s, int t) {
    	LL ret = 0; int lef(0), rig(0);
    	for (s += pre - 1, t += pre + 1; s ^ t ^ 1; s >>= 1, t >>= 1) {
    		if (~s & 1) (lef ? 1 : (Up(lef = s ^ 1),1)), ret += C[s ^ 1];
    		if ( t & 1) (rig ? 1 : (Up(rig = t ^ 1),1)), ret += C[t ^ 1];
    	}
    	return ret;
    }
    
    void Update(int s, int t, const int &val) {
    	int lef(0), rig(0);
    	for (s += pre - 1, t += pre + 1; s ^ t ^ 1; s >>= 1, t >>= 1) {
    		if (~s & 1) (lef ? 1 : (Up(lef = s ^ 1),1)), ly[s ^ 1] += val, C[s ^ 1] += (dr[s ^ 1] - dl[s ^ 1] + 1) * (LL)val;
    		if ( t & 1) (rig ? 1 : (Up(rig = t ^ 1),1)), ly[t ^ 1] += val, C[t ^ 1] += (dr[t ^ 1] - dl[t ^ 1] + 1) * (LL)val;
    	}
    	for (lef >>= 1; lef; lef >>= 1) C[lef] = C[lef << 1 | 1] + C[lef << 1];
    	for (rig >>= 1; rig; rig >>= 1) C[rig] = C[rig << 1 | 1] + C[rig << 1];
    }
    
    int main() {
    	int n = geti(), m = geti();
    	for (pre = 1; pre <= n + 1; pre <<= 1);
    	for (int i = 1; i <= n; ++i) C[i + pre] = geti(), dl[i + pre] = dr[i + pre] = i;
    	for (int i = pre; i; --i) C[i] = C[i << 1] + C[i << 1 | 1], dl[i] = dl[i << 1], dr[i] = dr[i << 1 | 1];
    	char op; int x, y, z;
    	while (m--) {
    		while ((op = getchar()) < 'C' && op > 'Q');
    		if (op ^ 'C') {
    			x = geti(), y = geti();
    			if (x > y) x ^= y ^= x ^= y;
    			cout << Query(x, y) << endl;
    		} else {
    			x = geti(), y = geti(), z = geti();
    			if (x > y) x ^= y ^= x ^= y;
    			Update(x, y, z);
    		}
    	}
    	return 0;
    }
    
  • 相关阅读:
    ionic 导航
    vscode多光标编辑(MAC)
    vscode保存文件时自动删除行尾空格
    ionic-native sqlite 插件5.x版的在ionic3.x上报错 cannot read property 'split' of undefined
    MAC OSX 自带Apache 配置及使用
    ionic中ion-item下的div,span,p不渲染,应该给这些元素加上item-content属性
    开发ionic + cordova应用时遇到的坑,resources/splash.png do not meet minimum size requirements: 2732x2732
    ionic 创建指令的命名规则
    Soldier and Badges (set的检索简单运用)
    打败大魔王之最小排列数问题(全排列)
  • 原文地址:https://www.cnblogs.com/cycleke/p/5838882.html
Copyright © 2020-2023  润新知