LCIS
Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6998 Accepted Submission(s): 2970
Problem Description
Given n integers.
You have two operations:
U A B: replace the Ath number by B. (index counting from 0)
Q A B: output the length of the longest consecutive increasing subsequence (LCIS) in [a, b].
You have two operations:
U A B: replace the Ath number by B. (index counting from 0)
Q A B: output the length of the longest consecutive increasing subsequence (LCIS) in [a, b].
Input
T in the first line, indicating the case number.
Each case starts with two integers n , m(0<n,m<=10 5).
The next line has n integers(0<=val<=10 5).
The next m lines each has an operation:
U A B(0<=A,n , 0<=B=10 5)
OR
Q A B(0<=A<=B< n).
Each case starts with two integers n , m(0<n,m<=10 5).
The next line has n integers(0<=val<=10 5).
The next m lines each has an operation:
U A B(0<=A,n , 0<=B=10 5)
OR
Q A B(0<=A<=B< n).
Output
For each Q, output the answer.
Sample Input
1 10 10 7 7 3 3 5 9 9 8 1 8 Q 6 6 U 3 4 Q 0 1 Q 0 5 Q 4 7 Q 3 5 Q 0 2 Q 4 6 U 6 10 Q 0 9
Sample Output
1 1 4 2 3 1 2 5
Author
shǎ崽
Source
题意:给出n个数,有两个操作,Q是询问区间内最大的连续上升子序列,U是更新节点
解题思路:线段树,每个节点多记录一下左向右能延伸几个,右向左能延伸几个
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
using namespace std;
#define LL long long
int n,m,t,l,r;
char ch[10];
int a[100005];
struct node
{
int l,lr,r,rl,m;
}x[400005],ans;
void Merge(node &x, node lx, node rx)
{
x.l=lx.l;x.r=rx.r;
if(lx.lr==lx.r&&a[lx.r]<a[rx.l]) x.lr=rx.lr;
else x.lr=lx.lr;
if(rx.rl==rx.l&&a[rx.l]>a[lx.r]) x.rl=lx.rl;
else x.rl=rx.rl;
x.m=max(lx.m,rx.m);
if(a[lx.r]<a[rx.l]) x.m=max(x.m,rx.lr-lx.rl+1);
}
void build(int k,int l,int r)
{
if(l==r)
{
x[k].l=l;x[k].r=r;
x[k].lr=x[k].r;
x[k].rl=x[k].l;
x[k].m=1;
}
else
{
int mid=(l+r)>>1;
build(k<<1,l,mid);
build(k<<1|1,mid+1,r);
Merge(x[k],x[k<<1],x[k<<1|1]);
}
}
void Insert(int k,int l,int r,int p,int val)
{
if(l==r) a[l]=val;
else
{
int mid=(l+r)>>1;
if(p<=mid) Insert(k<<1,l,mid,p,val);
else Insert(k<<1|1,mid+1,r,p,val);
Merge(x[k],x[k<<1],x[k<<1|1]);
}
}
void Find(int k,int l,int r,int ll,int rr)
{
if(ll<=l&&r<=rr)
{
if(ans.l==-1) ans=x[k];
else
{
node pp=ans;
Merge(ans,pp,x[k]);
}
}
else
{
int mid=(l+r)>>1;
if(ll<=mid) Find(k<<1,l,mid,ll,rr);
if(rr>mid) Find(k<<1|1,mid+1,r,ll,rr);
}
}
int solve(int l,int r)
{
ans.l=-1;
Find(1,1,n,l,r);
return ans.m;
}
int main()
{
scanf("%d",&t);
while(t--)
{
scanf("%d %d",&n,&m);
for(int i=1; i<=n; i++)
scanf("%d",&a[i]);
build(1,1,n);
while(m--)
{
scanf("%s %d %d",ch,&l,&r);
if(ch[0]=='Q') printf("%d\n",solve(l+1,r+1));
else Insert(1,1,n,l+1,r);
}
}
return 0;
}