Submission #72530359


Source Code Expand

#include <bits/stdc++.h>
#include <bits/extc++.h>
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
const int maxn = 5e5 + 5;
int a[maxn];
class BIT //树状数组
{
public:
    long long tr[maxn*2+100];
    int n;

    void init(int x) //清空并初始化
    {
        for (int i = 1; i <= x; i++)
            tr[i] = 0;
        n = x;
    }

    int lowbit(int x)
    {
        return x & -x;
    }

    void add(int x, long long d) //将第x个数+k
    {
        for (int i = x; i <= n; i += lowbit(i))
            tr[i] += d;
    }

    long long sum(int x) //求前x个数的和
    {
        long long ans = 0;
        for (int i = x; i; i -= lowbit(i))
            ans += tr[i];
        return ans;
    }
} tr;
int sum[maxn];
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int n;
    cin>>n;
    string s;
    cin>>s;
    s=' '+s;
    long long ans=0;
    sum[0]=n+2;
    tr.n=2*n+10;
    tr.add(sum[0],1);
    for (int i=1;i<=n;i++)
    {
        sum[i]=sum[i-1];
        if (s[i]=='A')
            sum[i]++;
        else if (s[i]=='B')
            sum[i]--;
        ans+=tr.sum(sum[i]-1);
        tr.add(sum[i],1);
    }
    cout<<ans;
}

Submission Info

Submission Time
Task E - A > B substring
User Alliy666
Language C++23 (GCC 15.2.0)
Score 450
Code Size 1290 Byte
Status AC
Exec Time 11 ms
Memory 9808 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 450 / 450
Status
AC × 3
AC × 35
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 1 ms 3556 KiB
00_sample_01.txt AC 1 ms 3528 KiB
00_sample_02.txt AC 1 ms 3612 KiB
01_random_03.txt AC 11 ms 5916 KiB
01_random_04.txt AC 11 ms 5952 KiB
01_random_05.txt AC 11 ms 5940 KiB
01_random_06.txt AC 11 ms 5988 KiB
01_random_07.txt AC 11 ms 5908 KiB
01_random_08.txt AC 11 ms 5972 KiB
01_random_09.txt AC 11 ms 5860 KiB
01_random_10.txt AC 11 ms 5984 KiB
01_random_11.txt AC 11 ms 6004 KiB
01_random_12.txt AC 11 ms 5864 KiB
01_random_13.txt AC 11 ms 5968 KiB
01_random_14.txt AC 11 ms 5972 KiB
01_random_15.txt AC 11 ms 5976 KiB
01_random_16.txt AC 9 ms 5332 KiB
01_random_17.txt AC 7 ms 4864 KiB
01_random_18.txt AC 5 ms 4844 KiB
01_random_19.txt AC 5 ms 4700 KiB
01_random_20.txt AC 6 ms 4864 KiB
01_random_21.txt AC 7 ms 4988 KiB
01_random_22.txt AC 4 ms 4496 KiB
01_random_23.txt AC 4 ms 4532 KiB
01_random_24.txt AC 8 ms 5852 KiB
01_random_25.txt AC 7 ms 5972 KiB
01_random_26.txt AC 10 ms 8532 KiB
01_random_27.txt AC 9 ms 9696 KiB
01_random_28.txt AC 10 ms 7068 KiB
01_random_29.txt AC 9 ms 9780 KiB
01_random_30.txt AC 10 ms 9808 KiB
01_random_31.txt AC 7 ms 5852 KiB
01_random_32.txt AC 1 ms 3528 KiB
01_random_33.txt AC 1 ms 3652 KiB
01_random_34.txt AC 1 ms 3556 KiB