QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#577850#4405. 普罗霍洛夫卡guosoun100 ✓11498ms51936kbC++173.6kb2024-09-20 15:07:132024-09-20 15:07:14

Judging History

你现在查看的是最新测评结果

  • [2024-09-20 15:07:14]
  • 评测
  • 测评结果:100
  • 用时:11498ms
  • 内存:51936kb
  • [2024-09-20 15:07:13]
  • 提交

answer

#include <bits/stdc++.h>
using ll = long long;
const int B = 500;

template <unsigned int k>
struct block {
  const static int v = (1 << k) - 1;
  std::array<int, 1 << k> sum, cnt[2];
  std::array<std::vector<int>, 1 << k> val;
  std::vector<int> ans[2], a;
  int tga[2], tgm;
  auto calc(std::array<int, 1 << k> a) {
    std::array<int, 1 << k> ret{};
    for (int i = k - 1; i >= 0; i--) {
      int t = (1 << (i + 1)) - 1;
      for (int j = 0; j <= t; j++)
        if (a[j]) ret[((1 << i) - 1 - j) & t] ^= t;
      for (int j = 0; j <= t; j++)
        if (j >> i & 1) a[j ^ (1 << i)] ^= a[j], ret[j] ^= ret[j ^ (1 << i)];
    }
    for (int i = 0; i < (int)k; i++) {
      for (int j = 0; j < (1 << (i + 1)); j++)
        if (j >> i & 1) ret[j] ^= ret[j ^ (1 << i)];
    }
    return ret;
  }
  void rebuild() {
    cnt[0] = cnt[1] = sum = {}, tgm = 0, val.fill({});
    for (int i = 0; i < (int)a.size(); i++) val[a[i] & v].push_back(i), sum[a[i] & v] ^= 1;
    sum = calc(sum);
  }
  int queryh(int t) { return tga[t]; }
  int query(int l, int r, int t) {
    assert(0 <= l && l <= r && r < (int)a.size());
    auto tmp = calc(cnt[t]);
    int ret = 0;
    for (int i = l; i <= r; i++)
      ret ^= ans[t][i], ret ^= tmp[a[i] & v];
    return ret;
  }
  void addh(int t) {
    for (int i : val[v - (tgm & v)]) 
      ans[t][i] ^= (a[i] + tgm) ^ (a[i] + tgm + 1), 
      tga[t] ^= (a[i] + tgm) ^ (a[i] + tgm + 1);
    tga[t] ^= sum[tgm & v], cnt[t][tgm & v] ^= 1;
    tgm += 1;
  }
  void add(int l, int r, int t) {
    assert(0 <= l && l <= r && r < (int)a.size());
    for (int t : {0, 1}) {
      auto tmp = calc(cnt[t]);
      for (int i = 0; i < (int)a.size(); i++) 
        ans[t][i] ^= tmp[a[i] & v];
    }
    for (int &x : a) x += tgm;
    for (int i = l; i <= r; i++) 
      ans[t][i] ^= a[i] ^ (a[i] + 1), tga[t] ^= a[i] ^ (a[i] + 1), a[i] += 1;
    rebuild();
  }
  block(std::vector<int> a) : a(a) {
    ans[0].assign(a.size(), 0), ans[1].assign(a.size(), 0), tga[0] = tga[1] = 0, rebuild();
  }
};
int main() {
  std::cin.tie(0)->sync_with_stdio(0);
  int n, q;
  std::cin >> n >> q;
  std::vector<int> a(n + 1), ans(q), bel(n + 1), st, ed;
  for (int i = 1; i <= n; i++) std::cin >> a[i];
  std::vector<block<8>> blk;
  for (int l = 1, r = B; l <= n; l = r + 1, r += B) {
    if (r > n) r = n;
    std::fill(bel.begin() + l, bel.begin() + r + 1, blk.size());
    blk.push_back(std::vector<int>(r - l + 1)), st.push_back(l), ed.push_back(r);
  }
  std::vector<std::vector<std::pair<int, int>>> que(n + 1);
  for (int i = 0; i < q; i++) {
    int l, r;
    std::cin >> l >> r;
    que[r].emplace_back(l, i);
  }
  auto add = [&](int l, int r, int t) {
    if (bel[l] == bel[r]) blk[bel[l]].add(l - st[bel[l]], r - st[bel[l]], t);
    else {
      blk[bel[l]].add(l - st[bel[l]], ed[bel[l]] - st[bel[l]], t);
      blk[bel[r]].add(st[bel[r]] - st[bel[r]], r - st[bel[r]], t);
      for (int i = bel[l] + 1; i < bel[r]; i++) blk[i].addh(t);
    }
  };
  auto query = [&](int l, int r, int t) {
    if (bel[l] == bel[r]) return blk[bel[l]].query(l - st[bel[l]], r - st[bel[l]], t);
    int ret = blk[bel[l]].query(l - st[bel[l]], ed[bel[l]] - st[bel[l]], t);
    ret ^= blk[bel[r]].query(st[bel[r]] - st[bel[r]], r - st[bel[r]], t);
    for (int i = bel[l] + 1; i < bel[r]; i++) ret ^= blk[i].queryh(t);
    return ret;
  };
  std::vector<int> lst(n + 1);
  for (int i = 1; i <= n; i++) {
    add(lst[a[i]] + 1, i, i & 1), lst[a[i]] = i;
    for (auto [l, id] : que[i]) ans[id] = query(l, i, i & 1);
    if (i % 5000 == 0) std::cerr << i << '\n';
  }
  for (int i = 0; i < q; i++) std::cout << ans[i] << '\n';
}

