2015 Multi-University Training Contest 2 hdu 5306 Gorgeous Sequence

Gorgeous Sequence

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 349    Accepted Submission(s): 57


Problem Description
There is a sequence  a of length n. We use ai to denote the i-th element in this sequence. You should do the following three types of operations to this sequence.

0 x y t: For every xiy, we use min(ai,t) to replace the original ai's value.
1 x y: Print the maximum value of ai that xiy.
2 x y: Print the sum of ai that xiy.
 

 

Input
The first line of the input is a single integer  T, indicating the number of testcases.

The first line contains two integers n and m denoting the length of the sequence and the number of operations.

The second line contains n separated integers a1,,an (1in,0ai<231).

Each of the following m lines represents one operation (1xyn,0t<231).

It is guaranteed that T=100n1000000, m1000000.
 

 

Output
For every operation of type  1 or 2, print one line containing the answer to the corresponding query.
 

 

Sample Input
1
5 5
1 2 3 4 5
1 1 5
2 1 5
0 3 5 3
1 1 5
2 1 5
 

 

Sample Output
5
15
3
12
Hint
Please use efficient IO method
 

 

Source
 
解题:线段树,对着标程写的,还是没搞清到底是怎么回事
 
2015 Multi-University Training Contest 2 hdu 5306 Gorgeous Sequence
 1 #include <bits/stdc++.h>

 2 using namespace std;

 3 typedef long long LL;

 4 const int maxn = 1000010;

 5 struct node {

 6     int cover,s,tag,maxtag,maxv;

 7     LL sum;

 8 } tree[maxn<<2];

 9 node put(node c,int t) {

10     if(!t) return c;

11     if(c.cover != c.s) c.maxv = t;

12     c.maxtag = t;

13     c.sum += (LL)t*(c.s - c.cover);

14     c.cover = c.s;

15     return c;

16 }

17 node calc(const node &a,const node &b,int t) {

18     node c;

19     c.tag = t;

20     c.s = a.s + b.s;

21     c.sum = a.sum + b.sum;

22     c.cover = a.cover + b.cover;

23     c.maxv = max(a.maxv,b.maxv);

24     c.maxtag = max(a.maxtag,b.maxtag);

25     return put(c,t);

26 }

27 int tmp,n,m;

28 void build(int L,int R,int v) {

29     tree[v].tag = 0;

30     tree[v].s = R - L + 1;

31     if(L == R) {

32         scanf("%d",&tmp);

33         tree[v].sum = tree[v].tag = tree[v].maxtag = tree[v].maxv = tmp;

34         tree[v].cover = 1;

35         return;

36     }

37     int mid = (L + R)>>1;

38     build(L,mid,v<<1);

39     build(mid+1,R,v<<1|1);

40     tree[v] = calc(tree[v<<1],tree[v<<1|1],0);

41 }

42 node query(int L,int R,int lt,int rt,int v) {

43     if(lt <= L && rt >= R) return tree[v];

44     int mid = (L + R)>>1;

45     if(rt <= mid) return put(query(L,mid,lt,rt,v<<1),tree[v].tag);

46     if(lt > mid) return put(query(mid+1,R,lt,rt,v<<1|1),tree[v].tag);

47     return calc(query(L,mid,lt,rt,v<<1),query(mid+1,R,lt,rt,v<<1|1),tree[v].tag);

48 }

49 void cleartag(int v,int t) {

50     if(tree[v].maxtag < t) return;

51     if(tree[v].tag >= t) tree[v].tag = 0;

52     if(tree[v].s > 1) {

53         cleartag(v<<1,t);

54         cleartag(v<<1|1,t);

55     }

56     if(tree[v].s == 1) {

57         tree[v].sum = tree[v].maxtag = tree[v].maxv = tree[v].tag;

58         tree[v].cover = (tree[v].tag > 0);

59     } else tree[v] = calc(tree[v<<1],tree[v<<1|1],tree[v].tag);

60 }

61 void update(int L,int R,int lt,int rt,int t,int v) {

62     if(tree[v].tag && tree[v].tag <= t) return;

63     if(lt <= L && rt >= R) {

64         cleartag(v,t);

65         tree[v].tag = t;

66         if(L == R) {

67             tree[v].sum = tree[v].tag = tree[v].maxv = tree[v].maxtag = t;

68             tree[v].cover = (tree[v].tag > 0);

69         } else tree[v] = calc(tree[v<<1],tree[v<<1|1],t);

70     } else {

71         int mid = (L + R)>>1;

72         if(rt <= mid) update(L,mid,lt,rt,t,v<<1);

73         else if(lt > mid) update(mid+1,R,lt,rt,t,v<<1|1);

74         else {

75             update(L,mid,lt,rt,t,v<<1);

76             update(mid+1,R,lt,rt,t,v<<1|1);

77         }

78         tree[v] = calc(tree[v<<1],tree[v<<1|1],tree[v].tag);

79     }

80 }

81 int main() {

82     int kase,op,x,y,t;

83     scanf("%d",&kase);

84     while(kase--) {

85         scanf("%d%d",&n,&m);

86         build(1,n,1);

87         while(m--) {

88             scanf("%d%d%d",&op,&x,&y);

89             if(!op) {

90                 scanf("%d",&t);

91                 update(1,n,x,y,t,1);

92             } else if(op == 1) printf("%d\n",query(1,n,x,y,1).maxv);

93             else printf("%I64d\n",query(1,n,x,y,1).sum);

94         }

95     }

96     return 0;

97 }
View Code

 

你可能感兴趣的:(sequence)