洛谷P3396 哈希衝突

分塊還真是應用普遍啊......數組

題意:求ide

解:以n0.5爲界。spa

當p小於n0.5的時候,直接用p²大小的數組儲存答案。code

預處理n1.5,修改n0.5blog

當p大於n0.5的時候,直接按照定義計算,複雜度n0.5it

因此總複雜度n1.5,實在是巧妙不堪啊......(什麼SB詞彙)io

 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代碼
相關文章
相關標籤/搜索