#pragma GCC optimize("O3,unroll-loops") /* #pragma GCC optimize("Ofast,unroll-loops") */ #pragma GCC target("avx2,popcnt,fma") #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define pb push_back #define mp make_pair #define all(a) begin(a),end(a) #define FOR(x,val,to) for(int x=(val);x<int((to));++x) #define FORE(x,val,to) for(auto x=(val);x<=(to);++x) #define FORR(x,arr) for(auto &x: arr) #define FORS(x,plus,arr) for(auto x = begin(arr)+(plus); x != end(arr); ++x) #define FORREV(x,plus,arr) for(auto x = (arr).rbegin()+(plus); x !=(arr).rend(); ++x) #define REE(s_) {cout<<s_<<'\n';exit(0);} #define GET(arr) for(auto &i: (arr)) sc(i) #define whatis(x) cerr << #x << " is " << (x) << endl; #define debug(x...) cerr << "[" #x "]: ", [](auto... $) {((cerr << $ << "; "), ...); }(x), cerr << '\n' #define e1 first #define e2 second #define INF 0x7f7f7f7f typedef std::pair<int,int> pi; typedef std::vector<int> vi; typedef std::vector<std::string> vs; typedef int64_t ll; typedef uint64_t ull; #define umap unordered_map #define uset unordered_set using namespace std; using namespace __gnu_pbds; #ifdef ONLINE_JUDGE #define whatis(x) ; #define debug(x...) ; #endif #ifdef _WIN32 #define getchar_unlocked() _getchar_nolock() #define _CRT_DISABLE_PERFCRIT_LOCKS #endif template<class A, class B> auto& operator<<(ostream &o, pair<A, B> p) { return o << '(' << p.first << ", " << p.second << ')'; } template<class T, enable_if_t<!is_same<T, string>::value, bool> = true> auto operator<<(ostream &o, T x) -> decltype(x.end(), o) { o << '{'; int i = 0; for(auto e : x) o << (", ")+2*!i++ << e; return o << '}'; } inline void getch(char &x){while(x = getchar_unlocked(), x < 33){;}} inline void getstr(string &str){str.clear(); char cur;while(cur=getchar_unlocked(),cur<33){;}while(cur>32){str+=cur;cur=getchar_unlocked();}} template<typename T> inline bool sc(T &num){ bool neg=0; int c; num=0; while(c=getchar_unlocked(),c<33){if(c == EOF) return false;} if(c=='-'){ neg=1; c=getchar_unlocked(); } for(;c>47;c=getchar_unlocked()) num=num*10+c-48; if(neg) num*=-1; return true;}template<typename T, typename ...Args> inline void sc(T &num, Args &...args){ bool neg=0; int c; num=0; while(c=getchar_unlocked(),c<33){;} if(c=='-'){ neg=1; c=getchar_unlocked(); } for(;c>47;c=getchar_unlocked()) num=num*10+c-48; if(neg) num*=-1; sc(args...); } template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; //s.find_by_order(), s.order_of_key() <- works like lower_bound template<typename T> using ordered_map = tree<T, int, less<T>, rb_tree_tag, tree_order_statistics_node_update>; #define N 1000001 int main(){ ios_base::sync_with_stdio(0);cin.tie(0); string s; getstr(s); map<pair<int,int>, int> mp; ++mp[{0,0}]; /* map<int, int> mpab; */ /* map<int, int> mpbc; */ /* map<int, int> mpac; */ ll res{}; pair<int,int> cr{}; /* static int crab, crbc, crac; */ /* ++mp[{0,0}]; */ /* ++mpab[0]; */ /* ++mpbc[0]; */ /* ++mpac[0]; */ /* map<int,int> mp2; */ /* pair<char,char> wh2 = {-1,-1}; */ /* int lspos[128]; */ /* lspos['a'] = lspos['b'] = lspos['c'] = -1; */ static int it; FORR(i,s){ if(i == 'a') ++cr.e1; else if(i == 'b') --cr.e1, ++cr.e2; else --cr.e2; /* lspos[i] = it++; */ /* if(jj */ /* if(i == 'a') */ /* ++cr.e1, ++crab, ++crac; */ /* else if(i == 'b') */ /* --cr.e1, ++cr.e2, --crab, ++crbc; */ /* else */ /* --cr.e2, --crac, --crbc; */ res += mp[cr]++; /* res -= 3 * mp[cr]++; */ /* res += mpab[crab]++; */ /* res += mpac[crac]++; */ /* res += mpbc[crbc]++; */ } /* debug(res); */ map<int,int> mp2; static int cr2; FOR(i,0,s.size()){ /* debug(i); */ /* debug(res); */ /* int it = i; */ int it = i - 1; mp2.clear(); ++mp2[0]; cr2 ^= cr2; while(it + 1 < s.size() && s[it + 1] == s[i]) ++it, ++mp2[++cr2]; int sth = it; int mxsi = sth, mxsi2 = sth + 1; /* while(it + 1 < s.size() && s[it + 1] == s[sth + 1] || s[it + 1] == s[i]){ */ /* debug(it, s[it + 1], s[i], s[sth + 1]); */ while(it + 1 < s.size() && (s[it + 1] == s[sth + 1] || s[it + 1] == s[i])){ ++it; if(s[it] == s[i]) ++cr2, mxsi = it; else --cr2, mxsi2 = it; /* debug(cr2, mp2[cr2]); */ res += mp2[cr2]++; } /* debug(mxsi, mxsi2); */ /* debug(res); */ /* debug(s[i], s[mxsi]); */ /* i = mxsi; */ i = min(mxsi, mxsi2); } /* debug(res); */ FOR(i,0,s.size()){ int it = i; while(it + 1 < s.size() && s[it + 1] == s[i]) ++it, ++mp2[++cr2]; res += 1ll*(it - i + 2)*(it - i + 1) >> 1; i = it; } cout << res << '\n'; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | #pragma GCC optimize("O3,unroll-loops") /* #pragma GCC optimize("Ofast,unroll-loops") */ #pragma GCC target("avx2,popcnt,fma") #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define pb push_back #define mp make_pair #define all(a) begin(a),end(a) #define FOR(x,val,to) for(int x=(val);x<int((to));++x) #define FORE(x,val,to) for(auto x=(val);x<=(to);++x) #define FORR(x,arr) for(auto &x: arr) #define FORS(x,plus,arr) for(auto x = begin(arr)+(plus); x != end(arr); ++x) #define FORREV(x,plus,arr) for(auto x = (arr).rbegin()+(plus); x !=(arr).rend(); ++x) #define REE(s_) {cout<<s_<<'\n';exit(0);} #define GET(arr) for(auto &i: (arr)) sc(i) #define whatis(x) cerr << #x << " is " << (x) << endl; #define debug(x...) cerr << "[" #x "]: ", [](auto... $) {((cerr << $ << "; "), ...); }(x), cerr << '\n' #define e1 first #define e2 second #define INF 0x7f7f7f7f typedef std::pair<int,int> pi; typedef std::vector<int> vi; typedef std::vector<std::string> vs; typedef int64_t ll; typedef uint64_t ull; #define umap unordered_map #define uset unordered_set using namespace std; using namespace __gnu_pbds; #ifdef ONLINE_JUDGE #define whatis(x) ; #define debug(x...) ; #endif #ifdef _WIN32 #define getchar_unlocked() _getchar_nolock() #define _CRT_DISABLE_PERFCRIT_LOCKS #endif template<class A, class B> auto& operator<<(ostream &o, pair<A, B> p) { return o << '(' << p.first << ", " << p.second << ')'; } template<class T, enable_if_t<!is_same<T, string>::value, bool> = true> auto operator<<(ostream &o, T x) -> decltype(x.end(), o) { o << '{'; int i = 0; for(auto e : x) o << (", ")+2*!i++ << e; return o << '}'; } inline void getch(char &x){while(x = getchar_unlocked(), x < 33){;}} inline void getstr(string &str){str.clear(); char cur;while(cur=getchar_unlocked(),cur<33){;}while(cur>32){str+=cur;cur=getchar_unlocked();}} template<typename T> inline bool sc(T &num){ bool neg=0; int c; num=0; while(c=getchar_unlocked(),c<33){if(c == EOF) return false;} if(c=='-'){ neg=1; c=getchar_unlocked(); } for(;c>47;c=getchar_unlocked()) num=num*10+c-48; if(neg) num*=-1; return true;}template<typename T, typename ...Args> inline void sc(T &num, Args &...args){ bool neg=0; int c; num=0; while(c=getchar_unlocked(),c<33){;} if(c=='-'){ neg=1; c=getchar_unlocked(); } for(;c>47;c=getchar_unlocked()) num=num*10+c-48; if(neg) num*=-1; sc(args...); } template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; //s.find_by_order(), s.order_of_key() <- works like lower_bound template<typename T> using ordered_map = tree<T, int, less<T>, rb_tree_tag, tree_order_statistics_node_update>; #define N 1000001 int main(){ ios_base::sync_with_stdio(0);cin.tie(0); string s; getstr(s); map<pair<int,int>, int> mp; ++mp[{0,0}]; /* map<int, int> mpab; */ /* map<int, int> mpbc; */ /* map<int, int> mpac; */ ll res{}; pair<int,int> cr{}; /* static int crab, crbc, crac; */ /* ++mp[{0,0}]; */ /* ++mpab[0]; */ /* ++mpbc[0]; */ /* ++mpac[0]; */ /* map<int,int> mp2; */ /* pair<char,char> wh2 = {-1,-1}; */ /* int lspos[128]; */ /* lspos['a'] = lspos['b'] = lspos['c'] = -1; */ static int it; FORR(i,s){ if(i == 'a') ++cr.e1; else if(i == 'b') --cr.e1, ++cr.e2; else --cr.e2; /* lspos[i] = it++; */ /* if(jj */ /* if(i == 'a') */ /* ++cr.e1, ++crab, ++crac; */ /* else if(i == 'b') */ /* --cr.e1, ++cr.e2, --crab, ++crbc; */ /* else */ /* --cr.e2, --crac, --crbc; */ res += mp[cr]++; /* res -= 3 * mp[cr]++; */ /* res += mpab[crab]++; */ /* res += mpac[crac]++; */ /* res += mpbc[crbc]++; */ } /* debug(res); */ map<int,int> mp2; static int cr2; FOR(i,0,s.size()){ /* debug(i); */ /* debug(res); */ /* int it = i; */ int it = i - 1; mp2.clear(); ++mp2[0]; cr2 ^= cr2; while(it + 1 < s.size() && s[it + 1] == s[i]) ++it, ++mp2[++cr2]; int sth = it; int mxsi = sth, mxsi2 = sth + 1; /* while(it + 1 < s.size() && s[it + 1] == s[sth + 1] || s[it + 1] == s[i]){ */ /* debug(it, s[it + 1], s[i], s[sth + 1]); */ while(it + 1 < s.size() && (s[it + 1] == s[sth + 1] || s[it + 1] == s[i])){ ++it; if(s[it] == s[i]) ++cr2, mxsi = it; else --cr2, mxsi2 = it; /* debug(cr2, mp2[cr2]); */ res += mp2[cr2]++; } /* debug(mxsi, mxsi2); */ /* debug(res); */ /* debug(s[i], s[mxsi]); */ /* i = mxsi; */ i = min(mxsi, mxsi2); } /* debug(res); */ FOR(i,0,s.size()){ int it = i; while(it + 1 < s.size() && s[it + 1] == s[i]) ++it, ++mp2[++cr2]; res += 1ll*(it - i + 2)*(it - i + 1) >> 1; i = it; } cout << res << '\n'; } |