标签:
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 662 Accepted Submission(s): 329
#include<stdio.h> #include<algorithm> #include<string.h> #include<math.h> #include<string> #include<iostream> #include<queue> #include<stack> #include<map> #include<vector> #include<set> using namespace std; typedef long long LL; #define mid (L+R)/2 #define lson rt*2,L,mid #define rson rt*2+1,mid+1,R #pragma comment(linker, "/STACK:102400000,102400000") const int maxn = 1e5+300; const int INF = 0x3f3f3f3f; typedef long long LL; typedef unsigned long long ULL; struct Seg{ LL val, Min, Max; int idmin, idmax; }segs[maxn*4]; LL a[maxn]; LL ans[maxn]; int n; void PushUP(int rt){ segs[rt].Max = max(segs[rt*2].Max,segs[rt*2+1].Max); segs[rt].Min = min(segs[rt*2].Min,segs[rt*2+1].Min); if(segs[rt*2].Max > segs[rt*2+1].Max) segs[rt].idmax = segs[rt*2].idmax; else segs[rt].idmax = segs[rt*2+1].idmax; if(segs[rt*2].Min < segs[rt*2+1].Min) segs[rt].idmin = segs[rt*2].idmin; else segs[rt].idmin = segs[rt*2+1].idmin; } void buildtree(int rt,int L,int R){ if(L == R){ segs[rt].val = a[L]; segs[rt].idmax = segs[rt].idmin = L; segs[rt].Min = segs[rt].Max = a[L]; return; } buildtree(lson); buildtree(rson); PushUP(rt); } int querymin(int rt,int L,int R,int l_ran,int r_ran){ if(l_ran<=L&&R<=r_ran){ return segs[rt].idmin; } int id = 0; if(l_ran <= mid){ id = querymin(lson,l_ran,r_ran); } if(r_ran > mid){ int tid = querymin(rson,l_ran,r_ran); if(id != 0){ if(a[id] > a[tid]) id = tid; }else{ id = tid; } } return id; } int querymax(int rt,int L,int R,int l_ran,int r_ran){ if(l_ran<=L&&R<=r_ran){ return segs[rt].idmax; } int id = 0; if(l_ran <= mid){ id = querymax(lson,l_ran,r_ran); } if(r_ran > mid){ int tid = querymax(rson,l_ran,r_ran); if(id != 0){ if(a[id] < a[tid]) id = tid; }else{ id = tid; } } return id; } void dfs(int l, int r){ if(l > r) return; int idmin, idmax; idmin = querymin(1,1,n,l,r); idmax= querymax(1,1,n,l,r); if(idmax < idmin) swap(idmin,idmax); LL tmp = a[idmax]*a[idmin]; for(int i = idmax-idmin+1; i <= r-l+1; i++){ ans[i] = max(ans[i], tmp); } if(a[idmax] > a[idmin]){ dfs(l,idmin-1); dfs(idmin+1,r); }else{ dfs(l,idmax-1); dfs(idmax+1,r); } } int main(){ while(scanf("%d",&n)!=EOF){ LL ret = 0; for(int i = 1; i <= n; i++){ scanf("%lld",&a[i]); } buildtree(1,1,n); for(int i = 1; i <= n; ++i){ ans[i] = 1; } dfs(1,n); for(int i = 1; i <= n; i++){ printf("%lld\n",ans[i]); } } return 0; } /* 5 5 1 2 4 4 */
HDU 5696 ——区间的价值——————【线段树、快排思想】
标签:
原文地址:http://www.cnblogs.com/chengsheng/p/5529783.html