Codechef FNCS Chef and Churu

Disciption

Chef has recently learnt Function and Addition. He is too exited to teach this to his friend Churu. Chef and Churu are very fast friends, they share their knowledge whenever they meet. Chef use to give a lot of exercises after he teaches some concept to Churu.

 

Chef has an array of N numbers. He also has N functions. Each function will return the sum of all numbers in the array from Li to Ri. So Chef asks churu a lot of queries which are of two types.

 

  • Type 1: Change the xth element of the array to y
  • Type 2: Return the sum of all functions from m to n.


Now Churu has started to solve, but Chef realize that it is tough for him to decide whether Churu is correct or not. So he needs your help , will you help him out ?

Input Format

First Line is the size of the array i.e. N 
Next Line contains N space separated numbers Ai denoting the array 
Next N line follows denoting Li and Ri for each functions. 
Next Line contains an integer Q , number of queries to follow. 
Next Q line follows , each line containing a query of Type 1 or Type 2. 
x y : denotes a type 1 query,where x and y are integers 
m n : denotes a type 2 query where m and n are integers 

Output Format

For each query of type 2 , output as asked above. 

Constraints

1 ≤ N ≤ 10 5 
1 ≤ A i ≤ 10 9 
1 ≤ L i ≤ N 
i ≤ R i ≤ N 
1 ≤ Q ≤ 10 5 
1 ≤ x ≤ N 
1 ≤ y ≤ 10 9 
1 ≤ m ≤ N 
m ≤ n ≤ N 

Subtask

  • Subtask 1: N ≤ 1000 , Q ≤ 1000 , 10 points
  • Subtask 2: R-L ≤ 10 , all x will be distinct ,10 points
  • Subtask 3: Refer to constraints above , 80 points

Sample Input


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

2 1 4 
1 3 7 
2 1 4 
2 3 5 

Sample Output

41 
53 
28 

Explanation

Functions values initially : 
F[1] = 1+ 2 + 3 = 6 
F[2] = 2 + 3 + 4 + 5 = 14 
F[3] = 4+5 = 9 
F[4] = 3+4+5 = 12 
F[5] = 1+2 = 3 
Query 1: F[1] + F[2] + F[3] + F[4] = 41 
After Update , the Functions are : 
F[1] = 10 , F[2] = 18 , F[3] = 9 , F[4] = 16 , F[5] = 3 
Query 3: F[1] + F[2] + F[3] + F[4] = 53 
Query 4: F[3]+F[4]+F[5] = 28 

 

本来想用分块看看能不能水过的,,,结果怎么就水过了23333

我们设每个块内的元素有M个,那么就有(N/M)个块。

我们对函数分块了之后,可以用数组tag[i][j]表示第i个块内的函数总共加了几次第j个元素。

这个数组的预处理差分之后前缀和一下就好了。

然后有了这个数组之后我们就可以很方便的维护每个块内函数的和了。

对于整块的话,修改的复杂度O(N/M)  [考虑这个元素对每个块的总和的影响] ,查询的复杂度也是 O(N/M) ,因为最多要查询N/M个块。

 

而零散的块用树状数组维护一下元素数组的前缀和就好了,修改复杂度 O(log N),查询复杂度 O(M * log N)。

可以解出 M = sqrt(N/ log N) 的时候程序的效果应该是最好的,但是限于我们开不出这么大的数组,所以M只能取sqrt(N)稍小一点。

我取的是sqrt(N)/1.414 ,然后就A了。

 

#include<bits/stdc++.h>
#define ll unsigned long long
#define maxn 100005
using namespace std;
int n,a[maxn],sz,opt,le,ri,mx,m;
int tag[505][maxn],bl[maxn];
ll tot[505],f[maxn],ans;
int l[maxn],r[maxn];

inline void update(int x,int y){
	for(;x<=n;x+=x&-x) f[x]+=(ll)y;
}

inline ll query(int x){
	ll an=0;
	for(;x;x-=x&-x) an+=f[x];
	return an;
}

inline void input(){
	scanf("%d",&n),sz=sqrt(n/2+1);
	for(int i=1;i<=n;i++){
		scanf("%d",a+i);
		update(i,a[i]);
	}
	
	for(int i=1;i<=n;i++){
		bl[i]=(i-1)/sz+1;
		scanf("%d%d",l+i,r+i);
		tag[bl[i]][l[i]]++;
		tag[bl[i]][r[i]+1]--;
	}
}

inline void prework(){
	mx=bl[n];
	for(int i=1;i<=mx;i++)
	    for(int j=1;j<=n;j++){
	    	tag[i][j]+=tag[i][j-1];
	    	tot[i]+=a[j]*(ll)tag[i][j];
		}	
}

inline void TOL(){
	int derta=ri-a[le];
	update(le,derta);
	for(int i=1;i<=mx;i++) tot[i]+=tag[i][le]*(ll)derta;
	
	a[le]=ri;
}

inline void REQ(){
	ans=0;
	
	if(bl[le]==bl[ri]){
		for(;le<=ri;le++) ans+=query(r[le])-query(l[le]-1);
	}
	else{
		for(;bl[le]==bl[le-1];le++) ans+=query(r[le])-query(l[le]-1);
		for(;bl[ri]==bl[ri+1];ri--) ans+=query(r[ri])-query(l[ri]-1);
		for(int j=bl[le];j<=bl[ri];j++) ans+=tot[j];
	}
	
	printf("%llu\n",ans);
}

int main(){
	input();
	prework();
	scanf("%d",&m);
	while(m--){
		scanf("%d%d%d",&opt,&le,&ri);
		if(opt==1) TOL();
		else REQ();
	}
	return 0;
}

  

 

转载于:https://www.cnblogs.com/JYYHH/p/8524473.html

内容概要:本文详细探讨了基于阻尼连续可调减振器(CDC)的半主动悬架系统的控制策略。首先建立了CDC减振器的动力学模型,验证了其阻尼特性,并通过实验确认了模型的准确性。接着,搭建了1/4车辆悬架模型,分析了不同阻尼系数对悬架性能的影响。随后,引入了PID、自适应模糊PID和模糊-PID并联三种控制策略,通过仿真比较它们的性能提升效果。研究表明,模糊-PID并联控制能最优地提升悬架综合性能,在平顺性和稳定性间取得最佳平衡。此外,还深入分析了CDC减振器的特性,优化了控制策略,并进行了系统级验证。 适用人群:从事汽车工程、机械工程及相关领域的研究人员和技术人员,尤其是对车辆悬架系统和控制策略感兴趣的读者。 使用场景及目标:①适用于研究和开发基于CDC减振器的半主动悬架系统的工程师;②帮助理解不同控制策略(如PID、模糊PID、模糊-PID并联)在悬架系统中的应用及其性能差异;③为优化车辆行驶舒适性和稳定性提供理论依据和技术支持。 其他说明:本文不仅提供了详细的数学模型和仿真代码,还通过实验数据验证了模型的准确性。对于希望深入了解CDC减振器工作原理及其控制策略的读者来说,本文是一份极具价值的参考资料。同时,文中还介绍了多种控制策略的具体实现方法及其优缺点,为后续的研究和实际应用提供了有益的借鉴。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值