HDU 3308 LCIS
2014-07-12 20:22
211 查看
LCIS
Time Limit: 2000ms
Memory Limit: 32768KB
This problem will be judged on HDU. Original ID: 3308
64-bit integer IO format: %I64d Java class name: Main
[b]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].
[/b]
Input
T in the first line, indicating the case number.Each case starts with two integers n , m(0<n,m<=105).
The next line has n integers(0<=val<=105).
The next m lines each has an operation:
U A B(0<=A,n , 0<=B=105)
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 解题思路:线段树的区间合并。哎。。。写了三四遍才过的!!!!!!!!int lt,rt,lx,rx,mx,wth;依次为左边界,右边界,左上升区间长度,右上升区间长度,lt rt中的最大上升区间长度。
#include <iostream> #include <cstdio> using namespace std; const int maxn = 100010; struct node{ int lt,rt,lx,rx,mx,wth; }tree[maxn<<2]; int d[maxn]; void fix(int v,int mid){ tree[v].lx = tree[v<<1].lx; tree[v].rx = tree[v<<1|1].rx; if(d[mid] < d[mid+1]){ tree[v].lx += tree[v].lx == tree[v<<1].wth?tree[v<<1|1].lx:0; tree[v].rx += tree[v].rx == tree[v<<1|1].wth?tree[v<<1].rx:0; tree[v].mx = max(tree[v<<1].mx,tree[v<<1|1].mx); tree[v].mx = max(tree[v].mx,tree[v<<1].rx+tree[v<<1|1].lx); }else tree[v].mx = max(tree[v<<1].mx,tree[v<<1|1].mx); } void build(int lt,int rt,int v){ tree[v].lt = lt; tree[v].rt = rt; tree[v].wth = rt-lt+1; if(lt == rt){tree[v].lx = tree[v].rx = tree[v].mx = 1;return;} int mid = (lt+rt)>>1; build(lt,mid,v<<1); build(mid+1,rt,v<<1|1); fix(v,mid); } void update(int u,int val,int v){ int mid = (tree[v].lt+tree[v].rt)>>1; if(tree[v].lt == tree[v].rt) {d[tree[v].lt] = val;return;} if(u <= mid) update(u,val,v<<1); else update(u,val,v<<1|1); fix(v,mid); } int query(int lt,int rt,int v){ if(tree[v].lt == lt && tree[v].rt == rt) return tree[v].mx; int mid = (tree[v].lt+tree[v].rt)>>1; if(rt <= mid) return query(lt,rt,v<<1); else if(lt > mid) return query(lt,rt,v<<1|1); else{ int temp = max(query(lt,mid,v<<1),query(mid+1,rt,v<<1|1)); if(d[mid] < d[mid+1]){ temp = max(min(mid-lt+1,tree[v<<1].rx)+min(rt-mid,tree[v<<1|1].lx),temp); //看看合并是否可以使取值更优 } return temp; } } int main(){ int ks,n,m,i,x,y; char op[5]; scanf("%d",&ks); while(ks--){ scanf("%d %d",&n,&m); for(i = 1; i <= n; i++) scanf("%d",d+i); build(0,n,1); for(i = 0; i < m; i++){ scanf("%s%d%d",op,&x,&y); if(op[0] == 'Q'){ printf("%d\n",query(x+1,y+1,1)); }else update(x+1,y,1); } } return 0; }
View Code
相关文章推荐
- 【线段树】HDU 3308 LCIS
- HDU 3308 LCIS(线段树)
- hdu 3308 LCIS
- HDU 3308 LCIS 线段树
- hdu 3308 线段树区间 LCIS
- HDU 3308 LCIS
- HDU 3308 - LCIS(线段树+区间合并)
- HDU 3308 LCIS(最长连续上升子序列)
- HDU 3308 LCIS(线段树)
- hdu 3308 LCIS (线段树单点更新)
- HDU 3308 LCIS
- 【线段树】HDU 3308 LCIS
- hdu 3308 LCIS
- hdu 3308 LCIS 线段树
- hdu 3308 LCIS
- HDU 3308 LCIS
- HDU 3308 LCIS(线段树)
- HDU 3308 LCIS(线段树,连续递增子)
- 【HDU】 3308 LCIS
- (中等) 线段树 HDU 3308 LCIS