HDU 4288 线段树

题目链接

Coder

Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3667 Accepted Submission(s): 1427

Problem Description

  In mathematics and computer science, an algorithm describes a set of procedures or instructions that define a procedure. The term has become increasing popular since the advent of cheap and reliable computers. Many companies now employ a single coder to write an algorithm that will replace many other employees. An added benefit to the employer is that the coder will also become redundant once their work is done. 1
  You are now the signle coder, and have been assigned a new task writing code, since your boss would like to replace many other employees (and you when you become redundant once your task is complete).
Your code should be able to complete a task to replace these employees who do nothing all day but eating: make the digest sum.
  By saying “digest sum” we study some properties of data. For the sake of simplicity, our data is a set of integers. Your code should give response to following operations:
  1. add x – add the element x to the set;
  2. del x – remove the element x from the set;
  3. sum – find the digest sum of the set. The digest sum should be understood by

  where the set S is written as {a1, a2, … , ak} satisfying a1 < a2 < a3 < … < ak

  Can you complete this task (and be then fired)?

1 See http://uncyclopedia.wikia.com/wiki/Algorithm

Input

  There’re several test cases.
  In each test case, the first line contains one integer N ( 1 <= N <= 105 ), the number of operations to process.
  Then following is n lines, each one containing one of three operations: “add x” or “del x” or “sum”.
  You may assume that 1 <= x <= 109.
  Please see the sample for detailed format.
  For any “add x” it is guaranteed that x is not currently in the set just before this operation.
  For any “del x” it is guaranteed that x must currently be in the set just before this operation.
  Please process until EOF (End Of File).

Output

  For each operation “sum” please print one line containing exactly one integer denoting the digest sum of the current set. Print 0 if the set is empty.

Sample Input

9
add 1
add 2
add 3
add 4
add 5
sum
add 6
del 3
sum
6
add 1
add 3
add 5
add 7
add 9
sum

Sample Output

3
4
5

做法

  • 先将数据离散化,离线处理数据
  • 线段树的每个节点都存储在这个区间内sum[5],代表该区间的所有情况,外加一个cnt表示该区间元素个数用于更新sum[5]
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cassert>
using namespace std ;

typedef long long LL ;
const int N = 1e5 + 11 ;

int cnt[N<<2] ; LL sum[N<<2][5] ;
int oper[N][2] ; int num[N] , n_num ;
int n , m ;
int x , y ;

void up(int u) {
    int l = (u<<1) ;
    int r = (u<<1|1) ;
    cnt[u] = cnt[l] + cnt[r] ;
    for(int i = 0 ; i < 5 ; ++i) sum[u][i] = sum[l][i] ;
    for(int i = 0 ; i < 5 ; ++i) {
        sum[u][(cnt[l]+i)%5] += sum[r][i] ;
    }
}

void build(int u , int l , int r) {
    if(l == r) {
        for(int i = 0 ; i < 5 ; ++i) sum[u][i] = 0 ;
        cnt[u] = 0 ;
        return ;
    }
    int mid = (l+r)>>1 ;
    build(u<<1 , l , mid) ;
    build(u<<1|1 , mid+1 , r) ;
    up(u) ;
}

void update(int u , int l , int r , int d) {
    if(x <= l && r <= y) {
        if(d == -1) {
            for(int i = 0 ; i < 5 ; ++i) sum[u][i] = 0 ;
            cnt[u] = 0 ;
        }else {
            sum[u][1] = d ;
            cnt[u] = 1 ;
        }
        return ;
    }
    int mid = (l+r)>>1 ;
    if(x <= mid) update(u<<1 , l , mid , d) ;
    if(y > mid) update(u<<1|1 , mid+1 , r ,d) ;
    up(u) ;
}

int main() {//freopen("data.in" , "r" , stdin);
    char str[5] ;
    int a ;
    while(scanf("%d" ,&n)==1) {
        n_num = 0 ;
        for(int i = 0 ; i < n ; ++i) {
            scanf("%s", str) ;
            if(str[0] == 'a') {
                oper[i][0] = 0 ;
                scanf("%d" ,&oper[i][1]) ;
                num[n_num++] = oper[i][1] ;
            }else if(str[0] == 'd') {
                oper[i][0] = 1 ;
                scanf("%d" ,&oper[i][1]) ;
                num[n_num++] = oper[i][1] ;
            }else {
                oper[i][0] = 2 ;
            }
        }
        sort(num , num+n_num) ;
        n_num = unique(num , num+n_num)-num ;
        build(1 , 1 , n_num) ;
        for(int i = 0 ; i < n ; ++i) {
            x = y = lower_bound(num , num + n_num , oper[i][1])-num+1 ;
            if(oper[i][0] == 0) {
                update(1 , 1 , n_num , oper[i][1]) ;
            }else if(oper[i][0] == 1) {
                update(1 , 1 , n_num , -1) ;
            }else {
                printf("%I64d\n" , sum[1][3]) ;
            }
        }
    }
}
内容概要:该论文研究增程式电动汽车(REEV)的能量管理策略,针对现有优化策略实时性差的问题,提出基于工况识别的自适应等效燃油消耗最小策略(A-ECMS)。首先建立整车Simulink模型和基于规则的策略;然后研究动态规划(DP)算法和等效燃油最小策略;接着通过聚类分析将道路工况分为四类,并设计工况识别算法;最后开发基于工况识别的A-ECMS,通过高德地图预判工况类型并自适应调整SOC分配。仿真显示该策略比规则策略节油8%,比简单SOC规划策略节油2%,并通过硬件在环实验验证了实时可行性。 适合人群:具备一定编程基础,特别是对电动汽车能量管理策略有兴趣的研发人员和技术爱好者。 使用场景及目标:①理解增程式电动汽车能量管理策略的基本原理;②掌握动态规划算法和等效燃油消耗最小策略的应用;③学习工况识别算法的设计和实现;④了解基于工况识别的A-ECMS策略的具体实现及其优化效果。 其他说明:此资源不仅提供了详细的MATLAB/Simulink代码实现,还深入分析了各算法的原理和应用场景,适合用于学术研究和工业实践。在学习过程中,建议结合代码调试和实际数据进行实践,以便更好地理解策略的优化效果。此外,论文还探讨了未来的研究方向,如深度学习替代聚类、多目标优化以及V2X集成等,为后续研究提供了思路。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值