CF85D Sum of Medians

题意翻译

一个集合,初始为空。现有三个操作:

1.add:向集合里加入数x,保证加入前集合中没有数x;

2.del:从集合中删除数x,保证删除前集合中有x;

3.sum:询问将集合里的数从小到大排序后,求下标i模5余3的数的和。

现有n次操作,对于每个查询操作,输出答案


题解Here!

一开始感觉好不可做啊。。。

然后发现,线段树好像可以搞一搞。

线段树每个节点维护$5$个值,即区间中所有$\text{下标}\mod5$后结果相同的位置的值的和。

即:在区间$[l,r]$上维护:

$$\sum_{i=l}^rv_i[i\mod 5==0],\sum_{i=l}^rv_i[i\mod 5==1],\sum_{i=l}^rv_i[i\mod 5==2],\sum_{i=l}^rv_i[i\mod 5==3],\sum_{i=l}^rv_i[i\mod 5==4]$$

再维护区间中有多少个值$num$。

合并的时候左子树不动,右子树中所有$\text{下标}\mod5==x$的位置应该是$((i-num)\%5+5)\%5$。

至于线段树怎么动态加点。。。

其实离线一下就可以把线段树搞成静态,然后离散化一下就好。

记得开$long\ long$。

还与就是在$CF$上是不能用$\%lld$来读入、输出$long\ long$,所以还是老老实实用$cin,cout$。。。

附代码:

#include<iostream>
#include<algorithm>
#include<cstdio>
#define LSON rt<<1
#define RSON rt<<1|1
#define DATA(x,k) a[x].data[k]
#define NUM(x) a[x].num
#define LSIDE(x) a[x].l
#define RSIDE(x) a[x].r
#define MAXN 100010
using namespace std;
int n,m=0;
int lsh[MAXN];
struct Question{
int f,x;
}que[MAXN];
struct Segment_Tree{
long long data[5];
int num,l,r;
}a[MAXN<<2];
inline int read(){
int date=0,w=1;char c=0;
while(c<'0'||c>'9'){if(c=='-')w=-1;c=getchar();}
while(c>='0'&&c<='9'){date=date*10+c-'0';c=getchar();}
return date*w;
}
inline void pushup(int rt){
NUM(rt)=NUM(LSON)+NUM(RSON);
for(int i=0;i<5;i++)DATA(rt,i)=DATA(LSON,i)+DATA(RSON,((i-NUM(LSON))%5+5)%5);
}
void buildtree(int l,int r,int rt){
LSIDE(rt)=l;RSIDE(rt)=r;NUM(rt)=0;
if(l>=r)return;
int mid=l+r>>1;
buildtree(l,mid,LSON);
buildtree(mid+1,r,RSON);
}
void update(int k,int c,long long v,int rt){
if(LSIDE(rt)==RSIDE(rt)){
DATA(rt,1)+=v;
NUM(rt)+=c;
return;
}
int mid=LSIDE(rt)+RSIDE(rt)>>1;
if(k<=mid)update(k,c,v,LSON);
else update(k,c,v,RSON);
pushup(rt);
}
void work(){
for(int i=1,x;i<=n;i++){
if(que[i].f==1){
x=lower_bound(lsh+1,lsh+m+1,que[i].x)-lsh;
update(x,1,que[i].x,1);
}
else if(que[i].f==-1){
x=lower_bound(lsh+1,lsh+m+1,que[i].x)-lsh;
update(x,-1,-que[i].x,1);
}
else cout<<DATA(1,3)<<endl;
}
}
void init(){
char ch[2];
n=read();
for(int i=1,x;i<=n;i++){
scanf("%s",ch);
if(ch[0]=='a'){
x=read();
lsh[++m]=que[i].x=x;
que[i].f=1;
}
else if(ch[0]=='d'){
x=read();
que[i].x=x;
que[i].f=-1;
}
else que[i].f=0;
}
sort(lsh+1,lsh+m+1);
m=unique(lsh+1,lsh+m+1)-lsh-1;
buildtree(1,m,1);
}
int main(){
init();
work();
return 0;
}

其实还有一种更暴力的方法:

$vector$大法好!

各种$STL$乱搞就好。

附代码:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<vector>
using namespace std;
int n;
vector<int> a;
inline int read(){
int date=0,w=1;char c=0;
while(c<'0'||c>'9'){if(c=='-')w=-1;c=getchar();}
while(c>='0'&&c<='9'){date=date*10+c-'0';c=getchar();}
return date*w;
}
void work(){
char ch[2];
n=read();
for(int i=1,x;i<=n;i++){
scanf("%s",ch);
if(ch[0]=='a'){
x=read();
a.insert(lower_bound(a.begin(),a.end(),x),x);
}
else if(ch[0]=='d'){
x=read();
a.erase(lower_bound(a.begin(),a.end(),x));
}
else{
long long ans=0;
for(int i=2;i<a.size();i+=5)ans+=a[i];
cout<<ans<<endl;
}
}
}
int main(){
work();
return 0;
}

