Uestc1425 another lcis

Source: Internet
Author: User

/* Question: the length of the subsegment can be increased continuously for the interval query. You can increase or decrease the interval to be quickly cut into the template question. You still need to pay attention to the interval merging during the query. My time is almost stuck, but it seems that everyone is doing the merge of Line Segment tree intervals. Why is the time difference so big .. It took a little time to explore. Conclusion: Passing parameters has a slight impact on the time. The main reason is that define's Max and min are much slower than function writing. I don't know why it is OJ */

#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define ls (rt<<1)#define rs (rt<<1|1)int max(int a,int b) { return a>b?a:b; }int min(int a,int b) { return a<b?a:b; }const int MAXN = 101000;int lpa[MAXN<<2],rpa[MAXN<<2],mpa[MAXN<<2];int add[MAXN<<2];int begin[MAXN<<2],end[MAXN<<2];int L,R,d;void pushup(int len, int rt){lpa[rt] = lpa[ls];rpa[rt] = rpa[rs];if(lpa[rt] == len-(len>>1) && end[ls] < begin[rs]) lpa[rt] += lpa[rs];if(rpa[rt] == (len>>1) && end[ls] < begin[rs]) rpa[rt] += rpa[ls];mpa[rt] = (end[ls] < begin[rs])? rpa[ls]+lpa[rs] : max(rpa[ls],lpa[rs]);mpa[rt] = max(mpa[rt], max(lpa[rt],rpa[rt]));mpa[rt] = max(mpa[rt], max(mpa[ls],mpa[rs]));begin[rt] = begin[ls];end[rt] = end[rs];}void pushdown(int rt){if(add[rt] == 0) return ;add[ls] += add[rt];add[rs] += add[rt];begin[ls] += add[rt];begin[rs] += add[rt];end[ls] += add[rt];end[rs] += add[rt];add[rt] = 0;}void build(int l, int r, int rt){add[rt] = 0;if(l == r){scanf("%d", &d);begin[rt] = end[rt] = d;lpa[rt] = rpa[rt] = mpa[rt] = 1;}else{int m = (l+r)>>1;build(lson);build(rson);pushup(r-l+1,rt);}}void update(int num, int l, int r, int rt){if(L<=l && r<=R){begin[rt] += num;end[rt] += num;add[rt] += num;}else{int m = (l+r)>>1;pushdown(rt);if(L<=m) update(num,lson);if(R> m) update(num,rson);pushup(r-l+1,rt);}}int query(int l, int r, int rt){if(L<=l && r<=R){return mpa[rt];}else{int m = (l+r)>>1;pushdown(rt);int ans = 0;if(L<=m) ans = query(lson);if(R> m) ans = max(ans, query(rson));if(end[ls] < begin[rs]) ans = max(ans, min(m-L+1,rpa[ls])+min(R-m,lpa[rs]));return ans;}}int main(){int cas,ca = 0;int n,q;int x;char s[3];scanf("%d", &cas);while(cas--){printf("Case #%d:\n", ++ca);scanf("%d%d", &n, &q);build(1,n,1);while(q--){scanf("%s%d%d", s,&L,&R);if(s[0] == 'a'){scanf("%d", &x);update(x,1,n,1);}else{printf("%d\n", query(1,n,1));}}}return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.