You can find my code for this contest here.

C. Rigged Bracket Sequence

This observation is not necessary but definitely makes the DP much easier to think about: the β€œshift right” observation is equivalent to swapping the final bracket to the front. This is nice because when we swap brackets 𝑙 and π‘Ÿ, only the balance between [𝑙,π‘Ÿ) is affected. Moreover, the balance cannot decrease unless 𝑠𝑙 and π‘ π‘Ÿ are ( and ) respectively. Therefore, we can let dp[i] denote the number of ways to swap 𝑠𝑖 to the left some number of times, assuming 𝑠𝑖 is currently ). This results in a concise π’ͺ(𝑛) solution:

void ac() {
    int n; cin >> n;
    string s; cin >> s;
    vector<int> ways(n);
    int tot = 0;
    for (
	    int i = 0, p2 = 1, bal = 0, sum_l = 0, sum_r = 0; i < n; 
	    i++, p2 = p2 * 2 % M
    ) {
        ways[i] = (1 + sum_l + sum_r) % M;
        if (s[i] == '(') {
            ad(tot, p2);
            ad(sum_l, ways[i]);
        }
        if (s[i] == ')') {
            ad(sum_r, ways[i]);
            ad(tot, ways[i]);
        }
        bal += (s[i] == '(' ? 1 : -1);
        if (bal < 2) sum_l = 0;
    }
    cout << tot << "\n";
}

D. Binary Not Search and Queries

A clear lower bound on the answer is the maximum gap between any two equal values. We will now show that this is also an upper bound. For an (𝑖,𝑗,π‘˜) triple, let 𝐼 denote the list of values that are contained only in π‘–β€˜s range, and let 𝐽 denote the set of values that are contained only in π‘—β€˜s range. Then, the average distance between pairs of equal elements in 𝐼 and 𝐽 must be at least π‘˜, which means there must exist a pair with distance β‰₯π‘˜.

This proof also shows that if π‘˜ is exactly equal to the maximum gap, 𝐼 must exactly equal 𝐽. Therefore, for a given gap size, we need only to maintain the ranges formed by the first elements of values that have that gap. This can be done with std::set spam in π’ͺ((π‘ž+𝑛)log𝑛) like so.