数列分块入门3
题意
有 个数, 次操作,每次操作为区间加或者询问区间内某个数的前驱(即比这个数小的最大元素)。
解法
分块。
- 每一个块维护一个有序数列。
- 区间加的小块暴力加即可,大块就加到一个 数组中,复杂度为
- 询问的时候小块只要暴力找满足 的值并维护 的最大值即可,大块二分查找 即可,如果不是第一个元素,就说明存在比 小的元素,维护最大值即可。复杂度为 。
- 所以总复杂度为
代码
#pragma region
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, a, n) for (int i = a; i <= n; ++i)
#define per(i, a, n) for (int i = n; i >= a; --i)
namespace fastIO {
#define BUF_SIZE 100000
#define OUT_SIZE 100000
//fread->R
bool IOerror = 0;
//inline char nc(){char ch=getchar();if(ch==-1)IOerror=1;return ch;}
inline char nc() {static char buf[BUF_SIZE], *p1 = buf + BUF_SIZE, *pend = buf + BUF_SIZE;if (p1 == pend) {p1 = buf;pend = buf + fread(buf, 1, BUF_SIZE, stdin);if (pend == p1) {IOerror = 1;return -1;}}return *p1++;
}
inline bool blank(char ch) { return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t'; }
template <class T>
inline bool R(T &x) {bool sign = 0;char ch = nc();x = 0;for (; blank(ch); ch = nc());if (IOerror) return false;if (ch == '-') sign = 1, ch = nc();for (; ch >= '0' && ch <= '9'; ch = nc()) x = x * 10 + ch - '0';if (sign) x = -x;return true;
}
inline bool R(double &x) {bool sign = 0;char ch = nc();x = 0;for (; blank(ch); ch = nc());if (IOerror) return false;if (ch == '-') sign = 1, ch = nc();for (; ch >= '0' && ch <= '9'; ch = nc()) x = x * 10 + ch - '0';if (ch == '.') {double tmp = 1;ch = nc();for (; ch >= '0' && ch <= '9'; ch = nc())tmp /= 10.0, x += tmp * (ch - '0');}if (sign)x = -x;return true;
}
inline bool R(char *s) {char ch = nc();for (; blank(ch); ch = nc());if (IOerror)return false;for (; !blank(ch) && !IOerror; ch = nc())*s++ = ch;*s = 0;return true;
}
inline bool R(char &c) {c = nc();if (IOerror) {c = -1;return false;}return true;
}
template <class T, class... U>
bool R(T &h, U &... t) { return R(h) && R(t...); }
#undef OUT_SIZE
#undef BUF_SIZE
}; // namespace fastIO
using namespace fastIO;
template <class T>
void _W(const T &x) { cout << x; }
void _W(const int &x) { printf("%d", x); }
void _W(const int64_t &x) { printf("%lld", x); }
void _W(const double &x) { printf("%.16f", x); }
void _W(const char &x) { putchar(x); }
void _W(const char *x) { printf("%s", x); }
template <class T, class U>
void _W(const pair<T, U> &x) { _W(x.F), putchar(' '), _W(x.S); }
template <class T>
void _W(const vector<T> &x) {for (auto i = x.begin(); i != x.end(); _W(*i++))if (i != x.cbegin()) putchar(' ');
}
void W() {}
template <class T, class... U>
void W(const T &head, const U &... tail) { _W(head), putchar(sizeof...(tail) ? ' ' : '\n'), W(tail...); }
#pragma endregion
const int maxn = 1e5 + 5;
ll a[maxn], B, n;
ll base[maxn];
vector<ll> v[maxn];
void update(int l, int r, ll x) {int idl = l / B, idr = r / B;if (idl == idr) {rep(i, l, r) a[i] += x;v[idl].clear();rep(i, max(1LL, idl * B), min((idl + 1) * B - 1, n)) v[idl].push_back(a[i]);sort(v[idl].begin(), v[idl].end());} else {rep(i, l, (idl + 1) * B - 1) a[i] += x;v[idl].clear();rep(i, max(1LL, idl * B), (idl + 1) * B - 1) v[idl].push_back(a[i]);sort(v[idl].begin(), v[idl].end());rep(id, idl + 1, idr - 1) base[id] += x;rep(i, idr * B, r) a[i] += x;v[idr].clear();rep(i, idr * B, min(n, (idr + 1) * B - 1)) v[idr].push_back(a[i]);sort(v[idr].begin(), v[idr].end());}
}
ll query(int l, int r, ll x) {int idl = l / B, idr = r / B;ll ans = -1e18;if (idl == idr) {rep(i, l, r) if (a[i] + base[idl] < x) ans = max(ans, a[i] + base[idl]);} else {rep(i, l, (idl + 1) * B - 1) if (a[i] + base[idl] < x) ans = max(ans, a[i] + base[idl]);rep(id, idl + 1, idr - 1) {auto it = lower_bound(v[id].begin(), v[id].end(), x - base[id]);if (it != v[id].begin()) ans = max(ans, *(--it) + base[id]);}rep(i, idr * B, r) if (a[i] + base[idr] < x) ans = max(ans, a[i] + base[idr]);}if (ans == -1e18) ans = -1;return ans;
}
int main() {R(n);B = sqrt(n);rep(i, 1, n) {R(a[i]);v[i / B].push_back(a[i]);if (i % B == B - 1) sort(v[i / B].begin(), v[i / B].end());}rep(i, 1, n) {ll op, l, r, c;R(op, l, r, c);if (op == 0)update(l, r, c);elseW(query(l, r, c));}
}