結果
| 問題 |
No.956 Number of Unbalanced
|
| コンテスト | |
| ユーザー |
waynetuinfor
|
| 提出日時 | 2020-01-09 04:57:44 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
MLE
|
| 実行時間 | - |
| コード長 | 2,255 bytes |
| コンパイル時間 | 2,278 ms |
| コンパイル使用メモリ | 199,504 KB |
| 最終ジャッジ日時 | 2025-01-08 16:59:54 |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 6 |
| other | AC * 19 MLE * 5 |
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:75:17: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
75 | int n; scanf("%d", &n);
| ~~~~~^~~~~~~~~~
main.cpp:77:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
77 | scanf("%d", &a[i]);
| ~~~~~^~~~~~~~~~~~~
ソースコード
#include <bits/stdc++.h>
using namespace std;
const int kN = 100'000 + 5;
const int kM = kN + kN;
long long st[kM * 4], cf[kM * 4], ct[kM * 4];
int a[kN], l[kN], r[kN];
vector<int> pos[kN];
vector<pair<long long*, long long>> hist;
void RollBack() {
while (!hist.empty()) {
*hist.back().first = hist.back().second;
hist.pop_back();
}
hist.clear();
}
void Assign(long long *p, long long v) {
hist.emplace_back(p, *p);
*p = v;
}
void Apply(int o, long long f, long long t, int l, int r) {
Assign(&cf[o], cf[o] + f);
Assign(&ct[o], ct[o] + t);
Assign(&st[o], st[o] + f * (l + r - 1) * (r - l) / 2);
Assign(&st[o], st[o] + t * (r - l));
}
void Push(int o, int l, int r) {
int m = (l + r) >> 1;
Apply(o * 2 + 1, cf[o], ct[o], l, m);
Apply(o * 2 + 2, cf[o], ct[o], m, r);
Assign(&cf[o], 0);
Assign(&ct[o], 0);
}
long long Query(int l, int r, int ql, int qr, int o = 0) {
if (l >= qr || ql >= r) return 0;
if (l >= ql && r <= qr) return st[o];
Push(o, l, r);
int m = (l + r) >> 1;
return Query(l, m, ql, qr, o * 2 + 1) + Query(m, r, ql, qr, o * 2 + 2);
}
void Modify(int l, int r, int ql, int qr, long long f, long long t, int o = 0) {
if (l >= qr || ql >= r) return;
if (l >= ql && r <= qr) return Apply(o, f, t, l, r);
int m = (l + r) >> 1;
Modify(l, m, ql, qr, f, t, o * 2 + 1);
Modify(m, r, ql, qr, f, t, o * 2 + 2);
Assign(&st[o], st[o * 2 + 1] + st[o * 2 + 2]);
}
long long Solve(int v, int n) {
int m = pos[v].size();
for (int i = 0; i < m; ++i) {
r[i] = pos[v][i] - 1;
l[i + 1] = pos[v][i];
}
r[m] = n;
long long res = 0;
for (int i = 0; i <= m; ++i) {
int ql = 2 * i - r[i] + kN, qr = 2 * i - l[i] + kN;
res += Query(0, kM, ql - 1, qr);
Modify(0, kM, qr + 1, kM, 0, qr - ql + 1);
Modify(0, kM, ql, qr + 1, 1, 1 - ql);
}
RollBack();
return res;
}
int main() {
int n; scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
scanf("%d", &a[i]);
pos[a[i]].push_back(i);
}
long long ans = 0;
for (int i = 0; i < kN; ++i) {
if (!pos[i].empty()) ans += Solve(i, n);
}
printf("%lld\n", ans);
return 0;
}
waynetuinfor