QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#655334#9313. Make MaxsahaunTL 0ms6408kbC++201.8kb2024-10-19 02:58:042024-10-19 02:58:04

Judging History

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

  • [2024-10-19 02:58:04]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:6408kb
  • [2024-10-19 02:58:04]
  • 提交

answer

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

#define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);
using ll = long long;

const int LG = 21;
const int MX = 200005;
vector<int> pos[MX];
int table[LG][MX];
int lg[MX];

void pre() {
  lg[1] = 0;
  for (int i = 2; i < MX; ++i) lg[i] = 1+lg[i>>1];
}

int query(int l, int r) {
  int i = lg[r-l+1];
  return max(table[i][l], table[i][r-(1<<i)+1]);
}

ll solve(ll l, ll r) {
  if (l >= r) return 0;

  ll x, m, p, i, j, ans;
  x = query(l, r);
  j = lower_bound(pos[x].begin(), pos[x].end(), l) - pos[x].begin();
  m = pos[x].size();
  i = pos[x][j];
  ans = i-l + solve(l, i-1);
  for (j = j+1; j < m; ++j) {
    p = i;
    i = pos[x][j];
    ans += min(i, r) - p - 1 + solve(p+1, i-1);
    if (i >= r) break;
  }
  if (i < r) ans += solve(i+1, r);

  return ans;
}

template<typename T>
vector<T> normalize(vector<T>& a, T start = 0) {
  int n = a.size();
  vector<T> b = a;
  sort(b.begin(), b.end());
  b.erase(unique(b.begin(), b.end()), b.end());
  vector<T> ret(n, start);
  for (int i = 0; i < n; ++i) {
    ret[i] += lower_bound(b.begin(), b.end(), a[i]) - b.begin();
  }
  return ret;
}

int main() {
  FAST;
  pre();
  
  int tc = 1, ti;
  cin >> tc;

  for (ti = 1; ti <= tc; ++ti) {
    int n, i, j, k;
    cin >> n;

    vector<int> a(n);
    for (i = 0; i < n; ++i) cin >> a[i];
    a = normalize(a);
    for (i = 0; i < n; ++i) table[0][i] = a[i];
    for (j = 1; j <= 20; ++j) {
      k = n-(1<<j);
      for (i = 0; i <= k; ++i) {
        table[j][i] = max(table[j-1][i], table[j-1][i+(1<<(j-1))]);
      }
    }

    for (i = 0; i < n; ++i) pos[i].clear();

    for (i = 0; i < n; ++i) {
      pos[a[i]].emplace_back(i);
    }

    cout << solve(0, n-1) << "\n";
  }

  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

4
2
1 2
2
2 2
7
1 1 1 2 2 2 2
3
1 2 3

output:

1
0
3
3

result:

ok 4 number(s): "1 0 3 3"

Test #2:

score: -100
Time Limit Exceeded

input:

2
198018
875421126 585870339 471894633 383529988 625397685 944061047 704695631 105113224 459022561 760848605 980735314 847376362 980571959 329939331 644635272 326439858 752879510 837384394 175179068 182094523 397239381 1199016 185143405 279638454 252374970 822030887 860312140 137248166 993229443 164...

output:


result: