BZOJ 3212 Pku3468 A Simple Problem with Integers

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

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;
}

时间: 2024-12-14 18:28:21

BZOJ 3212 Pku3468 A Simple Problem with Integers的相关文章

3212: Pku3468 A Simple Problem with Integers

3212: Pku3468 A Simple Problem with Integers Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 1053  Solved: 468[Submit][Status][Discuss] Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of opera

BZOJ3212: Pku3468 A Simple Problem with Integers

3212: Pku3468 A Simple Problem with Integers Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 810  Solved: 354[Submit][Status] Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to

bzoj3212: Pku3468 A Simple Problem with Integers(线段树)

3212: Pku3468 A Simple Problem with Integers 题目:传送门 题解: 感谢Rose_max大佬的倾情相(推)助(水题) 一起打线段树啊~~~~ 代码: 1 #include<cstdio> 2 #include<cstring> 3 #include<cstdlib> 4 #include<cmath> 5 #include<algorithm> 6 using namespace std; 7 type

BZOJ3212 Pku3468 A Simple Problem with Integers 题解

题目大意: 一个数列,有两个操作:1.修改操作,将一段区间内的数加上c:2.查询操作,查询一段区间内的数的和. 思路: 线段树裸题,区间修改.区间查询,维护和以及加上的数,由于无序,不需要向下推标记,只需在子树更新完之后更新根节点即可. 代码: 1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 using namespace std; 5 6 long long sum[400001],mor[4

poj3468 A Simple Problem with Integers 2011-12-20

A Simple Problem with Integers Time Limit: 5000MSMemory Limit: 131072K Total Submissions: 26062Accepted: 7202 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operat

E - A Simple Problem with Integers

#include <iostream> #include <stdio.h> #include <string.h> #include <stdlib.h> using namespace std; #define N 100002 struct node { int l,r; long long lz,w; }q[4*N]; void pushup(int rt) { q[rt].w=q[rt*2].w+q[rt*2+1].w; } void pushdo

POJ 3468 A Simple Problem with Integers

链接:http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 77302 Accepted: 23788 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two ki

poj 3468 A Simple Problem with Integers(线段树+区间更新+区间求和)

题目链接:id=3468http://">http://poj.org/problem? id=3468 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 83959   Accepted: 25989 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. Yo

poj3468 A Simple Problem with Integers 线段树区间更新

A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 97722   Accepted: 30543 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of