当前位置:网站首页>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;
}
边栏推荐
猜你喜欢

使用 Performance 看看浏览器在做什么

Stream常用操作以及原理探索

ICML 2022 | LIMO: 一种快速生成靶向分子的新方法

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

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

Variable declaration of typescript

程序员必备,一款让你提高工作效率N倍的神器uTools

免费的机器学习数据集网站(6300+数据集)

Mongodb series window environment deployment configuration

Wechat applet Registration Guide
随机推荐
Here Document免交互及Expect自动化交互
爱可可AI前沿推介(6.26)
In insect classes and objects
A must for programmers, an artifact utools that can improve your work efficiency n times
CVPR 2022文档图像分析与识别相关论文26篇汇集简介
使用 Performance 看看浏览器在做什么
2021-10-18 character array
D中不用GC
GEE——全球人类居住区网格数据 1975-1990-2000-2014
输入文本自动生成图像,太好玩了!
ICML 2022 | limo: a new method for rapid generation of targeted molecules
7-16 monetary system I
MySQL configuration improves data insertion efficiency
虫子 内存管理 上
Ring queue PHP
5+api, clear application cache
7.Consul服务注册与发现
Thinking caused by the error < note: candidate expectations 1 argument, 0 provided >
Cloudcompare - Poisson reconstruction
Guruiwat rushed to the Hong Kong stock exchange for listing: set "multiple firsts" and obtained an investment of 900million yuan from IDG capital