跳到正文

题解

题解 CF1638E 【Colorful Operations】

StudyingFather约 6 分钟
原文
文章信息与版本

文章信息

收录
2026年5月19日
更新
2026年10月22日
篇幅
约 6 分钟
题目
CF1638E

版本

  1. v1当前

注意到本题的的操作 1 是连续段染色,与 ODT 的 assign 操作类似,因此考虑 ODT 维护颜色段。

操作 2 是对所有同色元素加一个值,考虑对于颜色 ii 维护一个 tagitag_i,表示颜色 ii 加的值是多少,每次执行操作 2 时,只需要修改 tagitag_i 即可。

单点查询时,元素 pp 的值显然是 ap+tagcolpa_p + tag_{col_p}。

现在考虑 tagtag 的存在对操作 1 的影响(设某元素原来的颜色为 cc,新颜色为 c′c^\prime):

  • 首先,由于颜色的更新,tagctag_c 值应该累加到该元素上,以落实操作 2 的延迟修改。
  • 在颜色更新到 c′c^\prime 后,为了抵消 tagc′tag_{c^\prime} 的影响,该元素的值还要减去 tagc′tag_{c^\prime},从而确保单点查询时能得到正确的结果。

操作 1 涉及到元素的区间加,用树状数组维护即可。

显然操作 2 时间复杂度 O(1)O(1),操作 3 是树状数组单点查询,时间复杂度 O(log⁡n)O(\log n)。

问题来了,本题数据并不保证随机,操作 1 使用 ODT 的时间复杂度还有保障吗?

设一次 assign 操作需要合并 kk 个区间。在 assign 操作后,区间数会减少 k−1k-1,同时由于 assign 前需要 split 端点,又会产生至多 2 个区间。

整个过程中,区间数的增量为 O(q)O(q),因为区间数恒为正,assign 操作减少的区间数(即我们要合并的区间数)至多也为 O(q)O(q)。

在执行 assign 操作时,每处理一个区间,就要执行一次树状数组的区间修改。因此,该做法的时间复杂度为 O(qlog⁡n)O(q \log n)。

C++
// Problem: E. Colorful Operations
// Contest: Codeforces - Codeforces Round #771 (Div. 2)
// URL: https://codeforces.com/contest/1638/problem/E
// Memory Limit: 256 MB
// Time Limit: 4000 ms
//
// Powered by CP Editor (https://cpeditor.org)

#include <algorithm>
#include <iostream>
#include <set>
#include <vector>
using namespace std;
struct node {
  int l, r;
  mutable int c;
  node(int l, int r = -1, int c = 0) {
    this->l = l;
    this->r = r;
    this->c = c;
  }
  bool operator<(const node& a) const { return l < a.l; }
};
set<node> odt;
int n, q;
long long tag[1000005];
struct BIT {
  long long a[1000005];
  int n;
  void init(int n) { this->n = n; }
  int lowbit(int x) { return x & (-x); }
  void add(int p, long long x) {
    while (p <= n) {
      a[p] += x;
      p += lowbit(p);
    }
  }
  long long query(int p) {
    long long ans = 0;
    while (p) {
      ans += a[p];
      p -= lowbit(p);
    }
    return ans;
  }
} tr;
auto split(int pos) {
  auto it = odt.lower_bound(node(pos));
  if (it != odt.end() && it->l == pos) return it;
  it--;
  int l = it->l, r = it->r, c = it->c;
  odt.erase(it);
  odt.insert(node(l, pos - 1, c));
  return odt.insert(node(pos, r, c)).first;
}
void assign(int l, int r, int c) {
  auto itr = split(r + 1), itl = split(l);
  for (auto it = itl; it != itr; it++) {
    tr.add(it->l, tag[it->c]);
    tr.add(it->r + 1, -tag[it->c]);
  }
  odt.erase(itl, itr);
  odt.insert(node(l, r, c));
  tr.add(l, -tag[c]), tr.add(r + 1, tag[c]);
}
long long get_tag(int p) {
  auto it = odt.lower_bound(node(p));
  if (it != odt.end() && it->l == p) return tag[it->c];
  return tag[(--it)->c];
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n >> q;
  odt.insert(node(1, n, 1));
  tr.init(n);
  while (q--) {
    string type;
    cin >> type;
    if (type == "Color") {
      int l, r, c;
      cin >> l >> r >> c;
      assign(l, r, c);
    } else if (type == "Add") {
      int c, x;
      cin >> c >> x;
      tag[c] += x;
    } else {
      int x;
      cin >> x;
      cout << tr.query(x) + get_tag(x) << endl;
    }
  }
  return 0;
}
原文