在大牛分站交能过,主站卡常。
时间复杂度为 n√n ≈ 3.5 * 108,我都不知道怎么过的。。
——代码
1 #include <cmath> 2 #include <cstdio> 3 #include <iostream> 4 5 using namespace std; 6 7 const int MAXN = 500001; 8 int n, m, S, C; 9 int a[MAXN], st[MAXN], ed[MAXN], belong[MAXN], sum[MAXN]; 10 11 inline void init() 12 { 13 int i, j; 14 scanf("%d %d", &n, &m); 15 for(i = 1; i <= n; i++) scanf("%d", &a[i]); 16 S = sqrt(n) + 10; 17 for(i = 1; i <= n; i += S) 18 { 19 st[++C] = i; 20 ed[C] = min(i + S - 1, n); 21 } 22 for(i = 1; i <= C; i++) 23 for(j = st[i]; j <= ed[i]; j++) 24 sum[i] += a[j], belong[j] = i; 25 } 26 27 inline void update(int x, int k) 28 { 29 a[x] += k; 30 sum[belong[x]] += k; 31 } 32 33 inline int query(int x, int y) 34 { 35 int i, ans = 0, l = belong[x], r = belong[y]; 36 if(l == r) 37 { 38 for(i = x; i <= y; i++) ans += a[i]; 39 return ans; 40 } 41 for(i = x; i <= ed[l]; i++) ans += a[i]; 42 for(i = l + 1; i <= r - 1; i++) ans += sum[i]; 43 for(i = st[r]; i <= y; i++) ans += a[i]; 44 return ans; 45 } 46 47 inline void work() 48 { 49 int i, x, y, z; 50 for(i = 1; i <= m; i++) 51 { 52 scanf("%d %d %d", &z, &x, &y); 53 if(z == 1) update(x, y); 54 else printf("%d\n", query(x, y)); 55 } 56 } 57 58 int main() 59 { 60 init(); 61 work(); 62 return 0; 63 }