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 ...
随机推荐
- <转>程序员的心理疾病
注:本文转自大神王垠的博客 原文出处 http://www.yinwang.org/blog-cn/2014/02/09/programmer-mental/ 说实话,虽然似乎为之奋斗了十多年,在真正 ...
- 剑指Offer-正则表达式匹配(Python)
1 题干内容 请实现一个函数用来匹配包括.和*的正则表达式.模式中的字符.表示任意一个字符,而*表示它前面的字符可以出现任意次(包含0次). 在本题中,匹配是指字符串的所有字符匹配整个模式. 例如,字 ...
- 使用node.js 文档里的方法写一个web服务器
刚刚看了node.js文档里的一个小例子,就是用 node.js 写一个web服务器的小例子 上代码 (*^▽^*) //helloworld.js// 使用node.js写一个服务器 const h ...
- HDU - 1816 Get Luffy Out *(二分 + 2-SAT)
题目大意:有N串钥匙,M对锁.每串钥匙仅仅能选择当中一把.怎样选择,才干使开的锁达到最大(锁仅仅能按顺序一对一对开.仅仅要开了当中一个锁就可以) 解题思路:这题跟HDU - 3715 Go Deepe ...
- 经常使用的Hql语句
// HQL: Hibernate Query Language. // 特点: // >> 1,与SQL类似.SQL中的语法基本上都能够直接使用. // >> 2.SQL查询 ...
- iOS valueForKeyPath快速计算求和、平均值、最大、最小
iOS中开始取出数组中最大值,最小值除了使用排序的方式,还可以使用valueForKeyPath的方式直接取出 array = @[@(10),@(100),@(20),@(97)]; CGFloat ...
- Spring帖子汇总
SpringMVC表单标签简介 http://haohaoxuexi.iteye.com/blog/1807330 Spring MVC 教程,快速入门,深入分析 http://elf8848.ite ...
- Easyui tree方法扩展getImperfectCheck
$.extend($.fn.tree.methods, { getImperfectCheck : function (jq) { var checked = jq.find("span.t ...
- I.MX6 Ethernet MAC (ENET) MAC Address hacking
/********************************************************************* * I.MX6 Ethernet MAC (ENET) M ...
- Spring MVC生成RSS源
下面的示例演示如何使用Spring Web MVC框架生成RSS源. 首先使用Eclipse IDE,并按照以下步骤使用Spring Web Framework开发基于动态表单的Web应用程序: 创建 ...