 # EQSS - EDITORIAL

Setter: Shivansh Agarwal
Testers: Nishank Suresh and Abhinav sharma
Editorialist: Shivansh Agarwal

2734

# PREREQUISITES

Dynamic Programming, Segment Tree

# PROBLEM

You have an array A containing all the natural numbers from 1 to N twice. Find the number of ordered pairs of disjoint subsequences (P, Q) of A such that they are equal.

# EXPLANATION

Let,

• dp_i :- denote the number of ordered pairs of subsequences (P, Q) that can be formed taking elements from index 0 to i.

• f_i, s_i :- denote the indices of the first and the second occurrence of A_i.

• t_i :- denote the number of unordered pairs of subsequences (P, Q) ending at indices f_i and s_i respectively.

Now, let’s say we have already computed dp_{i-1}.

#### Case 1: If i = f_i,

No additional pair of (P, Q) can be formed by including the element at index i. So we can simply say, dp_i = dp_{i-1}.

#### Case 2: If i = s_i,

We have to add all the ordered pairs of subsequences (P, Q) ending at f_i and i to dp_{i-1}. So we can say,

dp_i = dp_{i-1} + 2\cdot t_i.

Let’s try to find t_i now. This comprises of 3 parts:

• Part 1: We can add A_{f_i} and A_i respectively at the end of all the ordered pairs of subsequences (P, Q) formed from indices 0 to f_i - 1.

• Part 2: We can add A_{f_i} and A_i at the end of all the unordered pairs of subsequences (P, Q) which end at f_x and s_x repectively, such that, 0 \leq f_x < f_i < s_x < i.

• Part 3: An additional subsequence pair ([A_{f_i}], [A_{s_i}]).

The first part is simply dp_{f_i-1} and as f_i is the first occurrence of A_i, we can also say dp_{f_i-1} = dp_{f_i} (Case 1).

Now, for the second part, you have f_i and i as fixed and you need to sum all the values of t_x where, f_x < f_i < s_x < i. So, let’s say we have maintained an array T (say) till index i-1 with the value t_x at the index of the first occurrence and -t_x at the index of the second occurrence of all the elements having s_x \leq i - 1.

The sum of this array T from j = 0 to j = {f_i - 1} will give us the value of the second part because in case of s_x < f_i, the values t_i and -t_i are simply going to cancel each other out.

For the condition of s_x < i to be fulfilled at all times, we will update the array T only as we iterate over i. We cannot pre-compute the values of T, otherwise, the summation will also include values where f_x < f_i but s_x > i.

Mathematically,

T \begin{cases} T_{f_x} = T_{s_x} = 0, & \text{if } i - 1 < s_x\\ T_{f_x} = t_x \text{ and } T_{s_x} = -t_x, & \text{if } i - 1 >= s_x \end{cases}

and, t_i = dp_{f_i} + \sum_{j=0}^{j=f_i-1} T_j + 1.

Finally, we can update the array T as follows:
T_{f_i} = t_i and T_i = -t_i

These range sum and point update queries in T can be handled efficiently using a Segment Tree.

Note: No updates in T were required in Case 1 as i < s_i.

# TIME COMPLEXITY

The time complexity is O(N\cdot \log (N)).

# SOLUTIONS

Setter's Solution
// author: Shivansh Agarwal
#include <bits/stdc++.h>
using namespace std;
#define fastio ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define int long long
const int mod = 998244353;

vector<int> T; // Segment Tree
int n;

// 0-indexed
void modify(int p, int value)
{
for (T[p += n] = value; p > 1; p >>= 1)
T[p >> 1] = (T[p] + T[p ^ 1]) % mod;
}

int query(int l, int r) //query [l, r)
{
int res = 0;
for (l += n, r += n; l < r; l >>= 1, r >>= 1)
{
if (l & 1)
res = (res + T[l++]) % mod;
if (r & 1)
res = (res + T[--r]) % mod;
}
return res;
}

int32_t main()
{
fastio;
int tt;
cin >> tt;
while (tt--)
{
cin >> n;
n *= 2;

// Segment Tree T for range sum queries and point updates
T.clear();
T.resize(2 * n, 0);

//Initializing f and dp vectors which stores the first occurrence of an element and the value of dp respectively
vector<int> f(n / 2, -1), dp(n, 0);

for (int i = 0; i < n; i++)
{
int x;
cin >> x;
x--;

//Case 1: f[x] = -1, implies i is the first occurrence
if (f[x] == -1)
{
f[x] = i;
if(i > 0)
dp[i] = dp[i - 1];
}

// Case 2: i is the second occurrence, f[x] is the first
else
{
//Calculating ti
int ti = (dp[f[x]] + query(0, f[x]) + 1) % mod;

//Calculating dp value from ti
dp[i] = (dp[i - 1] + 2 * ti) % mod;

//Updating T
modify(f[x], ti);
modify(i, mod - ti);
}
}
cout << dp[n - 1] << "\n";
}
}

Tester's Solution 1
mod = 998244353
class FenwickTree:
def __init__(self, x):
self.bit = x
for i in range(len(x)):
j = i | (i + 1)
if j < len(x):
x[j] += x[i]

def update(self, idx, x):
while idx < len(self.bit):
self.bit[idx] += x
idx |= idx + 1

def query(self, end):
x = 0
while end:
x += self.bit[end - 1]
end &= end - 1
return x

for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
dp = *(2*n+1)
mark = *(n+1)
other = [-1]*(n+1)
for i in reversed(range(2*n)):
if other[a[i]] == -1:
other[a[i]] = i

F = FenwickTree([0 for _ in range(2*n + 5)])

