poj 3468 A Simple Problem with Integers

poj 3468 A Simple Problem with Integers

题意:这道题是个标准的成段更新的线段树。

View Code
 1 #include<stdio.h>

 2 #include<stdlib.h>

 3 #include<iostream>

 4 #include<string>

 5 #include<queue>

 6 #include<map>

 7 #include<cmath>

 8 #include<stack>

 9 #include<algorithm>

10 #include<functional>

11 using namespace std;

12 //1.bulid();

13 //2.query(a,b)

14 //3.update(a,b)

15 #define lson l , m , rt << 1

16 #define rson m + 1 , r , rt << 1 | 1

17 typedef long long LL;

18 const int maxn = 100055;

19 LL sign[maxn<<2];

20 LL sum[maxn<<2];

21 int n;

22 //根据题意做相关修改,询问时的操作 

23 LL operate(LL a,LL b){

24     return a+b;

25 }

26 void PushUp(int rt){

27     sum[rt]=operate(sum[rt<<1],sum[rt<<1|1]);

28 }

29 void PushDown(int rt,int m) {

30     if (sign[rt]) {

31         sign[rt<<1]   += sign[rt];

32         sign[rt<<1|1] += sign[rt];

33         sum[rt<<1] += (m - (m >> 1)) * sign[rt];

34         sum[rt<<1|1] += (m >> 1) * sign[rt];

35         sign[rt] = 0;

36     }

37 }

38 void bulid(int l=1,int r=n,int rt=1){

39     sign[rt] = 0;

40     if(l==r){// 据题意做相关修改

41         scanf("%lld",&sum[rt]);return ;

42     }

43     int m=(l+r)>>1;

44     bulid(lson);

45     bulid(rson);

46     PushUp(rt);

47 }

48 void update(int L,int R,int add,int l=1,int r=n,int rt=1){

49     if(L<=l && r<=R){// 据题意做相关修改

50         sign[rt]+=add;

51             sum[rt]+=(LL)add*(r-l+1);return ;

52     }

53     PushDown(rt,r-l+1);

54     int m = (l + r) >> 1;

55     if (L <= m) update(L , R , add , lson);

56     if (R > m) update(L , R , add , rson);

57     PushUp(rt);

58 }

59 LL query(int L,int R,int l=1,int r=n,int rt=1) {

60     if (L <= l && r <= R) {

61         return sum[rt];

62     }

63     PushDown(rt , r - l + 1);

64     int m = (l + r) >> 1;

65     LL ret = 0;

66     if (L <= m) ret += query(L , R , lson);

67     if (m < R) ret += query(L , R , rson);

68     return ret;

69 }

70 

71 int main()

72 {

73     int m,x,y,z;

74     char op[2];

75     while(~scanf("%d%d",&n,&m)){

76         bulid();

77         while(m--){

78             scanf("%s",op);

79             if(op[0]=='Q'){

80                 scanf("%d%d",&x,&y);

81                 printf("%lld\n",query(x,y));

82             }else{

83                 scanf("%d%d%d",&x,&y,&z);

84                 update(x,y,z);

85             }

86             

87         }

88     }

89     

90     return 0;

91 }

你可能感兴趣的:(Integer)