Nourhan_Abo-Heba's blog

By Nourhan_Abo-Heba, history, 11 months ago, In English

--- https://codeforces.me/contest/2175/problem/A

Tip: Counting Distinct Elements in All Prefixes

When a problem asks for the number of distinct elements before or up to each index, use prefix traversal + frequency tracking.

Idea

  • Traverse the array from left to right.
  • Maintain a frequency map (or array).
  • When an element appears for the first time, increase the distinct count.
  • Store this count for each prefix.

Implementation

map<int, int> freq;
vector<int> distinct(n);
int cnt = 0;

for (int i = 0; i < n; i++) {
    if (++freq[x[i]] == 1)
        cnt++;              // new distinct element
    distinct[i] = cnt;      // distinct elements in prefix [0..i]
}

Sum of Distinct Counts Over All Prefixes

long long ans = 0;
for (int i = 0; i < n; i++) {
    ans += distinct[i];
}

Why This Works

Each element contributes to the distinct count exactly once, at its first occurrence.


--- https://codeforces.me/contest/2175/problem/B

Idea: Reconstruct the Array Using Prefix XOR

Sometimes we don’t know the array a directly, but we know how its prefix XOR array should look. In that case, the trick is:

  1. Construct a valid prefix XOR array p
  2. Recover the original array a from it

Key Observation

If [ p[i] = a[1] \oplus a[2] \oplus \dots \oplus a[i] ]

then: [ a[i] = p[i] \oplus p[i-1] ]

So once p is known, a is uniquely determined.


Constructing the Prefix Array p

We build an array p that represents how the prefix XOR of a should look:

  • p[0] = 0
  • For all indices:
  • p[i] = i

  • Except at position r, where p[r] = l - 1
int n, l, r;
cin >> n >> l >> r;

vector<int> p(n + 1);
p[0] = 0;

for (int i = 1; i <= n; i++) {
    if (i == r)
        p[i] = l - 1;
    else
        p[i] = i;
}

Recovering Array a

Using the XOR relation:

vector<int> a(n + 1);

for (int i = 1; i <= n; i++) {
    a[i] = p[i] ^ p[i - 1];
    cout << a[i] << " ";
}

Why This Works

  • XOR is reversible.
  • Knowing consecutive prefix values is enough to reconstruct the original array.
  • This technique is very common in problems involving prefix XOR constraints.

--- Custom Comparator for Pairs: Sort by Sum Tip: Sort pairs in descending order based on sum of elements. Implementation: cpp sort(vec.begin(), vec.end(), [](const pair<int,int> &a, const pair<int,int> &b) { return (a.first + a.second) > (b.first + b.second); });


Check Parity: Same or Different Even/Odd Check if two numbers have different parity: cppif ((a ^ b) & 1) { // a and b have different parity (one even, one odd) }

--- // map struct cmp{ bool operator()(const pair<int,int>&a,const pair<int,int>&b)const{ if(a.first!=b.first) return a.first>b.first; return a.second<b.second;

} }; multiset<pair<int,int>,cmp>st;

  • Vote: I like it
  • -26
  • Vote: I do not like it

»
11 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Maybe you can just use a set to record changes while using map, better than your O(n).

  • »
    »
    11 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    I am talking in case of using map how to get largest value and it's key

    set may be not working in some cases because you will just get value but what about the key if you need it

    i don't know if you mean to say "set of pairs " .

»
8 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by Nourhan_Abo-Heba (previous revision, new revision, compare).

»
8 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by Nourhan_Abo-Heba (previous revision, new revision, compare).

»
8 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by Nourhan_Abo-Heba (previous revision, new revision, compare).

»
8 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by Nourhan_Abo-Heba (previous revision, new revision, compare).

»
8 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by Nourhan_Abo-Heba (previous revision, new revision, compare).

»
8 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by Nourhan_Abo-Heba (previous revision, new revision, compare).

»
8 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by Nourhan_Abo-Heba (previous revision, new revision, compare).

»
8 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by Nourhan_Abo-Heba (previous revision, new revision, compare).