跳到正文

题解

ABC461赛后总结

WRT_Partisan约 15 分钟
原文
文章信息与版本

文章信息

发布
2026年6月6日
收录
2026年6月6日
更新
2026年11月14日
篇幅
约 15 分钟

版本

  1. v2当前
  2. v1

前言

时隔多年多周,At 终于出了一场正常的比赛。以及我在下午上课时说这次 At 会有线段树(原话是:“出道线段树吧求你了”),然后应验了。

这场神了

A - Armor

题目大意

判断 A(1≤A≤100)A(1\le A\le100) 是否小于等于 D(1≤D≤100)D(1\le D\le100),是则输出 Yes,否则输出 No。

思路

封装一个高精模板并重载小于号直接判断即可。

代码

C++
/*
 * @FilePath: \c++\ABC461A.cpp
 * @Author: WRT_Partisan
 * @Date: 2026-06-06 19:52:04
 */
#include <bits/stdc++.h>
using namespace std;
#define int long long
int a, d;
signed main()
{
    cin.tie(0)->sync_with_stdio(0);
    cin >> a >> d;
    cout << (a <= d ? "Yes" : "No");
    return 0;
}

B - The Honest Woodcutters

题目大意

“你掉的是这把金斧头,还是这把银斧头”

“不,两把都是我的”

N(1≤N≤100)N(1\le N\le100) 把斧子 NN 个樵夫,第 ii 个樵夫声称自己的斧子是 Ai(1≤Ai≤N)A_i(1\le A_i\le N),而斧子 ii 是樵夫 Bi(1≤Bi≤N)B_i(1\le B_i\le N) 的,判断是否每个樵夫都没有说谎,是则输出 Yes,否则输出 No。

思路

∀1≤i≤N\forall 1\le i\le N,判断 ABiA_{B_i} 是否等于 ii 即可。

代码

C++
/*
 * @FilePath: \c++\ABC461B.cpp
 * @Author: WRT_Partisan
 * @Date: 2026-06-06 19:52:59
 */
#include <bits/stdc++.h>
using namespace std;
#define int long long
int n, a[105], b[105];
signed main()
{
    cin.tie(0)->sync_with_stdio(0);
    cin >> n;
    for (int i = 1; i <= n; i++)
        cin >> a[i];
    for (int i = 1; i <= n; i++)
        cin >> b[i];
    for (int i = 1; i <= n; i++)
        if (a[b[i]] != i)
            cout << "No", exit(0);
    cout << "Yes";
    return 0;
}

C - Variety

题目大意

有 NN 个宝石,第 ii 个宝石的颜色为 Ci(1≤Ci≤N)C_i(1\le C_i\le N),价值为 Vi(1≤Vi≤109)V_i(1\le V_i\le10^9)。从中选出 KK 颗宝石,要求选出的宝石至少有 MM 中不同的颜色,求选出宝石价值之和的最大值。1≤M≤K≤N≤2×1051\le M\le K\le N\le2\times10^5。

思路

考虑贪心。

要保证有 MM 种颜色,那么就先从每种颜色价值最大的宝石中从大到小选 MM 个,再从剩下的宝石中从大到小选 K−MK-M 个。

对 vector 从大到小排序可以用 sort(a.rbegin(), a.rend());。

究竟是谁用 set 吃了一记罚时啊

代码

C++
/*
 * @FilePath: \c++\ABC461C.cpp
 * @Author: WRT_Partisan
 * @Date: 2026-06-06 19:53:10
 */
#include <bits/stdc++.h>
using namespace std;
#define int long long
int n, k, m, c, v, mx[200005], ans, book[200005];
vector<int> a[200005], f;
vector<pair<int, int>> b;
signed main()
{
    cin.tie(0)->sync_with_stdio(0);
    cin >> n >> k >> m;
    for (int i = 1; i <= n; i++)
        cin >> c >> v, a[c].push_back(v);
    for (int i = 1; i <= n; i++)
        sort(a[i].rbegin(), a[i].rend());
    for (int i = 1; i <= n; i++)
        if (!a[i].empty())
            b.push_back({a[i][0], i});
    sort(b.rbegin(), b.rend());
    for (int i = 0; i < m; i++)
        ans += b[i].first, book[b[i].second] = 1;
    for (int i = 1; i <= n; i++)
    {
        if (book[i])
            for (int j = 1; j < a[i].size(); j++)
                f.push_back(a[i][j]);
        else
            for (auto j : a[i])
                f.push_back(j);
    }
    sort(f.rbegin(), f.rend());
    for (int i = 0; i < k - m; i++)
        ans += f[i];
    cout << ans;
    return 0;
}

D - Count Subgrid Sum = K

题目大意

有一个 N×M(1≤N,M≤500)N\times M(1\le N,M\le500) 的矩阵,每个元素为 00 或 11,求满足元素之和等于 K(0≤K≤NM)K(0\le K\le NM) 的子矩阵的个数。

思路

若是使用二维前缀和,需要枚举子矩阵的两个端点,时间复杂度 O(N2M2)O(N^2M^2),效率太低。

考虑优化。发现子矩阵的扩充和缩减具有单调性,即扩充当前子矩阵时元素之和一定不减,缩减当前子矩阵时元素之和一定不增,那么可以使用双指针。

在 MM 维上跑双指针,这样只用枚举行的范围,加上双指针,时间复杂度 O(N2M)O(N^2M),勉强可过。

二位前缀和太过麻烦,且计算时容易出错,可以考虑在枚举下界的时候,顺便算出每一列的元素之和,这样会简单很多。

