Magician
Time Limit: 18000/9000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 543 Accepted Submission(s): 151
Problem Description
Fantasy magicians usually gain their ability through one of three usual methods: possessing it as an innate talent, gaining it through study and practice, or receiving it from another being, often a god, spirit, or demon of some sort. Some wizards are depicted as having a special gift which sets them apart from the vast majority of characters in fantasy worlds who are unable to learn magic.
Magicians, sorcerers, wizards, magi, and practitioners of magic by other titles have appeared in myths, folktales, and literature throughout recorded history, with fantasy works drawing from this background.
In medieval chivalric romance, the wizard often appears as a wise old man and acts as a mentor, with Merlin from the King Arthur stories representing a prime example. Other magicians can appear as villains, hostile to the hero.
Mr. Zstu is a magician, he has many elves like dobby, each of which has a magic power (maybe negative). One day, Mr. Zstu want to test his ability of doing some magic. He made the elves stand in a straight line, from position 1 to position n, and he used two kinds of magic, Change magic and Query Magic, the first is to change an elf’s power, the second is get the maximum sum of beautiful subsequence of a given interval. A beautiful subsequence is a subsequence that all the adjacent pairs of elves in the sequence have a different parity of position. Can you do the same thing as Mr. Zstu ?
Magicians, sorcerers, wizards, magi, and practitioners of magic by other titles have appeared in myths, folktales, and literature throughout recorded history, with fantasy works drawing from this background.
In medieval chivalric romance, the wizard often appears as a wise old man and acts as a mentor, with Merlin from the King Arthur stories representing a prime example. Other magicians can appear as villains, hostile to the hero.
Mr. Zstu is a magician, he has many elves like dobby, each of which has a magic power (maybe negative). One day, Mr. Zstu want to test his ability of doing some magic. He made the elves stand in a straight line, from position 1 to position n, and he used two kinds of magic, Change magic and Query Magic, the first is to change an elf’s power, the second is get the maximum sum of beautiful subsequence of a given interval. A beautiful subsequence is a subsequence that all the adjacent pairs of elves in the sequence have a different parity of position. Can you do the same thing as Mr. Zstu ?
Input
The first line is an integer T represent the number of test cases.
Each of the test case begins with two integers n, m represent the number of elves and the number of time that Mr. Zstu used his magic.
(n,m <= 100000)
The next line has n integers represent elves’ magic power, magic power is between -1000000000 and 1000000000.
Followed m lines, each line has three integers like
type a b describe a magic.
If type equals 0, you should output the maximum sum of beautiful subsequence of interval [a,b].(1 <= a <= b <= n)
If type equals 1, you should change the magic power of the elf at position a to b.(1 <= a <= n, 1 <= b <= 1e9)
Each of the test case begins with two integers n, m represent the number of elves and the number of time that Mr. Zstu used his magic.
(n,m <= 100000)
The next line has n integers represent elves’ magic power, magic power is between -1000000000 and 1000000000.
Followed m lines, each line has three integers like
type a b describe a magic.
If type equals 0, you should output the maximum sum of beautiful subsequence of interval [a,b].(1 <= a <= b <= n)
If type equals 1, you should change the magic power of the elf at position a to b.(1 <= a <= n, 1 <= b <= 1e9)
Output
For each 0 type query, output the corresponding answer.
Sample Input
1
1 1
1
0 1 1
Sample Output
1
Source
解题:线段树 水题,当时居然没想出来,愣是百分百傻逼啊
1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long LL; 4 const LL INF = 0x3f3f3f3f3f3f3f3f; 5 const int maxn = 100010; 6 struct node{ 7 LL a,b,c,d; 8 //分别代表奇奇、奇偶、偶偶、偶奇 9 }tree[maxn<<2]; 10 void pushup(node &z,const node &x,const node &y){ 11 z.a = max(x.a,y.a); 12 z.a = max(z.a,x.a + y.d); 13 z.a = max(z.a,x.b + y.a); 14 z.b = max(x.b,y.b); 15 z.b = max(z.b,x.b + y.b); 16 z.b = max(z.b,x.a + y.c); 17 z.c = max(x.c,y.c); 18 z.c = max(z.c,x.c + y.b); 19 z.c = max(z.c,x.d + y.c); 20 z.d = max(x.d,y.d); 21 z.d = max(z.d,x.c + y.a); 22 z.d = max(z.d,x.d + y.d); 23 } 24 void build(int lt,int rt,int v){ 25 if(lt == rt){ 26 LL tmp; 27 scanf("%I64d",&tmp); 28 if(lt&1){ 29 tree[v].a = tmp; 30 tree[v].b = tree[v].c = tree[v].d = -INF; 31 }else{ 32 tree[v].c = tmp; 33 tree[v].a = tree[v].b = tree[v].d = -INF; 34 } 35 return; 36 } 37 int mid = (lt + rt)>>1; 38 build(lt,mid,v<<1); 39 build(mid+1,rt,v<<1|1); 40 pushup(tree[v],tree[v<<1],tree[v<<1|1]); 41 } 42 void update(int L,int R,int lt,int rt,LL val,int v){ 43 if(lt <= L && rt >= R){ 44 if(lt&1){ 45 tree[v].a = val; 46 tree[v].b = tree[v].c = tree[v].d = -INF; 47 }else{ 48 tree[v].c = val; 49 tree[v].a = tree[v].b = tree[v].d = -INF; 50 } 51 return; 52 } 53 int mid = (L + R)>>1; 54 if(lt <= mid) update(L,mid,lt,rt,val,v<<1); 55 if(rt > mid) update(mid+1,R,lt,rt,val,v<<1|1); 56 pushup(tree[v],tree[v<<1],tree[v<<1|1]); 57 } 58 node query(int L,int R,int lt,int rt,int v){ 59 if(lt == L && rt == R) return tree[v]; 60 int mid = (L + R)>>1; 61 if(rt <= mid) return query(L,mid,lt,rt,v<<1); 62 else if(lt > mid) return query(mid+1,R,lt,rt,v<<1|1); 63 else{ 64 node x = query(L,mid,lt,mid,v<<1); 65 node y = query(mid+1,R,mid+1,rt,v<<1|1); 66 node z; 67 pushup(z,x,y); 68 return z; 69 } 70 } 71 int main(){ 72 int kase,n,m,op,x,y; 73 LL val; 74 scanf("%d",&kase); 75 while(kase--){ 76 scanf("%d%d",&n,&m); 77 build(1,n,1); 78 while(m--){ 79 scanf("%d%d",&op,&x); 80 if(!op){ 81 scanf("%d",&y); 82 node ret = query(1,n,x,y,1); 83 printf("%I64d ",max(max(ret.a,ret.b),max(ret.c,ret.d))); 84 }else{ 85 scanf("%I64d",&val); 86 update(1,n,x,x,val,1); 87 } 88 } 89 } 90 return 0; 91 }