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;
}
posted @ 2016-09-04 11:24  cycleke  阅读(206)  评论(0编辑  收藏  举报