前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >BZOJ 3211: 花神游历各国【线段树区间开方问题】

BZOJ 3211: 花神游历各国【线段树区间开方问题】

作者头像
Angel_Kitty
发布2018-04-09 15:35:40
6520
发布2018-04-09 15:35:40
举报
文章被收录于专栏:小樱的经验随笔

3211: 花神游历各国

Time Limit: 5 Sec  Memory Limit: 128 MB

Submit: 3514  Solved: 1306

[Submit][Status][Discuss]

Description

Input

Output

每次x=1时,每行一个整数,表示这次旅行的开心度

Sample Input

4 1 100 5 5 5 1 1 2 2 1 2 1 1 2 2 2 3 1 1 4

Sample Output

101 11 11

HINT

对于100%的数据, n ≤ 100000,m≤200000 ,data[i]非负且小于10^9

Source

SPOJ2713 gss4 数据已加强

题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3211

分析:

题目这么长,其实就是2个操作:

1.询问L,R中的各个数之和

2.将L,R中的每个数x,将x修x1/2,向下取整。。

我们其实可以发现1000000000中的数修改不了几次就会变成1,那么直接暴力修改,若一段区间内的数全部<=1也就不用再改下去了。。

下面给出AC代码:

代码语言:javascript
复制
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 inline int read()
 4 {
 5     int x=0,f=1;
 6     char ch=getchar();
 7     while(ch<'0'||ch>'9')
 8     {
 9         if(ch=='-')
10             f=-1;
11         ch=getchar();
12     }
13     while(ch>='0'&&ch<='9')
14     {
15         x=x*10+ch-'0';
16         ch=getchar();
17     }
18     return x*f;
19 }
20 const int N=100010;
21 int n,m,pos,x,y;
22 int a[N],add[N<<2];
23 typedef long long ll;
24 ll ans,sum[N<<2];
25 inline void buildtree(int l,int r,int pos)
26 {
27     if(l==r)
28     {
29         sum[pos]=a[l];
30         if(a[l]<=1)
31             add[pos]=1;
32         else
33             add[pos]=0;
34         return;
35     }
36     int mid=(l+r)/2;
37     buildtree(l,mid,pos*2);
38     buildtree(mid+1,r,pos*2+1);
39     sum[pos]=sum[pos*2]+sum[pos*2+1];
40     add[pos]=add[pos*2]&add[pos*2+1];
41 }
42 inline void update(int l,int r,int x,int y,int pos)
43 {
44     if(add[pos])
45         return;
46     if(l==r)
47     {
48         sum[pos]=(int)sqrt(sum[pos]);
49         if(sum[pos]<=1)
50             add[pos]=1;
51         return;
52     }
53     int mid=(l+r)/2;
54     if(x<=mid)
55         update(l,mid,x,y,pos*2);
56     if(y>mid)
57         update(mid+1,r,x,y,pos*2+1);
58     add[pos]=add[pos*2]&add[pos*2+1];
59     sum[pos]=sum[pos*2]+sum[pos*2+1];
60 }
61 inline void solve(int l,int r,int x,int y,int pos)
62 {
63     if(x<=l&&r<=y)
64     {
65         ans+=sum[pos];
66         return;
67     }
68     int mid=(l+r)/2;
69     if(x<=mid)
70         solve(l,mid,x,y,pos*2);
71     if(y>mid)
72         solve(mid+1,r,x,y,pos*2+1);
73 }
74 int main()
75 {
76     n=read();
77     for(int i=1;i<=n;i++)
78         a[i]=read();
79     buildtree(1,n,1);
80     m=read();
81     for(int i=1;i<=m;i++)
82     {
83         pos=read();
84         x=read();
85         y=read();
86         if(pos==1)
87         {
88             ans=0;
89             solve(1,n,x,y,1);
90             printf("%lld\n",ans);
91         }
92         else
93             update(1,n,x,y,1);
94     }
95     return 0;
96 }
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017-08-08 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 3211: 花神游历各国
  • Description
  • Input
  • Output
  • Sample Input
  • Sample Output
  • HINT
  • Source
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档