P1787 [入门赛 #22] 非众数 Hard Version.cpp

 1 #include<bits/stdc++.h>
 2 #define int int_fast64_t
 3 #define lowbit(x) (x&(-x))
 4 using namespace std;
 5 int n,s[int(2e5+5)],p[int(1e5+5)];
 6 void update(int x,int k){
 7     for(int i=x;i<(n<<1)+5;i+=lowbit(i)){
 8         s[i]+=k;
 9     }
10 }
11 int query(int x){
12     int ans=0;
13     for(int i=x;i>=1;i-=lowbit(i)){
14         ans+=s[i];
15     }
16     return ans;
17 }
18 int32_t main(){
19     ios::sync_with_stdio(false);
20     cin.tie(nullptr),cout.tie(nullptr);
21     string str;
22     cin>>str;
23     n=str.size();
24     int ans=((n*(n+1))>>1);
25     str=" "+str;
26     for(char c='a';c<='z';c++){
27         memset(s,0,sizeof s);
28         int res=0;
29         for(int i=1;i<=n;i++){
30             p[i]=p[i-1]+(str[i]==c);
31             update(i-(p[i-1]<<1)+n+1,1);
32             res+=query((n<<1)+4)-query(i-(p[i]<<1)+n+2);
33         }
34         ans-=res;
35     }
36     cout<<ans;
37     return 0;
38 }

 

posted @ 2026-08-20 20:51  longyitongxue  阅读(2)  评论(1)    收藏  举报
longint's blog

2025-2026, © longint's blog