QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#655323 | #9313. Make Max | sahaun | TL | 1ms | 3592kb | C++20 | 2.2kb | 2024-10-19 02:54:04 | 2024-10-19 02:54:07 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);
using ll = long long;
template<typename T>
struct SparseTable {
vector<vector<T>> table;
vector<int> lg;
int n, lg_n;
T merge(T a, T b) {
return max(a, b);
}
SparseTable() {}
template<typename DATA_T>
SparseTable(vector<DATA_T>& arr) {
n = arr.size();
lg_n = 1 + (int)log2(n);
table = vector<vector<T>>(lg_n+1, vector<T>(n));
lg = vector<int>(n+1, 0);
for (int i = 2; i <= n; ++i) lg[i] = 1+lg[i>>1];
int i, j, k;
for (i = 0; i < n; ++i) table[0][i] = arr[i];
for (j = 1; j <= lg_n; ++j) {
k = n-(1<<j);
for (i = 0; i <= k; ++i) {
table[j][i] = merge(table[j-1][i], table[j-1][i+(1<<(j-1))]);
}
}
}
T query(int l, int r) {
int i = lg[r-l+1];
return merge(table[i][l], table[i][r-(1<<i)+1]);
}
};
const int MX = 200005;
vector<int> pos[MX];
SparseTable<int> sp;
ll solve(ll l, ll r) {
if (l >= r) return 0;
ll x, m, p, i, j, ans;
x = sp.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;
int tc = 1, ti;
cin >> tc;
for (ti = 1; ti <= tc; ++ti) {
int n, i;
cin >> n;
vector<int> a(n);
for (i = 0; i < n; ++i) cin >> a[i];
a = normalize(a);
sp = SparseTable<int>(a);
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;
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 3592kb
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...