( Problem F ) Pikachu and Team Rocket

4.7

3 votes
Advanced Data Structures, Data Structures, Medium, Segment Trees
Problem

Team Rocket is back with N of their Pokemon to trouble Pikachu. The Team Rocket's Pokemon are numbered from 1 to N and the ith pokemon has health equal to ai 

Pikachu has to battle multiple Pokemon simultaneously. In a single battle, Team Rocket will make Pikachu fight against all the Pokemon in the range [l,r]. Pikachu can defeat a Pokemon if his attack value is atleast the Pokemon's health. So, he wants to know the minimum attack he must have to defeat all Pokemon in the range.

However, this time, Team Rocket is stronger than ever. They have designed a technology to modify their Pokemon's health as either of two ways:

  • Type 1: They choose some k (1kN), and then health changes as ai = ai+1 (ki<N) and aN = ak.
  • Type 2: They choose some k (1kN), and then health changes as ai = ai-1 (1<ik) and a1 = ak.

Note that, all health changes occur simultaneously.

There will be Q events. Each event will be either a battle, or some modification of Pokemon's health by Team Rocket. 

For each battle, help Pikachu by finding the minimum attack value he must have to win against all Pokemon in the range.

Constraints:

  • 1N,Q200000
  • 1ai109
  • In case of battling event, 1lrN
  • In case of modification event, 1kN

Input format:

  • First line contains two space separated integers, N and Q
  • Second line contains initial health values of N Pokemon
  • Nex Q lines contain one event each
  • If the event is a battle, the line contains 1lr where [l..r] is the range of Pokemon.
  • If the event is modification of type p (1p2), the line contains 2pk where k is the index chosen by Team Rocket. 

Output format:

  • Output one line each for a battling event, containing the minimum attack value required to defeat all Pokemon in the range.
Sample Input
5 5
100 200 300 400 500
1 2 5
2 2 4
1 1 2
2 1 2
1 3 5

Sample Output
500
400
500
Time Limit: 2
Memory Limit: 256
Source Limit:
Explanation
  1. In the range [2,5], the maximum health is 500.
  2. The new strengths of pokemon are [400,100,200,300,500].
  3. In the range [1,2], the maximum health is 400.
  4. The new strengths of pokemon are [400,200,300,500,100].
  5. In the range [3,5], the maximum health is 500 .
Contributers:
Editor Image

?