CF85D Sum of Medians的更多相关文章

  1. codeforces 85D D. Sum of Medians 线段树

    D. Sum of Medians time limit per test 3 seconds memory limit per test 256 megabytes input standard i ...

  2. Codeforces 85D Sum of Medians

    传送门 D. Sum of Medians time limit per test 3 seconds memory limit per test 256 megabytes input standa ...

  3. Yandex.Algorithm 2011 Round 1 D. Sum of Medians 线段树

    题目链接: Sum of Medians Time Limit:3000MSMemory Limit:262144KB 问题描述 In one well-known algorithm of find ...

  4. 数据结构(线段树):CodeForces 85D Sum of Medians

    D. Sum of Medians time limit per test 3 seconds memory limit per test 256 megabytes input standard i ...

  5. Codeforces 85D Sum of Medians(线段树)

    题目链接:Codeforces 85D - Sum of Medians 题目大意:N个操作,add x:向集合中加入x:del x:删除集合中的x:sum:将集合排序后,将集合中全部下标i % 5 ...

  6. codeforces 85D D. Sum of Medians Vector的妙用

    D. Sum of Medians Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/prob ...

  7. Coderforces 85 D. Sum of Medians(线段树单点修改)

    D. Sum of Medians time limit per test 3 seconds memory limit per test 256 megabytes input standard i ...

  8. 85D Sum of Medians

    传送门 题目 In one well-known algorithm of finding the k-th order statistics we should divide all element ...

  9. Sum of Medians

    Sum of Medians time limit per test 3 seconds memory limit per test 256 megabytes In one well-known a ...

随机推荐

  1. java之super关键字

    super关键字: 在java中使用super来引用基类的成分. 程序code: class FatherClass{ public int value; public void f(){ value ...

  2. 02-3设置第一启动项--进入BIOS设置USB方式启动

    设置USB方式启动 https://zhinan.sogou.com/guide/detail/?id=1610014869 如何设置电脑从U盘启动呢?今天小编教大家如何进入BIOS设置USB方式启动 ...

  3. 8.1.1 Service的生命周期

    2010-06-21 16:57 李宁 中国水利水电出版社 字号:T | T <Android/OPhone开发完全讲义>第8章Android服务,本章主要介绍了Android系统 中的服 ...

  4. Java设计模式-代理模式之动态代理(附源代码分析)

    Java设计模式-代理模式之动态代理(附源代码分析) 动态代理概念及类图 上一篇中介绍了静态代理,动态代理跟静态代理一个最大的差别就是:动态代理是在执行时刻动态的创建出代理类及其对象. 上篇中的静态代 ...

  5. 通过windows自带的系统监视器来查看IIS并发连接数(perfmon.msc)

    如果要查看IIS连接数,最简单方便的方法是通过“网站统计”来查看,“网站统计”的当前在线人数可以认为是当前IIS连接数.然而,“网站统计”的当前在线人数统计时间较长,一般为10分钟或15分钟,再加上统 ...

  6. xcode,不要将.a文件拖到xcode里

    如果将.a文件拖到xcode里,它自动生成的search path是错的.导致奇怪的编译错误,非常坑.这是xcode的一个bug. 或者为了省事直接将.a拖进xcode也行,但记着去Library S ...

  7. 手把手教你用git和SourceTree上传项目到github细解(转)

    尊重原创:https://blog.csdn.net/qq_32365567/article/details/52859166 一.引言 我想大家现在都很熟悉github了,也能运用github上开源 ...

  8. opencv模块介绍

    opencv主要模块介绍: [calib3d]——其实就是就是Calibration(校准)加3D这两个词的组合缩写.这个模块主要是相机校准和三维重建相关的内容.基本的多视角几何算法,单个立体摄像头标 ...

  9. cmpp 短信平台

    背景: 物联网一般是在设备上安装sim卡,通过2g网络来进行设备与云端系统的交互,网络都是通过移动的基站来进行网络传输的,所以一旦移动的基站有变动,比如流量降级,光缆割接,其他故障登 都会导致2g络的 ...

  10. Spring MVC文本框

    以下示例显示如何使用Spring Web MVC框架在表单中使用文本框.首先使用Eclipse IDE来创建一个Web工程,按照以下步骤使用Spring Web Framework开发基于动态表单的W ...