POJ 4047 Garden

Source: Internet
Author: User

Gardentime limit:1000msmemory limit:65536kbthis problem'll be judged onPKU. Original id:4047
64-bit integer IO format: %lld Java class name: Main There is n flowerpots in Daniel's garden. These flowerpots is placed in n positions, and these n positions is numbered from 1 to N. Each flower was assigned an aesthetic value. The aesthetic values vary during different time of a day and different seasons of a year. Naughty Daniel is a happy and hardworking gardener who takes pleasure in exchanging the position of the flowerpots. Friends of Daniel is great fans of his miniature gardens. Before they visit Daniel ' s home, they'll take their old-fashioned cameras which is unable to adjust the focus so that I T can give a shot of exactly k consecutive flowerpots. Daniel hopes his friends enjoy themselves, but he doesn ' t want his friend to see all of his flowers due to some secret rea Sons, so he guides his friends to the best place to catch the most beautiful view in the interval [x, y], that's to say, To maximize the sum of the aesthetics values of the K flowerpotsTaken in one camera shot when they is only allow to see the flowerpots between position x to position y.there is m opera tions or queries is given in form of (p, x, y), here p = 0, 1 or 2. The meanings of different value of P is shown below.1. p = 0 Set the aesthetic value of the pot in position x as Y. (1 <= x <= n; -100 <= y <= 100) 2. p = 1 Exchange the pot in position X and the pot in position y. (1 <= x, y <= n; x might equal to Y) 3.  p = 2 Print the maximum sum of aesthetics values of one camera shot in interval [x, y]. (1 <= x <= y <= N; We guarantee that Y-x+1>=k)Input

There is multiple test cases. The first line of the input file contains only one integer indicates the number of the test cases. For each test case, the first line contains three integers:n, M, K (1 <= k <= n <= 200,000; 0 <= m <= 200, 000). The second line contains n integers indicates the initial aesthetic values of flowers from position 1 to position N. S ome Flowers is sick, so their aesthetic values is negative integers.  The aesthetic values range from-100 to 100. ( Notice:The number of position is assigned 1 to n from left to right.) The next m lines, each line contains a triple (p, x, y). The meaning of triples is mentioned above.Output

For each query with P = 2, print the maximum sum of the aesthetics values in one shot in interval [x, y].

Sample Input
15 7 3-1 2-4 6 12 1 52 1 31 2 12 1 52 1 40 2 42 1 5
Sample Output
4-3316
SourceJinhua ACM-ICPC Provincial Contest problem Solving: line tree, because the interval length is k, that is, K is fixed, we just make each leaf node of the line tree represents a length of the interval k and can. 1..K 2..k+1 3..k+2 ...
1#include <iostream>2#include <cstdio>3#include <cstring>4 using namespacestd;5 Const intMAXN =200010;6 structNode {7     intLt,rt,maxv,lazy;8} tree[maxn<<2];9 intd[maxn],a[maxn],n,m,k;Ten voidPushup (intv) { OneTREE[V].MAXV = Max (tree[v<<1].MAXV + tree[v<<1].lazy,tree[v<<1|1].maxv+tree[v<<1|1].lazy); A } - voidPushdown (intv) { -     if(tree[v].lazy) { thetree[v<<1].lazy + =Tree[v].lazy; -tree[v<<1|1].lazy + =Tree[v].lazy; -Tree[v].lazy =0; -     } + } - voidBuildintLtintRtintv) { +tree[v].lt =lt; ATree[v].rt =RT; atTree[v].lazy =0; -     if(LT = =RT) { -TREE[V].MAXV = d[tree[v].lt + K-1]-d[tree[v].lt-1]; -         return; -     } -     intMid = (lt + rt) >>1; inBuild (lt,mid,v<<1); -Build (mid+1,rt,v<<1|1); to Pushup (v); + } - voidUpdateintLtintRtintValintv) { the     if(LT <= tree[v].lt && RT >=tree[v].rt) { *Tree[v].lazy + =Val; $         return;Panax Notoginseng     } - Pushdown (v); the     if(LT <= tree[v<<1].RT) Update (lt,rt,val,v<<1); +     if(Rt >= tree[v<<1|1].lt) Update (lt,rt,val,v<<1|1); A Pushup (v); the } + intQueryintLtintRtintv) { -     if(LT <= tree[v].lt && RT >= Tree[v].rt)returnTREE[V].MAXV +Tree[v].lazy; $ Pushdown (v); $     intAns =-0x3f3f3f3f; -     if(LT <= tree[v<<1].rt) ans = max (Ans,query (lt,rt,v<<1)); -     if(Rt >= tree[v<<1|1].lt) ans = max (Ans,query (lt,rt,v<<1|1)); the Pushup (v); -     returnans;Wuyi } the intMain () { -     intt,p,x,y; Wuscanf"%d",&T); -      while(t--) { Aboutscanf" %d%d%d",&n,&m,&k); $          for(inti =1; I <= N; ++i) { -scanf"%d", d+i); -A[i] =D[i]; -D[i] + = d[i-1]; A         } +Build1, n-k+1,1); the          while(m--){ -scanf" %d%d%d",&p,&x,&y); $             if(!p) { theUpdate (MAX (1, X-k +1), Min (n-k+1, x), Y-a[x],1); theA[X] =y; the}Else if(p = =1){ theUpdate (MAX (1, X-k +1), Min (n-k+1, x), A[y]-a[x],1); -Update (MAX (1, Y-k +1), Min (n-k+1, y), a[x]-a[y],1); in swap (a[x],a[y]); the}Else if(p = =2) printf ("%d\n", Query (x,y-k+1,1)); the         } About     } the     return 0; the}
View Code

POJ 4047 Garden

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.