QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#273587#7108. Couleurikaurov#AC ✓2799ms132112kbC++204.0kb2023-12-03 01:57:442023-12-03 01:57:45

Judging History

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

  • [2023-12-03 01:57:45]
  • 评测
  • 测评结果:AC
  • 用时:2799ms
  • 内存:132112kb
  • [2023-12-03 01:57:44]
  • 提交

answer

#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define all(arr) (arr).begin(), (arr).end()
#define ll long long
#define ld long double
#define pb push_back
#define sz(x) (int)(x).size()
#define fi first
#define se second
#define endl '\n'

const int N = 1e5 + 20;

int nodeidx = 0;
int root[N];

struct Node{
  int lidx, ridx;
  int sum;
  
  Node(){
    sum = 0;
    lidx = ridx = -1;
  };

  Node(int val){
    sum = val;
    lidx = ridx = -1;
  };

  Node(int lidx1, int ridx1, int sum1){
    lidx = lidx1, ridx = ridx1;
    sum = sum1;
  };
};

Node nodes[N * 100];

int make_node(){
  nodes[nodeidx] = Node();
  return nodeidx++;
}

int make_node(int val){
  nodes[nodeidx] = Node(val);
  return nodeidx++;
}

int make_node(int lidx, int ridx){
  nodes[nodeidx] = Node(lidx, ridx, nodes[lidx].sum + nodes[ridx].sum);
  return nodeidx++;
}

int inc(int v, int tl, int tr, int idx, int val){
  if (tl == tr){
    return make_node(nodes[v].sum + val);
  }
  int tm = (tl + tr) / 2;
  if (idx <= tm) return make_node(inc(nodes[v].lidx, tl, tm, idx, val), nodes[v].ridx);
  else return make_node(nodes[v].lidx, inc(nodes[v].ridx, tm + 1, tr, idx, val));
}

int build(int tl, int tr){
  if (tl == tr) return make_node();
  int tm = (tl + tr) / 2;
  return make_node(build(tl, tm), build(tm + 1, tr));
}

int rsq(int v, int tl, int tr, int l, int r){
  if (l > r) return 0;
  if (tl == l && tr == r) return nodes[v].sum;
  int tm = (tl + tr) / 2;
  return rsq(nodes[v].lidx, tl, tm, l, min(r, tm)) + 
  rsq(nodes[v].ridx, tm + 1, tr, max(l, tm + 1), r);
}

int n;

int getsmaller(int l, int r, int val){
  return rsq(root[r], 0, n - 1, 0, val - 1) - rsq(root[l - 1], 0, n - 1, 0, val - 1);
}

int getlarger(int l, int r, int val){
  return rsq(root[r], 0, n - 1, val + 1, n - 1) - rsq(root[l - 1], 0, n - 1, val + 1, n - 1);
}

int a[N], segend[N];
ll invcnt[N];

void solve(){
  cin >> n;
  for (int i = 0; i <= n + 1; i++) a[i] = segend[i] = invcnt[i] = 0;
  nodeidx = 0;
  for (int i = 1; i <= n; i++){
    cin >> a[i];
    a[i]--;
  }
  root[0] = build(0, n - 1);
  for (int i = 1; i <= n; i++){
    root[i] = inc(root[i - 1], 0, n - 1, a[i], 1);
  }
  set<int> starts;
  multiset<ll> answers;
  starts.insert(1);

  auto calc_invs = [&] (int l, int r){
    ll ret = 0;
    for (int i = l; i <= r; i++){
      ret += getlarger(l, i - 1, a[i]);
    }
    return ret;
  };

  invcnt[1] = calc_invs(1, n);
  answers.insert(invcnt[1]);
  segend[1] = n;

  for (int idx = 0; idx < n; idx++){
    ll z = *prev(answers.end());
    cout << z << " ";

    ll i;
    cin >> i;
    i ^= z;
    int start = *prev(starts.upper_bound(i));
    int fin = segend[start];
    ll valbef = invcnt[start] - getlarger(start, i - 1, a[i]) - getsmaller(i + 1, fin, a[i]);
    int len1 = i - start, len2 = fin - i;
    if (len1 < len2){
      ll ourans = calc_invs(start, i - 1);
      ll crossans = 0;
      for (int j = start; j < i; j++){
        crossans += getsmaller(i + 1, fin, a[j]);
      }
      answers.erase(answers.find(invcnt[start]));

      invcnt[start] = ourans;
      segend[start] = i - 1;

      invcnt[i + 1] = valbef - ourans - crossans;
      segend[i + 1] = fin;
      starts.insert(i + 1);

      answers.insert(invcnt[start]);
      answers.insert(invcnt[i + 1]);
    }
    else{
      ll ourans = calc_invs(i + 1, fin);
      ll crossans = 0;
      for (int j = i + 1; j <= fin; j++){
        crossans += getlarger(start, i - 1, a[j]);
      }
      answers.erase(answers.find(invcnt[start]));

      invcnt[start] = valbef - ourans - crossans;
      segend[start] = i - 1;

      invcnt[i + 1] = ourans;
      segend[i + 1] = fin;
      starts.insert(i + 1);

      answers.insert(invcnt[start]);
      answers.insert(invcnt[i + 1]);
    }
  }
  cout << endl;
}

signed main(){
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  // cout.precision(20);
  int t;
  cin >> t;
  while (t--){
    solve();
  }
}

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

詳細信息

Test #1:

score: 100
Accepted
time: 15ms
memory: 121776kb

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: 2799ms
memory: 132112kb

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