# 離線

becaido

• 建國中學 陳柏凱
• OJ handle：becaido

# 莫隊

Mo's Algorithm

$$N,Q\leq 10^5$$

$$O(1)$$ 維護目前眾數是多少

$$B 取 \frac{N}{\sqrt{Q}}$$ 最好

$$N,Q\leq 10^5,a_i\leq 10^9$$

$$p_i=a_1\oplus a_2\oplus\dots\oplus a_i$$

$$l\leq i\leq j\leq r$$

$$p_j\oplus p_{i-1}$$ 的最大值

01-Trie $$O(\log C)$$ 求 xor 極值

$$B$$ 一樣取 $$\frac{N}{\sqrt{Q}}$$

# 整體二分搜

#include <bits/stdc++.h>
using namespace std;

using ll = long long;

const int N = 1e5 + 5;

int n, m, q;
int ans[N];
int a[N], V[N];
vector<int> p[N];
int L[N], R[N], C[N];

int last[N];
ll sum[N];

ll bit[N];
void upd(int pos, int x) {
for (; pos <= m; pos += pos & -pos) bit[pos] += x;
}
ll que(int pos) {
ll re = 0;
for (; pos; pos -= pos & -pos) re += bit[pos];
return re;
}
ll que(int l, int r) {
return que(r) - que(l - 1);
}

void divide(int l, int r, vector<int> &ask) {
if (l == r || ask.size() == 0) {
for (int i : ask) ans[i] = l;
return;
}
int mid = (l + r) / 2;
vector<tuple<int, int, int>> op;
vector<int> pos;
for (int i = l; i <= mid; i++) {
op.emplace_back(L[i], L[i], C[i]);
op.emplace_back(R[i] + 1, L[i], -C[i]);
}
for (int i : ask) pos.insert(pos.end(), p[i].begin(), p[i].end());
sort(op.begin(), op.end());
sort(pos.begin(), pos.end());
int cur = 0;
for (int i : pos) {
while (cur < op.size() && get<0>(op[cur]) <= i) {
upd(get<1>(op[cur]), get<2>(op[cur]));
cur++;
}
sum[a[i]] += que(last[a[i]] + 1, i);
last[a[i]] = i;
}
for (int i : ask) {
else {
V[i] -= sum[i];
}
last[i] = sum[i] = 0;
}
for (int i = 0; i < cur; i++) upd(get<1>(op[i]), -get<2>(op[i]));
}

int main() {
ios::sync_with_stdio(false), cin.tie(0);
cin >> n >> m >> q;
for (int i = 1; i <= m; i++) cin >> a[i], p[a[i]].emplace_back(i);
for (int i = 1; i <= n; i++) cin >> V[i];
for (int i = 1; i <= q; i++) cin >> L[i] >> R[i] >> C[i];
for (int i = 1; i <= n; i++) {
if (ans[i] == q + 1) ans[i] = -1;
cout << ans[i] << '\n';
}
}

# 操作分塊

$$O(N+Q^2)$$

$$O(QK+\frac{NQ}{K})$$

$$K$$ 取 $$\sqrt{N}$$ 複雜度可以到 $$Q\sqrt{N}$$

TIOJ 2202：King Game

$$Q$$ 個詢問，每次給 $$L,R,x,y$$

$$N,Q,a_i,w_i\leq 10^5$$

By becaido

• 290