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. ASP.NET CORE 使用 EF CORE访问数据库

    asp.net core通过ef core来访问数据库,这里用的是代码优先,通过迁移来同步数据库与模型. 环境:vs2017,win10,asp.net core 2.1 一.从建立asp.net c ...

  2. 转】Neo4j集群安装实践

    原博文出自于: http://blog.fens.me/category/%E6%95%B0%E6%8D%AE%E5%BA%93/page/2/ 感谢! Posted: Oct 29, 2013 Ta ...

  3. easy ui diglog 点击关闭,触发事件

    $('#dialogDiv').dialog({ onClose:function(){ alert('11111111') ; }});

  4. [转]Azure 表存储和 Windows Azure SQL Database - 比较与对照

    本文转自:https://msdn.microsoft.com/library/azure/jj553018 更新时间: 2014年10月 作者:Valery Mizonov 和 Seth Manhe ...

  5. [书目20150303]软件工程的本质:运用SEMAT内核

    译者序Robert Martin作序Bertrand Meyer作序Richard Soley作序前言致谢第一部分   内核思想解释第1章   简要介绍如何使用内核1.1   为什么开发优秀软件具有很 ...

  6. [转]符号和运算符参考 (F#)

    本文转自:http://msdn.microsoft.com/zh-cn/library/dd233228.aspx 本主题包含一个表,其中列出了 F# 语言中使用的符号和运算符. 符号和运算符表   ...

  7. 可变类型的安全性——更锋利的C#代码小记(2)

    ReadOnlyCollection类型是.NET系统类库提供的一个只读集合类型,它与原来的List<string>不存在任何类型转换关系,因此可以从根本上阻止外部对其的修改操作using ...

  8. Java多线程——线程的创建方式

    Java多线程——线程的创建方式 摘要:本文主要学习了线程的创建方式,线程的常用属性和方法,以及线程的几个基本状态. 部分内容来自以下博客: https://www.cnblogs.com/dolph ...

  9. Tomcat源码分析----eclipse中搭建源码环境

    前提:JDK,至少1.7,ant,要设置ANT_HOME环境变量,需要再classpath中增加ant的lib目录,在path变量中增加ant的bin目录 1.官网下载tomcat源码包:apache ...

  10. addslashes,stripslashes

    官方介绍: (PHP 4, PHP 5) addslashes — 使用反斜线引用字符串 返回字符串,该字符串为了数据库查询语句等的需要在某些字符前加上了反斜线.这些字符是单引号(’).双引号(”). ...