POJ1195 Mobile phones 【二维树状数组】

Mobile phones
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 14288 Accepted: 6642

Description

Suppose that the fourth generation mobile phone base stations in the Tampere area operate as follows. The area is divided into squares. The squares form an S * S matrix with the rows and columns numbered from 0 to S-1. Each square contains a base station. The number of active mobile phones inside a square can change because a phone is moved from a square to another or a phone is switched on or off. At times, each base station reports the change in the number of active phones to the main base station along with the row and the column of the matrix. 

Write a program, which receives these reports and answers queries about the current total number of active mobile phones in any rectangle-shaped area. 

Input

The input is read from standard input as integers and the answers to the queries are written to standard output as integers. The input is encoded as follows. Each input comes on a separate line, and consists of one instruction integer and a number of parameter integers according to the following table. 

The values will always be in range, so there is no need to check them. In particular, if A is negative, it can be assumed that it will not reduce the square value below zero. The indexing starts at 0, e.g. for a table of size 4 * 4, we have 0 <= X <= 3 and 0 <= Y <= 3. 

Table size: 1 * 1 <= S * S <= 1024 * 1024 
Cell value V at any time: 0 <= V <= 32767 
Update amount: -32768 <= A <= 32767 
No of instructions in input: 3 <= U <= 60002 
Maximum number of phones in the whole table: M= 2^30 

Output

Your program should not answer anything to lines with an instruction other than 2. If the instruction is 2, then your program is expected to answer the query by writing the answer as a single line containing a single integer to standard output.

Sample Input

0 4
1 1 2 3
2 0 0 2 2 
1 1 1 2
1 1 2 -1
2 1 1 2 3 
3

Sample Output

3
4

【问题描写叙述】

如果第四代移动电话的收发站是这样执行。整个区域被切割成非常小的方格。全部的方格组成了一个S*S的矩阵,行和列从0~S-1编号。每一个小方格都包括一个收发站。每一个方格内的手机的移动电话数量能够不断改变,由于手机用户在各个方格之间移动,也实用户开机或者关机。一旦某个方格里面开机的移动电话数量发生了变化,该方格里的收发站就会向总部发送一条信息说明这个改变量。

总部要你写一个程序,用来管理从各个收发站收到的信息。老板可能随时会问:某个给定矩形区域内有多少部开机的移动电话啊?你的程序必需要能随时回答老板的问题。

【输入输出数据】

从标准输入读入整数,向标准输出写入你对老板的回答。

输入数据的格式例如以下:每一个输入独立成一行。一个输入包含一个指示数和一些參数,见下表:

指示数

參数

意义

0

S

初始指令。整个区域由S*S个小方格组成。这个指令仅仅会在一開始出现一次。

1

X Y A

方格(XY)内的开机移动电话量添加了AA可能是正数也可能是负数

2

L B R T

询问在矩形区域(L,B)(R,T)内有多少部开机的移动电话。矩形区域(L,B)(R,T)包含全部的格子(XY)满足L<=X<=R,B<=Y<=T.

3

 

终止程序。

这个指令仅仅会在最后出现一次。

全部的数据总是在给定范围内,你不须要差错。

特别的,假设A是负数,你能够觉得该操作不会让该格子的开机移动电话数变成负数。格子是从0開始编号的,比方一个4*4的区域,全部的格子(XY)应该表示为:0<=X<=3,0<=Y<=3

对于除了2之外的指示,你的程序不应该输出不论什么东西。

假设指示是2。那么你的程序应该向标准输出写入一个整数。

【数据限制】

区域大小 

S * S

1 * 1 <= S * S <= 1024 * 1024

每一个格子的值

V

0 <= V <= 32767

添加/降低量

A

-32768 <= A <= 32767

指令总数

U

3 <= U <= 60002

全部格子的和

M

M= 2^30


第一次在OJ题中用函数指针数组。感觉挺简洁的。

#include <stdio.h>

int tree[1030][1030], size;

int lowBit(int n)
{
	return n & (-n);
}

void getSize()
{
	scanf("%d", &size);
}

void update()
{
	int x, y, val;
	scanf("%d%d%d", &x, &y, &val);
	++x; ++y;
	
	int temp;
	while(x <= size){
		temp = y;
		while(temp <= size){
			tree[x][temp] += val;
			temp += lowBit(temp);
		}
		x += lowBit(x);
	}
}

int getSum(int x, int y)
{
	int sum = 0, temp;
	while(x > 0){
		temp = y;
		while(temp > 0){
			sum += tree[x][temp];
			temp -= lowBit(temp);
		}
		x -= lowBit(x);
	}
	return sum;
}

void query()
{
	int x1, y1, x2, y2;
	scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
	++x1; ++y1; ++x2; ++y2;
	
	int sum = getSum(x2, y2) - getSum(x2, y1 - 1) - 
		getSum(x1 - 1, y2) + getSum(x1 - 1, y1 - 1);
	printf("%d\n", sum);
}

void (*funArr[])() = {
	getSize, update, query
}; //函数指针数组

int main()
{
	int com;
	while(scanf("%d", &com), com != 3)
		(*funArr[com])();	
	return 0;
}


使用优化算法,以优化VMD算法的惩罚因子惩罚因子 (&alpha;) 和分解层数 (K)。 1、将量子粒子群优化(QPSO)算法与变分模态分解(VMD)算法结合 VMD算法背景: VMD算法是一种自适应信号分解算法,主要用于分解信号为不同频率带宽的模态。 VMD的关键参数包括: 惩罚因子 &alpha;:控制带宽的限制。 分解层数 K:决定分解出的模态数。 QPSO算法背景: 量子粒子群优化(QPSO)是一种基于粒子群优化(PSO)的一种改进算法,通过量子行为模型增强全局搜索能力。 QPSO通过粒子的量子行为使其在搜索空间中不受位置限制,从而提高算法的收敛速度与全局优化能力。 任务: 使用QPSO优化VMD中的惩罚因子 &alpha; 和分解层数 K,以获得信号分解的最佳效果。 计划: 定义适应度函数:适应度函数根据VMD分解的效果来定义,通常使用重构信号的误差(例如均方误差、交叉熵等)来衡量分解的质量。 初始化QPSO粒子:定义粒子的位置和速度,表示 &alpha; 和 K 两个参数。初始化时需要在一个合理的范围内为每个粒子分配初始位置。 执行VMD分解:对每一组 &alpha; 和 K 参数,运行VMD算法分解信号。 更新QPSO粒子:使用QPSO算法更新粒子的状态,根据适应度函数调整粒子的搜索方向和位置。 迭代求解:重复QPSO的粒子更新步骤,直到满足终止条件(如适应度函数达到设定阈值,或最大迭代次数)。 输出优化结果:最终,QPSO算法会返回一个优化的 &alpha; 和 K,从而使VMD分解效果最佳。 2、将极光粒子(PLO)算法与变分模态分解(VMD)算法结合 PLO的优点与适用性 强大的全局搜索能力:PLO通过模拟极光粒子的运动,能够更高效地探索复杂的多峰优化问题,避免陷入局部最优。 鲁棒性强:PLO在面对高维、多模态问题时有较好的适应性,因此适合海上风电时间序列这种非线性、多噪声的数据。 应用场景:PLO适合用于优化VMD参数(&alpha; 和 K),并将其用于风电时间序列的预测任务。 进一步优化的建议 a. 实现更细致的PLO更新策略,优化极光粒子的运动模型。 b. 将PLO优化后的VMD应用于真实的海上风电数据,结合LSTM或XGBoost等模型进行风电功率预测。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值