#include <iostream>
#include <stdio.h>
#include <algorithm>
#define lson rt<<1,L,mid
#define rson rt<<1|1,mid+1,R
/*
AC
水题
题意:给出n个数,两种操作
U i x 将第i个数改成x
Q x y 查询[x,y]中两个数的和的最大值,这两个数不能为同一个
即只要求该区间第一大和第二大的数,他们的和即为答案
*/
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=;
int n,m; struct Node{
long long m1,m2; //存储该区间第一大的数和第二大的数
}tree[maxn<<]; void pushUp(Node &rt,Node &ls,Node &rs){
if(ls.m1>rs.m1){
rt.m1=ls.m1;
if(ls.m2>=rs.m1)
rt.m2=ls.m2;
else
rt.m2=rs.m1;
}
else if(ls.m1==rs.m1){
rt.m1=rt.m2=ls.m1;
}
else{
rt.m1=rs.m1;
if(rs.m2>=ls.m1)
rt.m2=rs.m2;
else
rt.m2=ls.m1;
}
}
void build(int rt,int L,int R){
if(L==R){
scanf("%lld",&tree[rt].m1);
tree[rt].m2=-INF;
return;
}
int mid=(L+R)>>;
build(lson);
build(rson);
pushUp(tree[rt],tree[rt<<],tree[rt<<|]);
}
void update(int rt,int L,int R,int x,long long c){
if(L==R){
tree[rt].m1=c;
tree[rt].m2=-INF;
return;
}
int mid=(L+R)>>;
if(x<=mid)
update(lson,x,c);
else
update(rson,x,c);
pushUp(tree[rt],tree[rt<<],tree[rt<<|]);
} Node query(int rt,int L,int R,int l,int r){
if(l<=L&&R<=r){
return tree[rt];
}
int mid=(L+R)>>;
Node tmp,r1,r2;
if(r<=mid)
tmp=query(lson,l,r);
else if(l>mid)
tmp=query(rson,l,r);
else{
r1=query(lson,l,mid);
r2=query(rson,mid+,r);
pushUp(tmp,r1,r2);
}
return tmp;
}
int main()
{
char str[];
int x,y;
long long v;
scanf("%d",&n);
build(,,n);
scanf("%d",&m);
for(int i=;i<=m;i++){
scanf("%s",str);
if(str[]=='U'){
scanf("%d%lld",&x,&v);
update(,,n,x,v);
}
else{
scanf("%d%d",&x,&y);
Node ans=query(,,n,x,y);
//cout<<ans.m1<<" "<<ans.m2<<endl;
printf("%lld\n",ans.m1+ans.m2);
}
}
return ;
}

SPOJ 3693 Maximum Sum(水题,记录区间第一大和第二大数)的更多相关文章

  1. SPOJ CNTPRIME 13015 Counting Primes (水题,区间更新,求区间的素数个数)

    题目连接:http://www.spoj.com/problems/CNTPRIME/ #include <iostream> #include <stdio.h> #incl ...

  2. SPOJ 7259 Light Switching (水题,区间01取反)

    #include <iostream> #include <stdio.h> #include <algorithm> #define lson rt<< ...

  3. codeforces 577B B. Modulo Sum(水题)

    题目链接: B. Modulo Sum time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  4. codeforces 702A A. Maximum Increase(水题)

    题目链接: A. Maximum Increase time limit per test 1 second memory limit per test 256 megabytes input sta ...

  5. openjudge dp水题记录

    当发现自己竟然不会打dp的时候内心是崩溃的,然后按照一年前的刷题记录刷openjudge,然后发现自己准确率比一年前(刚学信竞两个月时)的准确率低得多,已经没救. 列一下最近打的几道sb题 2985: ...

  6. Distinct Substrings SPOJ - DISUBSTR(后缀数组水题)

    求不重复的子串个数 用所有的减去height就好了 推出来的... #include <iostream> #include <cstdio> #include <sst ...

  7. SPOJ - AMR11H Array Diversity (水题排列组合或容斥)

    题意:给定一个序列,让你求两种数,一个是求一个子序列,包含最大值和最小值,再就是求一个子集包含最大值和最小值. 析:求子序列,从前往记录一下最大值和最小值的位置,然后从前往后扫一遍,每个位置求一下数目 ...

  8. Codeforces Round #599 (Div. 2) A. Maximum Square 水题

    A. Maximum Square Ujan decided to make a new wooden roof for the house. He has

  9. NOIP前的水题记录

    CF147B Smile House 二分+矩阵快速幂,注意一下储存矩阵相乘结果的矩阵,初始化时,a[i][i]=-inf(而其他都可以a[i][i]=0,为了保证答案的可二分性). CF715B C ...

随机推荐

  1. Java中浮点数能连续精确表示整数的范围

    转自http://blog.csdn.net/seizef/article/details/5571783#ref_1,有删改. 先简单介绍一下浮点数在计算机中的组成,在Java中采用的浮点数表示法是 ...

  2. select的onChange事件问题解决

    一.onChange事件只有在值改变时才可触发,所以必须在每一次选择时(尤其第一次)保证选择的值是改变的! 所以<select name="inv_payee" id=&qu ...

  3. 关于LINQ一个简单例子

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cons ...

  4. dmp文件导入

    dmp文件导入 1,打开“开始”-->输入cmd—> sqlplus log: 2,输入 conn / as sysdba管理员账户登录: 3,需要创建表空间,如果我们知道需要导入的数据库 ...

  5. poj 3237 Tree 树链剖分+线段树

    Description You are given a tree with N nodes. The tree’s nodes are numbered 1 through N and its edg ...

  6. CHARINDEX,PATINDEX,STUFF函数

    -- CHARINDEX函数 -- 返回字符或者字符串在另一个字符串中的起始位置. -- 语法:CHARINDEX(expression1 , expression2 [,start_location ...

  7. 通过FTP将一个数据文件从A服务器下载到B服务器的整个过程

    现在的环境如下: 服务器A :192.168.1.104 服务器B:192.168.1.138 需要将A服务器上的某个数据文件下载到B服务器上,传输方式为:FTP 那么,要怎么去实现呢? 首先,需要添 ...

  8. Lua基础之字符串(string)

    1,计算字符串长度 2,返回字符串s的n个拷贝 3,返回字符串全部字母大写 4,返回字符串全部字母小写 5,返回一个类似printf的格式化字符串 6,根据下标截取字符串 7,在字符串中查找 8,在字 ...

  9. 微软职位内部推荐-Principal Dev Manager

    微软近期Open的职位: Title: Principal Dev Manager Location: Beijing The R&D of Shared Data Platform at S ...

  10. 【经验】Angularjs 中使用 layDate 日期控件

    layDate 控件地址:http://laydate.layui.com/ 前情:原来系统中使用的日期控件是UI bootstrap(地址:https://angular-ui.github.io/ ...