QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#159110#7108. Couleurucup-team1191#AC ✓5847ms87436kbC++204.8kb2023-09-02 17:29:262023-09-02 17:29:26

Judging History

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

  • [2023-09-02 17:29:26]
  • 评测
  • 测评结果:AC
  • 用时:5847ms
  • 内存:87436kb
  • [2023-09-02 17:29:26]
  • 提交

answer

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

typedef long long ll;

struct Fenwick2d {
  vector<vector<int>> coords;
  vector<vector<int>> vals;
  int n;

  Fenwick2d(int nn) {
    n = nn;
    coords.resize(n);
    vals.resize(n);
  }

  void fake_add(int x, int y) {
    for (int nx = x; nx < n; nx |= nx + 1) {
      for (int ny = y; ny <= n; ny |= ny + 1) {
        coords[nx].push_back(ny);
      }
    }
  }

  void compress() {
    for (int i = 0; i < n; ++i) {
      sort(coords[i].begin(), coords[i].end());
      coords[i].resize(unique(coords[i].begin(), coords[i].end()) -
                       coords[i].begin());
      vals[i].resize(coords[i].size());
    }
  }

  void add(int x, int y) {
    for (int nx = x; nx < n; nx |= nx + 1) {
      for (int ny = lower_bound(coords[nx].begin(), coords[nx].end(), y) -
                    coords[nx].begin();
           ny < (int)coords[nx].size(); ny |= ny + 1) {
        vals[nx][ny] += 1;
      }
    }
  }

  int get(int x, int y) {
    int ret = 0;
    for (int nx = x; nx >= 0; nx &= nx + 1, --nx) {
      for (int ny = upper_bound(coords[nx].begin(), coords[nx].end(), y) -
                    coords[nx].begin() - 1;
           ny >= 0; ny &= ny + 1, --ny) {
        ret += vals[nx][ny];
      }
    }
    return ret;
  }

  int get_leq(int lx, int rx, int y) { return get(rx, y) - get(lx - 1, y); }

  int get_geq(int lx, int rx, int y) {
    return (rx - lx + 1) - get_leq(lx, rx, y - 1);
  }
};

struct Fenwick {
  vector<int> fen;
  int n;
  Fenwick(int n) : n(n) { fen.resize(n); }
  void modify(int i, int x) {
    for (; i < n; i |= i + 1) {
      fen[i] += x;
    }
  }
  int get(int r) {
    int res = 0;
    for (; r >= 0; r &= r + 1, --r) {
      res += fen[r];
    }
    return res;
  }
};

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<int> a(n);
    for (int i = 0; i < n; ++i) {
      cin >> a[i];
      --a[i];
    }
    vector<ll> p(n);
    for (int i = 0; i < n; ++i) {
      cin >> p[i];
    }
    Fenwick seg_inv(n);

    auto calc = [&](int l, int r) {
      ll inv = 0;
      for (int i = r; i >= l; --i) {
        inv += seg_inv.get(a[i] - 1);
        seg_inv.modify(a[i], 1);
      }
      for (int i = l; i <= r; ++i) {
        seg_inv.modify(a[i], -1);
      }
      return inv;
    };

    vector<ll> seg_ans(n);
    seg_ans[0] = calc(0, n - 1);

    multiset<ll> all_ans;
    all_ans.insert(seg_ans[0]);

    set<int> dead;
    dead.insert(-1);
    dead.insert(n);

    Fenwick2d seg_count(n);

    for (int i = 0; i < n; ++i) {
      seg_count.fake_add(i, a[i]);
    }
    seg_count.compress();

    for (int i = 0; i < n; ++i) {
      seg_count.add(i, a[i]);
    }

    for (int i = 0; i < n; ++i) {
      ll ans = *all_ans.rbegin();
      cout << ans << ' ';
      /*cout << "!!!!! " << ans << endl;
      for (int i = 0; i < n; ++i) {
        cout << seg_ans[i] << ' ';
      }
      cout << endl;*/
      ll lind = (p[i] ^ ans) - 1;
      assert(0 <= lind && lind < n);
      int ind = (int)lind;
      assert(!dead.count(ind));
      auto it = dead.upper_bound(ind);
      // cout << "mem1" << endl;
      int r = (*it) - 1;
      int l = (*prev(it)) + 1;
      // cout << "mem2" << endl;
      dead.insert(ind);
      all_ans.erase(all_ans.find(seg_ans[l]));
      ll old_ans = seg_ans[l];
      seg_ans[l] = 0;
      // cout << "here" << endl;
      if (ind - l < r - ind) {
        // cout << "start1" << endl;
        if (l <= ind - 1) {
          seg_ans[l] = calc(l, ind - 1);
          all_ans.insert(seg_ans[l]);
        }
        if (ind + 1 <= r) {
          for (int j = l; j <= ind; ++j) {
            old_ans -= seg_count.get_leq(j + 1, r, a[j] - 1);
          }
          seg_ans[ind + 1] = old_ans;
          all_ans.insert(seg_ans[ind + 1]);
        }
        // cout << "finish1" << endl;
      } else {
        // cout << "start2" << endl;
        if (ind + 1 <= r) {
          seg_ans[ind + 1] = calc(ind + 1, r);
          all_ans.insert(seg_ans[ind + 1]);
        }
        if (l <= ind - 1) {
          for (int j = r; j >= ind; --j) {
            old_ans -= seg_count.get_geq(l, j - 1, a[j] + 1);
          }
          seg_ans[l] = old_ans;
          all_ans.insert(seg_ans[l]);
        }
        // cout << "finish2" << endl;
      }
    }
    cout << '\n';
  }
}

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

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 5847ms
memory: 87436kb

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