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的更多相关文章
- 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 ...
- Codeforces 85D Sum of Medians
传送门 D. Sum of Medians time limit per test 3 seconds memory limit per test 256 megabytes input standa ...
- Yandex.Algorithm 2011 Round 1 D. Sum of Medians 线段树
题目链接: Sum of Medians Time Limit:3000MSMemory Limit:262144KB 问题描述 In one well-known algorithm of find ...
- 数据结构(线段树):CodeForces 85D Sum of Medians
D. Sum of Medians time limit per test 3 seconds memory limit per test 256 megabytes input standard i ...
- Codeforces 85D Sum of Medians(线段树)
题目链接:Codeforces 85D - Sum of Medians 题目大意:N个操作,add x:向集合中加入x:del x:删除集合中的x:sum:将集合排序后,将集合中全部下标i % 5 ...
- codeforces 85D D. Sum of Medians Vector的妙用
D. Sum of Medians Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/prob ...
- 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 ...
- 85D Sum of Medians
传送门 题目 In one well-known algorithm of finding the k-th order statistics we should divide all element ...
- Sum of Medians
Sum of Medians time limit per test 3 seconds memory limit per test 256 megabytes In one well-known a ...
随机推荐
- win10 在当前目录下 打开cmd
windows7 : 按住 shift 键 , 右键 就有该选项. windows10 : 运行下面的 注册表文件: 该注册表文件内容如下: Windows Registry Editor Versi ...
- RIP协议
1.概念:RIP协议是一种内部网关协议(IGP),是一种动态路由选择协议,用于自治系统(AS)内的路由信息的传递. RIP协议基于距离矢量算法(DistanceVectorAlgorit ...
- JDBC的常用API
一.Connection接口: 1.createStatement():创建数据库连接 2.prepareStatement(Stringsql):创建预处理语句 3.prepareCall(Stri ...
- Windows下免费软件的首选推荐
PS:以下按装机顺序排列,“|”号后面是备选软件. 启动引导:EasyBCD 虚拟机:VirtualBox Linux:Zorin | Linux Mint(Mate) | Ubuntu 驱动工具:驱 ...
- Tomcat启动报错:SERVER: Error ListenerStart 排查过程记录
报错的Tomcat截图: 要排查此问题,首先需要调整tomcat的日志级别,调整成通过log4j来记录日志的方式,具体的调整方式: http://tomcat.apache.org/tomcat- ...
- Spell checker - poj 1035 (hash)
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 22541 Accepted: 8220 Description Yo ...
- 多媒体开发之---h264格式详解
http://blog.csdn.net/bluebirdssh/article/details/6533501 http://blog.csdn.net/d_l_u_f/article/detail ...
- 面试题:谈谈你对hibernate的理解
说说这类问题一般要和一个东西比較.说说他们的长处和缺点,hibernate就和JDBC比較呗.你就说说JDBC的优缺点.然后说说hibernate的优缺点,最后对照得出hibernate更好. hib ...
- 那些可爱的 Linux 命令
环境 root@15b883:~# uname -a ##需要是Ubuntu环境 Linux 15b883 --generic #- :: UTC x86_64 x86_64 x86_64 GNU/L ...
- std::vector<std::vector<> >
上次看到这个有点晕了,其实这个vector保存的是std::vector<> #include <vector> #include <iostream> using ...