北大ACM3468——A Simple Problem with Integers~~线段树的应用

题目的意思很明确,有两种操作,一种是计算一个数列的第 a 到 第b的和,另一种是第 a 到 第 b 之间的数加上 c。由于这些操作的数目很大,用普通的办法无法办到,会超时。

对于这类问题,用线段树可以很好解决。对于线段树还只是学习阶段,还不是很熟,需要多加练习与理解。

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

typedef __int64 ll;

const int DAT_Size = (1 << 18) - 1;
const int MAX_N = 100000 + 1;
const int MAX_Q = 100000 + 1;
//存储输入数据
int N, Q;
int A[MAX_N];
char T[MAX_Q];
int L[MAX_Q], R[MAX_Q], X[MAX_Q];
//线段树
ll data[DAT_Size], data1[DAT_Size];
//取最大值
int max(int x, int y)
{
	return x > y ? x : y;
}
//取最小值
int min(int x, int y)
{
	return x < y ? x : y;
}
//对于区间[a,b)加上x
//k是结点的编号,对应的区间为[l, r)
void add(int a, int b, int x, int k, int l, int r)
{
	if(a <= l && r <= b)        //找到区间,加上x
		data[k] += x;
	else if(l < b && a < r)
	{
		data1[k] += (min(b, r) - max(a, l)) * x;
		add(a, b, x, 2 * k + 1, l, (l + r) / 2);
		add(a, b, x, 2 * k + 2, (l + r) / 2, r);
	}
}
//计算[a, b)的和
ll sum(int a, int b, int k, int l, int r)
{       //<span style="font-family: Arial, Helvetica, sans-serif;">区间[a,b)不包含[l, r)</span>
	if(b <= l || r <= a)
		return 0;
	else if(a <= l && b >= r)//区间[a,b)完全包含[l,r)
	{
		return  data[k] * (r - l) + data1[k];
	}
	else
	{
		__int64 ans = (min(b, r) - max(a, l)) * data[k];
		ans += sum(a, b, k * 2 + 1, l, (l + r) / 2);
		ans += sum(a, b, k * 2 + 2, (l + r) / 2, r);
		return ans;
	}
}

void solve()
{
	for(int i = 0; i < N; i++)               //将线段树的区间定在[1, N)
		add(i + 1, i + 2, A[i], 0, 1, N + 1);
	for(int j = 0; j < Q; j++)
	{
		if(T[j] == 'C')
			add(L[j], R[j] + 1, X[j], 0, 1, N + 1);
		else
			printf("%I64d\n", sum(L[j], R[j] + 1, 0, 1, N + 1));
	}
}

int main()
{
	memset(data, 0, sizeof(data));
	memset(data1, 0, sizeof(data1));
	scanf("%d%d", &N, &Q);
	for(int i = 0; i < N; i++)          //输入数据
		scanf("%d", &A[i]);
	getchar();
	for(int j = 0; j < Q; j++)
	{
		scanf("%c", &T[j]);
		if(T[j] == 'Q')
			scanf("%d%d", &L[j], &R[j]);
		else
			scanf("%d%d%d", &L[j], &R[j], &X[j]);
		getchar();
	}
	solve();
	return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-09 10:08:25

北大ACM3468——A Simple Problem with Integers~~线段树的应用的相关文章

POJ 3468 A Simple Problem with Integers(线段树区间更新)

题目地址:POJ 3468 打了个篮球回来果然神经有点冲动..无脑的狂交了8次WA..居然是更新的时候把r-l写成了l-r... 这题就是区间更新裸题.区间更新就是加一个lazy标记,延迟标记,只有向下查询的时候才将lazy标记向下更新.其他的均按线段树的来就行. 代码如下: #include <iostream> #include <cstdio> #include <cstring> #include <math.h> #include <stac

HDU4267 A Simple Problem with Integers 线段树/树状数组

HDU4267 A Simple Problem with Integers  线段树/树状数组 2012长春网络赛A题 Problem Description Let A1, A2, ... , AN be N elements. You need to deal with two kinds of operations. One type of operation is to add a given number to a few numbers in a given interval. T

POJ3468_A Simple Problem with Integers(线段树/成段更新)

解题报告 题意: 略 思路: 线段树成段更新,区间求和. #include <iostream> #include <cstring> #include <cstdio> #define LL long long #define int_now int l,int r,int root using namespace std; LL sum[500000],lazy[500000]; void push_up(int root,int l,int r) { sum[ro

【POJ】3468 A Simple Problem with Integers ——线段树 成段更新 懒惰标记

A Simple Problem with Integers Time Limit:5000MS   Memory Limit:131072K Case Time Limit:2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each

POJ3468__A Simple Problem with Integers (线段树)

本文出自blog.csdn.net/svitter --我大C++的指针岂是尔等能够简单领悟! 题意 给N个节点,标号A1~An,然后有Q个操作,操作分为Q i j,查询i,j间的区间和.C i j k,i到j个数字,每个数字增加k,并且输出. 输入输出分析 给N,Q,然后跟操作.注意判断Q,C使用scanf("%s"). 测试数据: Sample Input 10 5 1 2 3 4 5 6 7 8 9 10 Q 4 4 Q 1 10 Q 2 4 C 3 6 3 Q 2 4 Samp

POJ-3468 A Simple Problem with Integers(线段树)

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

poj 3468 A Simple Problem with Integers 线段树第一次 + 讲解

A Simple Problem with Integers Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of

POJ 3468 A Simple Problem with Integers (线段树区域更新)

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

[POJ] 3468 A Simple Problem with Integers [线段树区间更新求和]

A Simple Problem with Integers Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of