当前位置:网站首页>Luogu p4145 seven minutes of God created questions 2 / Huashen travels around the world
Luogu p4145 seven minutes of God created questions 2 / Huashen travels around the world
2022-06-26 13:57:00 【__ LazyCat__】
Line segment tree solution
Find the root of the interval , Because it doesn't exist s q r t ( ∑ i = 1 n a i ) = ∑ i = 1 n s q r t ( a i ) sqrt(\sum_{i=1}^{n}a_{i})=\sum_{i=1}^{n}sqrt(a_{i}) sqrt(∑i=1nai)=∑i=1nsqrt(ai) Excellent nature of , So every interval modification should recurse to the leaf node , Can not make a lazy mark . Because the value of the parent node needs the value of the child node to determine , It is impossible to calculate at one time .
Feasibility explanation
The complexity of this recursive modification method to leaf nodes is explosive ? It's better not to make achievements ? It's not like that , For one i n t int int Range of integers , Because each square root is rounded down , The number of square root is not greater than 6 6 6 Times will make the value of this number 1, There is no need to prescribe any more . Then each number is at most square 6 6 6 Time . So we can assign a value to each node v v v , Represents the number of this interval 1 1 1 . If this interval is 1 The number of is equal to the number of numbers in this interval , It means that the interval is full of 1 1 1 , You do not need to perform the square root operation for this interval , Just go back .
Concrete realization
Look at the code .
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long ll;
const int maxn=1e5+5;
int n,m;
struct node{
ll l,r,w,v;//w Interval sum ,v by 1 Quantity and sum
}t[maxn<<2];
ll a[maxn];
inline void pushup(int k)
{
// Update the data of the parent node at the same time
t[k].w=t[k<<1].w+t[k<<1|1].w;
t[k].v=t[k<<1].v+t[k<<1|1].v;
}
inline void build(int k,int l,int r)
{
t[k].l=l,t[k].r=r;
if(l==r){
t[k].w=a[l];
t[k].v=(t[k].w==1)?1:0;// The initial judgment is whether there is 1
return;
}
int mid=l+r>>1;
build(k<<1,l,mid);
build(k<<1|1,mid+1,r);
pushup(k);
}
inline void update(int k,int l,int r)
{
int x=t[k].l,y=t[k].r;
if(t[k].v==y-x+1)return;// All sections are 1, No need to update , Go straight back to
if(x==y){
// Must be updated to the leaf node
t[k].w=sqrt(t[k].w);
t[k].v=(t[k].w==1)?1:0;
return;
}
int mid=x+y>>1;
if(l<=mid)update(k<<1,l,r);
if(mid<r)update(k<<1|1,l,r);
pushup(k);
}
inline ll query(int k,int l,int r)
{
int x=t[k].l,y=t[k].r;
if(l<=x&&y<=r)return t[k].w;
int mid=x+y>>1;
ll ans=0;
if(l<=mid)ans+=query(k<<1,l,r);
if(mid<r)ans+=query(k<<1|1,l,r);
return ans;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin>>n;
for(int i=1;i<=n;i++)cin>>a[i];
build(1,1,n);
cin>>m;
for(int i=1;i<=m;i++){
int op,x,y;
cin>>op>>x>>y;
if(x>y){
int tmp=x;x=y;y=tmp;
}
if(!op){
update(1,x,y);
}
else{
cout<<query(1,x,y)<<"\n";
}
}
return 0;
}
边栏推荐
- shell脚本详细介绍(四)
- Teacher Li Hang's new book "machine learning methods" is on the market! Purchase link attached
- Logical operation
- 古瑞瓦特冲刺港交所上市:创下“多个第一”,获IDG资本9亿元投资
- ES6 module
- It is better and safer to choose which securities company to open an account for flush stock
- Included angle of 3D vector
- Es6: iterator
- 去某东面试遇到并发编程问题:如何安全地中断一个正在运行的线程
- Exercise set 1
猜你喜欢

Wechat applet -picker component is repackaged and the disabled attribute is added -- above

Network remote access using raspberry pie

Create your own cross domain proxy server

Thinking caused by the error < note: candidate expectations 1 argument, 0 provided >

ES中索引别名(alias)的到底有什么用

Echart stack histogram: add white spacing effect setting between color blocks

Free machine learning dataset website (6300+ dataset)

创建一个自己的跨域代理服务器

Bigint: handles large numbers (integers of any length)

Gurivat sprint Harbour Exchange listed: created “multiple first”, received 900 million yuan Investment from IDG capital
随机推荐
虫子 类和对象 上
Wechat applet Registration Guide
In insect classes and objects
Included angle of 3D vector
Luogu p3426 [poi2005]sza-template solution
【MySQL从入门到精通】【高级篇】(二)MySQL目录结构与表在文件系统中的表示
Traverse the specified directory to obtain the file name with the specified suffix (such as txt and INI) under the current directory
Insect operator overloaded a fun
Es snapshot based data backup and restore
D check type is pointer
Pytorch based generation countermeasure Network Practice (7) -- using pytorch to build SGAN (semi supervised GaN) to generate handwritten digits and classify them
7-16 monetary system I
7-2 the cubic root of a number
Here Document免交互及Expect自动化交互
虫子 运算符重载的一个好玩的
[node.js] MySQL module
李航老师新作《机器学习方法》上市了!附购买链接
CloudCompare——泊松重建
7-1 range of numbers
Go language - pipeline channel