标签:des style blog java color os
Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3896 Accepted Submission(s): 1766
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <iostream> 5 using namespace std; 6 #define N 100005 7 #define ll root*2 8 #define rr root*2+1 9 10 int num[N]; 11 int n, m; 12 13 14 struct node{ 15 int l, r; 16 int lmax, rmax; 17 int len; 18 }a[N*4]; 19 20 21 void pushup(int root){ 22 if(a[root].l==a[root].r) return; 23 a[root].lmax=a[ll].lmax; 24 a[root].rmax=a[rr].rmax; 25 a[root].len=max(a[ll].len,a[rr].len); 26 27 if(num[a[ll].r]<num[a[rr].l]){ 28 if(a[ll].lmax==(a[ll].r-a[ll].l+1)) a[root].lmax=a[ll].lmax+a[rr].lmax; 29 if(a[rr].rmax==(a[rr].r-a[rr].l+1)) a[root].rmax=a[rr].rmax+a[ll].rmax; 30 a[root].len=max(max(a[root].len,a[ll].rmax+a[rr].lmax),max(a[root].lmax,a[root].rmax)); 31 } 32 } 33 34 void build(int left,int right,int root){ 35 a[root].l=left; 36 a[root].r=right; 37 if(left==right){ 38 a[root].len=1; 39 a[root].lmax=a[root].rmax=1; 40 return ; 41 } 42 int mid=(left+right)/2; 43 build(left,mid,root*2); 44 build(mid+1,right,root*2+1); 45 pushup(root); 46 } 47 48 void update(int p,int val,int root){ 49 if(a[root].l==p&&a[root].r==p){ 50 num[p]=val; 51 return ; 52 } 53 int mid=(a[root].l+a[root].r)/2; 54 if(p<=mid) update(p,val,ll); 55 else update(p,val,rr); 56 pushup(root); 57 } 58 59 node query(int left,int right,int root){ 60 node n1, l1, r1; 61 if(a[root].l==left&&a[root].r==right){ 62 n1.lmax=a[root].lmax; 63 n1.rmax=a[root].rmax; 64 n1.len=a[root].len; 65 return n1; 66 } 67 if(left>=a[rr].l) return query(left,right,rr); 68 else if(right<=a[ll].r) return query(left,right,ll); 69 else{ //所求的连续子序列在中间,需要区间合并 70 int mid=(a[root].l+a[root].r)/2; 71 l1=query(left,mid,ll); 72 r1=query(mid+1,right,rr); 73 n1.lmax=l1.lmax; 74 n1.rmax=r1.rmax; 75 n1.len=max(l1.len,r1.len); 76 if(num[mid]<num[mid+1]){ 77 if(l1.lmax==(mid-left+1)) n1.lmax+=r1.lmax; 78 if(r1.rmax==(right-mid)) n1.rmax+=l1.rmax; 79 n1.len=max(max(n1.len,l1.rmax+r1.lmax),max(n1.lmax,n1.rmax)); 80 } 81 return n1; 82 } 83 } 84 85 main() 86 { 87 int t, i, j, k, x, y; 88 char c[5]; 89 cin>>t; 90 while(t--){ 91 cin>>n>>m; 92 for(i=1;i<=n;i++) scanf("%d",&num[i]); 93 build(1,n,1); 94 while(m--){ 95 96 scanf("%s %d %d",c,&x,&y); 97 if(strcmp(c,"U")==0){ 98 update(x+1,y,1); 99 } 100 else{ 101 printf("%d\n",query(x+1,y+1,1).len); 102 } 103 } 104 } 105 }
HDU 3308 线段树(区间合并),布布扣,bubuko.com
标签:des style blog java color os
原文地址:http://www.cnblogs.com/qq1012662902/p/3858587.html