码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 4027 2011上海赛区网络赛 线段树 成段平方根 ***

时间:2015-07-30 16:52:11      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:

不能直接使用成段增减的那种,因为一段和的平方根不等于平方根的和,直接记录是否为1,是1就不需要更新了

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<cmath>
 6 #include<queue>
 7 #include<map>
 8 using namespace std;
 9 #define MOD 1000000007
10 const int INF=0x3f3f3f3f;
11 const double eps=1e-5;
12 typedef long long ll;
13 #define cl(a) memset(a,0,sizeof(a))
14 #define ts printf("*****\n");
15 #define lson l,mid,rt<<1
16 #define rson mid+1,r,rt<<1|1
17 #define root 1,n,1
18 #define mid ((l+r)>>1)
19 const int MAXN=100100;
20 int n,m,t,Min;
21 ll sum[MAXN<<2];
22 int col[MAXN<<2];
23 void pushup(int rt)
24 {
25     sum[rt]=sum[rt<<1]+sum[rt<<1|1];
26     col[rt]=col[rt<<1]&&col[rt<<1|1];
27 }
28 void build(int l,int r,int rt){
29     col[rt]=0;
30     if(l==r)
31     {
32         scanf("%I64d",&sum[rt]);
33         return;
34     }
35     build(lson);
36     build(rson);
37     pushup(rt);
38 }
39 void update(int L,int R,int l,int r,int rt)
40 {
41     if(l==r)
42     {
43         sum[rt]=sqrt(sum[rt]);
44         if(sum[rt]==1)  col[rt]=1;
45         return;
46     }
47     if(L<=mid&&!col[rt<<1])  update(L,R,lson);
48     if(R>mid&&!col[rt<<1|1]) update(L,R,rson);
49     pushup(rt);
50 }
51 ll query(int L,int R,int l,int r,int rt)
52 {
53     if(l>=L&&r<=R)
54     {
55         return sum[rt];
56     }
57     ll ans=0;
58     if(L<=mid)  ans+=query(L,R,lson);
59     if(R>mid) ans+=query(L,R,rson);
60     return ans;
61 }
62 int main()
63 {
64     int i,j,k;
65     #ifndef ONLINE_JUDGE
66     freopen("1.in","r",stdin);
67     #endif
68 
69     int ca=1;
70     while(scanf("%d",&n)!=EOF)
71     {
72         build(root);
73         int q;
74         scanf("%d",&q);
75         printf("Case #%d:\n",ca++);
76         int s,L,R;
77         while(q--)
78         {
79             scanf("%d%d%d",&s,&L,&R);
80             if(L>R) swap(L,R);
81             if(s==0)
82             {
83                 update(L,R,root);
84             }
85             else
86             {
87                 ll w=query(L,R,root);
88                 printf("%I64d\n",w);
89             }
90         }
91         printf("\n");
92     }
93 }

 

hdu 4027 2011上海赛区网络赛 线段树 成段平方根 ***

标签:

原文地址:http://www.cnblogs.com/cnblogs321114287/p/4689524.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!