Блог пользователя __Handle__

Автор __Handle__, история, 6 лет назад, По-русски

Дан массив и q запросов. Каждый запрос это l, r, x на каждый запрос вам надо ответить количество чисел на отрезке l, r меньших чем x.

  • Проголосовать: нравится
  • +6
  • Проголосовать: не нравится

»
6 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Если запросы оффлайн, можно сделать сканлайн с фенвиком.

Если онлайн, то MergeSortTree.

»
6 лет назад, # |
Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

I think you can create for example fenwick tree or segment tree of order statistic trees and then sum up orders of x in all order statistic trees in [l, r].

EDIT: Or simply a merge sort tree.

»
6 лет назад, # |
Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

Можно решать через ДО. В вершинах будем хранить отсортированные отрезки массива, которые отвечают за соответствующие отрезки в ДО. Для запроса будем обращаться к соответствующим вершинам для нужных отрезков и будем делать бинпоиск в них.

»
6 лет назад, # |
  Проголосовать: нравится +3 Проголосовать: не нравится

You can solve offline the queries. 1) sort the queries in nondecreasing order regarding x; 2) build an array V with 2 parameters: the value of A[i] and i, the position, then sort V regarding first value; 3) after these operations, keep 2 pointers: you will start i from the first value from V and start with j from the first element in Q (sorted); update the values in the binary indexed tree unless V[i].first_parameter will be >= the x from actual j query, then calculate with BIT the answer like this: sum of first r values in BIT — sum of first l-1 values in BIT, then print this value.

»
6 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

For offline:Sort the queries by x,then for each query,add the elements(weren't added before)less than x into the i-th position of a BIT.Then check the sum in interval l and r.Complexity is .

For online:Discrete the array first.Then build a persistent segment tree.The i-th version contains the first i elements in the array.The difference between i and i-1 is the a[i]-th position is increased by 1.For a query,the answer is:the sum in [l,r] in the r-th tree minus the sum in [l,r] in the l-1-th tree.The Complexity is also .

  • »
    »
    6 лет назад, # ^ |
    Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

    Some mistakes here...

    For a query,the answer is:the sum in [l,r] in the r-th tree minus the sum in [l,r] in the l-1-th tree

    it should be:

    For a query,the answer is:the sum in [1,y] in the r-th tree minus the sum in [1,y] in the l-1-th tree(y is the number of elements strictly less than x in the whole array)