xindoo is
always here

# codeforces 315 B.Sereja and Array

B. Sereja and Array

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Sereja has got an array, consisting of n integers, a1, a2, …, an.
Sereja is an active boy, so he is now going to complete m operations. Each operation will have one of the three forms:

1. Make vi-th array
element equal to xi.
In other words, perform the assignment avi = xi.

2. Increase each array element by yi.
In other words, perform n assignments ai = ai + yi (1 ≤ i ≤ n).

3. Take a piece of paper and write out the qi-th
array element. That is, the element aqi.

//cf 315 B Sereja and Array
//2013-06-13-20.02
#include <stdio.h>
#include <string.h>

const int maxn = 100005;
int a[maxn];
int n;

inline int lowbit(int x)
{
return x&-x;
}

int update(int x, int v)
{
while (x <= n+1)
{
a[x] += v;
x += lowbit(x);
}
return 0;
}

int getsum(int x)
{
int sum = 0;
while (x)
{
sum += a[x];
x -= lowbit(x);
}
return sum;
}

int main()
{
int m;
while (scanf("%d %d", &n, &m) != EOF)
{
memset(a, 0, sizeof(a));
int t, op, x, v;
for (int i = 1; i <= n; i++)
{
scanf("%d", &t);
update(i, t);
update(i+1, -t);
}
while (m--)
{
scanf("%d", &op);
if (op == 1)
{
scanf("%d %d", &x, &v);
int tmp = getsum(x);
update(x, -tmp);
update(x+1, tmp);
update(x, v);
update(x+1, -v);
}
else if (op == 2)
{
scanf("%d", &v);
update(1, v);
}
else
{
scanf("%d", &x);
printf("%d\n", getsum(x));
}
}
}
return 0;
}


• QQ咨询
• 回顶