详细

Subtask #1:

score: 5
Accepted

Test #1:

score: 5
Accepted
time: 1ms
memory: 3788kb

Test #2:

score: 5
Accepted
time: 1ms
memory: 3660kb

Test #3:

score: 5
Accepted
time: 1ms
memory: 3632kb

Test #4:

score: 5
Accepted
time: 1ms
memory: 3584kb

Test #5:

score: 5
Accepted
time: 1ms
memory: 3572kb

Subtask #2:

score: 5
Accepted

Dependency #1:

100%
Accepted

Test #6:

score: 5
Accepted
time: 76ms
memory: 4080kb

Test #7:

score: 5
Accepted
time: 80ms
memory: 4084kb

Test #8:

score: 5
Accepted
time: 79ms
memory: 4160kb

Test #9:

score: 5
Accepted
time: 80ms
memory: 4076kb

Test #10:

score: 5
Accepted
time: 79ms
memory: 4136kb

Subtask #3:

score: 10
Accepted

Dependency #2:

100%
Accepted

Test #11:

score: 10
Accepted
time: 1994ms
memory: 14992kb

Test #12:

score: 10
Accepted
time: 1996ms
memory: 14840kb

Test #13:

score: 10
Accepted
time: 1990ms
memory: 14996kb

Test #14:

score: 10
Accepted
time: 2006ms
memory: 14996kb

Test #15:

score: 10
Accepted
time: 1998ms
memory: 15004kb

Subtask #4:

score: 10
Accepted

Dependency #3:

100%
Accepted

Test #16:

score: 10
Accepted
time: 4454ms
memory: 26496kb

Test #17:

score: 10
Accepted
time: 4463ms
memory: 26660kb

Test #18:

score: 10
Accepted
time: 4443ms
memory: 26484kb

Test #19:

score: 10
Accepted
time: 4482ms
memory: 26616kb

Test #20:

score: 10
Accepted
time: 4444ms
memory: 27212kb

Subtask #5:

score: 10
Accepted

Dependency #4:

100%
Accepted

Test #21:

score: 10
Accepted
time: 7393ms
memory: 39756kb

Test #22:

score: 10
Accepted
time: 7453ms
memory: 38872kb

Test #23:

score: 10
Accepted
time: 7407ms
memory: 39884kb

Test #24:

score: 10
Accepted
time: 7265ms
memory: 39476kb

Test #25:

score: 10
Accepted
time: 7422ms
memory: 38976kb

Subtask #6:

score: 10
Accepted

Dependency #5:

100%
Accepted

Test #26:

score: 10
Accepted
time: 9087ms
memory: 44984kb

Test #27:

score: 10
Accepted
time: 8969ms
memory: 44884kb

Test #28:

score: 10
Accepted
time: 8950ms
memory: 44896kb

Test #29:

score: 10
Accepted
time: 8972ms
memory: 45296kb

Test #30:

score: 10
Accepted
time: 9070ms
memory: 44268kb

Subtask #7:

score: 10
Accepted

Test #31:

score: 10
Accepted
time: 735ms
memory: 9812kb

Test #32:

score: 10
Accepted
time: 725ms
memory: 9744kb

Test #33:

score: 10
Accepted
time: 730ms
memory: 9832kb

Test #34:

score: 10
Accepted
time: 724ms
memory: 10016kb

Test #35:

score: 10
Accepted
time: 742ms
memory: 9960kb

Subtask #8:

score: 10
Accepted

Test #36:

score: 10
Accepted
time: 3627ms
memory: 43016kb

Test #37:

score: 10
Accepted
time: 3590ms
memory: 42292kb

Test #38:

score: 10
Accepted
time: 3587ms
memory: 43152kb

Test #39:

score: 10
Accepted
time: 3573ms
memory: 42664kb

Test #40:

score: 10
Accepted
time: 3576ms
memory: 42548kb

Subtask #9:

score: 10
Accepted

Dependency #8:

100%
Accepted

Test #41:

score: 10
Accepted
time: 3713ms
memory: 44368kb

Test #42:

score: 10
Accepted
time: 3707ms
memory: 44432kb

Test #43:

score: 10
Accepted
time: 3705ms
memory: 42792kb

Test #44:

score: 10
Accepted
time: 3689ms
memory: 44460kb

Test #45:

score: 10
Accepted
time: 3684ms
memory: 42992kb

Subtask #10:

score: 15
Accepted

Dependency #1:

100%
Accepted

Dependency #2:

100%
Accepted

Dependency #3:

100%
Accepted

Dependency #4:

100%
Accepted

Dependency #5:

100%
Accepted

Dependency #6:

100%
Accepted

Dependency #7:

100%
Accepted

Dependency #8:

100%
Accepted

Dependency #9:

100%
Accepted

Test #46:

score: 15
Accepted
time: 11082ms
memory: 50252kb

Test #47:

score: 15
Accepted
time: 11177ms
memory: 51760kb

Test #48:

score: 15
Accepted
time: 11498ms
memory: 51936kb

Test #49:

score: 15
Accepted
time: 11426ms
memory: 49944kb

Test #50:

score: 15
Accepted
time: 11021ms
memory: 50740kb