QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#157505#7108. Couleurucup-team859#AC ✓1853ms39972kbC++143.6kb2023-09-02 15:27:122023-09-02 15:27:12

Judging History

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

  • [2023-09-02 15:27:12]
  • 评测
  • 测评结果:AC
  • 用时:1853ms
  • 内存:39972kb
  • [2023-09-02 15:27:12]
  • 提交

answer

#include <bits/stdc++.h>

#define lsb(x) (x & (-x))

using ull = unsigned long long;
using ll = long long;

using namespace std;

struct node {
  int l, r, x;
};

struct interval {
  int l, r;
  ll x;

  bool operator < (const interval &aux) const {
    if (l != aux.l)
      return l < aux.l;
    return r < aux.r;
  }
};

const int NMAX = 1e5 + 1;

int n, nr;
node arb[31 * NMAX];
int a[NMAX];

void build(int nod = 0, int st = 1, int dr = n) {
  arb[nod].x = 0;

  if (st == dr)
    return;

  arb[nod].l = ++nr; 
  arb[nod].r = ++nr; 
  int mij = (st + dr) / 2;

  build(arb[nod].l, st, mij);
  build(arb[nod].r, mij + 1, dr);
}

int update(int p, int nod, int st = 1, int dr = n) {
  if (st == dr) {
    arb[++nr] = arb[nod];
    arb[nr].x += 1;
    return nr;
  }

  int mij = (st + dr) / 2;
  int now = ++nr;
  arb[now] = arb[nod];
  arb[now].x += 1;
  if (p <= mij) {
    arb[now].l = update(p, arb[now].l, st, mij);
  } else {
    arb[now].r = update(p, arb[now].r, mij + 1, dr);
  }

  return now;
}

int query(int x, int y, int n1, int n2, int st = 1, int dr = n) {
  if (x <= st && dr <= y)
    return arb[n2].x - arb[n1].x;

  int mij = (st + dr) / 2;
  int sum = 0;
  if (x <= mij)
    sum += query(x, y, arb[n1].l, arb[n2].l, st, mij);
  if (mij + 1 <= y)
    sum += query(x, y, arb[n1].r, arb[n2].r, mij + 1, dr);

  return sum;
}

void solve() {
  cin >> n;
  nr = 0;

  vector<vector<int>> v(n + 1);
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    v[a[i]].push_back(i);
  }

  vector<int> root = {0};
  build();
  for (int i = 1; i <= n; ++i) {
    int new_root = root[i - 1];
    for (auto p : v[i])
      new_root = update(p, new_root);
    root.push_back(new_root);
  }

  ll cnt = 0;
  for (int i = 1; i < n; ++i)
    cnt += query(i + 1, n, root[0], root[a[i] - 1]);

  set<interval> s;
  multiset<ll> val;
  s.insert({1, n, cnt});
  val.insert(cnt);
  for (int j = 1; j <= n; ++j) {
    ll ans = *val.rbegin();
    cout << ans << " ";
    ll y;
    cin >> y;
    int x = y ^ ans;
    if (j == n)
      break;

    auto it = s.lower_bound({x, 0, 0});
    if (it == s.end() || it->l > x)
      it = prev(it);

    int l1 = it->l, r1 = x - 1;
    int l2 = x + 1, r2 = it->r;

    ll x1, x2;
    if (r1 - l1 < r2 - l2) {
      x1 = 0, x2 = it->x;
      for (int i = l1; i <= r1; ++i) {
        if (i + 1 <= r1)
          x1 += query(i + 1, r1, root[0], root[a[i] - 1]);
        x2 -= query(i + 1, r2, root[0], root[a[i] - 1]);
      }

      if (x + 1 <= r2)
        x2 -= query(x + 1, r2, root[0], root[a[x] - 1]);
    } else {
      x1 = it->x, x2 = 0;
      for (int i = l2; i <= r2; ++i) {
        if (i + 1 <= r2) {
          int aux = query(i + 1, r2, root[0], root[a[i] - 1]);
          x2 += aux;
          x1 -= aux;
        }
        if (a[i] < n) {
          int aux = query(l1, x, root[a[i]], root[n]);
          //cout << l1 << " " << x << " " << aux << endl;
          x1 -= aux;
        }
      }

      if (a[x] < n && l1 <= x - 1)
        x1 -= query(l1, x - 1, root[a[x]], root[n]);
      //cout << x1 << " " << x2 << " " << l2 << " " << r2 << endl;
    }

    val.erase(val.find(it->x));
    val.insert(x1);
    val.insert(x2);
    s.erase(it);
    if (l1 <= r1)
      s.insert({l1, r1, x1});
    if (l2 <= r2)
      s.insert({l2, r2, x2});

  }
  cout << "\n";
}

int main() {
#ifdef HOME
  ifstream cin("input.in");
  ofstream cout("output.out");
#endif
  ios::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  
  int t = 1;
  cin >> t;
  while (t--)
    solve();

  return 0;
}

这程序好像有点Bug,我给组数据试试?

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 5936kb

input:

3
5
4 3 1 1 1
5 4 5 3 1
10
9 7 1 4 7 8 5 7 4 8
21 8 15 5 9 2 4 5 10 6
15
4 8 8 1 12 1 10 14 7 14 2 9 13 10 3
37 19 23 15 7 2 10 15 2 13 4 5 8 7 10

output:

7 0 0 0 0 
20 11 7 2 0 0 0 0 0 0 
42 31 21 14 14 4 1 1 1 0 0 0 0 0 0 

result:

ok 3 lines

Test #2:

score: 0
Accepted
time: 1853ms
memory: 39972kb

input:

11116
10
10 5 10 3 6 4 8 5 9 8
31 27 24 11 12 3 0 2 3 1
10
8 2 7 2 8 10 1 10 9 10
6 5 2 13 2 1 0 1 3 1
10
7 10 7 6 1 3 10 6 7 9
21 18 10 1 6 5 4 8 9 10
10
2 10 4 8 8 5 7 2 6 7
20 10 9 1 15 0 4 2 9 7
10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
10
1 2 3 4 5 6 7 8 9 10
6 3 5 2 7 10 9 1 4 8
10
1 10 1 3...

output:

21 18 16 12 10 6 4 1 1 0 
12 12 10 10 4 4 4 2 1 0 
20 16 9 5 3 3 3 0 0 0 
22 14 8 8 5 5 2 1 1 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
19 12 7 4 4 2 2 1 0 0 
20 18 8 3 1 1 0 0 0 0 
45 21 21 10 3 3 3 0 0 0 
17 11 8 2 1 1 1 0 0 0 
13 4 1 0 0 0 0 0 0 0 
29 27 22 15 9 7 4 3 1 0 
26 16 9 2 1 1 1 1 1 ...

result:

ok 11116 lines

Extra Test:

score: 0
Extra Test Passed