Coder



题意:有三种类型的操作,(1)."add x",表示往集合里加入�数x。(2).“del x”表示将集合中数x删除。(3).“sum”求出从小到大排列的集合中下标模5为3的数的和。集合中的数都是唯一的。

思路:这题巧妙的地方在于先离线输入,然后离散化。输入的数字依照从小到大排序,然后作为线段树的叶子结点。每一个结点包括两个部分,一是该结点包括的数字个数,二是依照区间内模5的余数分组求和。当须要向上pushup时,左子树的区间不变,而右子树的区间内,每一个数字的位置发生改变,一開始为i,之后变为i+cnt(cnt表示左子树区间内数字个数)

代码:
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<cmath>
#include<cstdio>
#include<vector>
#include<string>
#include<fstream>
#include<cstring>
#include<ctype.h>
#include<iostream>
#include<algorithm>
#define INF (1<<30)
#define PI acos(-1.0)
#define mem(a, b) memset(a, b, sizeof(a))
#define rep(i, n) for (int i = 0; i < n; i++)
#define debug puts("===============")
typedef long long ll;
using namespace std;
const int maxn = 100200;
ll sum[maxn << 2][5];
int cnt[maxn << 2];
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
int n, tot, op[maxn], a[maxn];
char str[maxn][10];
void pushup(int rt) {
cnt[rt] = cnt[rt << 1] + cnt[rt << 1 | 1];
int p = cnt[rt << 1];
for (int i = 0; i < 5; i++) {
sum[rt][i] = sum[rt << 1][i] + sum[rt << 1 | 1][((i - p) % 5 + 5) % 5];
}
//cout<<rt<<" "<<cnt[rt]<<endl;
}
void update(int pos, int x, int l, int r, int rt) {
if (l == r) {
if (x == 1) {
sum[rt][1] = a[pos - 1];
cnt[rt] = 1;
} else {
sum[rt][1] = 0;
cnt[rt] = 0;
}
return ;
}
int m = (l + r) >> 1;
if (pos <= m) update(pos, x, lson);
else update(pos, x, rson);
pushup(rt);
}
void build(int l, int r, int rt) {
for (int i = 0; i < 5; i++) sum[rt][i] = 0;
cnt[rt] = 0;
if (l == r) return ;
int m = (l + r) >> 1;
build(lson);
build(rson);
}
int main () {
while(~scanf("%d", &n)) {
tot = 0;
for (int i = 0; i < n; i++) {
scanf("%s", str[i]);
if (str[i][0] != 's') {
scanf("%d", op + i);
a[tot++] = op[i];
}
}
sort(a, a + tot);
tot = unique(a, a + tot) - a;
build(1, tot, 1);
for (int i = 0; i < n; i++) {
if (str[i][0] == 's') printf("%I64d\n", sum[1][3]);
else {
int pos = lower_bound(a, a + tot, op[i]) - a + 1;
if (str[i][0] == 'a') update(pos, 1, 1, tot, 1);
else update(pos, -1, 1, tot, 1);
}
}
}
return 0;
}<strong>
</strong>

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

  1. HDU 4288 Coder(线段树)

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

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

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

  3. hdu 4288 离线线段树+间隔求和

    Coder Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Su ...

  4. hdu 4031 attack 线段树区间更新

    Attack Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)Total Subm ...

  5. hdu 3016 dp+线段树

    Man Down Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total S ...

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

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

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

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

  8. HDU 5877 dfs+ 线段树(或+树状树组)

    1.HDU 5877  Weak Pair 2.总结:有多种做法,这里写了dfs+线段树(或+树状树组),还可用主席树或平衡树,但还不会这两个 3.思路:利用dfs遍历子节点,同时对于每个子节点au, ...

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

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

随机推荐

  1. Delphi 中 FindWindow 和 FindWindowEx 找到外部进程,然后发送消息(比如最大化)

    FindWindow(lpClassName,        {窗口的类名}lpWindowName: PChar {窗口的标题}): HWND;              {返回窗口的句柄; 失败返 ...

  2. 如何解决ORA-12547: TNS:lost contact错

    执行环境:ubuntu+oracle 11.2.0 为了启动oracle时间,出现ORA-12547: TNS:lost contact错误. 中午好好的纳,下午就无论了.以为是链接失效,关机重新启动 ...

  3. HDU 4704 Sum (费马定理+快速幂)

    Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Subm ...

  4. PHP操作Mysql中间BLOB场

    1.MySQL在BLOB字段类型 BLOB场的类型用于存储二进制数据. MySQL在.BLOB它是一种类型的一系列.含有:TinyBlob.Blob.MediumBlob.LongBlob.大小上不同 ...

  5. 零基Android手机嵌入式开发培训课程

    亲爱的朋友,我这里有一组当然想和大家分享,假设有兴趣在这个过程中,可以加我QQ2059055336和我联系. 课程章节分布: 第一部分 嵌入式C与数据结构篇 (20讲) 第二部分ARM裸机开发篇 (4 ...

  6. 【Android小应用】颈椎保健操Android开源项目

    前段时间在知乎上回答已入 IT 行业的前辈,有哪些关于保护身体健康的知识分享给 IT 新人? ,回复了一张图片,评论里面有知友希望通过程序可以实现,我后面尝试着通过程序实现了效果,现开源出来,大家可以 ...

  7. Erlangserver紧内存优化解决方案

    提出的问题:server100万人在线,16G内存快被吃光. 玩家进程占用内存偏高 解决方法: 第一步: erlang:system_info(process_count). 查看进程数目是否正常,是 ...

  8. 2014/08/23——OJ出现waiting...

    问题: 今天中午,裴主解决OJ他缓慢的问题后,开着.我跟着oj他递给发现了一个话题waiting该..... 和全哥.均觉得測评程序挂了.于是重新启动測系统,还waiting.....(測评系统的进程 ...

  9. 二、第一个ExtJS程序:helloExtJS

    开发前的准备 下载并解压ExtJS包后,能够得到下图的文件文件夹结构: 在实际开发过程中并不须要全部的文件和文件夹,所需的包括例如以下文件夹就可以: 若使用eclipse进行开发,仅仅需将上述文件拷贝 ...

  10. UVA 11090 - Going in Cycle!!(Bellman-Ford)

    UVA 11090 - Going in Cycle!! option=com_onlinejudge&Itemid=8&page=show_problem&category= ...