a323困難離線位元運算・最後一次強制設定位元與 XOR 後綴

TOI 2019/05 潛力組:邏輯運算

避開對同一批倍數重複執行大量指令,依查詢編號的因數重建最終值。

前往題目練習 →回報教學錯誤設 U 是相異查詢數、D 是這些編號的有效因數組總數,B=32、R≤25000。時間 O(N+BR+U√N+BD log(R+1)),空間 O(N+BR+R)。N≤10⁶;每個查詢最多有 32 種強制時間。比逐條修改全部倍數的 O(NR) 更能處理重複小 P。

解題思路

原題未界定 P、Q 或負數位元表示;本教學明示採 P 為正整數、合法查詢編號與 32 位元二補數的常見解讀,Q 只取低 32 位,這些未明示邊界仍待出題方確認。對每一個位元,AND 的 0 或 OR 的 1 會強制設定值,AND 的 1、OR 的 0 不影響值,XOR 的 1 則翻轉。最終值只取決於最後一次強制設定及它後面的 XOR;沒有強制設定的位元從初值出發。

依 P 分組,記錄每個位元的最後強制設定時間與值,並保存 XOR 指令時間及 XOR 前綴。查詢編號 i 時只考慮 P|i 的組;枚舉 i 的因數,合併各位元的最後設定,再依設定時間分組成位元遮罩。對每個時間遮罩、每個因數組,用二分搜尋求該時間之後的 XOR 後綴並套用。修改都在查詢前,因此資料可一次建完,重複查詢另有快取。此法保留跨 P 的時間順序,不能將各 P 的完整轉換任意交換。

Step-by-step animation

較小示例:初值 7,7,7,7;AND(1,6)、OR(2,1)、XOR(1,3)

回報動畫錯誤
步驟 1 / 3

AND 清最低位

四個值由 111 變成 110。

6
6
6
6
最低位最後設定時間
1
設定值
0

Reference solution

回報程式錯誤

完整程式

#include <iostream>
#include <vector>
#include <array>
#include <unordered_map>
#include <map>
#include <algorithm>
#include <cstdint>
using namespace std;
struct Group {
    array<int, 32> last{};
    uint32_t ones = 0;
    vector<int> times;
    vector<uint32_t> prefix{0};
};
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;
    cin >> n;
    vector<uint32_t> initial(n + 1);
    for (int i = 1; i <= n; ++i) {
        long long x;
        cin >> x;
        initial[i] = (uint32_t)x;
    }
    unordered_map<int, Group> groups;
    unordered_map<int, uint32_t> cache;
    int type, time = 0;
    while (cin >> type && type) {
        int p;
        cin >> p;
        ++time;
        if (type != 1) {
            long long q;
            cin >> q;
            if (p <= 0 || p > n)
                continue;
            uint32_t mask = (uint32_t)q;
            auto &g = groups[p];
            if (type == 4) {
                g.times.push_back(time);
                g.prefix.push_back(g.prefix.back() ^ mask);
            } else {
                uint32_t force = type == 2 ? ~mask : mask;
                for (int b = 0; b < 32; ++b)
                    if (force & (uint32_t(1) << b)) {
                        g.last[b] = time;
                        if (type == 3)
                            g.ones |= uint32_t(1) << b;
                        else
                            g.ones &= ~(uint32_t(1) << b);
                    }
            }
            continue;
        }
        if (p < 1 || p > n)
            continue;
        if (!cache.count(p)) {
            vector<const Group *> applicable;
            auto include = [&](int d) {
                auto it = groups.find(d);
                if (it != groups.end())
                    applicable.push_back(&it->second);
            };
            for (int d = 1; 1LL * d * d <= p; ++d)
                if (p % d == 0) {
                    include(d);
                    if (d * d != p)
                        include(p / d);
                }
            array<int, 32> last{};
            uint32_t value = initial[p];
            for (auto g : applicable)
                for (int b = 0; b < 32; ++b)
                    if (g->last[b] > last[b]) {
                        last[b] = g->last[b];
                        uint32_t bit = uint32_t(1) << b;
                        value = (value & ~bit) | (g->ones & bit);
                    }
            map<int, uint32_t> masks;
            for (int b = 0; b < 32; ++b)
                masks[last[b]] |= uint32_t(1) << b;
            for (auto g : applicable)
                for (auto [cutoff, mask] : masks) {
                    int k =
                        upper_bound(g->times.begin(), g->times.end(), cutoff) - g->times.begin();
                    value ^= (g->prefix.back() ^ g->prefix[k]) & mask;
                }
            cache[p] = value;
        }
        uint32_t value = cache[p];
        cout << (int64_t(value) - (value >> 31 ? 4294967296LL : 0)) << '\n';
    }
}

先試著自己完成,再切換語言比對解法。參考程式可複製到題目編輯器。

更多