七人组-赛前训练9——总结
A - 洛谷 P2801
通过分块处理修改和询问。
#include<bits/stdc++.h>
typedef int int32;
#define int long long
using namespace std;
const int N = 1e6 + 5, M = 1e3 + 5;
int n, q, a[N], b[N], id[N], tag[N], sum[N], cnt, len;
pair<int, int>c[M][M];
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n >> q;
len = sqrt(n);
for (int i = 1; i <= n; i++)
{
if (i % len == 1)
cnt++;
id[i] = cnt;
sum[cnt]++;
}
for (int i = 1; i <= n; i++)
cin >> a[i], c[id[i]][(i - 1) % len + 1] = { a[i],i };
for (int i = 1; i <= cnt; i++)
{
sort(c[i] + 1, c[i] + 1 + sum[i]);
for (int j = 1; j <= sum[i]; j++)
b[c[i][j].second] = j;
}
while (q--)
{
char opt;
int l, r, x;
cin >> opt >> l >> r >> x;
if (opt == 'M')
{
for (int i = id[l] + 1; i < id[r]; i++)
tag[i] += x;
for (int i = l; id[i] == id[l] && i <= r; i++)
c[id[l]][b[i]].first += x;
sort(c[id[l]] + 1, c[id[l]] + 1 + sum[id[l]]);
for (int i = 1; i <= sum[id[l]]; i++)
b[c[id[l]][i].second] = i;
if (id[l] != id[r])
{
for (int i = r; id[i] == id[r]; i--)
c[id[r]][b[i]].first += x;
sort(c[id[r]] + 1, c[id[r]] + 1 + sum[id[r]]);
for (int i = 1; i <= sum[id[r]]; i++)
b[c[id[r]][i].second] = i;
}
}
else
{
int ans = 0;
for (int i = id[l] + 1; i < id[r]; i++)
{
auto tmp = lower_bound(c[i] + 1, c[i] + 1 + sum[i],
pair<int, int>{x - tag[i], 0});
ans += c[i] + 1 + sum[i] - tmp;
}
for (int i = l; id[i] == id[l] && i <= r; i++) // <-- 分块写错了,挂了 10 分。
ans += c[id[l]][b[i]].first + tag[id[l]] >= x;
if (id[l] != id[r])
for (int i = r; id[i] == id[r]; i--)
ans += c[id[r]][b[i]].first + tag[id[r]] >= x;
cout << ans << '\n';
}
}
return 0;
}B - 洛谷 P5012
并查集预处理选 的得分最大值,分块存储区间的最大值(因为 ST 表会内存超限)。
#include<bits/stdc++.h>
typedef int int32;
#define int long long
using namespace std;
const int N = 1e6 + 5, M = 1e3 + 5;
int n, t, a[N], fa[N], sum[N], ans, sum1, len, id[N], cnt, lastans;
pair<int, int>b[N], ans1[N], c[M];
int find(int x)
{
return x == fa[x] ? x : fa[x] = find(fa[x]);
}
void unionn(int x, int y)
{
x = find(x);
y = find(y);
if (x != y)
{
fa[x] = y;
ans -= sum[x] * sum[x] + sum[y] * sum[y];
sum[y] += sum[x];
ans += sum[y] * sum[y];
}
return;
}
pair<int, int>work(pair<int, int>x, pair<int, int>y)
{
if (x == pair<int, int>{0, 0})
return y;
if (x.second * y.first == x.first * x.second)
return y.second > x.second ? y : x;
return x.first * y.second < y.first * x.second ? y : x; // 这里不要写错
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n >> t;
for (int i = 1; i <= n; i++)
cin >> a[i], b[i] = { a[i],i };
sort(b + 1, b + 1 + n);
for (int i = 1; i <= n; i++)
{
if (i > 1 && b[i].first != b[i - 1].first)
ans1[sum1] = work(ans1[sum1], { ans,b[i - 1].first });
int tmp = b[i].second;
fa[tmp] = tmp;
sum[tmp] = 1;
ans++;
sum1++;
if (fa[tmp - 1])
unionn(tmp - 1, tmp), sum1--;
if (fa[tmp + 1])
unionn(tmp + 1, tmp), sum1--;
}
ans1[sum1] = work(ans1[sum1], { ans,b[n].first });
len = sqrt(n);
for (int i = 1; i <= n; i++)
{
if (i % len == 1)
cnt++;
id[i] = cnt;
c[cnt] = work(c[cnt], ans1[i]);
}
while (t--)
{
int a, b, x, y, l, r;
cin >> a >> b >> x >> y;
l = (a * lastans + x - 1) % n + 1;
r = (b * lastans + y - 1) % n + 1;
if (l > r)
swap(l, r);
pair<int, int>ans = { 0,0 };
for (int i = id[l] + 1; i < id[r]; i++)
ans = work(ans, c[i]);
for (int i = l; id[i] == id[l] && i <= r; i++)
ans = work(ans, ans1[i]);
if (id[l] != id[r])
for (int i = r; id[i] == id[r]; i--)
ans = work(ans, ans1[i]);
if (ans == pair<int, int>{0, 0})
ans = { -1,-1 };
cout << ans.first << ' ' << ans.second << '\n' << l << ' '
<< r << ' ' << lastans % n << '\n';
lastans = ans.first * ans.second % n;
}
return 0;
}C - 洛谷P11659
采用莫队离线处理。维护三个计数数组:
- 表示当前区间内值为 的元素个数;
- 表示当前区间内能与值 构成比例 关系的元素的 个数;
- 表示当前区间内能与值 构成比例 2:3 的元素 的个数。
#include<bits/stdc++.h>
typedef int int32;
#define int long long
using namespace std;
const int N = 2e5 + 5;
int n, q, len, a[N], ans[N], cnt[N], cnt42[N], cnt23[N], sum;
struct node {
int l, r, id;
bool operator<(const node& u) const
{
if (l / len != u.l / len)
return l < u.l;
if ((l / len) & 1)
return r < u.r;
return r > u.r;
}
}b[N];
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n >> q;
len = sqrt(n);
for (int i = 1; i <= n; i++)
cin >> a[i];
for (int i = 1; i <= q; i++)
{
cin >> b[i].l >> b[i].r;
b[i].id = i;
}
sort(b + 1, b + 1 + q);
int l = 1, r = 0;
for (int i = 1; i <= q; i++)
{
while (l > b[i].l)
{
l--;
cnt[a[l]]++;
if (!(a[l] % 4))
cnt42[a[l] / 2] += cnt[a[l] / 2];
if (~a[l] & 1)
cnt23[a[l]] += cnt[a[l] / 2 * 3];
if (!(a[l] % 4))
sum += cnt23[a[l] / 2];
}
while (r < b[i].r)
{
r++;
cnt[a[r]]++;
if (!(a[r] % 2))
cnt42[a[r]] += cnt[a[r] * 2];
if (!(a[r] % 3))
cnt23[a[r] / 3 * 2] += cnt[a[r] / 3 * 2];
if (!(a[r] % 3))
sum += cnt42[a[r] / 3 * 2];
}
while (l < b[i].l)
{
cnt[a[l]]--;
if (!(a[l] % 4))
cnt42[a[l] / 2] -= cnt[a[l] / 2];
if (~a[l] & 1)
cnt23[a[l]] -= cnt[a[l] / 2 * 3];
if (!(a[l] % 4))
sum -= cnt23[a[l] / 2];
l++;
}
while (r > b[i].r)
{
cnt[a[r]]--;
if (!(a[r] % 2))
cnt42[a[r]] -= cnt[a[r] * 2];
if (!(a[r] % 3))
cnt23[a[r] / 3 * 2] -= cnt[a[r] / 3 * 2];
if (!(a[r] % 3))
sum -= cnt42[a[r] / 3 * 2];
r--;
}
ans[b[i].id] = sum;
}
for (int i = 1; i <= q; i++)
cout << ans[i] << '\n';
return 0;
}D - 洛谷 P4135
利用分块处理区间合并,存储区间 的答案和前缀和。
#include<bits/stdc++.h>
typedef int int32;
#define int long long
using namespace std;
const int N = 1e5 + 5, M = 400 + 5;
int n, c, m, ans, id[N], cnt, a[N], sum[M][N],
tot[N], ans1[M][M], len;
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n >> c >> m;
len = sqrt(n);
for (int i = 1; i <= n; i++)
{
cin >> a[i];
if (i % len == 1)
{
cnt++;
for (int j = 1; j <= c; j++)
sum[cnt][j] = sum[cnt - 1][j];
}
id[i] = cnt;
sum[id[i]][a[i]]++;
}
for (int i = 1; i <= cnt; i++)
{
int sum = 0;
for (int j = i; j <= cnt; j++)
{
for (int k = j * len - (len - 1);
k <= min(n, j * len); k++)
{
tot[a[k]]++;
if ((tot[a[k]] & 1) && tot[a[k]] > 1)
sum--;
else if (~tot[a[k]] & 1)
sum++;
}
ans1[i][j] = sum;
}
for (int j = 1; j <= c; j++)
tot[j] = 0;
}
while (m--)
{
int l, r;
cin >> l >> r;
l = (l + ans) % n + 1;
r = (r + ans) % n + 1;
if (l > r)
swap(l, r);
ans = 0;
if (id[l] == id[r])
{
for (int i = l; i <= r; i++)
{
tot[a[i]]++;
if ((tot[a[i]] & 1) && tot[a[i]] > 1)
ans--;
else if (~tot[a[i]] & 1)
ans++;
}
for (int i = l; i <= r; i++)
tot[a[i]] = 0;
}
else
{
ans = ans1[id[l] + 1][id[r] - 1];
for (int i = l; id[i] == id[l]; i++)
{
tot[a[i]]++;
int tmp = sum[id[r] - 1][a[i]] - sum[id[l]][a[i]] +
tot[a[i]];
if ((tmp & 1) && tmp > 1)
ans--;
else if (~tmp & 1)
ans++;
}
// <-- 这里不应该清空
if (id[l] != id[r])
for (int i = r; id[i] == id[r]; i--)
{
tot[a[i]]++;
int tmp = sum[id[r] - 1][a[i]] -
sum[id[l]][a[i]] + tot[a[i]];
if ((tmp & 1) && tmp > 1)
ans--;
else if (~tmp & 1)
ans++;
}
for (int i = l; id[i] == id[l]; i++)
tot[a[i]] = 0;
if (id[l] != id[r])
for (int i = r; id[i] == id[r]; i--)
tot[a[i]] = 0;
}
cout << ans << '\n';
}
return 0;
}