CSU-1532-JuQueen(线段树+离散化)

本文介绍了一道关于线段树的经典问题,通过离散化处理大量数据,实现高效区间更新与查询操作。文章详细解析了解题思路及具体代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

(一)题面:

Problem description

JuQueen is the super computer with the best performance allover Germany. It is on rank 8 in the famous top500 list with its 458 752 cores. It draws a lot of energy (up to 2 301 kW), so we want to reduce that by underclocking the unused cores.

The cluster scheduling algorithm which is in charge of distributing jobs over the nodes and cores of a cluster will issue the following speedstepping commands:

·change X S changes the frequency of core X by S steps

·groupchange A B S changes the frequency of every core in range [A,B] by S steps

·state X returns the current state of core X

To be safe for the future, your program should be able to handle 4 587 520 cores. The initial frequency for each core is 0.


Input


Output

Output one line for every operation in the input. For change and groupchange print the changed number of steps, for state print the current state.


Sample Input
10 10 5
state 0
groupchange 2 9 7
state 9
groupchange 0 2 10
change 0 -5

4587520 10000 5
groupchange 0 4587010 9950
groupchange 23 4587000 42
groupchange 4710 4587001 -1000
state 1234560
groupchange 6666 3060660 10000
Sample Output
0
7
7
3
-3

9950
42
-1000
8992
1008

(二)题目大意:

        有C个数,O次操作,每次操作有三种可能:
                ①查询某个位置的值;

                ②将区间[l,r]的所有数+v;

                ③将某个位置的值-v。并且每个元素的值不能小于0且不能大于N。

        若为操作①则输出查询位置的值;若为操作②或③则输出该操作实际能改变数值的大小(见样例)。

(三)解题思路:

        ①根据题意就可以知道这个题几乎是裸的线段树,包括单点区间修改和单点修改(可以转化为区间修改)。
        ②维护区间的最大值和最小值,每次进行操作的时候先根据最大值和最小值确定能修改的数值大小,再进行修改。

        ③考虑到原来的下标是从0~4587520,而总共的操作数最多只有5e5,即最多用到的坐标数不会超过1e6,我们可以将坐标离散化,然后再建树。

(四)具体代码:

