Coder

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

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
线段树:
区间一个维护该集合的个数,一个维护模5之后该集合对应1,2,3,4,0即sum[0,1,2,3,4](一一对应记录)的和。当添加一个数时,对应区间更新之后再向上更新。更新父区间时,等于左区间的Lsum[0,1,2,3,4]+右区间的Rsum[0,1,2,3,4]加上左区间个数来说。比如左区间有num个数,假设num%5 = 3,则sum[0] = Lsum[0] + Rsum[2]因为右区间的第三个数相当于整个左右区间模5余1。
sum[1] = Lsum[1] + Rsum[3],
sum[2] = Lsum[2] + Rsum[4],
sum[3] = Lsum[3] + Rsum[0],
sum[4] = Lsum[4] + Rsum[1];
即:sum[i] = Lsum[i] + Rsum[(((i - (num%5)) % 5 + 5) % 5]
 #include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iomanip>
#include <set>
#include <map>
#include <vector>
#include <queue>
#define N 100005
#define llt long long int
using namespace std; int num[N << ];//记录区间的个数
llt segtree[N << ][];//
struct node
{
int x;
char s[];
}a[N];
int b[N];
map<int, int> mp;//使用map离散化
void build(int l, int r, int p)
{
memset(segtree[p], , sizeof(segtree[p]));
num[p] = ;
if (l < r)
{
int mid = (l + r) >> , pp = p << ;
build(l, mid, pp);
build(mid + , r, pp + );
}
}
void update(int l, int r, int p, int pos, int v)
{
if (pos == l && pos == r)
{
segtree[p][] += 1ll * v;
num[p] = v > ? : ;
return;
}
int mid = (l + r) >> , pp = p << ;
if (mid >= pos)
update(l, mid, pp, pos, v);
else
update(mid + , r, pp + , pos, v);
for (int i = ; i < ; i++)
segtree[p][i] = segtree[pp][i] + segtree[pp + ][((i - num[pp]) % + ) % ];
num[p] = num[pp] + num[pp + ];
}
int main()
{
int n, i, k;
while (~scanf("%d", &n))
{
k = ;
mp.clear();
for (i = ; i <= n; i++)
{
scanf("%s", a[i].s);
if (a[i].s[] == 'a')
{
scanf("%d", &a[i].x);
b[k++] = a[i].x;
}
else
if (a[i].s[] == 'd')
{
scanf("%d", &a[i].x);
a[i].x = -a[i].x;
}
}
build(, k - , );
sort(b + , b + k);
for (i = ; i < k; i++)
mp[b[i]] = i;//重新编号
for (i = ; i <= n; i++)
{
if (a[i].s[] == 's')
printf("%I64d\n", segtree[][]);
else
update(, k - , , mp[abs(a[i].x)], a[i].x);
}
}
return ;
}

hdu4428(Coder)线段树的更多相关文章

  1. HDU4288:Coder(线段树单点更新版 && 暴力版)

    Problem Description In mathematics and computer science, an algorithm describes a set of procedures ...

  2. HDU4288 Coder(线段树)

    注意添加到集合中的数是升序的,先将数据读入,再离散化. sum[rt][i]表示此节点的区域位置对5取模为i的数的和,删除一个数则右边的数循环左移一位,添加一个数则右边数循环右移一位,相当于循环左移4 ...

  3. HDU 4288 Coder(线段树)

    题意: 给定三种操作 1. add x 向序列中添加x,添加之后序列还保持有序 2. del x  删除序列中值为x的元素 3. sum  求下边模5等于3的元素和 思路: 直接暴力也可以过,就是看暴 ...

  4. hdu 4288 Coder (线段树+离线)

    题意: 刚开始有一个空集合.有三种操作: 1.往集合中加入一个集合中不存在的数 x 2.从集合中删除一个已经存在的数 x 3.计算集合的digest sum并输出.       digest sum求 ...

  5. HDU 4288 Coder (线段树)

    Coder 题目:http://acm.hdu.edu.cn/showproblem.php?pid=4288 题意:有三种类型的操作,(1)."add x",表示往集合里加入�数 ...

  6. 线段树(多棵) HDOJ 4288 Coder

    题目传送门 题意:集合,add x, del x, 求和 分析:首先,暴力可以过这题.用上线段树能大大降低时间的消耗,具体就是类似开了5棵线段树,每个节点都有5个空间,表示该区间的id%5后的和,区间 ...

  7. HDU 4288 Coder 【线段树+离线处理+离散化】

    题意略. 离线处理,离散化.然后就是简单的线段树了.需要根据mod 5的值来维护.具体看代码了. /* 线段树+离散化+离线处理 */ #include <cstdio> #include ...

  8. HDU 4288 Coder ( 离散化 + 离线 + 线段树 )

    这题跟ZOJ 3606的解题思路很相似. 题意:有3中操作:1.向集合中增加一个数x(1≤x≤1e9):2.从集合中删去一个数x(保证这个数存在):3.查询集合中所有位置满足i%5==3的数a[i]的 ...

  9. Coder(线段树)

    求一部分和的线段树,因为是对5取余,所以给定一段区间a-b,假设其位置会有变化,最多会有5种和,那么就可以保留这五种和,在用lz进行延迟标记时,保存位置变化了多少也就知道了该从当前和转到哪一个和. 当 ...

随机推荐

  1. 洛谷P2221 [HAOI2012]高速公路(线段树+概率期望)

    传送门 首先,答案等于$$ans=\sum_{i=l}^r\sum_{j=i}^r\frac{sum(i,j)}{C_{r-l+1}^2}$$ 也就是说所有情况的和除以总的情况数 因为这是一条链,我们 ...

  2. [App Store Connect帮助]七、在 App Store 上发行(3.2)提交至“App 审核”:查看 App 状态历史记录

    您可以查看您 App 的某一版本的 App 状态历史记录.在历史记录表中的每一行都包含 App 状态.App 状态更改时间,以及更改的发起人.使用此信息追踪“App 审核”流程中的 App. 若想在 ...

  3. Mysql根据数据库的时间字段到点更新另外的状态。

    转载:https://blog.csdn.net/xingfuzhijianxia/article/details/53727820 需求如下: 添加一条提醒,被提醒人在提醒时间未到达的时候收不到此提 ...

  4. Luogu P1073 最优贸易【最短路/建反图】 By cellur925

    题目传送门 这么经典的题目,还是看了lyd的题解....唉难过. 一句话题意:在一张点有全都的图上找一条从1到n的路径,存在两个点p,q(p<q),使val[q]-val[p]最大. 给出的图是 ...

  5. 《开源自主OdooERP部署架构指南》试读:第二章数据库服务构建

    文/开源智造联合创始人老杨 本文来自<开源自主OdooERP部署架构指南>的试读章节.书籍尚未出版,请勿转载.欢迎您反馈阅读意见. 使用apt.postgresql.org 您可以选择使用 ...

  6. tableView 加载更多

    在ios开中中,由于屏幕尺寸限制,如果需要显示的数据很多,需要用到分页加载. 原理:先数据放到一个table中,先显示10条,table底部有一察看更多选项,点击察看更多查看解析的剩余数据.基本上就是 ...

  7. LoadRunner12学习之路(1-5)

    本次LoadRunner12学习用户指南,学习周期预计3天,每天学习1-2单元内容! 2017.12.17 一.使用HPE Web Tours示例应用程序 本教程使用 HPE Web Tours(一个 ...

  8. java的构造方法 this 重载

    this1.隐含的局部变量在方法中指向调用该方法的对象()使用:当成员变量与局部变量同名的时候,通过this说明哪一个是成员变量.(this指向的是成员变量) 2.作为当前类的构造方法名存在作用:在构 ...

  9. 总结用CoreText绘制文本时遇到的问题以及解决办法

    关于CoreText不做解释.用的人自然知道这个是干什么的. 功能非常强大,可以绘制文本,图片等. 这次用的Xcode7.0的版本.所以之前很多方法,现在不能用.也不是不能用,就是有黄色警告很不爽. ...

  10. 支付宝添加scheme的方法

    点击项目名称,点击“Info”选项卡,在“URL Types”选项中,点击“+”,在“URL Schemes”中输入“myAlipay”.“myAlipay”来自于文件“APViewControlle ...