hdu 4288 Coder (线段树+离线)
题意:
刚开始有一个空集合。有三种操作:
1.往集合中加入一个集合中不存在的数 x
2.从集合中删除一个已经存在的数 x
3.计算集合的digest sum并输出。 digest sum求法:将集合中所有数从小到大排序,得到a1<a2<...<an。 digest sum = sum(ai) where i mod 5 = 3
数据范围:
N ( 1 <= N <= 105 )
1 <= x <= 109.
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.
思路:
先将所有增加过的,删除过的数存下来,去重,搞成线段树。
每个结点(区间)里有两个东西,一个是 cnt,记录该区间的在集合中的元素的个数。一个是sum[i] , sum[i]:该区间序号%5等于i的存在于集合中的元素的总和。
插入和删除时维护即可。
代码:
int const maxn=1e5+5; struct node{
int cnt;
ll sum[5];
}tree[maxn<<2]; int n,tot;
int q[maxn],a[maxn];
char ope[maxn][15]; void build(int l,int r,int rt){
tree[rt].cnt=0;
memset(tree[rt].sum,0,sizeof(tree[rt].sum));
if(l==r) return;
int m=(l+r)>>1;
build(lson);
build(rson);
} void pushUp(int rt){
rep(i,0,4)
tree[rt].sum[i]=tree[rt<<1].sum[i]+tree[rt<<1|1].sum[((5-tree[rt<<1].cnt%5)%5+i)%5];
}
void update(int k,int pos,int num,int l,int r,int rt){
tree[rt].cnt+=k;
if(l==r){
tree[rt].sum[0]+=(k*num);
return;
}
int m=(l+r)>>1;
if(pos<=m)
update(k,pos,num,lson);
else
update(k,pos,num,rson);
pushUp(rt);
} int main(){
//freopen("test.in","r", stdin);
while(scanf("%d",&n)!=EOF){
tot=0;
rep(i,1,n){
scanf("%s",ope[i]);
if(ope[i][0]!='s'){
scanf("%d",&q[i]);
a[tot++]=q[i];
}
}
sort(a,a+tot);
tot=unique(a,a+tot)-a;
if(tot==0)
memset(tree[1].sum,0,sizeof(tree[1].sum));
else
build(1,tot,1);
rep(i,1,n){
int pos=lower_bound(a,a+tot,q[i])-a+1;
if(ope[i][0]=='a'){
update(1,pos,q[i],1,tot,1);
continue;
}
if(ope[i][0]=='d'){
update(-1,pos,q[i],1,tot,1);
continue;
}
printf("%I64d\n",tree[1].sum[2]);
}
}
//fclose(stdin);
}
hdu 4288 Coder (线段树+离线)的更多相关文章
- HDU 4288 Coder(线段树)
题意: 给定三种操作 1. add x 向序列中添加x,添加之后序列还保持有序 2. del x 删除序列中值为x的元素 3. sum 求下边模5等于3的元素和 思路: 直接暴力也可以过,就是看暴 ...
- HDU 4638-Group(线段树+离线处理)
题意: 给n个编号,m个查询每个查询l,r,求下标区间[l,r]中能分成标号连续的组数(一组内的标号是连续的) 分析: 我们认为初始,每个标号为一个组(线段树维护区间组数),从左向右扫序列,当前标号, ...
- HDU 4417 【线段树+离线处理】
http://acm.hdu.edu.cn/showproblem.php?pid=4417 题意:找出给定区间内,有多少个数小于等于给定的数.用线段树维护的话会超时,要用到线段树的离线操作,对询问与 ...
- HDU - 3874 Necklace (线段树 + 离线处理)
欢迎參加--每周六晚的BestCoder(有米! ) Necklace Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 65536/3 ...
- HDU 3333 & 3874 (线段树+离线询问)
两个题目都是求区间之内,不重复的数字之和,3333需要离散化处理................. 调试了一下午........说多了都是泪........... #include <iostr ...
- Necklace HDU - 3874 (线段树/树状数组 + 离线处理)
Necklace HDU - 3874 Mery has a beautiful necklace. The necklace is made up of N magic balls. Each b ...
- hdu 4031 attack 线段树区间更新
Attack Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)Total Subm ...
- hdu 3016 dp+线段树
Man Down Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...
- 线段树+离线 hdu5654 xiaoxin and his watermelon candy
传送门:点击打开链接 题意:一个三元组假设满足j=i+1,k=j+1,ai<=aj<=ak,那么就好的.如今告诉你序列.然后Q次询问.每次询问一个区间[l,r],问区间里有多少个三元组满足 ...
随机推荐
- pymysql基础教程
pymysql基础教程 1.下载pymysql 在命令框输入指令即可 pip install pymysql 2.连接pymysql 连接数据库: import pymysql conn = pymy ...
- [Python]爬虫获取知乎某个问题下所有图片并去除水印
获取URL 进入某个知乎问题的主页下,按F12打开开发者工具后查看network面板. network面板可以查看页面向服务器请求的资源.资源的大小.加载资源花费的时间以及哪些资源加载失败等信息.还可 ...
- python BeautifulSoup html解析
* BeautifulSoup 的.find(), .findAll() 函数原型 findAll(tag, attributes, recursive, text, limit, keywords) ...
- AT4353-[ARC101D]Robots and Exits【LIS】
正题 题目链接:https://www.luogu.com.cn/problem/AT4353 题目大意 数轴上有\(n\)个球\(m\)个洞,每次可以将所有球左移或者右移,球到洞的位置会掉下去. 求 ...
- k8s负载资源StatefulSet工作解析
在k8s中工作负载资源StatefulSet用于管理有状态应用. 什么是无状态? 组成一个应用的pod是对等的,它们之前没有关联和依赖关系,不依赖外部存储. 即我们上篇小作文中deployment创建 ...
- PYTHON django 关于时间转换
在安装django.默认会pytz时区库,import pytzpytz.timezone("UTC")now.astimezone("要转换的aware类型" ...
- Python自动化测试发送邮件太麻烦?!一起聊一聊 Python 发送邮件的3种方式
1. 前言 发送邮件,我们在平时工作中经用到,做为测试人员,在自动化测试中用的也比较多,需要发送邮件给某领导 SMTP是Python默认的邮件模块,可以发送纯文本.富文本.HTML 等格式的邮件 今天 ...
- == 和 equals区别
== equals是两种字符串的方式 区别 == 是比较两个对象的引用地址值 equals是比较两个对象的具体内容 示例 package com.oop.demo06; public class De ...
- Python标准库模块之heapq – 堆构造
Python标准库模块之heapq – 堆构造 读前福利:几百本经典书籍https://www.johngo689.com/2158/ 原文链接:https://www.johngo689.com/2 ...
- 无法解析的外部符号"void_cdecl caffe::caffe_gpu_dot<double>(int,double........)"
将源码中的.cu文件添加到项目中即可,即使创建的就是NVIDIA的项目,也需要把这些个.cu文件添加进来