跑双指针时需要注意找到满足条件的子矩阵时要继续向后扩张,因为有可能后面全是 00,这一点从样例一可以看出。

注意需要特别判断 K=0K=0 的情况,为统计全 00 子矩阵,计算方法和答案是不一样的,究竟是谁因为这个吃了一记罚时啊。

代码

C++
/*
 * @FilePath: \c++\ABC461D.cpp
 * @Author: WRT_Partisan
 * @Date: 2026-06-06 19:53:19
 */
#include <bits/stdc++.h>
using namespace std;
#define int long long
int n, m, a[505][505], k, ans, sm[505];
char c;
signed main()
{
    cin.tie(0)->sync_with_stdio(0);
    cin >> n >> m >> k;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            cin >> c, a[i][j] = c - '0';
    if (k == 0)
    {
        for (int xa = 1; xa <= n; xa++)
        {
            memset(sm, 0, sizeof(sm));
            for (int xb = xa; xb <= n; xb++)
            {
                for (int j = 1; j <= m; j++)
                    sm[j] += a[xb][j];
                int l = 0;
                for (int j = 1; j <= m; j++)
                {
                    if (sm[j] == 0)
                        l++, ans += l;
                    else
                        l = 0;
                }
            }
        }
        cout << ans;
        return 0;
    }
    for (int xa = 1; xa <= n; xa++)
    {
        memset(sm, 0, sizeof(sm));
        for (int xb = xa; xb <= n; xb++)
        {
            for (int j = 1; j <= m; j++)
                sm[j] += a[xb][j];
            int sum = 0;
            for (int l = 1, r = 0; l <= m; l++)
            {
                while (r < m && sum < k)
                    sum += sm[++r];
                if (sum == k)
                {
                    int tmp = sum, rt = r;
                    while (rt <= m && tmp == k)
                        ans++, rt++, tmp += rt <= m ? sm[rt] : 0;
                }
                if (l <= r)
                    sum -= sm[l];
                else
                    r = l - 1;
            }
        }
    }
    cout << ans;
    return 0;
}

E - E-liter

题目大意

一个 N×N(1≤N≤3×105)N\times N(1\le N\le3\times10^5) 的网格,有 Q(1≤Q≤3×105)Q(1\le Q\le3\times10^5) 次操作,每次操作为将某行涂黑或将某列涂白,每次操作后输出网格中黑色点的数量。

思路

考虑计算每次操作的贡献。发现涂一行的贡献为在该行上次被涂之后涂过的列数,同理涂一列的贡献为在该列上次被涂之后涂过的行数。

我们可以记录每行和每列最近一次被涂的时间,然后用线段树按时间为索引维护该时间(段)有多少个行或列被涂过。计算贡献就取上次被涂时间和这次被涂时间之间的行或列数,若是涂行就加上,若是涂列就减去。

代码

C++
/*
 * @FilePath: \c++\ABC461E.cpp
 * @Author: WRT_Partisan
 * @Date: 2026-06-06 19:53:27
 */
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define mid ((l + r) >> 1)
int n, q, op, x, ht[300005], lt[300005], ans;
struct ST
{
    int s[1200005];
    void pushup(int x) { s[x] = s[x << 1] + s[x << 1 | 1]; }
    void change(int x, int l, int r, int p, int u)
    {
        if (l == r)
            return s[x] += u, void();
        if (p <= mid)
            change(x << 1, l, mid, p, u);
        else
            change(x << 1 | 1, mid + 1, r, p, u);
        pushup(x);
    }
    int ask(int x, int l, int r, int ql, int qr)
    {
        if (ql <= l && r <= qr)
            return s[x];
        int res = 0;
        if (ql <= mid)
            res += ask(x << 1, l, mid, ql, qr);
        if (mid < qr)
            res += ask(x << 1 | 1, mid + 1, r, ql, qr);
        return res;
    }
} h, l;
signed main()
{
    cin.tie(0)->sync_with_stdio(0);
    cin >> n >> q, h.change(1, 1, q + 1, 1, n), l.change(1, 1, q + 1, 1, n);
    for (int i = 1; i <= q; i++)
    {
        cin >> op >> x;
        if (op == 1)
        {
            ans += l.ask(1, 1, q + 1, 1, i) - (ht[x] == 0 ? 0 : l.ask(1, 1, q + 1, 1, ht[x]));
            h.change(1, 1, q + 1, ht[x] + 1, -1);
            h.change(1, 1, q + 1, i + 1, 1);
            ht[x] = i;
        }
        else
        {
            ans += h.ask(1, 1, q + 1, 1, lt[x] + 1) - h.ask(1, 1, q + 1, 1, i + 1);
            l.change(1, 1, q + 1, lt[x] + 1, -1);
            l.change(1, 1, q + 1, i + 1, 1);
            lt[x] = i;
        }
        cout << ans << "\n";
    }
    return 0;
}

附

官方题解

这是 E 题官方题解的 Python 代码,非常短,并用了神秘 AtCoder Library 中封装的树状数组。

后记

Rating

从我的 Rating 变化就可以看出前几次 ABC 有多恶心,这回终于可以一雪前耻,若不是 C 和 D 各吃了一记罚时,排名说不定能进一千。

明天是 6 月 7 日,是高考的第一天(那今天岂不是 Day 0)。让我们抛开 Alea 的理论,并祝即将参加高考的诸位:rp++;。

zifu 的神秘表情包:

地铁站都比你努力

原文