Having trouble in solving segment tree problem.

Revision en3, by NotHandsomeBoizz, 2024-01-12 16:37:26

Hi everyone!

I have just participated in an IT contest in my city recently. And there was a segment tree problem that for now, I cant think of the way to use the segment tree properly. Here's the problem:

Given an array a contains only two numbers: 1 and -1, all the elements index from 1 to n.

Input:

The first line contains two integer n and q (1 <= n,q <= 10^5), corresponding to the size of the array and number of operation. The next line contains array of integers a1, a2, a3, ..., an of length n (a[i] ∈ {1;-1}). The next q following lines contain the description of the operations. The description of each operation is as follows:

  • 1 i v: set the element with index i to v (1 <= i <= n, v ∈ {1;-1})
  • 2 l r k (1 <= l <= r <= n, |k| <= n): this operation needs to find two number x, y (l >= x >= y >= r) such that the sum of all element from x to y equals to k.

Output

For each operation of the second type print the corresponding x, y. If no answer exists, print -1.

  • Example:

I first approached this problem by doing some classic segment tree to find sum in a subsegment of the array, but I was soon realized that if I do that way, I can't find the proper way to find the number x and y. So I'm wonder what is the another way to approach this segment tree question more optimal.

And finally, thank you for reading my blog!

Tags segment tree, math, problem solving, binary search

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en7 English NotHandsomeBoizz 2025-05-29 17:50:54 0 (published)
en6 English NotHandsomeBoizz 2024-01-14 04:48:05 6 Tiny change: 'r x, y (l >= x >= y >= r) such ' -> 'r x, y (l <= x <= y <= r) such ' (saved to drafts)
en5 English NotHandsomeBoizz 2024-01-12 16:40:13 0 (published)
en4 English NotHandsomeBoizz 2024-01-12 16:39:20 20 Tiny change: 'ntegers a1, a2, a3, ' -> 'ntegers a1\n-, a2, a3, '
en3 English NotHandsomeBoizz 2024-01-12 16:37:26 391
en2 English NotHandsomeBoizz 2024-01-12 16:23:39 796 Tiny change: 'nt -1.\n\n\n`hello`\n' -> 'nt -1.\n\n* Example:\n'
en1 English NotHandsomeBoizz 2024-01-11 19:13:40 349 Initial revision (saved to drafts)