【HDU】4027 Can you answer these queries?

 1 #include<cstdio>

 2 #include<cmath>

 3 typedef __int64 LL;

 4 #define EPS 1e-9

 5 #define MAXN 100010

 6 struct node

 7 {

 8     LL sum;

 9     int flag;

10 };

11 node tree[MAXN<<2];

12 inline void PushUp(int rt)

13 {

14     tree[rt].flag=tree[rt<<1].flag&tree[rt<<1|1].flag;

15     tree[rt].sum=tree[rt<<1].sum+tree[rt<<1|1].sum;

16 }

17 void Build(int L,int R,int rt)

18 {

19     if(L==R)

20     {

21         scanf("%I64d",&tree[rt].sum);

22         if(tree[rt].sum==1)

23             tree[rt].flag=1;

24         else

25             tree[rt].flag=0;

26     }

27     else

28     {

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

30         Build(L,mid,rt<<1);

31         Build(mid+1,R,rt<<1|1);

32         PushUp(rt);

33     }

34 }

35 LL Query(int x,int y,int L,int R,int rt)

36 {

37     if(x<=L&&R<=y)

38         return tree[rt].sum;

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

40     LL ans=0;

41     if(mid>=x)

42         ans+=Query(x,y,L,mid,rt<<1);

43     if(y>mid)

44         ans+=Query(x,y,mid+1,R,rt<<1|1);

45     return ans;

46 }

47 void Update(int x,int y,int L,int R,int rt)

48 {

49     if(tree[rt].flag)

50         return;

51     if(L==R)

52     {

53         tree[rt].sum=(LL)(sqrt((double)tree[rt].sum)+EPS);

54         if(tree[rt].sum==1)

55             tree[rt].flag=1;

56         else

57             tree[rt].flag=0;

58     }

59     else

60     {

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

62         if(mid>=x)

63             Update(x,y,L,mid,rt<<1);

64         if(y>mid)

65             Update(x,y,mid+1,R,rt<<1|1);

66         PushUp(rt);

67     }

68 }

69 inline void swap(int &x,int &y)

70 {

71     int temp=x;

72     x=y;

73     y=temp;

74 }

75 int main()

76 {

77     int n,q,t,x,y,ca=1;

78     while(~scanf("%d",&n))

79     {

80         Build(1,n,1);

81         scanf("%d",&q);

82         printf("Case #%d:\n",ca++);

83         while(q--)

84         {

85             scanf("%d%d%d",&t,&x,&y);

86             if(x>y)

87                 swap(x,y);

88             if(t)

89                 printf("%I64d\n",Query(x,y,1,n,1));

90             else

91                 Update(x,y,1,n,1);

92         }

93         putchar('\n');

94     }

95     return 0;

96 }

你可能感兴趣的:(HDU)