A problem that asks for the maximum and the second largest value.
At the time of the update, the maximum number of the two sub-range is counted, and then the order is loaded.
#include <iostream>
#include <cstdio>
#include <algorithm>
#define MAXN 800005
using namespace Std;
struct tree
{
int left,right;
int value1,value2;
}NODE[MAXN];
struct ans
{
int A, B;
};
int n,q,father[maxn],type,x,y;
void build (int i,int left,int right)
{
Node[i].left=left;
Node[i].right=right;
node[i].value1=0;
node[i].value2=0;
if (left==right)
{
Father[left]=i;
Return
}
Else
{
int mid= (left+right) >>1;
i=i<<1;
Build (I,left,mid);
Build (I+1,mid+1,right);
}
}
void modify (int x)
{
int fath=x>>1,a[5];
int ls=fath<<1,rs= (fath<<1) +1;
a[1]=node[ls].value1;
A[2]=node[ls].value2;
a[3]=node[rs].value1;
A[4]=node[rs].value2;
Sort (a+1,a+5);
NODE[FATH].VALUE1=A[4];
NODE[FATH].VALUE2=A[3];
if (fath==1) return;
else modify (fath);
}
Ans Ask (int i,int l,int R)
{
int left=node[i].left,right=node[i].right;
int value1=node[i].value1,value2=node[i].value2;
if ((left==l) && (right==r))
{
Ans tk;
tk.a=node[i].value1;
Tk.b=node[i].value2;
return TK;
}
Else
{
int mid= (left+right) >>1;
if (R<=mid)
Return ask (I<<1,L,R);
else if (l>=mid+1)
Return Ask ((i<<1) +1,l,r);
Else
{
Ans tk,yk,rep;
Tk=ask (I<<1,l,mid);
Yk=ask ((i<<1) +1,mid+1,r);
int a[5];
a[1]=tk.a;a[2]=tk.b;
a[3]=yk.a;a[4]=yk.b;
Sort (a+1,a+5);
REP.A=A[4];REP.B=A[3];
Return rep;
}
}
}
int main ()
{
scanf ("%d%d", &n,&q);
Build (1,1,n);
for (int i=1;i<=q;i++)
{
scanf ("%d%d%d", &type,&x,&y);
if (type==1)
{
Node[father[x]].value1+=y;
Modify (Father[x]);
}
Else
{
Ans K=ask (1,x,y);
printf ("%d\n", k.a-k.b);
}
}
return 0;
}
Codevs 2018 Anti-virus software