before, ans = 1, 0
for x in a:
if mark[x] == 1:
F.update(other[x], -dp[x]%mod)
before += dp[x]
before %= mod
else:
mark[x] = 1
dp[x] = 2*before + F.query(other[x])
ans += dp[x]
F.update(other[x], dp[x]%mod)
ans %= mod
print(ans)

Tester's Solution 2
#include <bits/stdc++.h>
using namespace std;

/*
------------------------Input Checker----------------------------------
*/

long long readInt(long long l,long long r,char endd){
long long x=0;
int cnt=0;
int fi=-1;
bool is_neg=false;
while(true){
char g=getchar();
if(g=='-'){
assert(fi==-1);
is_neg=true;
continue;
}
if('0'<=g && g<='9'){
x*=10;
x+=g-'0';
if(cnt==0){
fi=g-'0';
}
cnt++;
assert(fi!=0 || cnt==1);
assert(fi!=0 || is_neg==false);

assert(!(cnt>19 || ( cnt==19 && fi>1) ));
} else if(g==endd){
if(is_neg){
x= -x;
}

if(!(l <= x && x <= r))
{
cerr << l << ' ' << r << ' ' << x << '\n';
assert(1 == 0);
}

return x;
} else {
assert(false);
}
}
}
string ret="";
int cnt=0;
while(true){
char g=getchar();
assert(g!=-1);
if(g==endd){
break;
}
cnt++;
ret+=g;
}
assert(l<=cnt && cnt<=r);
return ret;
}
long long readIntSp(long long l,long long r){
}
long long readIntLn(long long l,long long r){
}
}
}

/*
------------------------Main code starts here----------------------------------
*/

const int MAX_T = 1e5;
const int MAX_N = 1e5;
const int MAX_SUM_LEN = 1e5;

#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define ff first
#define ss second
#define mp make_pair
#define ll long long
#define rep(i,n) for(int i=0;i<n;i++)
#define rev(i,n) for(int i=n;i>=0;i--)
#define rep_a(i,a,n) for(int i=a;i<n;i++)
#define pb push_back

int sum_n = 0, sum_m = 0;
int max_n = 0, max_m = 0;
int yess = 0;
int nos = 0;
int total_ops = 0;
ll mod = 998244353;

using ii = pair<ll,ll>;

struct fentree{
// 0 based indexing
vector<ll> v;
int _n;
fentree(int n){
v.assign(n+5,0);
_n = n+5;
}

void upd(int pos, ll val){
while(pos<_n){
v[pos]+=val;
v[pos]%=mod;
pos|=(pos+1);
}
}

ll qr(int pos){
ll ret = 0;
while(pos>=0){
ret += v[pos];
ret%=mod;
pos&=(pos+1);
pos--;
}
return ret;
}
};

void solve(){
sum_n+=n;

int a[2*n];
int cnt[2*n+1] = {0};
rep(i,2*n-1){
cnt[a[i]]++;
}
cnt[a[2*n-1]]++;

rep_a(i,1,n+1) assert(cnt[i]==2);

struct fentree ft(2*n);

vector<int> pos(n+1,-1);
ll ans = 0;
rev(i,2*n-1){
if(pos[a[i]]!=-1){
ll tmp = ft.qr(pos[a[i]])+1;
ft.upd(0, 2*tmp);
ft.upd(i, -2*tmp);
ft.upd(i, tmp);
ft.upd(pos[a[i]], -tmp);

ans+=2*tmp;
ans%=mod;
}
else pos[a[i]]=i;
}

if(ans<0) ans+=mod;

cout<<ans%mod<<'\n';

}

signed main()
{

#ifndef ONLINE_JUDGE
freopen("input.txt", "r" , stdin);
freopen("output.txt", "w" , stdout);
#endif
fast;

int t = 1;

for(int i=1;i<=t;i++)
{
solve();
}

assert(getchar() == -1);
assert(sum_n<=6e5);

cerr<<"SUCCESS\n";
cerr<<"Tests : " << t << '\n';
cerr<<"Sum of lengths : " << sum_n <<" "<<sum_m<<'\n';
// cerr<<"Maximum length : " << max_n <<'\n';
// // cerr<<"Total operations : " << total_ops << '\n';
// cerr<<"Answered yes : " << yess << '\n';
// cerr<<"Answered no : " << nos << '\n';

cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n";
}


Feel free to share your approach. Suggestions are welcomed. 4 Likes

what does ordered and unordered pair of subsequences mean?

1 Like

An unordered pair does not depend on the order of (X, Y). For example,

For a sequence A, ([A_1, A_4], [A_2, A_3]) and ([A_2, A_3], [A_1, A_4]) are counted as 2 distinct ordered pairs of subsequences whereas they are considered the same unordered pair of subsequences.

Now, For Part 1, because both A_{f_i} and A_i can be added at the ends of both the sequences P and Q (we are considering the sequences which end before the index f_i), hence we need to count the ordered pairs.

Whereas, for Part 2, A_{f_i} can only be added after the sequence ending with f_x and A_i after the sequence ending with s_x (vice versa cannot be done). Hence, we need to take the number of unordered pairs of subsequences ending at f_x and s_x respectively, \forall x, such that, 0≤f_x<f_i<s_x<i .

I hope this helps! 1 Like

Can anyone, please explain why 2.ti?

1 Like

Because we are adding ordered pair of subsequences (P,Q) ending at fi and i . Since last element of both P and Q are same it does not matter whether we add fi to P and i to Q or fi to Q or i to P. And since ordered pair defines these 2 to be different pairs we count them twice . Hence 2*ti.
I think this is how it is
I am correct @shivansh0809 ?

1 Like

I don’t know, why setter’s solution get wrong answer in several test cases?

1 Like

Thank you for pointing that out!!. The solution is updated. (It was missing one of my favorite defines, #define int long long. )

Sorry for the inconvenience!

Yep, correct!