QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#154761#5500. Barsberarchegas#WA 213ms28252kbC++172.3kb2023-08-31 22:01:242023-08-31 22:01:25

Judging History

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

  • [2023-08-31 22:01:25]
  • 评测
  • 测评结果:WA
  • 用时:213ms
  • 内存:28252kb
  • [2023-08-31 22:01:24]
  • 提交

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];

ll 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: 2ms
memory: 27372kb

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: 213ms
memory: 28252kb

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
348594618207
648502397262
530519566119
419319677308
279477203617
786965194600
431805595669
422089825744
208867115146
613185221330
461648192521
282778834550
548846509339
175559404698
336548831210
444912059518
530190318382
463028437647
407336297127
618566552136
742822037261
722371359567
22559385...

result:

wrong answer 3rd lines differ - expected: '382465638565', found: '348594618207'