码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 4047 Garden

时间:2015-04-22 20:36:15      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:

Garden

Time Limit: 1000ms
Memory Limit: 65536KB
This problem will be judged on PKU. Original ID: 4047
64-bit integer IO format: %lld      Java class name: Main
There are n flowerpots in Daniel‘s garden.  These flowerpots are placed in n positions, and these n positions are numbered from 1 to n. Each flower is 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 are great fans of his miniature gardens. Before they visit Daniel‘s home, they will take their old-fashioned cameras which are unable to adjust the focus so that it 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 reasons, so he guides his friends to the best place to catch the most beautiful view in the interval [x, y], that is to say, to maximize the sum of the aesthetics values of the k flowerpots taken in one camera shot when they are only allow to see the flowerpots between position x to position y.
There are m operations or queries are given in form of (p, x, y), here p = 0, 1 or 2. The meanings of different value of p are 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 are multiple test cases.
The first line of the input file contains only one integer indicates the number of 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. Some flowers are sick, so their aesthetic values are negative integers. The aesthetic values range from -100 to 100.  (Notice: The number of position is assigned 1 to n from left to right.)
In 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

1
5 7 3
-1 2 -4 6 1
2 1 5
2 1 3
1 2 1
2 1 5
2 1 4
0 2 4
2 1 5

Sample Output

4
-3
3
1
6

Source

 
解题:线段树,由于区间长度为k,即k是固定的,我们只要使得线段树的每个叶节点代表一个长度为k的区间的和即可。
 
1..k 2..k+1 3..k+2 ...
 
技术分享
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5 const int maxn = 200010;
 6 struct node {
 7     int lt,rt,maxv,lazy;
 8 } tree[maxn<<2];
 9 int d[maxn],a[maxn],n,m,k;
10 void pushup(int v) {
11     tree[v].maxv = max(tree[v<<1].maxv + tree[v<<1].lazy,tree[v<<1|1].maxv+tree[v<<1|1].lazy);
12 }
13 void pushdown(int v) {
14     if(tree[v].lazy) {
15         tree[v<<1].lazy += tree[v].lazy;
16         tree[v<<1|1].lazy += tree[v].lazy;
17         tree[v].lazy = 0;
18     }
19 }
20 void build(int lt,int rt,int v) {
21     tree[v].lt = lt;
22     tree[v].rt = rt;
23     tree[v].lazy = 0;
24     if(lt == rt) {
25         tree[v].maxv = d[tree[v].lt + k - 1] - d[tree[v].lt-1];
26         return;
27     }
28     int mid = (lt + rt)>>1;
29     build(lt,mid,v<<1);
30     build(mid+1,rt,v<<1|1);
31     pushup(v);
32 }
33 void update(int lt,int rt,int val,int v){
34     if(lt <= tree[v].lt && rt >= tree[v].rt){
35         tree[v].lazy += val;
36         return;
37     }
38     pushdown(v);
39     if(lt <= tree[v<<1].rt) update(lt,rt,val,v<<1);
40     if(rt >= tree[v<<1|1].lt) update(lt,rt,val,v<<1|1);
41     pushup(v);
42 }
43 int query(int lt,int rt,int v){
44     if(lt <= tree[v].lt && rt >= tree[v].rt) return tree[v].maxv + tree[v].lazy;
45     pushdown(v);
46     int ans = -0x3f3f3f3f;
47     if(lt <= tree[v<<1].rt) ans = max(ans,query(lt,rt,v<<1));
48     if(rt >= tree[v<<1|1].lt) ans = max(ans,query(lt,rt,v<<1|1));
49     pushup(v);
50     return ans;
51 }
52 int main() {
53     int T,p,x,y;
54     scanf("%d",&T);
55     while(T--) {
56         scanf("%d %d %d",&n,&m,&k);
57         for(int i = 1; i <= n; ++i) {
58             scanf("%d",d+i);
59             a[i] = d[i];
60             d[i] += d[i-1];
61         }
62         build(1,n-k+1,1);
63         while(m--){
64             scanf("%d %d %d",&p,&x,&y);
65             if(!p){
66                 update(max(1,x - k + 1),min(n-k+1,x),y - a[x],1);
67                 a[x] = y;
68             }else if(p == 1){
69                 update(max(1,x - k + 1),min(n-k+1,x),a[y] - a[x],1);
70                 update(max(1,y - k + 1),min(n-k+1,y),a[x] - a[y],1);
71                 swap(a[x],a[y]);
72             }else if(p == 2) printf("%d\n",query(x,y-k+1,1));
73         }
74     }
75     return 0;
76 }
View Code

 

POJ 4047 Garden

标签:

原文地址:http://www.cnblogs.com/crackpotisback/p/4448485.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!