poj 3468 & hdu 1698 【线段树-区间更新】

本文介绍了解决区间更新和查询问题的有效方法,通过构建线段树并实现懒惰传播,能够高效处理大规模数据集上的加法操作及求和查询。

A Simple Problem with Integers
Time Limit: 5000MS Memory Limit: 131072K
Total Submissions: 105731 Accepted: 33027
Case Time Limit: 2000MS

Description

You have N integers, A1A2, ... , 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 numbers in a given interval.

Input

The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000.
The second line contains N numbers, the initial values of A1A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of AaAa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of AaAa+1, ... , Ab.

Output

You need to answer all Q commands in order. One answer in a line.

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

Sample Output

4
55
9
15



题目大意:

给出了一个序列,你需要处理如下两种询问。

"C a b c"表示给[a, b]区间中的值全部增加c (-10000 ≤ c ≤ 10000)。

"Q a b" 询问[a, b]区间中所有值的和。

AC代码:

# include <stdio.h>
# include <string.h>


# define MAXN 100005

__int64 arr[MAXN * 4];
__int64 add[MAXN * 4];
__int64 n, q;

void Pushdown(__int64 node, __int64 mid)
{
	if (add[node])
	{
		add[node * 2] += add[node];
		add[node * 2 + 1] += add[node];
		arr[node * 2] += (add[node] * (mid - (mid / 2)));
		arr[node * 2 + 1] += (add[node] * (mid / 2));
		add[node] = 0;
	}
}

void Build(__int64 node, __int64 nstart, __int64 nend)
{
	add[node] = 0;
	if (nstart == nend)
	{
		scanf("%I64d", &arr[node]);
		return ;
	}
	__int64 mid = (nstart + nend) / 2;
	Build(node * 2, nstart, mid);
	Build(node * 2 + 1, mid + 1, nend);
	arr[node] = arr[node * 2] + arr[node * 2 + 1];
}

void Update(__int64 node, __int64 nstart, __int64 nend, __int64 ustart, __int64 uend, __int64 updatenum)
{
	if (ustart <= nstart && uend >= nend)
	{
		add[node] += updatenum;
		arr[node] += updatenum * (nend - nstart + 1);
		return;
	} 
	
	__int64 mid = (nstart + nend) / 2;
	Pushdown(node, (nend - nstart) + 1);

	if (ustart <= mid)
	{
		Update(node * 2, nstart, mid, ustart, uend, updatenum);
	}
	if (uend > mid)
	{
		Update(node * 2 + 1, mid + 1, nend, ustart, uend, updatenum);
	}
	arr[node] = arr[node * 2] + arr[node * 2 + 1];
}

__int64 Query(__int64 node, __int64 nstart, __int64 nend, __int64 qstart, __int64 qend)
{
	if (qstart <= nstart && qend >= nend)
	{		
		return arr[node];
	}
	__int64 mid = (nstart + nend) / 2;
	Pushdown(node, (nend - nstart) + 1);
	__int64 ret = 0;

	if (qstart <= mid)
	{
		ret += Query(node * 2, nstart, mid, qstart, qend);
	}
	if (qend > mid)
	{
		ret += Query(node * 2 + 1, mid + 1, nend, qstart, qend);
	}
	return ret;
}

int main(void)
{
	char str[3];
	__int64 a, b, c;
	
	while (~scanf("%I64d %I64d", &n, &q))
	{
		memset(arr, 0, sizeof(arr));
		memset(add, 0, sizeof(add));
		Build(1, 1, n);
		while (q--)
		{
			scanf("%s", str);
			if ('C' == str[0])
			{
				scanf("%I64d %I64d %I64d", &a, &b, &c);
				Update(1, 1, n, a, b, c);
			}
			else if ('Q' == str[0])
			{
				scanf("%I64d %I64d", &a, &b);				
				printf("%I64d\n", Query(1, 1, n, a, b));
			}
		}
	}

	return 0;
}

Just a Hook

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 30668    Accepted Submission(s): 15125


Problem Description
In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes. The hook is made up of several consecutive metallic sticks which are of the same length.



Now Pudge wants to do some operations on the hook.

Let us number the consecutive metallic sticks of the hook from 1 to N. For each operation, Pudge can change the consecutive metallic sticks, numbered from X to Y, into cupreous sticks, silver sticks or golden sticks.
The total value of the hook is calculated as the sum of values of N metallic sticks. More precisely, the value for each kind of stick is calculated as follows:

For each cupreous stick, the value is 1.
For each silver stick, the value is 2.
For each golden stick, the value is 3.

Pudge wants to know the total value of the hook after performing the operations.
You may consider the original hook is made up of cupreous sticks.
 

Input
The input consists of several test cases. The first line of the input is the number of the cases. There are no more than 10 cases.
For each case, the first line contains an integer N, 1<=N<=100,000, which is the number of the sticks of Pudge’s meat hook and the second line contains an integer Q, 0<=Q<=100,000, which is the number of the operations.
Next Q lines, each line contains three integers X, Y, 1<=X<=Y<=N, Z, 1<=Z<=3, which defines an operation: change the sticks numbered from X to Y into the metal kind Z, where Z=1 represents the cupreous kind, Z=2 represents the silver kind and Z=3 represents the golden kind.
 

Output
For each case, print a number in a line representing the total value of the hook after the operations. Use the format in the example.
 

Sample Input
  
1 10 2 1 5 2 5 9 3
 

Sample Output
  
Case 1: The total value of the hook is 24.

题目大意:帕杰有一个神奇的钩子,这个钩子可以任意改变铜,金,银这三种材质, 每种材质的价值不同。问题是,当对钩子的材质改变操作完时,请你计算现在钩子的价值、

AC代码:

# include <cstdio>
# include <cstring>

# define MAXN 100005

int arr[MAXN << 2];
int n;
int result;
int count;
int add[MAXN << 2];

void Pushdown(int node, int nstart, int nend)
{
	int m = (nend - nstart + 1);
	if (add[node])
	{
		add[node << 1] = add[node];
		add[node << 1 | 1] = add[node];
		arr[node << 1] = add[node] * (m - m / 2);
		arr[node << 1 | 1] = add[node] * (m / 2);
		add[node] = 0;
	}
}

void Build(int node, int nstart, int nend)
{
	add[node] = 0;
	arr[node] = 1;
	if (nstart == nend)
	{
		return;
	}
	int mid = (nstart + nend) >> 1;
	Build(node << 1, nstart, mid);
	Build(node << 1 | 1, mid + 1, nend);
}

void Update(int node, int nstart, int nend, int ustart, int uend, int updatenum)
{
	if (ustart <= nstart && uend >= nend)
	{
		add[node] = updatenum;
		arr[node] = updatenum * (nend - nstart + 1);
		return;
	}
	Pushdown(node, nstart, nend);
	int mid = (nstart + nend) >> 1;
	if (ustart <= mid)
	{
		Update(node << 1, nstart, mid, ustart, uend, updatenum);
	}
	if (uend > mid)
	{
		Update(node << 1 | 1, mid + 1, nend, ustart, uend, updatenum);
	}
	arr[node] = arr[node << 1] + arr[node << 1 | 1];
}

int main(void)
{
	int t;
	scanf("%d", &t);
	count = 1;
	while (t--)
	{
		int q;
		scanf("%d", &n);
		Build(1, 1, n);
		scanf("%d", &q);
		while (q--)
		{
			int a, b, c;
			scanf("%d %d %d", &a, &b, &c);
			Update(1, 1, n, a, b, c);;
		}

		printf("Case %d: The total value of the hook is %d.\n", count++, arr[1]);
	}
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值