HDU-4027 Can you answer these queries -? Segment tree

Topic links:

http://acm.hdu.edu.cn/showproblem.php?pid=4027

Meaning of the questions and ideas:

      A row of ships, given the ability value for each of the ship, there are two operations: the first is the ability to ship all within a range of values ​​and the square root rounding, the second is the ability to find all ships in a certain area and value. If each point on we will TLE violence recursive update interval (Do not ask me how I know). We can do something a little optimization. Such as the ability value 0,1. They square root or themselves, need to be updated. We can add a tag tag. If this interval is 0 or 1, we put the tag labeled 1. Once again, we update later time if the interval is 1 tag, we can not continue down recursively.

Code:

 1 #include <iostream>
 2 #include <cmath>
 3 #include <algorithm>
 4 #include <cstdio>
 5 #include <cstring>
 6 #define maxn 100000+5
 7 #define LL long long
 8 using namespace std;
 9 typedef struct node
10 {
11     LL a;
12     int tag;
13 }node;
14 node tree[maxn<<2];
15 void pushup(int rt)      //更新节点的和和tag值
16 {
17     tree[rt].a=tree[rt<<1].a+tree[rt<<1|1].a;
18     if(tree[rt<<1].tag&&tree[rt<<1|1].tag)
19         tree[rt].tag=1;
20     return ;
21 }
22 int build(int l,int r,int rt)
23 {
24     if(l==r)
25     {
26         scanf("%lld",&tree[rt].a);
27         if(tree[rt].a==0||tree[rt].a==1)
28             tree[rt].tag=1;
29         return  1;
30     }
31     int m=(l+r)>>1;
32     build(l,m,rt<<1);
33     build(m+1,r,rt<<1|1);
34     pushup(rt);
35 }
36 void update(int L,intR & lt, int L, int R & lt, int RT)
 37 [  {
 38 is      IF (L <= R & lt && L <= R & lt && Tree [RT] .tag == 1 )     // if the tag value is an interval, direct return 
39           return ;
 40      IF (L == R & lt)
 41 is      {
 42 is          Tree [RT] = II.A (LL) (sqrt ( 1.0 * Tree [RT] II.A));
 43 is          IF (Tree [RT] == II.A . 1 || Tree [RT ] II.A == 0 )
 44 is              Tree [RT] = .tag . 1 ;
 45          return ;
 46 is      }
47     int m=(l+r)>>1;
48     if(L <= m)
49         update(L,R,l,m,rt<<1);
50     if(R >  m)
51         update(L,R,m+1,r,rt<<1|1);
52     pushup(rt);
53 }
54 LL query(int L,int R,int l,int r,int rt)
55 {
56     if(L <= l && r <= R)
57     {
58         return tree[rt].a;
59     }
60     int m=(l+r)>>1;
61     LL ans=0;
62     if(L <= m)
63         ans+=query(L,R,l,m,rt<<1);
64     if(R > m)
65         ans+=query(L,R,m+1,r,rt<<1|1);
66     return ans;
67 }
68 int main()
69 {
70     int n,jishu;
71     jishu=0;
72     while(scanf("%d",&n)!=EOF)
73     {
74         printf("Case #%d:\n",++jishu);
75         build(1,n,1);
76         int k;
77         scanf("%d",&k);
78         while(k--)
79         {
80             int c,a,b;
81             scanf("%d%d%d",&c,&a,&b);
82             if(a>b)
83                 swap(a,b);
84             if(c==0)
85                 update(a,b,1,n,1);
86             else
87                 cout<<query(a,b,1,n,1)<<endl;
88         }
89         cout<<endl;
90     }
91 }

 

Guess you like

Origin www.cnblogs.com/blame/p/11369474.html