QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#154758 | #5500. Bars | berarchegas# | WA | 177ms | 28700kb | C++17 | 2.4kb | 2023-08-31 22:00:36 | 2023-08-31 22:00:37 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
mt19937 rng((int) chrono::steady_clock::now().time_since_epoch().count());
const int MOD = 1e9 + 7;
const int MAXN = 5e5 + 5;
const ll INF = 2e18;
struct no {
int mx, posFirst, posLast;
no(int _mx = 0, int _posFirst = 0, int _posLast = 0) {
mx = _mx;
posFirst = _posFirst;
posLast = _posLast;
}
no operator + (const no &x) const {
no aux;
if (mx == x.mx) {
aux.mx = mx;
aux.posFirst = min(posFirst, x.posFirst);
aux.posLast = max(posLast, x.posLast);
}
else if (mx > x.mx) {
aux = *this;
}
else {
aux = x;
}
return aux;
}
} a[4 * MAXN];
int v[MAXN];
void build(int node, int i, int j) {
if (i == j) {
a[node] = no(v[i], i, i);
}
else {
int m = (i + j) / 2;
build(2 * node, i, m);
build(2 * node + 1, m + 1, j);
a[node] = a[2 * node] + a[2 * node + 1];
}
}
no query(int node, int i, int j, int ini, int fim) {
if (fim < i || j < ini) return no(0);
if (ini <= i && j <= fim) return a[node];
int m = (i + j) / 2;
return query(2 * node, i, m, ini, fim) + query(2 * node + 1, m + 1, j, ini, fim);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> v[i];
}
build(1, 1, n);
ll ans = 0;
vector<int> get = {1, n};
int ini = 1, fim = n;
while (true) {
// cout << ini << ' ' << fim << endl;
int tam = fim - ini - 1;
if (tam == 0) break;
no at = query(1, 1, n, ini + 1, fim - 1);
if (at.mx <= min(v[ini], v[fim])) {
break;
}
else if (at.mx > min(v[ini], v[fim]) && at.mx <= max(v[ini], v[fim])) {
if (v[ini] >= v[fim]) {
get.push_back(at.posLast);
fim = at.posLast;
}
else {
get.push_back(at.posFirst);
ini = at.posFirst;
}
}
else {
get.push_back(at.posFirst);
ini = at.posFirst;
}
}
sort(get.begin(), get.end());
for (int i = 0; i < (int)get.size(); i++) {
// cout << get[i] << ' ';
if (i < (int)get.size() - 1) {
ans += v[get[i]] * (get[i + 1] - get[i]);
}
if (i > 0) {
ans += v[get[i]] * (get[i] - get[i - 1]);
}
}
// cout << '\n';
cout << ans << '\n';
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 5ms
memory: 28700kb
input:
2 4 5 2 2 6 5 1 5 4 4 1
output:
33 29
result:
ok 2 lines
Test #2:
score: -100
Wrong Answer
time: 177ms
memory: 28476kb
input:
10000 4 5 2 2 6 5 1 5 4 4 1 197 763787596 15221694 898228999 187472305 466351873 822742732 437754202 800092772 843092246 915675776 166265020 346340615 796714085 497548541 182089610 64356048 363276768 181268733 257949015 236568898 752096761 928725929 443146784 114577469 833053207 38120723 14891030 41...
output:
33 29 -3592700065 -37664434 -2056378585 2707849596 4599296673 -3308787864 2308866069 -3111936560 2708684938 -995101998 2086691849 -689006986 -909304549 -534254438 1541382122 -1764539266 -2385626322 3466936975 -685595993 4386228808 -207304947 816853839 2255554970 4777067949 -1478609675 2695498362 195...
result:
wrong answer 3rd lines differ - expected: '382465638565', found: '-3592700065'