单点更新 区间最值
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
#define lson l,m,rt<<1 //函数用左儿子
#define rson m+1,r,rt<<1|1 //函数用右儿子
#define havemid int m=(l+r)>>1 //取节点中点
#define left (rt<<1) //左儿子
#define right (rt<<1|1) //右儿子
const int maxn=100005; //最大节点个数
int Max[maxn<<2]; //节点个数,开maxn的4倍
int id[maxn<<2];
void pushup(int rt){
if(Max[left]>Max[right]){Max[rt]=Max[left];id[rt]=id[left];}
else {Max[rt]=Max[right];id[rt]=id[right];}
}
void build(int l,int r,int rt){ //建树(1,n,1)
if(l==r){ //跟新到叶子节点 输入值 也为当前节点的和值
scanf("%d",&Max[rt]); //返回
id[rt]=l;
return ;
}
havemid;
build(lson); //递归建树左
build(rson); //右
pushup(rt); //更新到父结点信息 来自左儿子和右儿子
}
void update(int p,int c,int l,int r,int rt){//跟新函数(位置,加几,1,n,1)
if(l==r){
Max[rt]=c;return ;
}
havemid;
if(p<=m)update(p,c,lson); //位置在左儿子里
else update(p,c,rson); //右
pushup(rt); //加完数要向上更新父节点信息
}
int query(int L,int R,int l,int r,int rt){//查询函数(查询区间的左端点,右,1,n,1)
if(L<=l&&r<=R)return rt; //查询区间包括节点区间 return 节点的和值
havemid;
int ret=0;
if(R<=m)return query(L,R,lson); //左儿子在查询区间
if(L>m)return query(L,R,rson); //右
else
{
int a,b;
a=query(l,m,L,m,rt<<1);
b=query(m+1,r,m+1,R,rt<<1|1);
if(Max[a]<Max[b])return b;
else return a;
}
}
int main()
{
int m,n,p,q;
char s[20];
while(scanf("%d",&n)!=EOF)
{
build(1,n,1);
scanf("%d",&m);
while(m--)
{
scanf("%s",s);
if(s[0]=='C'){scanf("%d%d",&p,&q);update(p,q,1,n,1);}
else {int res=query(1,n,1,n,1);printf("%d %d\n",id[res],Max[res]);}
}
printf("\n");
}
return 0;
}
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
#define lson l,m,rt<<1 //函数用左儿子
#define rson m+1,r,rt<<1|1 //函数用右儿子
#define havemid int m=(l+r)>>1 //取节点中点
#define left (rt<<1) //左儿子
#define right (rt<<1|1) //右儿子
const int maxn=100005; //最大节点个数
int Max[maxn<<2]; //节点个数,开maxn的4倍
int id[maxn<<2];
void pushup(int rt){
if(Max[left]>Max[right]){Max[rt]=Max[left];id[rt]=id[left];}
else {Max[rt]=Max[right];id[rt]=id[right];}
}
void build(int l,int r,int rt){ //建树(1,n,1)
if(l==r){ //跟新到叶子节点 输入值 也为当前节点的和值
scanf("%d",&Max[rt]); //返回
id[rt]=l;
return ;
}
havemid;
build(lson); //递归建树左
build(rson); //右
pushup(rt); //更新到父结点信息 来自左儿子和右儿子
}
void update(int p,int c,int l,int r,int rt){//跟新函数(位置,加几,1,n,1)
if(l==r){
Max[rt]=c;return ;
}
havemid;
if(p<=m)update(p,c,lson); //位置在左儿子里
else update(p,c,rson); //右
pushup(rt); //加完数要向上更新父节点信息
}
int main()
{
int m,n,p,q;
char s[20];
while(scanf("%d",&n)!=EOF)
{
build(1,n,1);
scanf("%d",&m);
while(m--)
{
scanf("%s",s);
if(s[0]=='C'){scanf("%d%d",&p,&q);update(p,q,1,n,1);}
else {printf("%d %d\n",id[1],Max[1]);}
}
printf("\n");
}
return 0;
}