0

I want to know the fast algorithm to solve this simple problem:

  • You are given a sequence a[0], a[1],..., a[N - 1].
  • You are given Q queries. All queries are query 1 or 2. Process all queries.
    1. You are given integers l, r, x. Update a[i] = max(a[i], x) for l <= i <= r.
    2. You are given integers l, r. Find a[l] + a[l + 1] + ... + a[r].

I only have a naive O(NQ) algorithm, so please find more faster algorithm because I want to solve for N <= 200000, Q <= 200000.

square1001
  • 1,402
  • 11
  • 26

1 Answers1

0

This problem can be easily solved by interval tree(or segment tree) with lazy tag.If you are not familiar with interval tree,you may refer to this article.The time complexity is O(Q*logN).

norshtein
  • 87
  • 1
  • 10