HDU 3308 LCIS(线段树)

题目链接

模板题吧,忘了好多,终于A了...

#include <cstring>
#include <cstdio>
#include <string>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
#define maxn 1000000
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
struct node
{
int lis,ris,smax,lnum,rnum;
} p[*maxn];
void pushup(int rt,int l,int r)
{
int m;
m = (l + r) >>;
p[rt].smax = max(p[rt<<].smax,p[rt<<|].smax);
if(p[rt<<].rnum < p[rt<<|].lnum)
{
p[rt].smax = max(p[rt].smax,p[rt<<].ris+p[rt<<|].lis);
if(p[rt<<].lis == p[rt<<].ris&&p[rt<<].lis == m-l+)//少写了个判断...
p[rt].lis = p[rt<<].lis + p[rt<<|].lis;
else
p[rt].lis = p[rt<<].lis;
if(p[rt<<|].lis == p[rt<<|].ris&&p[rt<<|].lis == r-m)
p[rt].ris = p[rt<<].ris + p[rt<<|].ris;
else
p[rt].ris = p[rt<<|].ris;
}
else
{
p[rt].lis = p[rt<<].lis;
p[rt].ris = p[rt<<|].ris;
}
p[rt].lnum = p[rt<<].lnum;
p[rt].rnum = p[rt<<|].rnum;
}
void build(int l,int r,int rt)
{
int m;
if(l == r)
{
scanf("%d",&p[rt].lnum);
p[rt].rnum = p[rt].lnum;
p[rt].smax = ;
p[rt].ris = ;
p[rt].lis = ;
return ;
}
m = (l + r) >> ;
build(lson);
build(rson);
pushup(rt,l,r);
}
void update(int x,int sc,int l,int r,int rt)
{
int m;
if(l == x&&r == x)
{
p[rt].lnum = sc;
p[rt].rnum = sc;
p[rt].smax = ;
p[rt].lis = ;
p[rt].ris = ;
return ;
}
m = (l + r)>>;
if(x <= m)
update(x,sc,lson);
else
update(x,sc,rson);
pushup(rt,l,r);
}
int query(int L,int R,int l,int r,int rt)
{
int m,ans,s1,s2;
if(l >= L&&r <= R)
{
return p[rt].smax;
}
m = (l + r)>>;
if(L > m)
return query(L,R,rson);
if(R <= m)
return query(L,R,lson);
ans = max(query(L,R,lson),query(L,R,rson));
if(p[rt<<].rnum < p[rt<<|].lnum)
{
s1 = min(m-L+,p[rt<<].ris);
s2 = min(R-m,p[rt<<|].lis);
ans = max(s1+s2,ans);
}
return ans;
}
int main()
{
int t,i,n,m,a,b;
scanf("%d",&t);
char str[];
while(t--)
{
scanf("%d%d",&n,&m);
build(,n-,);
for(i = ; i < m; i ++)
{
scanf("%s%d%d",str,&a,&b);
if(str[] == 'Q')
{
printf("%d\n",query(a,b,,n-,));
}
else
{
update(a,b,,n-,);
}
}
}
return ;
}
上一篇:笔记本双系统XP与Ubuntu,重装XP后如何恢复grup引导,另附操作系统启动过程


下一篇:JavaScript 记录页面停留时间-通过测试