现在的位置: 首页 > 综合 > 正文

BZOJ 3212 Pku3468 A Simple Problem with Integers

2017年11月21日 ⁄ 综合 ⁄ 共 1534字 ⁄ 字号 评论关闭

题目大意:你拍一,我拍一,大家一起刷水题。

CODE:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define MAX 100010
#define LEFT (pos << 1)
#define RIGHT (pos << 1|1)
#define CNT (r - l + 1)
using namespace std;

struct SegTree{
	long long sum,c;
}tree[MAX << 2];

int cnt,asks;
int src[MAX];
char c[10];

void BuildTree(int l,int r,int pos)
{
	if(l > r)	return ;
	if(l == r) {
		tree[pos].sum = src[l];
		return ;
	}
	int mid = (l + r) >> 1;
	BuildTree(l,mid,LEFT);
	BuildTree(mid + 1,r,RIGHT);
	tree[pos].sum = tree[LEFT].sum + tree[RIGHT].sum;
}

inline void PushDown(int pos,int cnt)
{
	if(tree[pos].c) {
		tree[LEFT].c += tree[pos].c;
		tree[RIGHT].c += tree[pos].c;
		tree[LEFT].sum += tree[pos].c * (cnt - (cnt >> 1));
		tree[RIGHT].sum += tree[pos].c * (cnt >> 1);
		tree[pos].c = 0;
	}
}

void Modify(int l,int r,int x,int y,int pos,long long c)
{
	if(l == x && y == r) {
		tree[pos].sum += CNT * c;
		tree[pos].c += c;
		return ;
	}
	PushDown(pos,CNT);
	int mid = (l + r) >> 1;
	if(y <= mid)	Modify(l,mid,x,y,LEFT,c);
	else if(x > mid)	Modify(mid + 1,r,x,y,RIGHT,c);
	else {
		Modify(l,mid,x,mid,LEFT,c);
		Modify(mid + 1,r,mid + 1,y,RIGHT,c);
	}
	tree[pos].sum = tree[LEFT].sum + tree[RIGHT].sum;
}

long long Ask(int l,int r,int x,int y,int pos)
{
	if(l == x && y == r)	return tree[pos].sum;
	PushDown(pos,CNT);
	int mid = (l + r) >> 1;
	if(y <= mid)	return Ask(l,mid,x,y,LEFT);
	if(x > mid)		return Ask(mid + 1,r,x,y,RIGHT);
	long long left = Ask(l,mid,x,mid,LEFT);
	long long right = Ask(mid + 1,r,mid + 1,y,RIGHT);
	return left + right;
}

int main()
{
	cin >> cnt >> asks;
	for(int i = 1; i <= cnt; ++i)
		scanf("%d",&src[i]);
	BuildTree(1,cnt,1);
	for(int x,y,i = 1; i <= asks; ++i) {
		scanf("%s",c);
		if(c[0] == 'Q') {
			scanf("%d%d",&x,&y);
			printf("%lld\n",Ask(1,cnt,x,y,1));
		}
		else {
			long long z;
			scanf("%d%d%lld",&x,&y,&z);
			Modify(1,cnt,x,y,1,z);
		}
	}
	return 0;
}

抱歉!评论已关闭.