1 条题解
-
0
#include <bits/stdc++.h> using namespace std; int n, opt, l, r, c; int a[55004], b[55004]; int SZ, CNT, ID[55004], L[1004], R[1004], sum[1004], cnt[1004]; void update(int l, int r) { if (ID[l] == ID[r]) { for (int i = l; i <= r; i++) { if (a[i] == 1 || a[i] == 0) continue; sum[ID[l]] -= a[i]; a[i] = sqrt(a[i]); sum[ID[l]] += a[i]; cnt[ID[i]] += (a[i] == 1 || a[i] == 0); } return; } for (int i = l; i <= R[ID[l]]; i++) { if (a[i] == 1 || a[i] == 0) continue; sum[ID[l]] -= a[i]; a[i] = sqrt(a[i]); sum[ID[l]] += a[i]; cnt[ID[i]] += (a[i] == 1 || a[i] == 0); } for (int i = ID[l] + 1; i <= ID[r] - 1; i++) { if (cnt[i] == SZ) continue; for (int j = L[i]; j <= R[i]; j++) { if (a[j] == 1 || a[j] == 0) continue; sum[i] -= a[j]; a[j] = sqrt(a[j]); sum[i] += a[j]; cnt[i] += (a[j] == 1 || a[j] == 0); } } for (int i = L[ID[r]]; i <= r; i++) { if (a[i] == 1 || a[i] == 0) continue; sum[ID[r]] -= a[i]; a[i] = sqrt(a[i]); sum[ID[r]] += a[i]; cnt[ID[r]] += (a[i] == 1 || a[i] == 0); } } int query(int l, int r) { int res = 0; if (ID[l] == ID[r]) { for (int i = l; i <= r; i++) res += a[i]; return res; } for (int i = l; i <= R[ID[l]]; i++) res += a[i]; for (int i = L[ID[r]]; i <= r; i++) res += a[i]; for (int i = ID[l] + 1; i <= ID[r] - 1; i++) res += sum[i]; return res; } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n; SZ = sqrt(n); for (int i = 1; i <= n; i++) { cin >> a[i]; ID[i] = (i - 1) / SZ + 1; sum[ID[i]] += a[i]; cnt[ID[i]] += (a[i] == 1 || a[i] == 0); } CNT = n / SZ + (n % SZ > 0); for (int i = 1; i <= CNT; i++) { L[i] = (i - 1) * SZ + 1; R[i] = i * SZ; } cnt[CNT] += SZ - n % SZ; for (int i = 1; i <= n; i++) { cin >> opt >> l >> r >> c; if (opt == 0) update(l, r); else cout << query(l, r) << "\n"; } return 0; }
- 1
信息
- ID
- 876
- 时间
- 500ms
- 内存
- 256MiB
- 难度
- 7
- 标签
- 递交数
- 62
- 已通过
- 15
- 上传者