HDU4288 Coder(线段树)
注意添加到集合中的数是升序的,先将数据读入,再离散化。
sum[rt][i]表示此节点的区域位置对5取模为i的数的和,删除一个数则右边的数循环左移一位,添加一个数则右边数循环右移一位,相当于循环左移4位,线段树与树状数组结合,树状数组确定位置。
le[rt]表示左移的位数,区间更新懒惰标记
#include <iostream>
#include <cstdio>
#include<cstdlib>
#include<map>
#include<algorithm>
#include<cstring>
using namespace std;
const int N = 100008;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
typedef long long LL; int C[N];//注意初始化n
inline int lowbit(int x){
return x&-x;
}
void add(int x, int val, int n){//将第x个数增加val,从1计数
for(int i=x;i<=n;i+=lowbit(i)){
C[i] += val;
}
}
int getsum(int x){//求1到x的和
int ret = 0;
for(int i=x;i>0;i-=lowbit(i)){
ret+=C[i];
}
return ret;
} LL sum[N<<2][5];
int le[N<<2];
LL a[N], b[N]; void PushDown(int l, int r, int rt){
le[rt] %= 5;
if(le[rt]){
if(l < r){
le[rt<<1] = (le[rt] + le[rt<<1]) % 5;
le[rt<<1|1] = (le[rt] + le[rt<<1 | 1]) % 5;
} int t = le[rt];
while(t--){
LL tp = sum[rt][0];
for(int i = 0; i <= 3; i++){
sum[rt][i] = sum[rt][i + 1];
}
sum[rt][4] = tp;
} le[rt] = 0;
}
} void PushUp(int l, int r, int rt){
if(l < r){
int m = (l + r)>>1;
if(le[rt<<1]){//注意
PushDown(lson);
}
if(le[rt << 1 |1]){
PushDown(rson);
}
for(int i = 0; i < 5; i++){
sum[rt][i] = sum[rt <<1 ][i] + sum[rt<<1|1][i];
}
}
}
void build(int l,int r,int rt){
if(l == r){
for(int i = 0 ; i < 5; i++){
sum[rt][i] = 0;
}
le[rt] = 0;
return ;
}
int m = (l + r)>>1;
build(lson);
build(rson);
PushUp(l, r, rt);
} void update(int x, int rdx, int val, int l, int r, int rt){\
PushDown(l, r, rt);
if(l == r){
if(rdx == -1){
for(int i = 0; i < 5; i++){
sum[rt][i] = 0;
}
}else{
sum[rt][rdx] = val;
}
le[rt] = 0; }else{ int m = (l + r)>>1;
if(x <= m){
update(x, rdx, val, lson);
}else{
update(x, rdx, val, rson);
} }
PushUp(l, r, rt);
} void shift(int p, int a, int b, int l , int r, int rt){
PushDown(l ,r , rt);
if(a <= l && b >= r){
le[rt] += p;
PushDown(l, r, rt);
}else{ int m = (l + r)>>1;
if(a <= m){
shift(p, a, b, lson);
}
if(b > m){
shift(p, a, b, rson);
} }
PushUp(l, r, rt);
} int main(){
int n;
while(~scanf("%d", &n)){
memset(C, 0, sizeof(int) * (n + 2));
char str[10]; int cnt = 0;
for(int i = 1; i <= n; i++){
scanf("%s", str);
LL x;
if(str[0] == 'a'){
scanf("%I64d", &x);
a[i] = x << 1|1;
b[cnt++] = x;
}else if(str[0] == 'd'){
scanf("%I64d", &x);
a[i] = x << 1;
}else{
a[i] = -1;
}
}
sort(b, b + cnt);
cnt = unique(b, b + cnt) - b;
build(1, cnt, 1);
for(int i = 1; i <= n; i++){
if(a[i] == -1){
PushDown(1, cnt, 1);
printf("%I64d\n", sum[1][3]);
}else if(a[i] & 1){
a[i] >>= 1;
int pos= lower_bound(b, b + cnt, a[i]) - b + 1;
int rdx = getsum(pos) + 1;
add(pos, 1, cnt); update(pos, rdx % 5, a[i], 1, cnt, 1);
if(pos + 1 <= cnt){
shift(4, pos + 1, cnt, 1, cnt, 1);
}
}else{
a[i] >>= 1;
int pos= lower_bound(b, b + cnt, a[i]) - b + 1;
add(pos, -1, cnt);
update(pos, -1, 0, 1, cnt, 1);
shift(1, pos +1, cnt, 1, cnt, 1);
}
}
}
return 0;
}
为什么我线段树总是写不好,总是要好长时间的debug……
HDU4288 Coder(线段树)的更多相关文章
- HDU4288:Coder(线段树单点更新版 && 暴力版)
Problem Description In mathematics and computer science, an algorithm describes a set of procedures ...
- hdu4428(Coder)线段树
Coder Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- HDU 4288 Coder(线段树)
题意: 给定三种操作 1. add x 向序列中添加x,添加之后序列还保持有序 2. del x 删除序列中值为x的元素 3. sum 求下边模5等于3的元素和 思路: 直接暴力也可以过,就是看暴 ...
- hdu 4288 Coder (线段树+离线)
题意: 刚开始有一个空集合.有三种操作: 1.往集合中加入一个集合中不存在的数 x 2.从集合中删除一个已经存在的数 x 3.计算集合的digest sum并输出. digest sum求 ...
- HDU 4288 Coder (线段树)
Coder 题目:http://acm.hdu.edu.cn/showproblem.php?pid=4288 题意:有三种类型的操作,(1)."add x",表示往集合里加入�数 ...
- 线段树(多棵) HDOJ 4288 Coder
题目传送门 题意:集合,add x, del x, 求和 分析:首先,暴力可以过这题.用上线段树能大大降低时间的消耗,具体就是类似开了5棵线段树,每个节点都有5个空间,表示该区间的id%5后的和,区间 ...
- HDU 4288 Coder 【线段树+离线处理+离散化】
题意略. 离线处理,离散化.然后就是简单的线段树了.需要根据mod 5的值来维护.具体看代码了. /* 线段树+离散化+离线处理 */ #include <cstdio> #include ...
- hdu4288 Coder(段树+分离)
主题链接: huangjing 题意: 题目中给了三个操作 1:add x 就是把x插进去 2:delete x 就是把x删除 3:sum 就是求下标%5=3的元素的和. 另一个条件是插入和删除最后 ...
- HDU 4288 Coder ( 离散化 + 离线 + 线段树 )
这题跟ZOJ 3606的解题思路很相似. 题意:有3中操作:1.向集合中增加一个数x(1≤x≤1e9):2.从集合中删去一个数x(保证这个数存在):3.查询集合中所有位置满足i%5==3的数a[i]的 ...
随机推荐
- Python中sorted()方法
Python中sorted()方法的用法 1.先说一下iterable,中文意思是迭代器. Python的帮助文档中对iterable的解释是:iteralbe指的是能够一次返回它的一个成员的对象.i ...
- mysql 表关联查询报错 ERROR 1267 (HY000)
解决翻案:http://stackoverflow.com/questions/1008287/illegal-mix-of-collations-mysql-error 即: SET collati ...
- intellij Idea快捷键
CTRL+ALT+O 优化导入的类和包 Alt + Center 导入类,实现接口 CTRL+N 查找类CTRL+SHIFT+N 查找文件CTRL+SHIFT+ALT+N 查找类中的方法或变 ...
- 项目中Ajax调用ashx页面中的Function的实战
前台页面: 使用几个display=none的空间存储DropdownList中的值,点击Search Button后刷新页面再次给DropdownList赋值使用 <%@ Page Langu ...
- ssh免密码登陆设置
服务器端 CentOS 6.5下编辑/etc/ssh/sshd_config MacOSx下编辑/etc/sshd_config #开启公钥验证 RSAAuthentication yes Pubke ...
- Nginx如何设置拒绝或允许指定ip访问
location ~ /druid/ { #deny 192.168.1.1; allow 192.168.1.1; deny all; proxy_pass http://127.0.0.1:808 ...
- DELPHI控件:DBLookupComboBOX组件的使用方法
在许多数据表中,数据是以代码方式存放的,如在班级编码数据表tB03(表5.5)中,系部字段TB0309采用编码方式存放,系部真实名称则存放在系部编码表TB06.使用代码的好处是,用户可在编码表TB06 ...
- codeforces 515B. Drazil and His Happy Friends 解题报告
题目链接:http://codeforces.com/problemset/problem/515/B 题目意思:有 n 个 boy 和 m 个 girl,有 b 个 boy 和 g 个 girl ( ...
- 谷歌 Uncaught SecurityError: Failed to execute 'replaceState' on 'History 错误
今天在用sui mobil做一个内联页面的时候遇到了这个问题. 然而这个问题只出现在chrome浏览器中,在火狐中没有一点问题. 他说明的是一个安全问题,chrome中有了新的安全机制
- js中sort()方法的用法,参数以及排序原理
sort() 方法用于对数组的元素进行排序. 语法:arrayObject.sort(sortby):参数sortby可选.规定排序顺序.必须是函数. 注:如果调用该方法时没有使用参数,将按字母顺序对 ...