Olympiad in Informatics

Codeforces 1240D: Stack Exterminable Arrays — Solution

Main Idea

When I first saw the divide-and-conquer tag, I made up my mind to solve it with divide and conquer, but before long I found the information extremely hard to make use of and gave up on it. Let's consider solving this problem with a method like the Aho–Corasick automaton, which is similar to KMP.

First, let \(dp[i]\) be the answer for those starting at \(i\) and extending to the right; then when we go left we can merge these, and we set \(nxt[i]\) to jump with. Since doing the process of getting the \(nxt\) array by brute force would make the time complexity GG, we can merge the information with a Map in a way similar to the Aho–Corasick automaton: swap in the Map at position \(nxt[i]+1\) and set the current position. Finally, just do the DP.

Code

// CF1240D.cpp
#include <bits/stdc++.h>
#define ll long long

using namespace std;

const int MAX_N = 3e5 + 200;

int q, n, ai[MAX_N], nxt[MAX_N], dp[MAX_N];
map<int, int> nxt_map[MAX_N];
ll ans;

int main()
{
    scanf("%d", &q);
    while (q--)
    {
        scanf("%d", &n), ans = 0;
        for (int i = 1; i <= n; i++)
            scanf("%d", &ai[i]);
        for (int i = 1; i <= n + 2; i++)
            nxt[i] = -1, dp[i] = 0, nxt_map[i].clear();
        for (int i = n; i >= 1; i--)
        {
            if (nxt_map[i + 1].count(ai[i]))
            {
                int pos = nxt_map[i + 1][ai[i]];
                if (pos <= n && ai[i] == ai[pos])
                {
                    nxt[i] = pos, swap(nxt_map[i], nxt_map[pos + 1]);
                    if (pos <= n - 1)
                        nxt_map[i][ai[pos + 1]] = pos + 1;
                }
            }
            nxt_map[i][ai[i]] = i;
        }
        for (int i = n; i >= 1; i--)
            if (nxt[i] != -1)
                dp[i] = 1 + dp[nxt[i] + 1], ans += dp[i];
        printf("%lld\n", ans);
    }
    return 0;
}