# 洛谷P3396 哈希冲突

 1 #include <cstdio>
2 #include <algorithm>
3 #include <cmath>
4
5 const int N = 150010;
6
7 int fr[N], le[N], re[N];
8 int ans[400][400], a[N];
9 char str[3];
10
11 int main() {
12     int n, m;
13     scanf("%d%d", &n, &m);
14     int T = sqrt(n);
15     for(int i = 1; i <= n; i++) {
16         scanf("%d", &a[i]);
17         fr[i] = (i - 1) / T + 1;
18     }
19     for(int i = 1; i <= fr[n]; i++) {
20         le[i] = re[i - 1] + 1;
21         re[i] = le[i] + T - 1;
22         if(i == fr[n]) {
23             re[i] = n;
24         }
25     }
26     for(int i = 1; i <= T; i++) {
27         for(int j = 1; j <= n; j++) {
28             ans[i][j % i] += a[j];
29         }
30     }
31
32     for(int i = 1, x, y; i <= m; i++) {
33         scanf("%s%d%d", str, &x, &y);
34         if(str[0] == 'A') { // ask
35             if(x <= T) {
36                 printf("%d\n", ans[x][y]);
37             }
38             else {
39                 int ans = 0;
40                 for(int k = 0; k * x + y <= n; k++) {
41                     ans += a[k * x + y];
42                 }
43                 printf("%d\n", ans);
44             }
45         }
46         else { // change
47             for(int j = 1; j <= T; j++) {
48                 ans[j][x % j] += y - a[x];
49             }
50             a[x] = y;
51         }
52     }
53
54     return 0;
55 }
AC代码

posted @ 2018-12-27 21:23  huyufeifei  阅读(...)  评论(...编辑  收藏