QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#590658#8338. Quad Kingdoms ChessDBsoleilML 0ms0kbC++234.1kb2024-09-26 09:33:132024-09-26 09:33:18

Judging History

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

  • [2024-09-26 09:33:18]
  • 评测
  • 测评结果:ML
  • 用时:0ms
  • 内存:0kb
  • [2024-09-26 09:33:13]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

static constexpr int Maxn = 1e5 + 5, SqrN = 705, MaxB = 145;

static constexpr uint64_t MOD1 = 1e9 + 9, MOD2 = 1e9 + 7;
static constexpr uint64_t BASE1 = 1e5 + 3, BASE2 = 1e5 + 19;
using hash_t = pair<uint64_t, uint64_t>;
hash_t operator + (hash_t lhs, int v)
{ return hash_t((lhs.first * BASE1 + v) % MOD1, (lhs.second * BASE2 + v) % MOD2); }
hash_t operator + (hash_t lhs, hash_t rhs)
{ return hash_t((lhs.first + rhs.first) % MOD1, (lhs.second + rhs.second) % MOD2); }
hash_t operator * (hash_t lhs, hash_t rhs)
{ return hash_t(lhs.first * rhs.first % MOD1, lhs.second * rhs.second % MOD2); }
const hash_t BASE = hash_t(BASE1, BASE2);
hash_t pw[Maxn];
using data_t = pair<int, hash_t>;
data_t operator + (data_t lhs, data_t rhs) {
  if (lhs.first == 0) return rhs;
  if (rhs.first == 0) return lhs;
  return data_t(lhs.first + rhs.first, hash_t(lhs.second * pw[rhs.first] + rhs.second));
} data_t New(uint64_t v) { return data_t(1, hash_t(v, v)); }

struct Array {
  int n, bn, bs, a[Maxn];
  int bel[Maxn], bL[Maxn], bR[Maxn];
  data_t h[Maxn];
  bool upd[Maxn];
  vector<pair<int, data_t>> hv[MaxB];
  data_t mp[MaxB][Maxn];
  bool has[MaxB][Maxn];
  int stk[MaxB][Maxn], top[MaxB];
  Array() { }
  ~Array() { }
  void rebuild(int b) {
    int l = bL[b], r = bR[b];
    for (int i = l; i <= r; ++i) upd[i] = false;
    h[l] = New(a[l]); upd[l] = true;
    for (int i = l + 1, p = l; i <= r; ++i)
      if (a[i] >= a[p])
        h[i] = h[p] + New(a[i]), p = i, upd[i] = true;
      else h[i] = h[i - 1];
    hv[b].clear();
    while (top[b]) has[b][stk[b][top[b]--]] = 0;
    hash_t cur = {0, 0};
    int len = 0;
    for (int i = r; i >= l; --i)
      if (upd[i]) {
        (cur.first += pw[len].first * a[i]) %= MOD1;
        (cur.second += pw[len].second * a[i]) %= MOD2;
        ++len;
        hv[b].emplace_back(a[i], data_t(len, cur));
      }
    reverse(hv[b].begin(), hv[b].end());
  } // Array::rebuild
  data_t qblock(int b, data_t res, int &pre) {
    if (pre > hv[b].back().first) return res;
    if (has[b][pre]) {
      auto rt = mp[b][pre];
      pre = hv[b].back().first;
      return res + rt;
    } auto &rem = mp[b][pre];
    int l = bL[b], r = bR[b];
    auto it = lower_bound(hv[b].begin(), hv[b].end(), pair(pre, data_t()));
    // fprintf(stderr, "l = %d, r = %d, it = %d\n", l, r, it - hv[b].begin());
    // fprintf(stderr, "|hv[%d]| = %d\n", b, (int)hv[b].size()); fflush(stderr);
    has[b][pre] = true;
    stk[b][++top[b]] = pre;
    pre = hv[b].back().first;
    // fprintf(stderr, "pre = %d\n", pre);
    res = res + it->second;
    rem = it->second;
    return res;
  } // qblock
  data_t query(void) {
    data_t res = h[bR[1]];
    int cur = hv[1].back().first;
    // fprintf(stderr, "cur = %d\n", cur);
    for (int i = 2; i <= bn; ++i)
      res = qblock(i, res, cur);
    return res;
  } // Array::query
  void input(void) {
    scanf("%d", &n); bs = min(700, n);
    for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
    reverse(a + 1, a + n + 1);
    for (int i = 1; i <= n; ++i) bel[i] = (i - 1) / bs + 1;
    bn = bel[n];
    for (int i = 1; i <= bn; ++i) {
      bL[i] = (i - 1) * bs + 1;
      bR[i] = min(i * bs, n);
    }
    for (int i = 1; i <= bn; ++i) rebuild(i);
  } // Array::input
  void update(int x, int y) {
    x = n - x + 1, a[x] = y;
    rebuild(bel[x]);
  } // update
} a[2];

int main(void) {
  pw[0] = {1, 1};
  for (int i = 1; i < Maxn; ++i)
    pw[i] = pw[i - 1] * hash_t(BASE1, BASE2);
  a[1].input(); a[0].input();
  int qn; scanf("%d", &qn);
  while (qn--) {
    int op, x, y;
    scanf("%d%d%d", &op, &x, &y);
    // fprintf(stderr, "------------------------- %d, %d, %d\n", op, x, y); fflush(stderr);
    a[op % 2].update(x, y);
    // fprintf(stderr, "update success!\n");
    auto a1 = a[1].query(), a2 = a[0].query();
    // fprintf(stderr, "a1 = (%d, (%lu, %lu))\n", a1.first, a1.second.first, a1.second.second);
    // fprintf(stderr, "a2 = (%d, (%lu, %lu))\n", a2.first, a2.second.first, a2.second.second);
    if (a1 != a2) printf("NO\n");
    else printf("YES\n");
  }
  return 0;
} // main

详细

Test #1:

score: 0
Memory Limit Exceeded

input:

5
1 2 3 4 5
5
5 4 3 2 1
8
1 1 5
1 4 2
1 2 4
1 5 1
1 5 5
2 1 4
2 3 5
2 5 5

output:

NO
NO
NO
YES
NO
NO
NO
YES

result: