1223F - Stack Exterminable Arrays - CodeForces Solution


data structures divide and conquer dp hashing *2600

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 3e5;
const int mod = 998244853;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int P;
int v[N];
pair<int,int> h[N];
stack <int> stk;
map<pair<int,int>,int> f;
int fastexp[N];
ll solve(int l,int r){
    ll ans = 0;
    int mij = (l + r)/2;
    if(l < mij)ans+=solve(l,mij);
    if(mij + 1 < r)ans+=solve(mij + 1,r);
    //cout<<l<<' '<<r<<'\n';
    f.clear();
    ///l - > mij
    while(!stk.empty())stk.pop();
    int sz = 0,hsh = 0,hsh2 = 0;
    for(int i = mij;i >= l;i--){
        if(!stk.empty() && stk.top() == v[i]){
            sz--;
            hsh = (hsh - 1ll*h[stk.top()].first*fastexp[sz]%mod + mod)%mod;
            hsh2 = (hsh2 - 1ll*h[stk.top()].second*fastexp[sz]%mod + mod)%mod;
            stk.pop();
        }else{
            stk.push(v[i]);
            hsh = (hsh + 1ll*h[stk.top()].first*fastexp[sz]%mod)%mod;
            hsh2 = (hsh2 + 1ll*h[stk.top()].second*fastexp[sz]%mod)%mod;
            sz++;
        }
        //cout<<mij<<' '<<i<<' '<<hsh<<'\n';
        f[{hsh,hsh2}]++;
    }
    ///mij + 1 - > r
    while(!stk.empty())stk.pop();
    sz = 0;hsh = 0;hsh2 = 0;
    for(int i = mij + 1;i <= r;i++){
        if(!stk.empty() && stk.top() == v[i]){
            sz--;
            hsh = (hsh - 1ll*h[stk.top()].first*fastexp[sz]%mod + mod)%mod;
            hsh2 = (hsh2 - 1ll*h[stk.top()].second*fastexp[sz]%mod + mod)%mod;
            stk.pop();
        }else{
            stk.push(v[i]);
            hsh = (hsh + 1ll*h[stk.top()].first*fastexp[sz]%mod)%mod;
            hsh2 = (hsh2 + 1ll*h[stk.top()].second*fastexp[sz]%mod)%mod;
            sz++;
        }
        ans+=f[{hsh,hsh2}];
    }
    return ans;
}
void solve(){
    int n;
    cin>>n;
    for(int i = 0;i < n;i++){
        cin>>v[i];
        v[i]--;
    }
    cout<<solve(0,n - 1)<<'\n';
}
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    P = rng()%300 + 300;
    fastexp[0] = 1;
    for(int i = 0;i < N;i++){
        h[i] = {rng()%mod,rng()%mod};
        if(i)fastexp[i] = 1ll*fastexp[i - 1]*P%mod;
    }
    int t;
    cin>>t;
    while(t--)solve();
    return 0;
}


Comments

Submit
0 Comments
More Questions

1560C - Infinity Table
1605C - Dominant Character
1399A - Remove Smallest
208A - Dubstep
1581A - CQXYM Count Permutations
337A - Puzzles
495A - Digital Counter
796A - Buying A House
67A - Partial Teacher
116A - Tram
1472B - Fair Division
1281C - Cut and Paste
141A - Amusing Joke
112A - Petya and Strings
677A - Vanya and Fence
1621A - Stable Arrangement of Rooks
472A - Design Tutorial Learn from Math
1368A - C+=
450A - Jzzhu and Children
546A - Soldier and Bananas
32B - Borze
1651B - Prove Him Wrong
381A - Sereja and Dima
41A - Translation
1559A - Mocha and Math
832A - Sasha and Sticks
292B - Network Topology
1339A - Filling Diamonds
910A - The Way to Home
617A - Elephant