| Time Limit: 5000MS | Memory Limit: 131072K | |
| Total Submissions: 105731 | Accepted: 33027 | |
| 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 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 A1, A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of Aa, Aa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of Aa, Aa+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
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.
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.
1 10 2 1 5 2 5 9 3
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;
}
本文介绍了解决区间更新和查询问题的有效方法,通过构建线段树并实现懒惰传播,能够高效处理大规模数据集上的加法操作及求和查询。

1235

被折叠的 条评论
为什么被折叠?



