O1_TooEasy_VK's blog

By O1_TooEasy_VK, history, 5 months ago, In English

I am trying to learn sqrt decomposition from cp algorithms in their practice problems session there is this problem called destiny i am stuck at this problem from a long time, not able to find out what the error is

here is the Link to the problem: https://mirror.codeforces.com/contest/840/problem/D

This is my solution: include<bits/stdc++.h> using namespace std; struct Query{ int L, R, K, idx; }; int rootN; vector v;

set S; vector freq(300001); bool comp(Query X, Query Y){ if(X.L/rootN == Y.L/rootN){ return X.R < Y.R; } else return X.L/rootN < Y.L/rootN; }

int main(){ int n, q; cin >> n >> q;

v.resize(n+1);
for(int i = 1; i <= n; ++i) cin >> v[i];

vector<Query> Q(q);
rootN = sqrtl(n);
for(int i = 0 ; i < q; ++i){
    cin >> Q[i].L >> Q[i].R >> Q[i].K;
    Q[i].idx = i;
}

sort(Q.begin(), Q.end(), comp);
int currL = 1, currR = 1;
freq[v[1]]++;
S.insert(v[1]);

vector<int> ans(q);
for(auto query : Q){
    int L = query.L, R = query.R, K = query.K;
    int treshold = (R - L + 1)/K;
    while(currL < L){
       freq[v[currL]]--;
       if(freq[v[currL]] == treshold){
         S.erase(v[currL]);
       }
       currL++;
    }
    while(currL > L){
       currL--;
       freq[v[currL]]++;
       if(freq[v[currL]] == 1 + treshold){
         S.insert(v[currL]);
       }
    }
    while(currR < R){
       currR++;
       freq[v[currR]]++;
       if(freq[v[currR]] == 1 + treshold){
         S.insert(v[currR]);
       }
    }
    while(currR > R){
       freq[v[currR]]--;
       if(freq[v[currR]] == treshold){
         S.erase(v[currR]);
       }
       currR--;
    }
    ans[query.idx] = (S.size()) ? *(S.begin()) : -1;
}
for(auto x : ans) cout << x << endl;

} Some one please help me solve the errors, and also recommend some problems to learn sqrt decomp properly

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

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

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

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

Imagine what happens if K just decides to increase while L and R stays the same. Clearly, the set S will remain unchanged. So the above code's idea is wrong. For the second code, 351935076, instead of brute forcing (so called "Fixed deterministic sampling"), just try to pick $$$\log_{1-1/k}(p)$$$ ($$$p$$$ is the failure rate, $$$10^{-9}$$$ is fine, also you should prepare the number of iterations as constants) numbers from the segment $$$[L..R]$$$ as indices.

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

    Thanks a lot for helping me out! I now understand why my answer was wrong. However, I got lost at this part and didn’t quite understand it:

    "Pick log(1 — 1/k(p)) (where p is the failure rate; 10⁻⁹ is fine, and the number of iterations should be prepared as constants) numbers from the segment [L..R] as indices."

    Could you please explain this in more detail? I’m especially confused about how to interpret the formula and how to actually pick numbers from the segment.

    • »
      »
      »
      5 months ago, hide # ^ |
      Rev. 2  
      Vote: I like it 0 Vote: I do not like it

      If such a element that occurs at least $$$(r-l+1)/k$$$ times, then you have at least $$$1/k$$$ chance of picking it by picking a number from $$$a[L..R]$$$. So there's at most $$$1-1/k$$$ chance that you wouldn't be able to pick it. So after $$$x$$$ iterations, there's at most a chance of $$$(1-1/k)^x$$$ that you wouldn't be able to pick the answer, hence the formula. Choosing a random index can be done simply as rng() % (r-l+1) + l.

      k Number of iterations for $$$p=10^{-10}$$$
      2 34
      3 57
      4 81
      5 104