constint inf = 0x3fffffff; int n, m, s; int a[100100], chunk[100100], lazy[100100];
voidmodify(int x, int y, int z) { int i = (x-1)/s+1; int j = (y-1)/s+1; chunk[i] = -inf; chunk[j] = -inf; if (i == j) { for (int k = x; k <= y; k++) { a[k] += z; } for (int k = (i-1)*s+1; k <= j*s; k++) { if (a[k] > chunk[i]) chunk[i] = a[k]; } } else { for (int k = x; k <= i*s; k++) { a[k] += z; } for (int k = (i-1)*s+1; k <= i*s; k++) { if (a[k] > chunk[i]) chunk[i] = a[k]; } for (int k = (j-1)*s+1; k <= y; k++) { a[k] += z; } for (int k = (j-1)*s+1; k <= j*s; k++) { if (a[k] > chunk[j]) chunk[j] = a[k]; } for (int k = i+1; k < j; k++) { lazy[k] += z; } } }
intsearch(int x, int y) { int i = (x-1)/s+1; int j = (y-1)/s+1; int ans = -inf; if (i == j) { for (int k = x; k <= y; k++) { if (a[k]+lazy[i] > ans) ans = a[k]+lazy[j]; } } else { for (int k = x; k <= i*s; k++) { if (a[k]+lazy[i] > ans) ans = a[k]+lazy[i]; } for (int k = (j-1)*s+1; k <= y; k++) { if (a[k]+lazy[j] > ans) ans = a[k]+lazy[j]; } for (int k = i+1; k < j; k++) { if (chunk[k]+lazy[k] > ans) ans = chunk[k]+lazy[k]; } } return ans; }
signedmain() { scanf("%d", &n); s = sqrt(n); for (int i = 1; i <= n; i++) { scanf("%d", a+i); if (i%s == 1 || a[i] > chunk[(i-1)/s+1]) chunk[(i-1)/s+1] = a[i]; } cin >> m; while (m--) { char op[3]; int x, y, z; scanf("%s%d%d", op, &x, &y); if (op[2] == 'D') { cin >> z; modify(x, y, z); } else { cout << search(x, y) << endl; } } }
voidmodify(int k, int t) { // 单点修改,需要排序 int F = (k-1)/s+1; a[k] = t; b[F].clear(); for (int i = (F-1)*s+1; i <= F*s; i++) { b[F].push_back(a[i]); } sort(b[F].begin(), b[F].end()); }
intcheck(int x, int y, int f) { // 统计信息 int i = (x-1)/s+1; int j = (y-1)/s+1; int ans = 0; if (i == j) { for (int k = x; k <= y; k++) { if (a[k] < f) ans++; } } else { for (int k = x; k <= i*s; k++) { if (a[k] < f) ans++; } for (int k = (j-1)*s+1; k <= y; k++) { if (a[k] < f) ans++; } for (int k = i+1; k < j; k++) { ans += lower_bound(b[k].begin(), b[k].end(), f) -b[k].begin(); // 直接二分统计整块内的答案 } } return ans; }
intquery(int x, int y, int k) { // 二分答案 int l = 0, r = 50005, mid; while (l+1 < r) { mid = (l+r)>>1; if (check(x, y, mid) >= k) r = mid; else l = mid; } return l; }
signedmain() { cin >> n; s = sqrt(n); for (int i = 1; i <= n; i++) { cin >> a[i]; } cin >> m; for (int i = 1; i <= m; i++) { cin >> q[i].l >> q[i].r; q[i].id = i; } sort(q+1, q+m+1, cmp); // 处理询问 int l = 0, r = 0; for (int i = 1; i <= m; i++) { while (r < q[i].r) add(++r); while (r > q[i].r) del(r--); while (l < q[i].l) del(l++); while (l > q[i].l) add(--l); ans[q[i].id] = tot; } for (int i = 1; i <= m; i++) { cout << ans[i] << endl; } return0; }