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. c++ vector容器的使用,序列倒叙reverse(),容器底部插入一个数值push_back()

    问题:程序实现将que[i]添加到que2最后,再将que2反转输出. 例如: 输入 4 1 2 3 4 输出 4 2 1 3 #include<iostream> #include< ...

  2. Pushlet后台推送

    1.Pushlet 是一个开源的 Comet 框架,Pushlet 使用了观察者模型:客户端发送请求,订阅感兴趣的事件:服务器端为每个客户端分配一个会话 ID 作为标记,事件源会把新产生的事件以多播的 ...

  3. 常用sql语句记录

    1.表 --建表 if OBJECT_ID('Student') is not null create table Student( ID ,) not null, Name ), Code ), f ...

  4. Jquery的promise对象

    一直用jquery,ajax一直是这么写: $.ajax({ url: 'abc.com/index', type: 'post', data: { abc:1 }, success: functio ...

  5. MVC 的八个扩展点

    Asp.net MVC中常用的八个扩展点并举例说明. 一.ActionResult ActionResult代表了每个Action的返回结果.asp.net mvc提供了众多内置的ActionResu ...

  6. Atitit.播放系统规划新版本 v4 q18 and 最近版本回顾

    Atitit.播放系统规划新版本 v4  q18  and 最近版本回顾 1 版本12 (ing)4 1.1 无映射nas系统..4 1.2 图片简介搜刮其4 1.3 12.8. 电影图片增加png, ...

  7. FPGA和DSP间基于SRIO的高速通信系统设计

    作者:陈婷,岳强,汪洋 解放军信息工程大学 摘要: 现代信号处理系统通常需要在不同处理器之间实现高速数据通信,SRIO协议由于高效率.低延时的特性被广泛使用.本文研究了在FPGA和DSP两种处理器之间 ...

  8. LeetCode459. Repeated Substring Pattern

    Description Given a non-empty string check if it can be constructed by taking a substring of it and ...

  9. jquery特效 商品SKU属性规格选择实时联动

    转载,原文链接 功能:各种属性的值选中后,SKU填写表格进行变动,对属性.属性值的数量自适应,编辑时不因去掉勾选导致原有值不显示 所有代码均属原创,现在共享,如果有更好的实现方式,希望互相交流 最终实 ...

  10. cpu故障定位 top strace pstack

    一次服务器CPU占用率高的定位分析 推荐   背景:通过性能监控发现上线服务器cpu某核占用率已经达到了100%,而且是由我们的某个核心服务导致的.幸亏由于我们的服务进程由多个相同worker(线程) ...