Submission #72507930
Source Code Expand
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define all(x) x.begin(), x.end()
#ifndef ONLINE_JUDGE
#include "debug.h"
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template <class T> void output(vector < T > & a) { for (auto & x: a) {cout << x << ' ';}cout << '\n';}
// Golden Rules
/*
Solutions are simple.
Proofs are simple.
Implementations are simple.
~ TEJA DRONADULA SIR!!
*/
// source : cp-algorithm
struct FenwickTree {
vector<int> bit; // binary indexed tree
int n;
FenwickTree(int n) {
this->n = n;
bit.assign(n, 0);
}
FenwickTree(vector<int> const &a) : FenwickTree(a.size()) {
for (size_t i = 0; i < a.size(); i++)
add(i, a[i]);
}
int sum(int r) {
int ret = 0;
for (; r >= 0; r = (r & (r + 1)) - 1)
ret += bit[r];
return ret;
}
int sum(int l, int r) {
return sum(r) - sum(l - 1);
}
void add(int idx, int delta) {
for (; idx < n; idx = idx | (idx + 1))
bit[idx] += delta;
}
};
void solve() {
int N;
cin >> N;
string S;
cin >> S;
vector<int> pre(N + 1);
for (int i = 0; i < N; ++i) {
pre[i + 1] = (S[i] == 'A' ? 1 : S[i] == 'B' ? -1 : 0) + pre[i];
}
for (int i = 0; i <= N; ++i) {
pre[i] += N;
}
FenwickTree fen(2 * N + 1);
int ans = 0;
for (int i = 0; i <= N; ++i) {
ans += fen.sum(pre[i] - 1);
fen.add(pre[i], 1);
}
cout << ans << "\n";
}
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
freopen("error.txt", "w", stderr);
#endif
int t = 1;
// cin >> t;
for (int cases = 1; cases <= t; ++cases) {
solve();
}
#ifndef ONLINE_JUDGE
double T = 1000.0 * clock() / CLOCKS_PER_SEC;
cout << "\n[Finished in " << T << "ms]";
cerr << "\n[Finished in " << T << "ms]";
#endif
return 0;
}
Submission Info
| Submission Time | |
|---|---|
| Task | E - A > B substring |
| User | Gokuu007 |
| Language | C++23 (GCC 15.2.0) |
| Score | 450 |
| Code Size | 2319 Byte |
| Status | AC |
| Exec Time | 15 ms |
| Memory | 15852 KiB |
Judge Result
| Set Name | Sample | All | ||||
|---|---|---|---|---|---|---|
| Score / Max Score | 0 / 0 | 450 / 450 | ||||
| Status |
|
|
| Set Name | Test Cases |
|---|---|
| Sample | 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt |
| All | 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 01_random_10.txt, 01_random_11.txt, 01_random_12.txt, 01_random_13.txt, 01_random_14.txt, 01_random_15.txt, 01_random_16.txt, 01_random_17.txt, 01_random_18.txt, 01_random_19.txt, 01_random_20.txt, 01_random_21.txt, 01_random_22.txt, 01_random_23.txt, 01_random_24.txt, 01_random_25.txt, 01_random_26.txt, 01_random_27.txt, 01_random_28.txt, 01_random_29.txt, 01_random_30.txt, 01_random_31.txt, 01_random_32.txt, 01_random_33.txt, 01_random_34.txt |
| Case Name | Status | Exec Time | Memory |
|---|---|---|---|
| 00_sample_00.txt | AC | 2 ms | 3516 KiB |
| 00_sample_01.txt | AC | 1 ms | 3572 KiB |
| 00_sample_02.txt | AC | 1 ms | 3468 KiB |
| 01_random_03.txt | AC | 15 ms | 15800 KiB |
| 01_random_04.txt | AC | 15 ms | 15652 KiB |
| 01_random_05.txt | AC | 15 ms | 15648 KiB |
| 01_random_06.txt | AC | 15 ms | 15788 KiB |
| 01_random_07.txt | AC | 15 ms | 15664 KiB |
| 01_random_08.txt | AC | 15 ms | 15720 KiB |
| 01_random_09.txt | AC | 15 ms | 15780 KiB |
| 01_random_10.txt | AC | 15 ms | 15812 KiB |
| 01_random_11.txt | AC | 14 ms | 15812 KiB |
| 01_random_12.txt | AC | 14 ms | 15724 KiB |
| 01_random_13.txt | AC | 14 ms | 15624 KiB |
| 01_random_14.txt | AC | 15 ms | 15852 KiB |
| 01_random_15.txt | AC | 15 ms | 15728 KiB |
| 01_random_16.txt | AC | 12 ms | 13240 KiB |
| 01_random_17.txt | AC | 9 ms | 10296 KiB |
| 01_random_18.txt | AC | 7 ms | 8700 KiB |
| 01_random_19.txt | AC | 7 ms | 8624 KiB |
| 01_random_20.txt | AC | 8 ms | 9596 KiB |
| 01_random_21.txt | AC | 10 ms | 11044 KiB |
| 01_random_22.txt | AC | 5 ms | 6956 KiB |
| 01_random_23.txt | AC | 5 ms | 7040 KiB |
| 01_random_24.txt | AC | 11 ms | 15648 KiB |
| 01_random_25.txt | AC | 13 ms | 15776 KiB |
| 01_random_26.txt | AC | 14 ms | 15772 KiB |
| 01_random_27.txt | AC | 12 ms | 15696 KiB |
| 01_random_28.txt | AC | 14 ms | 15732 KiB |
| 01_random_29.txt | AC | 12 ms | 15700 KiB |
| 01_random_30.txt | AC | 12 ms | 15696 KiB |
| 01_random_31.txt | AC | 11 ms | 15808 KiB |
| 01_random_32.txt | AC | 1 ms | 3572 KiB |
| 01_random_33.txt | AC | 1 ms | 3468 KiB |
| 01_random_34.txt | AC | 1 ms | 3472 KiB |