KQUERY

Can anyone help me to solve this problem? I have trouble to put it in BIT way on thinking.

Thanks

# | User | Rating |
---|---|---|

1 | Petr | 3325 |

2 | OO0OOO00O0OOO0O0…O | 3289 |

3 | Um_nik | 3278 |

4 | Syloviaely | 3274 |

5 | tourist | 3206 |

6 | Radewoosh | 3197 |

7 | V--o_o--V | 3167 |

8 | mnbvmar | 3096 |

9 | dotorya | 3086 |

10 | FizzyDavid | 3023 |

# | User | Contrib. |
---|---|---|

1 | tourist | 183 |

2 | rng_58 | 168 |

3 | csacademy | 161 |

4 | Petr | 160 |

5 | lewin | 151 |

6 | Swistakk | 150 |

7 | matthew99 | 142 |

8 | BledDest | 141 |

9 | Errichto | 140 |

10 | adamant | 139 |

Codeforces (c) Copyright 2010-2018 Mike Mirzayanov

The only programming contests Web 2.0 platform

Server time: Apr/19/2018 22:10:18 (d2).

Desktop version, switch to mobile version.

User lists

Name |
---|

it can be solved by sorting the query list by k in descending order (same go for array A)

then with each query we add those element from A which is greater than k to it original position in the BIT (you need to keep track of those element which is already added so the total number of update is n (the reason we sort A))

then it retrun to a basic problem of adding element and counting the number of element in range :p

Can you give me a little bit more information?

well, let sort A in descending order and B[i] be the position of A[i] in the original array , we will solve from the query with greatest k to the query with smallest k

now we have query (i, j, k), let x be the number such as all element 1..x from A is added to the tree (BIT), set by 0 at first

then we add all the element A[j] such as j > x and A[j] > k to the tree (at B[i]) element j <= x is already added so ignore them

assign x as the greatest j we found above

all the element added into the tree so far is greater than k so the only thing left is to count the number of element added in range (i, j)

is this enough for an explaination I wonder ? :p

nicely explained :p

superb explaination

Actually, we can solve this problem with a more "classic" way : let's build a segment tree, with a sorted subarray in every vertex. Then we can divide [l, r] into subsegments, and do a Binary Search on subsegments. O(logN ^ 2) for the query.

TLE http://pastebin.com/y5kzd5mE

Check this code http://mohamedmosamin.wordpress.com/2013/09/03/spoj-3266-k-query-kquery/. Use it the same idea that shows lazycode97

Create a large area in which you are going to store both the queries (represented by their

`k`

values) and the numbers of the original array. Now sort this array in non-increasing order (remember to store the original positions in a separate array).Suppose that you have a segment tree (initially all zeros).

Now scan the array from left to right. If the current number was a number in the original array, update it's value to 1 (in the segment tree). If the current number (call it

`x`

) is a query then we know that all numbers greater than`x`

have been inserted in the segment tree because we sorted the array (so all intervals that contain it have been updated); execute a range sum query to get the answer.Queries were scrambled when we sorted the array, so you need to print them in the order they were given.

The time limit for this problem is tight (1500 ms). Use a Fenwick tree and use static arrays instead of vectors.