Link

A

前缀和,考虑答案的每个位置该为 00 还是 11

O(31n+31q)O(31n+31q)

C++ Code
#include "bits/stdc++.h"

using namespace std;
using i64 = long long;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    cin >> n;
    vector<int> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
  
    vector<array<int, 31>> sum(n + 1);
    for (int i = 0; i < n; i++) {
        sum[i + 1] = sum[i];
        for (int j = 0; j < 31; j++) {
            sum[i + 1][j] += (a[i] >> j & 1);
        }
    }

    int q;
    cin >> q;
    while (q--) {
        int l, r;
        cin >> l >> r;
        l--;

        int ans = 0;
        for (int j = 0; j < 31; j++) {
            if (2 * (sum[r][j] - sum[l][j]) < r - l) {
                ans += (1 << j);
            }
        }

        cout << ans << '\n';
    }

    return 0;
}

B

dp\text{dp}

D

O(logm)O(\log m)

C++ Code
#include "bits/stdc++.h"

using namespace std;
using i64 = long long;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int n, m;
    cin >> n >> m;
    n -= m;
    int ans = 0;
    while (n > 0) {
        auto cur = m;
        cur = (cur + 1) / 2;
        if (cur == m) {
            ans += (n + cur - 1) / cur; 
            break;
        } else {
            n -= cur;
            ans++;
        }
        m = cur;
    }
    cout << ans << '\n';
    
    return 0;
}

E

博弈,打表可得:sg(x)=xmod(a+b)a\text{sg}(x)=\frac{x\mod(a+b)}{a}

全部异或起来看是不是 00

O(n)O(n)

C++ Code
#include "bits/stdc++.h"

using namespace std;
using i64 = long long;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int n, a, b;
    cin >> n >> a >> b;

    auto sg = [&](int x) {
        return x % (a + b) / a;
    };

    int ans = 0;
    for (int i = 0; i < n; i++) {
        int x;
        cin >> x;
        ans ^= sg(x);
    }
    cout << (ans ? "C" : "Y") << '\n';
    
    return 0;
}

H

dp\text{dp}

O(nm)O(nm)

C++ Code
#include "bits/stdc++.h"

using namespace std;
using i64 = long long;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, m, k;
    cin >> n >> m >> k;
    vector<int> a(n);
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            int x;
            cin >> x;
            a[i] = max(a[i], x);
        }
    }

    vector<i64> f(n);
    i64 ans = 0;
    for (int i = 0; i < n; i++) {
        if (!i) {
            f[i] = a[i];
        } else {
            f[i] = max(f[i], f[i - 1]);
        }            
        if (i >= k) {
            f[i] = max(f[i], f[i - k] + a[i]);
        }
        ans = max(ans, f[i]);
    }
    cout << ans << '\n';
    
    return 0;
}

I

贪心。

O(32)O(32)

C++ Code
#include "bits/stdc++.h"

using namespace std;
using i64 = long long;

#define range(a) begin(a), end(a)

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    i64 n;
    cin >> n;

    vector<int> a;
    for (int i = 0; i < 32; i += 8) {
        int res = 0;
        for (int j = 7; j >= 0; j--) {
            res *= 2;
            res += n >> (i + j) & 1;
        }
        a.push_back(res);
    }

    sort(range(a), greater());
 
    i64 ans = 0;
    for (auto &ai : a) {
        ans *= 256;
        ans += ai;
    }
    cout << ans << '\n';
    
    return 0;
}

J

二维前缀和,可以用双指针优化一下,这里使用暴力。

O(n2m2)O(n^2m^2)

C++ Code
#include "bits/stdc++.h"

using namespace std;
using i64 = long long;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, m;
    cin >> n >> m;
    vector<int> a(n), b(m);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    for (int i = 0; i < m; i++) {
        cin >> b[i];
    }

    vector<vector<i64>> c(n + 1, vector<i64>(m + 1));
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            c[i + 1][j + 1] = 1LL * a[i] * b[j]; 
        }
    }

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            c[i + 1][j + 1] += c[i][j + 1] + c[i + 1][j] - c[i][j];
        }
    }

    int mx;
    cin >> mx;

    int ans = 0;
    for (int x = 1; x <= n; x++) {
        for (int y = 1; y <= m; y++) {
            for (int i = x; i <= n; i++) {
                for (int j = y; j <= m; j++) {
                    if (c[i][j] - c[i - x][j] - c[i][j - y] + c[i - x][j - y] <= mx) {
                        ans = max(ans, x * y);
                    }
                }
            }            
        }
    }
    cout << ans << '\n';

    return 0;
}

L

前缀和,枚举每个位置作为区间的右端点,看他的左边有几个满足条件的左端点。

这里使用 __gnu_pbds::tree\text{\_\_gnu\_pbds::tree}

O(nlogn)O(n\log n)

C++ Code
#include "bits/stdc++.h"

using namespace std;
using i64 = long long;

#include "ext/pb_ds/assoc_container.hpp"

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, x;
    cin >> n >> x;
    vector<int> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }

    vector<i64> sum(n + 1);
    for (int i = 0; i < n; i++) {
        sum[i + 1] = sum[i] + a[i];
    }
    
    i64 ans = 0;
    __gnu_pbds::tree<pair<i64, int>, __gnu_pbds::null_type, std::less<>,
                    __gnu_pbds::rb_tree_tag,
                    __gnu_pbds::tree_order_statistics_node_update> s;

    for (int i = 0; i <= n; i++) {
        ans += s.order_of_key({sum[i] - x + 1, 0});
        s.insert({sum[i], i});
    }
    cout << ans << '\n';

    return 0;
}