hdu 3308 LCIS
2011-10-06 19:12
218 查看
依旧线段树。
这题以前见过,没仔细看题,以为求的是最长XX子序列。。。
不过这个题简单了,是求连续的最长子序列,这个很显然了,结点里建立三个域,从左边起最长的连续序列,从右边起,以及这个区间中最长的。。。(这种做法是某些题的通法了,看来。。)
更新的时候,注意左右区间衔接部分的长度。。
这个样来的话,更新会很随意。本来想查询肯定就是取max啦,后来想了,不对,囧。。
因为查询的区间可能分开,如果分开的话就需要判断两个区间是否可以合起来,也就是左区间的rval 和右区间的lval是否可以合并。。
所以查询写得狠狠纠结。。。回归原始线段树写法,就是if。。else。。。if。。。因为如果查询区间完全在左区间的话,就不用考虑合并问题了。。。
有个小细节想错了,WA了一次,改后顺利AC。。。
以前遇到过题,就是可以直接在一个数组(存的叶节点的值)改变,然后从叶节点更新。这次想,能不能直接从叶节点开始更新呢,这次想了想,可以的,用一个数组记录叶子在树的结点编号,然后以这个编号循环上去,不就改变了么。。。过了。但是速度没快多少。。
循环更新。。
传统单点更新。。
这题以前见过,没仔细看题,以为求的是最长XX子序列。。。
不过这个题简单了,是求连续的最长子序列,这个很显然了,结点里建立三个域,从左边起最长的连续序列,从右边起,以及这个区间中最长的。。。(这种做法是某些题的通法了,看来。。)
更新的时候,注意左右区间衔接部分的长度。。
这个样来的话,更新会很随意。本来想查询肯定就是取max啦,后来想了,不对,囧。。
因为查询的区间可能分开,如果分开的话就需要判断两个区间是否可以合起来,也就是左区间的rval 和右区间的lval是否可以合并。。
所以查询写得狠狠纠结。。。回归原始线段树写法,就是if。。else。。。if。。。因为如果查询区间完全在左区间的话,就不用考虑合并问题了。。。
有个小细节想错了,WA了一次,改后顺利AC。。。
以前遇到过题,就是可以直接在一个数组(存的叶节点的值)改变,然后从叶节点更新。这次想,能不能直接从叶节点开始更新呢,这次想了想,可以的,用一个数组记录叶子在树的结点编号,然后以这个编号循环上去,不就改变了么。。。过了。但是速度没快多少。。
循环更新。。
void Updata(int t) { while( t != 1 ) { Updata_val(t/2); t /= 2; } }
传统单点更新。。
#include <set> #include <map> #include <queue> #include <stack> #include <math.h> #include <stdio.h> #include <stdlib.h> #include <iostream> #include <limits.h> #include <string.h> #include <string> #include <algorithm> #define MID(x,y) ( ( x + y ) >> 1 ) #define L(x) ( x << 1 ) #define R(x) ( x << 1 | 1 ) #define FOR(i,s,t) for(int i=(s); i<(t); i++) #define BUG puts("here!!!") #define STOP system("pause") #define file_r(x) freopen(x, "r", stdin) #define file_w(x) freopen(x, "w", stdout) using namespace std; const int MAX = 100010; struct Tnode{ // 一维线段树 int l,r,lval, rval, max; int len() { return r - l;} int mid() { return MID(l,r);} bool in(int ll,int rr) { return l >= ll && r <= rr; } void lr(int ll,int rr){ l = ll; r = rr;} }; Tnode node[MAX<<2]; int a[MAX]; void Updata_val(int t) { node[t].lval = node[L(t)].lval; if( node[L(t)].lval == node[L(t)].len() && a[node[L(t)].r] > a[node[L(t)].r - 1] ) node[t].lval += node[R(t)].lval; node[t].rval = node[R(t)].rval; if( node[R(t)].rval == node[R(t)].len() && a[node[R(t)].l] > a[node[L(t)].r - 1] ) node[t].rval += node[L(t)].rval; int mval = 0; if( a[node[R(t)].l] > a[node[L(t)].r - 1] ) mval = node[L(t)].rval + node[R(t)].lval; node[t].max = max(mval, max(node[L(t)].max, node[R(t)].max)); node[t].max = max(node[t].max, max(node[t].lval, node[t].rval)); } void Build(int t,int l,int r) { node[t].lr(l,r); if( node[t].len() == 1 ) { node[t].lval = node[t].rval = node[t].max = 1; return ; } int mid = MID(l,r); Build(L(t),l,mid); Build(R(t),mid,r); Updata_val(t); } void Updata(int t,int l,int r,int val) { if( node[t].in(l,r) ) return ; if( node[t].len() == 1 ) return ; int mid = node[t].mid(); if( l < mid ) Updata(L(t),l,r,val); if( r > mid ) Updata(R(t),l,r,val); Updata_val(t); } int Query(int t,int l,int r) { if( node[t].in(l,r) ) return node[t].max; if( node[t].len() == 1 ) return 0; int mid = node[t].mid(); int ans = 0; if( r <= mid ) ans = max(ans, Query(L(t),l,r)); else if( l >= mid ) ans = max(ans, Query(R(t),l,r)); else { ans = max(ans, Query(L(t),l,r)); ans = max(ans, Query(R(t),l,r)); int ll, rr; if( node[L(t)].r - node[L(t)].rval > l ) ll = node[L(t)].rval; else ll = node[L(t)].r - l; if( node[R(t)].l + node[R(t)].lval > r ) rr = r - node[R(t)].l; else rr = node[R(t)].lval; ans = max(ans, ll); ans = max(ans, rr); if( a[node[L(t)].r] > a[node[L(t)].r - 1] ) { int mval = rr + ll; ans = max(ans, mval); } } return ans; } int main() { int ncases, n, m, x, y; char op[2]; scanf("%d", &ncases); while( ncases-- ) { scanf("%d%d", &n, &m); FOR(i, 0, n) scanf("%d", &a[i]); Build(1, 0, n); while( m-- ) { scanf("%s%d%d", op, &x, &y); if( op[0] == 'U' ) { a[x] = y; Updata(1, x, x+1, y); } else { int ans = Query(1, x, y+1); printf("%d\n", ans); } } } return 0; }
相关文章推荐
- 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