using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static string InputPattern = "InputX";
static List<string> GetInputList()
{
var WillReturn = new List<string>();
if (InputPattern == "Input1") {
WillReturn.Add("5 4");
WillReturn.Add("3 3 1 4 5");
WillReturn.Add("2 1 3");
WillReturn.Add("2 5 5");
WillReturn.Add("1 3 3");
WillReturn.Add("2 2 4");
//1
//0
//2
}
else if (InputPattern == "Input2") {
WillReturn.Add("1 1");
WillReturn.Add("1000000000");
WillReturn.Add("2 1 1");
//0
}
else if (InputPattern == "Input3") {
WillReturn.Add("8 9");
WillReturn.Add("2 4 4 3 9 1 1 2");
WillReturn.Add("1 5 4");
WillReturn.Add("2 7 7");
WillReturn.Add("2 2 6");
WillReturn.Add("1 4 4");
WillReturn.Add("2 2 5");
WillReturn.Add("2 2 7");
WillReturn.Add("1 1 1");
WillReturn.Add("1 8 1");
WillReturn.Add("2 1 8");
//0
//1
//0
//2
//4
}
else {
string wkStr;
while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
}
return WillReturn;
}
static void Main()
{
List<string> InputList = GetInputList();
int[] AArr = InputList[1].Split(' ').Select(pX => int.Parse(pX)).ToArray();
int UB = AArr.GetUpperBound(0);
var InsSegmentTree = new SegmentTree(UB);
for (int I = 0; I <= UB; I++) {
InsSegmentTree.Update(I, AArr[I]);
}
int[] wkArr = { };
Action<string> SplitAct = pStr =>
wkArr = pStr.Split(' ').Select(pX => int.Parse(pX)).ToArray();
var sb = new System.Text.StringBuilder();
foreach (string EachStr in InputList.Skip(2)) {
SplitAct(EachStr);
int Type = wkArr[0];
if (Type == 1) {
int Ind = wkArr[1] - 1;
int NewVal = wkArr[2];
InsSegmentTree.Update(Ind, NewVal);
}
if (Type == 2) {
int Sta = wkArr[1] - 1;
int End = wkArr[2] - 1;
SegmentTree.MaxInfo Result = InsSegmentTree.Internal_Query(Sta, End);
if (Result.SecondMax == 0) {
sb.AppendLine("0");
}
else {
sb.Append(Result.SecondCnt);
sb.AppendLine();
}
}
}
Console.Write(sb.ToString());
}
}
#region SegmentTree
// SegmentTreeクラス (区間の最大値情報 and 1点更新)
internal class SegmentTree
{
private MaxInfo[] mTreeNodeArr;
private long UB; // 木のノードの配列のUB
private long mLeafCnt; // 葉ノードの数
private long mExternalArrUB;
internal struct MaxInfo
{
internal long FirstMax;
internal long FirstCnt;
internal long SecondMax;
internal long SecondCnt;
}
// ノードの添字を引数とし、範囲の開始添字と終了添字を持つ配列
private struct RangeInfoDef
{
internal long StaInd;
internal long EndInd;
}
private RangeInfoDef[] mRangeInfo;
// ノードのIndexの列挙を返す
internal IEnumerable<long> GetNodeIndEnum()
{
for (long I = 0; I <= mExternalArrUB; I++) {
yield return I;
}
}
// 木のノードのUBを返す
internal long GetUB()
{
return mExternalArrUB;
}
// コンストラクタ
internal SegmentTree(long pExternalArrUB)
{
mExternalArrUB = pExternalArrUB;
// 簡単のため、葉ノード数を2のべき乗に
long ArrLength = 0;
for (long I = 1; I < long.MaxValue; I *= 2) {
ArrLength += I;
mLeafCnt = I;
if (pExternalArrUB + 1 < mLeafCnt) break;
}
// すべての値を初期化
UB = ArrLength - 1;
mTreeNodeArr = new MaxInfo[UB + 1];
for (long I = 0; I <= UB; I++) {
mTreeNodeArr[I] = new MaxInfo();
}
// ノードの添字を引数とし、範囲の開始添字と終了添字を持つ配列の作成
mRangeInfo = new RangeInfoDef[UB + 1];
for (long I = 0; I <= UB; I++) {
if (I == 0) {
RangeInfoDef WillSet1;
WillSet1.StaInd = 0;
WillSet1.EndInd = mLeafCnt - 1;
mRangeInfo[I] = WillSet1;
continue;
}
long ParentNode = DeriveParentNode(I);
RangeInfoDef ParentRangeInfo = mRangeInfo[ParentNode];
RangeInfoDef WillSet2;
long Mid = (ParentRangeInfo.StaInd + ParentRangeInfo.EndInd) / 2;
if (I % 2 == 1) { // 奇数ノードの場合
WillSet2.StaInd = ParentRangeInfo.StaInd;
WillSet2.EndInd = Mid;
}
else { // 偶数ノードの場合
WillSet2.StaInd = Mid + 1;
WillSet2.EndInd = ParentRangeInfo.EndInd;
}
mRangeInfo[I] = WillSet2;
}
}
// 親ノードの添字を取得
private long DeriveParentNode(long pTarget)
{
return (pTarget - 1) / 2;
}
// 子ノードの添字(小さいほう)を取得
private long DeriveChildNode(long pTarget)
{
return pTarget * 2 + 1;
}
// 葉ノードの配列の添字を木の添字に変換して返す
private long DeriveTreeNode(long pLeafArrInd)
{
long BaseInd = UB - mLeafCnt + 1;
return BaseInd + pLeafArrInd;
}
// 葉ノードの配列のK番目の値をNewValに変更
internal void Update(long pK, long pNewVal)
{
long CurrNode = DeriveTreeNode(pK);
mTreeNodeArr[CurrNode] = new MaxInfo();
mTreeNodeArr[CurrNode].FirstMax = pNewVal;
mTreeNodeArr[CurrNode].FirstCnt = 1;
// 登りながら更新
while (CurrNode > 0) {
CurrNode = DeriveParentNode(CurrNode);
long ChildNode1 = DeriveChildNode(CurrNode);
long ChildNode2 = ChildNode1 + 1;
mTreeNodeArr[CurrNode] =
ExecMerge(mTreeNodeArr[ChildNode1], mTreeNodeArr[ChildNode2]);
}
}
static MaxInfo ExecMerge(MaxInfo pInfo1, MaxInfo pInfo2)
{
MaxInfo WillReturn = pInfo1;
Action<long, long> UpdateAct = (pVal, pCnt) =>
{
if (WillReturn.FirstMax < pVal) {
WillReturn.SecondMax = WillReturn.FirstMax;
WillReturn.SecondCnt = WillReturn.FirstCnt;
WillReturn.FirstMax = pVal;
WillReturn.FirstCnt = pCnt;
return;
}
if (WillReturn.FirstMax == pVal) {
WillReturn.FirstCnt += pCnt;
return;
}
if (WillReturn.SecondMax < pVal) {
WillReturn.SecondMax = pVal;
WillReturn.SecondCnt = pCnt;
return;
}
if (WillReturn.SecondMax == pVal) {
WillReturn.SecondCnt += pCnt;
return;
}
};
UpdateAct(pInfo2.FirstMax, pInfo2.FirstCnt);
UpdateAct(pInfo2.SecondMax, pInfo2.SecondCnt);
return WillReturn;
}
// 開始添字と終了添字とカレントノードを引数として、MaxInfoを返す
internal MaxInfo Internal_Query(long pSearchStaInd, long pSearchEndInd)
{
return Private_Query(pSearchStaInd, pSearchEndInd, 0);
}
private MaxInfo Private_Query(long pSearchStaInd, long pSearchEndInd, long pCurrNode)
{
long CurrNodeStaInd = mRangeInfo[pCurrNode].StaInd;
long CurrNodeEndInd = mRangeInfo[pCurrNode].EndInd;
// OverLapしてなければ、空情報
if (CurrNodeEndInd < pSearchStaInd || pSearchEndInd < CurrNodeStaInd)
return new MaxInfo();
// 完全に含んでいれば、このノードの値
if (pSearchStaInd <= CurrNodeStaInd && CurrNodeEndInd <= pSearchEndInd)
return mTreeNodeArr[pCurrNode];
// そうでなければ、2つの子のMerge結果
long ChildNode1 = DeriveChildNode(pCurrNode);
long ChildNode2 = ChildNode1 + 1;
MaxInfo ChildVal1 = Private_Query(pSearchStaInd, pSearchEndInd, ChildNode1);
MaxInfo ChildVal2 = Private_Query(pSearchStaInd, pSearchEndInd, ChildNode2);
return ExecMerge(ChildVal1, ChildVal2);
}
}
#endregion