标签:typedef 结构 php code status print data enter 结构体
#include<bits/stdc++.h> using namespace std; typedef long long ll; #define lowbit(i) i&-i #define inf (ll)(1e18+7) #define N 1500010 struct data { ll lc,rc,mx,sum; }tree[N]; int n,m; int a[N]; inline ll min(ll x,ll y){return x<y?x:y;} inline ll max(ll x,ll y){return x>y?x:y;} inline int read(int&n) { int x=0,f=1; char c=getchar(); while(c<‘0‘||c>‘9‘) {if(c==‘-‘) f=-1;c=getchar();} while(c>=‘0‘&&c<=‘9‘) {x*=10;x+=c-‘0‘;c=getchar();} n=x*f; } void build(int l,int r,int x) { if(l==r) { tree[x].lc=tree[x].rc=tree[x].mx=tree[x].sum=a[l]; return; } int mid=(l+r)>>1; build(l,mid,x<<1); build(mid+1,r,x<<1|1); tree[x].sum=tree[x<<1].sum+tree[x<<1|1].sum; tree[x].mx=max(tree[x<<1].mx,tree[x<<1|1].mx); tree[x].mx=max(tree[x].mx,tree[x<<1].rc+tree[x<<1|1].lc); tree[x].lc=max(tree[x<<1].lc,tree[x<<1].sum+tree[x<<1|1].lc); tree[x].rc=max(tree[x<<1|1].rc,tree[x<<1|1].sum+tree[x<<1].rc); } data query(int l,int r,int x,int a,int b) { if(l>b||r<a) return {-inf,-inf,-inf,0}; if(l>=a&&r<=b) return tree[x]; int mid=(l+r)>>1; data ret; data ls=query(l,mid,x<<1,a,b); data rs=query(mid+1,r,x<<1|1,a,b); ret.sum=ls.sum+rs.sum; ret.lc=max(ls.lc,ls.sum+rs.lc); ret.rc=max(rs.rc,rs.sum+ls.rc); ret.mx=max(ls.mx,rs.mx); ret.mx=max(ret.mx,ls.rc+rs.lc); return ret; } void update(int l,int r,int x,int pos,ll num) { if(l==r) { tree[x].lc=tree[x].rc=tree[x].mx=tree[x].sum=num; return; } int mid=(l+r)>>1; if(pos<=mid) update(l,mid,x<<1,pos,num); else update(mid+1,r,x<<1|1,pos,num); tree[x].sum=tree[x<<1].sum+tree[x<<1|1].sum; tree[x].mx=max(tree[x<<1].mx,tree[x<<1|1].mx); tree[x].mx=max(tree[x].mx,tree[x<<1].rc+tree[x<<1|1].lc); tree[x].lc=max(tree[x<<1].lc,tree[x<<1].sum+tree[x<<1|1].lc); tree[x].rc=max(tree[x<<1|1].rc,tree[x<<1|1].sum+tree[x<<1].rc); } void ask(int a,int b) { if(a>b) swap(a,b); printf("%lld\n",query(1,n,1,a,b).mx); } void change(int pos,int x) { update(1,n,1,pos,x); } int main() { read(n); read(m); for(int i=1;i<=n;i++) read(a[i]); build(1,n,1); while(m--) { int opt,a,b; read(opt); read(a); read(b); if(opt==1) ask(a,b); if(opt==2) change(a,b); } return 0; }
标签:typedef 结构 php code status print data enter 结构体
原文地址:http://www.cnblogs.com/19992147orz/p/6365842.html