HDU-4288-Coder(线段树)

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 {a 1, a 2, ... , a k} satisfying a 1 < a 2 < a 3 < ... < a k 
  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 <= 10 5 ), 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 <= 10 9.
  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
Hint
C++ maybe run faster than G++ in this problem.
 

Source
 

思路:用sum[400005][5] 来保存对应区间的 下标模5为i的数的和,剩下的就是单点更新和根据左右儿子节点的数的数量来维护sum。

#include <cstdio>
#include <cstring>
#include <map>
#include <algorithm>
using namespace std;

struct S{
char op[5];
int x,id;
}e[100005];

bool cmpval(S a,S b)
{
    if(a.x==b.x)  return a.op[0]<b.op[0];

    return a.x<b.x;
}

bool cmpid(S a,S b)
{
    return a.id<b.id;
}

int n,cnt,val[100005],num[400005];
long long sum[400005][5];

void build(int idx,int s,int e)
{
    if(s!=e)
    {
        int mid=(s+e)>>1;

        build(idx<<1,s,mid);
        build(idx<<1|1,mid+1,e);
    }

    num[idx]=0;

    for(int i=0;i<5;i++) sum[idx][i]=0;
}

void update(int idx,int s,int e,int pos,int flag)
{
    num[idx]+=flag;

    if(s==e)
    {
        sum[idx][1]+=val[s]*flag;

        return;
    }

    int mid=(s+e)>>1;

    if(pos<=mid) update(idx<<1,s,mid,pos,flag);
    else update(idx<<1|1,mid+1,e,pos,flag);

    for(int i=0;i<5;i++) sum[idx][i]=sum[idx<<1][i]+sum[idx<<1|1][i-num[idx<<1]%5>=0?i-num[idx<<1]%5:i-num[idx<<1]%5+5];//更新对应区间内下标为模5等于i数的和
}

long long query(int idx,int s,int e,int mod)
{
    return sum[idx<<1][mod]+sum[idx<<1|1][mod-num[idx<<1]%5>=0?mod-num[idx<<1]%5:mod-num[idx<<1]%5+5];
}

int main()
{
    int i;
    long long ans;

    while(~scanf("%d",&n))
    {
        map<int,int>mp;//用于离散化过程中判重,有可能存在先add a,再del a,再 add a的情况

        for(i=0;i<n;i++)
        {
            scanf("%s",e[i].op);

            if(e[i].op[0]=='s') e[i].x=0;
            else scanf("%d",&e[i].x);

            e[i].id=i;
        }

        sort(e,e+n,cmpval);

        cnt=1;

        for(i=0;i<n;i++)//离散化
        {
            if(e[i].op[0]=='a')
            {
                if(!mp[e[i].x]) mp[e[i].x]=cnt++;

                val[mp[e[i].x]]=e[i].x;
                e[i].x=mp[e[i].x];

            }
            else if(e[i].op[0]=='d')
            {
                e[i].x=mp[e[i].x];
            }
        }

        sort(e,e+n,cmpid);

        cnt--;

        if(!cnt)//特判
        {
            for(i=0;i<n;i++)
            {
                if(e[i].op[0]=='s')
                {
                    printf("0\n");
                }
            }

            continue;
        }

        build(1,1,cnt);

        for(i=0;i<n;i++)
        {
            if(e[i].op[0]=='a') update(1,1,cnt,e[i].x,1);
            else if(e[i].op[0]=='d') update(1,1,cnt,e[i].x,-1);
            else printf("%I64d\n",query(1,1,n,3));
        }
    }
}


### 关于HDU - 6609 的题目解析 由于当前未提供具体关于 HDU - 6609 题目的详细描述,以下是基于一般算法竞赛题型可能涉及的内容进行推测和解答。 #### 可能的题目背景 假设该题目属于动态规划类问题(类似于多重背包问题),其核心在于优化资源分配或路径选择。此类问题通常会给出一组物品及其属性(如重量、价值等)以及约束条件(如容量限制)。目标是最优地选取某些物品使得满足特定的目标函数[^2]。 #### 动态转移方程设计 如果此题确实是一个变种的背包问题,则可以采用如下状态定义方法: 设 `dp[i][j]` 表示前 i 种物品,在某种条件下达到 j 值时的最大收益或者最小代价。对于每一种新加入考虑范围内的物体 k ,更新规则可能是这样的形式: ```python for i in range(n): for s in range(V, w[k]-1, -1): dp[s] = max(dp[s], dp[s-w[k]] + v[k]) ``` 这里需要注意边界情况处理以及初始化设置合理值来保证计算准确性。 另外还有一种可能性就是它涉及到组合数学方面知识或者是图论最短路等相关知识点。如果是后者的话那么就需要构建相应的邻接表表示图形结构并通过Dijkstra/Bellman-Ford/Floyd-Warshall等经典算法求解两点间距离等问题了[^4]。 最后按照输出格式要求打印结果字符串"Case #X: Y"[^3]。 #### 示例代码片段 下面展示了一个简单的伪代码框架用于解决上述提到类型的DP问题: ```python def solve(): t=int(input()) res=[] cas=1 while(t>0): n,k=list(map(int,input().split())) # Initialize your data structures here ans=find_min_unhappiness() # Implement function find_min_unhappiness() res.append(f'Case #{cas}: {round(ans)}') cas+=1 t-=1 print("\n".join(res)) solve() ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值