我是靠谱客的博主 爱撒娇树叶,这篇文章主要介绍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.

给N个元素的数组, 有三种操作 1 是把第i个元素变成v,  2是所有元素都加V, 3 询问第i个元素的值。

我用了树状数组,理论上用线段树也可以做,但树状数组明显要好写点,感觉还要比线段树快些。

树状数组原本用来就区间的和,只要稍微改进一下就和更新点,求点的值, 我们如果更新点x为v(当原来点是0事) 我们update(x , v) 和 update(x, -v) , 这样我们求1到x的和是求到的就是x点的值。


复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//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("%dn", getsum(x)); } } } return 0; }



最后

以上就是爱撒娇树叶最近收集整理的关于codeforces 315 B.Sereja and Array的全部内容,更多相关codeforces内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(64)

评论列表共有 0 条评论

立即
投稿
返回
顶部