5 条题解

  • 0
    @ 2026-11-23 8:35:19

    思路提示

    提示 1

    只乘了序列中的一部分数,什么时候就已经能确定这一次的记录值?

    查看提示 1 的解答

    只要这部分数的乘积达到 VV,就能确定记录 00:其余的数都至少是 11,全部乘进去只会让乘积不变或更大。

    V = 10 时乘到第三个数就能确定记录 0

    图中 V=10V=10,乘到 2×3×4=242\times3\times4=24 时已经达到上限,后面的数不必再乘。没有达到 VV 时,就还要继续乘其余的数。

    提示 2

    设 V=10V=10,已经乘了 2,3,42,3,4。之后把 44 改成 11,如果只记得“已经达到 1010”,能算出这三个数的新乘积吗?

    查看提示 2 的解答

    不能。原乘积是 2424,改完应为 24÷4=624\div4=6,降到了上限以下;只记成 1010 就无法除回去。

    保存精确乘积才能除去旧值

    所以已乘部分要保存精确乘积,修改其中某个数时先除去旧值。精确乘积不会太大:只在乘积小于 VV 时才会新乘入大于 11 的数,而每个因子都不超过 VV,所以已乘部分的乘积始终小于 V2V^2。

    提示 3

    把位置分成“已乘入”和“暂缓”两类,已乘入部分的乘积达到了 VV。这时修改一个暂缓位置,记录会变吗?修改一个已乘入的位置,又该怎么办?

    查看提示 3 的解答

    • 修改暂缓位置:已乘入的数没有变,乘积仍达到 VV,记录仍为 00,只要记下这个位置的新值。
    • 修改已乘入的位置:先除去旧值。若新值大于 11 且乘积仍不小于 VV,把它入栈暂缓。否则,该位置留在已乘入部分,把新值乘进去。若乘积降到 VV 以下,就从暂缓的位置里逐个取出当前值乘入,直到达到 VV 或暂缓位置用完。

    V = 20 时已乘入部分与暂缓位置的变化

    图中青色是已乘入的位置,橙色是暂缓位置,蓝框是本次修改。位置 44 暂缓期间被改成 44,不必处理;位置 33 改为 11 后乘积降到 66,才取出位置 44 的当前值 44 乘入。

    提示 4

    按上一条的做法,一个位置要被乘入多少次?总的乘法次数是多少?

    查看提示 4 的解答

    一个位置被乘入后就一直留在已乘入部分,直到它自己被修改,才可能变成暂缓。暂缓期间的修改只改值,不产生新的暂缓记录。

    一个位置在两种状态之间的转换

    入栈只发生在初始读入或这个位置自己被修改时。所以每个位置只在初始时、以及每次它被修改时,最多新增一次暂缓;取出的次数不超过放入的次数。总共至多入栈 n+mn+m 次,出栈次数也不超过 n+mn+m。算上初始读入,所有操作总计 O(n+m)O(n+m)。

    简要题解

    所有数都至少是 11,只要一部分数的乘积达到 VV,本次就记录 00,其余位置可以暂缓。维护已乘入部分的精确乘积 QQ,并用一个栈保存暂缓的位置。只在 Q<VQ<V 时才乘入大于 11 的因子,而每个因子都不超过 VV,所以 Q<V2Q<V^2。

    • 修改暂缓位置:只改值,QQ 仍不小于 VV,记录 00。
    • 修改已乘入的位置:QQ 除以旧值。新值为 11 时留在已乘入部分。新值大于 11 时,若 Q≥VQ\ge V 就入栈暂缓,否则乘入。之后只要 Q<VQ<V 且栈不空,就弹出一个位置,把它的当前值乘入。

    V = 20 时已乘入部分与暂缓位置的变化

    停止时 Q≥VQ\ge V 就记录 00;栈空且 Q<VQ<V 时,所有数都已乘入,记录 QQ。把初始和每次修改后的记录异或起来。

    每个位置只在初始或自己被修改时才可能入栈,入栈与出栈合计 O(n+m)O(n+m) 次。时间复杂度 O(n+m)O(n+m),空间复杂度 O(n)O(n)。

    满分解法(100 分)

    直接每次重新计算乘积需要 O(nm)O(nm)。关键是两点:乘积一旦达到上限,剩下的数就不必再乘;而修改可能让乘积降回上限以下,所以已乘部分要保存精确值,暂缓的部分在需要时再补乘。

    达到上限时只需部分因子

    从 11 开始逐个乘入序列里的数。已经乘过的一部分达到 VV 时,其余数都至少是 11,全部乘进去只会更大,本次记录一定是 00,剩下的数可以暂缓。乘完所有数仍小于 VV 时,手里的乘积就是要记录的值。

    V = 10 时乘到第三个数就能确定记录 0

    图中 V=10V=10,前三个数的乘积 2424 已经达到上限,后面三个数无论是多少,都不会改变记录 00。

    已乘部分要保存精确乘积

    修改可能让乘积降回上限以下,此时需要从已乘部分除去旧值。如果把乘积截成 VV,就无法再除回去。

    保存精确乘积才能除去旧值

    图中 V=10V=10,已乘 2,3,42,3,4。精确乘积 2424 除以 44 得 66;只记成 1010 时无法得到 66。

    精确值不会溢出:只在已乘部分的乘积小于 VV 时才乘入大于 11 的因子,而每个因子都不超过 VV,所以它始终小于 V2≤1018V^2\le10^{18}。除去的旧值是它的一个因子,除法也是精确的。

    已乘入与暂缓两类位置

    把位置分为两类:已乘入的位置用精确乘积 QQ 概括;暂缓的位置放在一个栈中,栈里只存下标,当前值随修改保存在原数组里。初始读到 11 时,可以把该位置直接记为已乘入,因为它不改变乘积。

    开始时 Q=1Q=1,依次读入:遇到大于 11 的数,若 Q<VQ<V 就乘入,否则把位置压栈。读完就能得到初始记录。

    每次修改分两种情况:

    • 修改暂缓位置:只改数组里的值。栈中有位置说明 Q≥VQ\ge V,已乘入的数没有变,记录仍为 00。
    • 修改已乘入的位置:先让 QQ 除以旧值。新值为 11 时略过;大于 11 时,若 Q<VQ<V 就乘入,否则把它压栈。接着只要 Q<VQ<V 且栈不空,就弹出一个位置,把它的当前值乘入 QQ。

    停止时有两种结果:Q≥VQ\ge V,已乘入的数就足以说明总乘积达到上限,记录 00;Q<VQ<V,此时栈已空,所有数都已乘入,记录 QQ。

    V = 20 时已乘入部分与暂缓位置的变化

    图中初始序列为 [2,3,5,7][2,3,5,7],依次乘入 2,3,52,3,5 得 Q=30Q=30,位置 44 暂缓。把位置 44 改成 44 时只改值;把位置 33 改成 11 后 Q=6<20Q=6<20,弹出位置 44,乘入它的当前值 44 得 2424;最后把位置 44 改成 11,Q=24÷4=6Q=24\div4=6,记录 66。

    位置 44 暂缓期间始终只有一条栈记录,弹出时使用它最新的值,所以不必维护暂缓部分的乘积,也不必从栈中间删除。

    实现时可以用一个带符号的数组同时保存值和类别:正数表示已乘入,负数表示暂缓,绝对值是当前值。修改暂缓位置时写入新值的相反数;弹出时改回正数,即使当前值已经变成 11 也照常弹出。

    总共乘入多少次

    一个位置弹出后一直留在已乘入部分,直到它被修改,才可能再次入栈;暂缓期间的修改不产生新的栈记录。

    一个位置在两种状态之间的转换

    图中的入栈箭头对应这样一次修改:位置本来已乘入,除去旧值后 QQ 仍不小于 VV,且新值大于 11。需要补乘时,位置从暂缓回到已乘入。因此初始至多入栈 nn 次,每次修改至多新增一次入栈,弹出次数不超过入栈次数,合计 O(n+m)O(n+m)。

    复杂度

    • 时间复杂度:O(n+m)O(n+m),所有入栈、出栈与乘除合计 O(n+m)O(n+m) 次。
    • 空间复杂度:O(n)O(n),保存带类别的数组和暂缓栈。

    AC 代码(C++20)

    #include <bits/stdc++.h>
    using namespace std;
    using i64 = long long;
    
    inline int read_int() {
      constexpr int block = 1 << 20;
      static char buffer[block + 1];
      static char* next = buffer;
      static char* end = buffer;
    
      auto refill = [&]() {
        int remaining = end - next;
        memmove(buffer, next, remaining);
        int added = fread(buffer + remaining, 1, block - remaining, stdin);
        next = buffer;
        end = buffer + remaining + added;
        *end = '\0';
        return added > 0;
      };
    
      while (*next < '0' || *next > '9') {
        if (next != end) {
          next++;
        } else if (!refill()) {
          return 0;
        }
      }
    
      // The largest input token has ten digits.
      if (end - next < 11) refill();
      int value = 0;
      do {
        value = value * 10 + *next - '0';
        next++;
      } while ('0' <= *next && *next <= '9');
      return value;
    }
    
    void solve() {
      int n = read_int(), m = read_int(), v = read_int();
      vector<int> a(n), pending;
      pending.reserve(n);
      i64 prod = 1;
    
      // A negative value marks a waiting position.
      auto add = [&](int i, int x) {
        if (prod >= v && x != 1) {
          a[i] = -x;
          pending.push_back(i);
        } else {
          a[i] = x;
          prod *= x;
        }
      };
    
      for (int i = 0; i < n; i++) add(i, read_int());
      int ans = prod < v ? prod : 0;
      for (int j = 0; j < m; j++) {
        int x = read_int() - 1, y = read_int();
        int old = a[x];
        if (old < 0) {
          a[x] = -y;
          continue;
        }
        if (old != y) {
          if (old > 1) prod /= old;
          add(x, y);
          while (prod < v && !pending.empty()) {
            int i = pending.back();
            pending.pop_back();
            a[i] = -a[i];
            prod *= a[i];
          }
        }
        if (prod < v) ans ^= prod;
      }
      cout << ans << '\n';
    }
    
    int main() {
      solve();
    }
    
    • 0
      @ 2026-11-23 8:35:18

      只增不减时提前结束(通过子任务 7,共 5 分)

      本组每次修改都满足 ax≤ya_x\le y,即只把数改大。其余数都是正数,所以乘积不会下降;一旦达到 VV,本次和之后的记录都是 00,不再影响异或。

      从初始序列累乘。已达到 VV 时直接输出 00。否则保存小于 VV 的精确乘积 PP,计入异或。读到修改 (x,y)(x,y) 时,用

      P←Pax⋅yP\gets \frac{P}{a_x}\cdot y

      得到新乘积。新乘积达到 VV 就输出当前的异或并结束;否则把它异或进去,并令 ax=ya_x=y。

      时间复杂度为 O(n+m)O(n+m),空间复杂度为 O(n)O(n)。允许把数改小时,超过上限的乘积可能再降下来,既不能提前结束,也不能一直保存完整的巨大乘积。

      参考代码(C++20)

      #include <bits/stdc++.h>
      using namespace std;
      using i64 = long long;
      
      inline int read_int() {
        constexpr int block = 1 << 20;
        static char buffer[block + 1];
        static char* next = buffer;
        static char* end = buffer;
      
        auto refill = [&]() {
          int remaining = end - next;
          memmove(buffer, next, remaining);
          int added = fread(buffer + remaining, 1, block - remaining, stdin);
          next = buffer;
          end = buffer + remaining + added;
          *end = '\0';
          return added > 0;
        };
      
        while (*next < '0' || *next > '9') {
          if (next != end) {
            next++;
          } else if (!refill()) {
            return 0;
          }
        }
      
        // The largest input token has ten digits.
        if (end - next < 11) refill();
        int value = 0;
        do {
          value = value * 10 + *next - '0';
          next++;
        } while ('0' <= *next && *next <= '9');
        return value;
      }
      
      void solve() {
        int n = read_int(), m = read_int(), v = read_int();
        vector<int> a(n);
        i64 prod = 1;
        for (int& x : a) {
          x = read_int();
          prod *= x;
          if (prod >= v) {
            cout << 0 << '\n';
            return;
          }
        }
      
        int ans = prod;
        for (int j = 0; j < m; j++) {
          int x = read_int() - 1, y = read_int();
          if (a[x] != y) prod = prod / a[x] * y;
          if (prod >= v) break;
          a[x] = y;
          ans ^= prod;
        }
        cout << ans << '\n';
      }
      
      int main() {
        solve();
      }
      
      • 0
        @ 2026-11-23 8:35:18

        小值域计数(通过子任务 5–6,共 20 分)

        本组 V≤1000V\le1000,值域很小,可以对每个值计数。修改时把旧值的次数减一、新值的次数加一;值 11 不影响乘积,只统计大于 11 的值。

        设大于 11 的数有 kk 个,bb 为满足 2b≥V2^b\ge V 的最小整数。若 k≥bk\ge b,这些数每个至少是 22,乘积至少 2b≥V2^b\ge V,记录 00。否则至多有 b−1b-1 个大于 11 的数。从 11 开始逐个乘入,每乘入一个就检查乘积是否达到 VV,一旦达到就记录 00。全部乘完仍小于 VV 时才记录精确乘积。

        为了快速找到这些数,另用一个数组存放次数为正的不同值:某个值的次数从 00 变正时放到末尾;次数变回 00 时用末尾的值填补它的位置,并更新被移动的值的下标。k<bk<b 时遍历这个数组,按次数乘入,一共至多乘 b−1b-1 次。

        时间复杂度为 O(V+n+mlog⁡V)O(V+n+m\log V),空间复杂度为 O(n+V)O(n+V)。V≤10V\le10 时至多检查三个数,V≤1000V\le1000 时至多九个。一般情况 VV 可达 10910^9,无法按值开数组。

        参考代码(C++20)

        #include <bits/stdc++.h>
        using namespace std;
        using i64 = long long;
        
        inline int read_int() {
          constexpr int block = 1 << 20;
          static char buffer[block + 1];
          static char* next = buffer;
          static char* end = buffer;
        
          auto refill = [&]() {
            int remaining = end - next;
            memmove(buffer, next, remaining);
            int added = fread(buffer + remaining, 1, block - remaining, stdin);
            next = buffer;
            end = buffer + remaining + added;
            *end = '\0';
            return added > 0;
          };
        
          while (*next < '0' || *next > '9') {
            if (next != end) {
              next++;
            } else if (!refill()) {
              return 0;
            }
          }
        
          // The largest input token has ten digits.
          if (end - next < 11) refill();
          int value = 0;
          do {
            value = value * 10 + *next - '0';
            next++;
          } while ('0' <= *next && *next <= '9');
          return value;
        }
        
        void solve() {
          int n = read_int(), m = read_int(), v = read_int();
          vector<int> a(n), cnt(v + 1), pos(v + 1, -1), values;
          int non_ones = 0, cutoff = 0;
          for (i64 p = 1; p < v; p *= 2) cutoff++;
        
          auto adjust = [&](int x, int delta) {
            if (x == 1) return;
            non_ones += delta;
            int before = cnt[x];
            cnt[x] += delta;
            if (before == 0) {
              pos[x] = values.size();
              values.push_back(x);
            } else if (cnt[x] == 0) {
              int p = pos[x];
              values[p] = values.back();
              pos[values[p]] = p;
              values.pop_back();
              pos[x] = -1;
            }
          };
          auto record = [&]() -> int {
            if (non_ones >= cutoff) return 0;
            i64 prod = 1;
            for (int x : values) {
              for (int j = 0; j < cnt[x]; j++) {
                prod *= x;
                if (prod >= v) return 0;
              }
            }
            return prod;
          };
        
          for (int& x : a) {
            x = read_int();
            adjust(x, 1);
          }
          int ans = record();
          for (int j = 0; j < m; j++) {
            int x = read_int() - 1, y = read_int();
            if (a[x] != y) {
              adjust(a[x], -1);
              a[x] = y;
              adjust(y, 1);
            }
            ans ^= record();
          }
          cout << ans << '\n';
        }
        
        int main() {
          solve();
        }
        
        • 0
          @ 2026-11-23 8:35:18

          截断乘积线段树(通过子任务 1–4,共 45 分)

          本组 n,m≤2×106n,m\le2\times10^6,用线段树维护截断后的区间乘积。

          区间 II 保存 pI=min⁡(V,∏i∈Iai)p_I=\min\bigl(V,\prod_{i\in I}a_i\bigr)。两个子区间中有一个已达到 VV 时,另一个的数都至少为 11,合起来也达到 VV;两个都小于 VV 时,各自是精确乘积,相乘再与 VV 比较即可。所以左右子区间 L,RL,R 合并为

          pI=min⁡(V,pLpR)p_I=\min(V,p_Lp_R)

          叶子补齐时用 11。修改只改一个叶子,沿路重新合并到根;根小于 VV 时就是整个序列的精确乘积,否则记录 00。

          V = 10 时截断乘积线段树的一次修改

          图中序列为 [2,3,1,5][2,3,1,5],根被截成 1010,记录 00。把位置 44 改为 11 后,只重算蓝色的三个节点,根变为 66。

          这个做法让区间摘要负责判断是否超限,不必维护哪些数已经乘入。建树 O(n)O(n),每次修改 O(log⁡n)O(\log n),总时间复杂度为 O(n+mlog⁡n)O(n+m\log n),空间复杂度为 O(n)O(n)。n,mn,m 接近 5×1065\times10^6 时,每次修改更新整条祖先路径太慢。

          参考代码(C++20)

          #include <bits/stdc++.h>
          using namespace std;
          using i64 = long long;
          
          inline int read_int() {
            constexpr int block = 1 << 20;
            static char buffer[block + 1];
            static char* next = buffer;
            static char* end = buffer;
          
            auto refill = [&]() {
              int remaining = end - next;
              memmove(buffer, next, remaining);
              int added = fread(buffer + remaining, 1, block - remaining, stdin);
              next = buffer;
              end = buffer + remaining + added;
              *end = '\0';
              return added > 0;
            };
          
            while (*next < '0' || *next > '9') {
              if (next != end) {
                next++;
              } else if (!refill()) {
                return 0;
              }
            }
          
            // The largest input token has ten digits.
            if (end - next < 11) refill();
            int value = 0;
            do {
              value = value * 10 + *next - '0';
              next++;
            } while ('0' <= *next && *next <= '9');
            return value;
          }
          
          struct segment_tree {
            int v, base = 1;
            vector<int> prod;
          
            segment_tree(vector<int>& a, int v) : v(v) {
              int n = a.size();
              while (base < n) base <<= 1;
              prod.assign(base << 1, 1);
              for (int i = 0; i < n; i++) prod[base + i] = a[i];
              for (int p = base - 1; p; p--) push_up(p);
            }
          
            void push_up(int p) {
              prod[p] = min<i64>(v, 1LL * prod[p << 1] * prod[p << 1 | 1]);
            }
          
            void set(int i, int x) {
              int p = base + i;
              prod[p] = x;
              for (p >>= 1; p; p >>= 1) push_up(p);
            }
          
            int record() {
              return prod[1] < v ? prod[1] : 0;
            }
          };
          
          void solve() {
            int n = read_int(), m = read_int(), v = read_int();
            vector<int> a(n);
            for (int& x : a) x = read_int();
            segment_tree seg(a, v);
            int ans = seg.record();
            for (int j = 0; j < m; j++) {
              int x = read_int() - 1, y = read_int();
              seg.set(x, y);
              ans ^= seg.record();
            }
            cout << ans << '\n';
          }
          
          int main() {
            solve();
          }
          
          • 0
            @ 2026-11-23 8:35:18

            逐次重算乘积(通过子任务 1,共 10 分)

            本组 n,m≤3000n,m\le3000,初始和每次修改后都从头重新累乘。

            从 11 开始依次乘入每个数,乘积达到 VV 就停下记录 00(其余数都至少为 11);乘到最后仍小于 VV 就记录这个乘积。修改直接写回数组,把各次记录依次异或。

            每次至多查看 nn 个数,时间复杂度为 O(nm)O(nm),空间复杂度为 O(n)O(n)。数据变大后,乘积可能长时间保持在 VV 以下,每次都要扫过整个序列。

            参考代码(C++20)

            #include <bits/stdc++.h>
            using namespace std;
            using i64 = long long;
            
            inline int read_int() {
              constexpr int block = 1 << 20;
              static char buffer[block + 1];
              static char* next = buffer;
              static char* end = buffer;
            
              auto refill = [&]() {
                int remaining = end - next;
                memmove(buffer, next, remaining);
                int added = fread(buffer + remaining, 1, block - remaining, stdin);
                next = buffer;
                end = buffer + remaining + added;
                *end = '\0';
                return added > 0;
              };
            
              while (*next < '0' || *next > '9') {
                if (next != end) {
                  next++;
                } else if (!refill()) {
                  return 0;
                }
              }
            
              // The largest input token has ten digits.
              if (end - next < 11) refill();
              int value = 0;
              do {
                value = value * 10 + *next - '0';
                next++;
              } while ('0' <= *next && *next <= '9');
              return value;
            }
            
            void solve() {
              int n = read_int(), m = read_int(), v = read_int();
              vector<int> a(n);
              for (int& x : a) x = read_int();
            
              auto record = [&]() -> int {
                i64 prod = 1;
                for (int x : a) {
                  prod *= x;
                  if (prod >= v) return 0;
                }
                return prod;
              };
            
              int ans = record();
              for (int j = 0; j < m; j++) {
                int x = read_int() - 1, y = read_int();
                a[x] = y;
                ans ^= record();
              }
              cout << ans << '\n';
            }
            
            int main() {
              solve();
            }
            
            • 1

            信息

            ID
            2534
            时间
            750ms
            内存
            512MiB
            难度
            9
            标签
            (无)
            递交数
            94
            已通过
            13
            上传者