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],问区间里有多少个三元组满足 ...
随机推荐
- 【C++基础教程】第五课
上次的作业答案,非常简单. 第一题: 我们需要知道,字符(char类型)在计算机中存储的时候,是把这个字符对应的代码(专业术语叫做编码)进行存储.例如,换行符'\n'的代码就是10,'0'对应的代码就 ...
- ThinkPHP5通过composer安装Workerman安装失败问题
报错: topthink/think-worker v3.0.2 requires topthink/framework ^6 https://blog.csdn.net/Douz_lungfish/ ...
- ecshop调用商品的购买次数方法
这时候我们修改一下 写成一个函数放到lib_goods.php 这样就可以随便调用了 --------------------------------------------------------- ...
- Linux系列(32) - rpm命令管理之RPM查询(4)
RPM包默认安装位置 RPM包默认安装路径 /etc/ 配置文件安装目录 /usr/bin/ 可执行的命令安装目录 /usr/lib/ 程序所使用的函数库保存位置 /usr/share/doc/ 基本 ...
- requests接口自动化-excel参数化
在数据量大的时候,需要使用文件参数化. excel文件是其中一种. 安装xlrd读取excel文件.(这里是在pycharm安装) 发现选择豆瓣安装失败,阿里云安装成功. 准备excel文件,放在te ...
- phpQuery
以下资料均来自官方文档,官方文档地址:https://code.google.com/archive/p/phpquery/wikis 基础 示例 phpQuery::newDocumentFileX ...
- nginx负载轮询
下面是一个可以使用nginx负载轮询,如果有一台服务器连接不通,返404,500,502,503,504,会自动切换到下一台服务器 upstream www { server 111.111.111. ...
- 虚拟机安装配置centos7
安装 https://blog.csdn.net/babyxue/article/details/80970526 主机环境预设 更换国内yum源 epel源 https://www.cnblogs. ...
- AT2390-[AGC016F]Games on DAG【状压dp,SG函数】
正题 题目链接:https://www.luogu.com.cn/problem/AT2390 解题思路 \(n\)个点的\(DAG\),\(m\)条边可有可无,\(1\)和\(2\)上有石头.求有多 ...
- Python3入门系列之-----看完这一篇文章我终于学会了类
前言 类顾名思义,就是一类事物.或者叫做实例,它用来描述具有共同特征的一类事物.我们在Python中声明类的关键词是class,类还有功能和属性,属性就是这类事物的特征,而功能就是它能做什么,也是就是 ...