hdu 5316 Magician 2015 Multi-University Training Contest 3
2015-08-21 23:33
453 查看
看了半天题目才意识到子序列sequence和子串substring不同,前者不连续,后者须连续。
线段树的每一个节点可以维护四种子序列的和:奇数下标开头,奇数下标结尾的子序列之和;奇数下标开头,偶数下标结尾的子序列之和;偶数下标开头,奇数下标结尾的子序列之和;偶数下标开头,偶数下标结尾的子序列之和。
这里面的更新操作只有点修改,所以不涉及父节点的值改变影响子节点的问题,所以不需要PushDown。
for PushUp,因为子序列不连续且可以有负数,所以当前节点的最优值可以是Merge(lson,rson),lson,rson,-INF。
区间查询需要返回一个对象,如果仅仅返回当前节点的最大值,可能在之后因为奇偶下标的限制与相邻节点的子序列无法合并。
区间查询分三种case,lson和rson对应的子序列再合并,lson对应的子序列,rson对应的子序列。之前一直TLE就是因为没有把这三个case分开。如果是普通的求和直接相加就可以,但是涉及到子序列合并,需要分开考虑。模板题结果还卡了这么久><
线段树的每一个节点可以维护四种子序列的和:奇数下标开头,奇数下标结尾的子序列之和;奇数下标开头,偶数下标结尾的子序列之和;偶数下标开头,奇数下标结尾的子序列之和;偶数下标开头,偶数下标结尾的子序列之和。
这里面的更新操作只有点修改,所以不涉及父节点的值改变影响子节点的问题,所以不需要PushDown。
for PushUp,因为子序列不连续且可以有负数,所以当前节点的最优值可以是Merge(lson,rson),lson,rson,-INF。
区间查询需要返回一个对象,如果仅仅返回当前节点的最大值,可能在之后因为奇偶下标的限制与相邻节点的子序列无法合并。
区间查询分三种case,lson和rson对应的子序列再合并,lson对应的子序列,rson对应的子序列。之前一直TLE就是因为没有把这三个case分开。如果是普通的求和直接相加就可以,但是涉及到子序列合并,需要分开考虑。模板题结果还卡了这么久><
#include<iostream> #include<stdio.h> #include<cstdio> #include<stdlib.h> #include<vector> #include<string> #include<cstring> #include<cmath> #include<algorithm> #include<stack> #include<queue> #include<ctype.h> #include<map> #include<time.h> #include<bitset> #include<set> #include<list> using namespace std; //hdu 5316 int T; #define lson l , m , rt << 1 #define rson m + 1 , r , rt << 1 | 1 #define LL long long const int maxn = 100010; const LL INF=0x3f3f3f3f; int n; int m; int cnt; /* Node Begin */ int flgoe[maxn<<2];//whether a leaf node is odd or even class node { public: LL oe; LL oo; LL ee; LL eo; public: node() { oe=0; oo=0; ee=0; eo=0; } }; node sum[maxn<<2]; /* Node End */ void PushUp(int rt) { //odd-even subsequence sum[rt].oe=max(sum[rt<<1].oe,sum[rt<<1|1].oe); sum[rt].oe=max(sum[rt].oe,sum[rt<<1].oo+sum[rt<<1|1].ee); sum[rt].oe=max(sum[rt<<1].oe+sum[rt<<1|1].oe,sum[rt].oe); sum[rt].oe=max(sum[rt].oe,-INF); //odd-odd subsequence sum[rt].oo=max(sum[rt<<1].oo,sum[rt<<1|1].oo); sum[rt].oo=max(sum[rt].oo,sum[rt<<1].oo+sum[rt<<1|1].eo); sum[rt].oo=max(sum[rt<<1].oe+sum[rt<<1|1].oo,sum[rt].oo); sum[rt].oo=max(sum[rt].oo,-INF); //even-even subsequence sum[rt].ee=max(sum[rt<<1].ee,sum[rt<<1|1].ee); sum[rt].ee=max(sum[rt].ee,sum[rt<<1].eo+sum[rt<<1|1].ee); sum[rt].ee=max(sum[rt<<1].ee+sum[rt<<1|1].oe,sum[rt].ee); sum[rt].ee=max(sum[rt].ee,-INF); //even-odd subsequence sum[rt].eo=max(sum[rt<<1].eo,sum[rt<<1|1].eo); sum[rt].eo=max(sum[rt].eo,sum[rt<<1].eo+sum[rt<<1|1].eo); sum[rt].eo=max(sum[rt<<1].ee+sum[rt<<1|1].oo,sum[rt].eo); sum[rt].eo=max(sum[rt].eo,-INF); } node Merge(node a,node b) { node ret=node(); //odd-even subsequence ret.oe=max(a.oe,b.oe); ret.oe=max(ret.oe,a.oo+b.ee); ret.oe=max(a.oe+b.oe,ret.oe); ret.oe=max(ret.oe,-INF); //odd-odd subsequence ret.oo=max(a.oo,b.oo); ret.oo=max(ret.oo,a.oo+b.eo); ret.oo=max(a.oe+b.oo,ret.oo); ret.oo=max(ret.oo,-INF); //even-even subsequence ret.ee=max(a.ee,b.ee); ret.ee=max(ret.ee,a.eo+b.ee); ret.ee=max(a.ee+b.oe,ret.ee); ret.ee=max(ret.ee,-INF); //even-odd subsequence ret.eo=max(a.eo,b.eo); ret.eo=max(ret.eo,a.eo+b.eo); ret.eo=max(a.ee+b.oo,ret.eo); ret.eo=max(ret.eo,-INF); return ret; } void build(int l,int r,int rt) { if (l == r) { //LL x; // scanf("%I64d",&x); if((cnt&1)==0)//even { flgoe[rt]=0; scanf("%I64d",&sum[rt].ee); // sum[rt].ee=x; sum[rt].eo=sum[rt].oe=sum[rt].oo=-INF; } else//odd { flgoe[rt]=1; scanf("%I64d",&sum[rt].oo); //sum[rt].oo=x; sum[rt].eo=sum[rt].oe=sum[rt].ee=-INF; } cnt++; return ; } int m = (l + r) >> 1; build(lson); build(rson); PushUp(rt); } void updateleaf(int q,int c,int l,int r, int rt) //q是要更新的节点 { if (l==r&&l==q)// { if(flgoe[rt]==0) { sum[rt].ee=c; } else if(flgoe[rt]==1) { sum[rt].oo=c; } return ; } int m = (l+r) >> 1; if (q <= m) updateleaf(q,c,lson); else if (m < q) updateleaf(q,c,rson); PushUp(rt); } node query(int L,int R,int l,int r,int rt) { if (L <= l && r <= R) { return sum[rt]; } // PushDown(rt);改变的都是叶子节点,不会出现父节点的值改变影响子节点的case int m = (l + r) >> 1; node ret=node(); if(L<=m&&m<R) { ret=Merge(query(L,R,lson),query(L,R,rson)); } else if(L<= m) ret = query(L , R , lson); else if (m < R) ret = query(L , R , rson); return ret; } int main() { freopen("input.txt","r",stdin); scanf("%d",&T); for(int ca=1;ca<=T;ca++) { scanf("%d%d",&n,&m); memset(sum,0,sizeof(sum)); memset(flgoe,0,sizeof(flgoe)); cnt=1; build(1 , n , 1); while(m--) { int op=0; int a=0; int b=0; scanf("%d",&op); // if(n==0) // { // if(op==0) // { // puts("0"); // continue; // } // } if (op == 0) { scanf("%d%d",&a,&b); // if(a>b) // { // swap(a,b); // } node tmp=query(a , b , 1 , n , 1); LL t1=max(tmp.ee,tmp.eo); LL t2=max(tmp.oe,tmp.oo); LL t3=max(t1,t2); printf("%I64d\n",t3); } else if(op==1) { scanf("%d %d",&a,&b); updateleaf(a,b,1,n,1); } } } return 0; }
相关文章推荐
- 关于pkg: /data/local/tmp/com.example.zy.deyijia Failure [INSTALL_FAILED_OLDER_SD问题
- 【LeetCode】(172)Factorial Trailing Zeroes(Easy)
- uva 11168 - Airport(凸包)
- Selenium 高阶应用之WebDriverWait 和 expected_conditions
- Selenium 高阶应用之WebDriverWait 和 expected_conditions
- 2015 Multi-University Training Contest 10 hdu 5411 CRB and Puzzle
- wget+webmin+dns+lamp+openwebmail
- hdu1021Fibonacci Again
- [杂谈]机器学习:数学、概率与人工智能
- Codeforces Gym 100338H High Speed Trains 组合数学+dp+高精度
- ip,email,银行卡号匹配
- 03-树3. Tree Traversals Again (25)将先序遍历和中序遍历转为后序遍历
- main函数的参数(一)
- USACO 2009 Feb Gold 3.Revamping Trails
- editor does not contain a main type 解决方法
- codeforces Gym 100338H High Speed Trains (递推,高精度)
- 具有 Button 风格的 Panel(覆盖TCustomPanel的Paint函数,用到了ThemeServices)
- HDOJ1021题 Fibonacci Again 应用求模公式
- HDOJ1021题 Fibonacci Again 应用求模公式
- Container With Most Water