标签:
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1476 Accepted Submission(s): 541
#include<iostream> #include<cstdio> #include<algorithm> #define N 100005 using namespace std; int a[N]; struct Tree{ int l,r; int lv,rv,mv; }tree[4*N]; void PushUp(int l,int r,int idx){ tree[idx].lv = tree[idx<<1].lv; tree[idx].rv = tree[idx<<1|1].rv; tree[idx].mv = max(tree[idx<<1].mv,tree[idx<<1|1].mv); int mid = (l+r)>>1; int len = r- l+1; if(a[mid]==a[mid+1]){ if(tree[idx].lv==len-(len>>1)) tree[idx].lv+=tree[idx<<1|1].lv; if(tree[idx].rv==(len>>1)) tree[idx].rv+=tree[idx<<1].rv; tree[idx].mv = max(tree[idx].mv,tree[idx<<1].rv+tree[idx<<1|1].lv); } } void build(int l,int r,int idx){ tree[idx].l = l; tree[idx].r = r; if(l==r){ tree[idx].lv = tree[idx].rv = tree[idx].mv = 1; return; } int mid = (l+r)>>1; build(l,mid,idx<<1); build(mid+1,r,idx<<1|1); PushUp(l,r,idx); } int query(int l,int r,int idx){ if(tree[idx].l>=l&&tree[idx].r<=r){ return tree[idx].mv; } int mid = (tree[idx].l+tree[idx].r)>>1; int ans = 0; if(l<=mid) ans = max(ans,query(l,r,idx<<1)); if(r>mid) ans=max(ans,query(l,r,idx<<1|1)); if(a[mid]==a[mid+1]){ ans = max(ans,min(mid-l+1,tree[idx<<1].rv)+min(r-mid,tree[idx<<1|1].lv)); } return ans; } int main() { int n,m; while(scanf("%d",&n)!=EOF,n){ scanf("%d",&m); for(int i=1;i<=n;i++){ scanf("%d",&a[i]); } build(1,n,1); while(m--){ int a,b; scanf("%d%d",&a,&b); printf("%d\n",query(a,b,1)); } } return 0; }
标签:
原文地址:http://www.cnblogs.com/liyinggang/p/5351795.html