#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<vector>
#include<algorithm>
#define INF 1000000000
using namespace std;
const int maxn=500005;
int n,c,q,val,_Max,_Min;
struct Tree{
    int L,R,_max,_min,lazy;
    int mid(){return (L+R)>>1;}
}tree[maxn<<4];
struct Oper{
    int l,r,v,o;
}op[maxn];
vector<int>V;
int getid(int x){return lower_bound(V.begin(),V.end(),x)-V.begin()+1;}
void BuildTree(int l,int r,int i){
    tree[i].L=l;tree[i].R=r;
    tree[i]._max=tree[i]._min=tree[i].lazy=0;
    if(l==r)return;
    int m=tree[i].mid();
    BuildTree(l,m,i<<1);
    BuildTree(m+1,r,i<<1|1);
}
void Push_down(int i){
    if(tree[i].lazy){
        tree[i<<1]._max+=tree[i].lazy;
        tree[i<<1|1]._max+=tree[i].lazy;
        tree[i<<1]._min+=tree[i].lazy;
        tree[i<<1|1]._min+=tree[i].lazy;
        tree[i<<1].lazy+=tree[i].lazy;
        tree[i<<1|1].lazy+=tree[i].lazy;
        tree[i].lazy=0;
    }
}
void Push_up(int i){
    tree[i]._max=max(tree[i<<1]._max,tree[i<<1|1]._max);
    tree[i]._min=min(tree[i<<1]._min,tree[i<<1|1]._min);
}
void QueryTree(int l,int r,int i){
    int L=tree[i].L,R=tree[i].R;
    if(l<=L&&R<=r){
        _Max=max(_Max,tree[i]._max);
        _Min=min(_Min,tree[i]._min);
        return;
    }
    Push_down(i);
    int m=tree[i].mid();
    if(r<=m)QueryTree(l,r,i<<1);
    else if(l>m)QueryTree(l,r,i<<1|1);
    else{
        QueryTree(l,m,i<<1);
        QueryTree(m+1,r,i<<1|1);
    }
}
void UpdateTree(int l,int r,int i,int v){
    int L=tree[i].L,R=tree[i].R;
    if(l<=L&&R<=r){
        tree[i]._max+=v;
        tree[i]._min+=v;
        tree[i].lazy+=v;
        return;
    }
    Push_down(i);
    int m=tree[i].mid();
    if(r<=m)UpdateTree(l,r,i<<1,v);
    else if(l>m)UpdateTree(l,r,i<<1|1,v);
    else{
        UpdateTree(l,m,i<<1,v);
        UpdateTree(m+1,r,i<<1|1,v);
    }
    Push_up(i);
}
void scan_d(int &x){
    int sig=1;x=0;
    char ch=getchar();
    while(ch!='-'&&(ch<'0')||(ch>'9'))ch=getchar();
    if(ch=='-')sig=-1,ch=getchar();
    while(ch>='0'&&ch<='9')x=x*10+(ch&15),ch=getchar();
    x*=sig;
}
int main(){
    freopen("in.txt","r",stdin);
    while(~scanf("%d%d%d",&n,&c,&q)){getchar();
        for(int i=0;i<q;i++){
            char Ch=getchar();
            if(Ch=='s'){
                scan_d(op[i].l);op[i].r=op[i].l;op[i].o=1;
                V.push_back(op[i].l);
            }
            else{
                scan_d(op[i].l);op[i].r=op[i].l;
                if(Ch=='g')scan_d(op[i].r);scan_d(op[i].v);op[i].o=2;
                V.push_back(op[i].l);V.push_back(op[i].r);
            }
        }
        sort(V.begin(),V.end());
        V.erase(unique(V.begin(),V.end()),V.end());
        for(int i=0;i<q;i++){
            op[i].l=getid(op[i].l);
            op[i].r=getid(op[i].r);
        }
        BuildTree(1,V.size(),1);
        for(int i=0;i<q;i++){
            _Max=-1e9;_Min=1e9;
            QueryTree(op[i].l,op[i].r,1);
            if(op[i].o==1){printf("%d\n",_Max);}
            else{
                if(!op[i].v){printf("0\n");continue;}
                else if(op[i].v>0)val=min(op[i].v,c-_Max);
                else val=max(op[i].v,-_Min);
                printf("%d\n",val);
                UpdateTree(op[i].l,op[i].r,1,val);
            }
        }
    }
    return 0;
}

(五)总结:

        其实这里主要想记录的是一个离散化的操作,当我们不在意值的具体大小而只关心值的相对大小时,将数据离散化可能可以很大程度地减少时间开支。线段树里面的离散化操作貌似还不少,平时需要多练。

内容概要:本文介绍了基于Python实现的SSA-GRU(麻雀搜索算法优化门控循环单元)时间序列预测项目。项目旨在通过结合SSA的全局搜索能力和GRU的时序信息处理能力,提升时间序列预测的精度和效率。文中详细描述了项目的背景、目标、挑战及解决方案,涵盖了从数据预处理到模型训练、优化及评估的全流程。SSA用于优化GRU的超参数,如隐藏层单元数、学习率等,以解决传统方法难以捕捉复杂非线性关系的问题。项目还提供了具体的代码示例,包括GRU模型的定义、训练和验证过程,以及SSA的种群初始化、迭代更新策略和适应度评估函数。; 适合人群:具备一定编程基础,特别是对时间序列预测和深度学习有一定了解的研究人员和技术开发者。; 使用场景及目标:①提高时间序列预测的精度和效率,适用于金融市场分析、气象预报、工业设备故障诊断等领域;②解决传统方法难以捕捉复杂非线性关系的问题;③通过自动化参数优化,减少人工干预,提升模型开发效率;④增强模型在不同数据集和未知环境中的泛化能力。; 阅读建议:由于项目涉及深度学习和智能优化算法的结合,建议读者在阅读过程中结合代码示例进行实践,理解SSA和GRU的工作原理及其在时间序列预测中的具体应用。同时,关注数据预处理、模型训练和优化的每个步骤,以确保对整个流程有全面的理解。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值