CF-85D-Sum of Medians(线段树)
2014-08-25 22:38
357 查看
In one well-known algorithm of finding the k-th order statistics we should divide all elements into groups of five consecutive elements and find the median
of each five. A median is called the middle element of a sorted array (it's the third largest element for a group of five). To increase the algorithm's performance speed on a modern video card, you should be able to find a sum of medians in each five of the
array.
A sum of medians of a sorted k-element set S = {a1, a2, ..., ak},
where a1 < a2 < a3 < ... < ak,
will be understood by as
The
operator
stands for taking the remainder, that is
stands
for the remainder of dividing x by y.
To organize exercise testing quickly calculating the sum of medians for a changing set was needed.
Input
The first line contains number n (1 ≤ n ≤ 105),
the number of operations performed.
Then each of n lines contains the description of one of the three operations:
add x — add the element x to
the set;
del x — delete the element x from
the set;
sum — find the sum of medians of the set.
For any add x operation it is true
that the element x is not included in the set directly before the operation.
For any del x operation it is true
that the element x is included in the set directly before the operation.
All the numbers in the input are positive integers, not exceeding 109.
Output
For each operation sum print on the single line the sum of medians of the current
set. If the set is empty, print 0.
Please, do not use the %lld specificator to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams
(also you may use the %I64d specificator).
Sample test(s)
input
output
input
output
思路:跟这题完全一样。点击打开链接 数据比HDU的强。
#include <cstdio>
#include <cstring>
#include <map>
#include <algorithm>
using namespace std;
struct S{
char op[5];
int x,id;
}e[100005];
bool cmpval(S a,S b)
{
if(a.x==b.x) return a.op[0]<b.op[0];
return a.x<b.x;
}
bool cmpid(S a,S b)
{
return a.id<b.id;
}
int n,cnt,val[100005],num[400005];
long long sum[400005][5];
void build(int idx,int s,int e)
{
if(s!=e)
{
int mid=(s+e)>>1;
build(idx<<1,s,mid);
build(idx<<1|1,mid+1,e);
}
num[idx]=0;
for(int i=0;i<5;i++) sum[idx][i]=0;
}
void update(int idx,int s,int e,int pos,int flag)
{
num[idx]+=flag;
if(s==e)
{
sum[idx][1]+=val[s]*flag;
return;
}
int mid=(s+e)>>1;
if(pos<=mid) update(idx<<1,s,mid,pos,flag);
else update(idx<<1|1,mid+1,e,pos,flag);
for(int i=0;i<5;i++) sum[idx][i]=sum[idx<<1][i]+sum[idx<<1|1][i-num[idx<<1]%5>=0?i-num[idx<<1]%5:i-num[idx<<1]%5+5];//更新对应区间内下标为模5等于i数的和
}
long long query(int idx,int s,int e,int mod)
{
return sum[idx<<1][mod]+sum[idx<<1|1][mod-num[idx<<1]%5>=0?mod-num[idx<<1]%5:mod-num[idx<<1]%5+5];
}
int main()
{
int i;
long long ans;
while(~scanf("%d",&n))
{
map<int,int>mp;//用于离散化过程中判重,有可能存在先add a,再del a,再 add a的情况
for(i=0;i<n;i++)
{
scanf("%s",e[i].op);
if(e[i].op[0]=='s') e[i].x=0;
else scanf("%d",&e[i].x);
e[i].id=i;
}
sort(e,e+n,cmpval);
cnt=1;
for(i=0;i<n;i++)//离散化
{
if(e[i].op[0]=='a')
{
if(!mp[e[i].x]) mp[e[i].x]=cnt++;
val[mp[e[i].x]]=e[i].x;
e[i].x=mp[e[i].x];
}
else if(e[i].op[0]=='d')
{
e[i].x=mp[e[i].x];
}
}
sort(e,e+n,cmpid);
cnt--;
if(!cnt)//特判
{
for(i=0;i<n;i++)
{
if(e[i].op[0]=='s')
{
printf("0\n");
}
}
continue;
}
build(1,1,cnt);
for(i=0;i<n;i++)
{
if(e[i].op[0]=='a') update(1,1,cnt,e[i].x,1);
else if(e[i].op[0]=='d') update(1,1,cnt,e[i].x,-1);
else printf("%I64d\n",query(1,1,n,3));
}
}
}
of each five. A median is called the middle element of a sorted array (it's the third largest element for a group of five). To increase the algorithm's performance speed on a modern video card, you should be able to find a sum of medians in each five of the
array.
A sum of medians of a sorted k-element set S = {a1, a2, ..., ak},
where a1 < a2 < a3 < ... < ak,
will be understood by as
The
operator
stands for taking the remainder, that is
stands
for the remainder of dividing x by y.
To organize exercise testing quickly calculating the sum of medians for a changing set was needed.
Input
The first line contains number n (1 ≤ n ≤ 105),
the number of operations performed.
Then each of n lines contains the description of one of the three operations:
add x — add the element x to
the set;
del x — delete the element x from
the set;
sum — find the sum of medians of the set.
For any add x operation it is true
that the element x is not included in the set directly before the operation.
For any del x operation it is true
that the element x is included in the set directly before the operation.
All the numbers in the input are positive integers, not exceeding 109.
Output
For each operation sum print on the single line the sum of medians of the current
set. If the set is empty, print 0.
Please, do not use the %lld specificator to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams
(also you may use the %I64d specificator).
Sample test(s)
input
6 add 4 add 5 add 1 add 2 add 3 sum
output
3
input
14
add 1
add 7
add 2
add 5
sum
add 6
add 8
add 9
add 3add 4
add 10
sum
del 1
sum
output
5 11 13
思路:跟这题完全一样。点击打开链接 数据比HDU的强。
#include <cstdio>
#include <cstring>
#include <map>
#include <algorithm>
using namespace std;
struct S{
char op[5];
int x,id;
}e[100005];
bool cmpval(S a,S b)
{
if(a.x==b.x) return a.op[0]<b.op[0];
return a.x<b.x;
}
bool cmpid(S a,S b)
{
return a.id<b.id;
}
int n,cnt,val[100005],num[400005];
long long sum[400005][5];
void build(int idx,int s,int e)
{
if(s!=e)
{
int mid=(s+e)>>1;
build(idx<<1,s,mid);
build(idx<<1|1,mid+1,e);
}
num[idx]=0;
for(int i=0;i<5;i++) sum[idx][i]=0;
}
void update(int idx,int s,int e,int pos,int flag)
{
num[idx]+=flag;
if(s==e)
{
sum[idx][1]+=val[s]*flag;
return;
}
int mid=(s+e)>>1;
if(pos<=mid) update(idx<<1,s,mid,pos,flag);
else update(idx<<1|1,mid+1,e,pos,flag);
for(int i=0;i<5;i++) sum[idx][i]=sum[idx<<1][i]+sum[idx<<1|1][i-num[idx<<1]%5>=0?i-num[idx<<1]%5:i-num[idx<<1]%5+5];//更新对应区间内下标为模5等于i数的和
}
long long query(int idx,int s,int e,int mod)
{
return sum[idx<<1][mod]+sum[idx<<1|1][mod-num[idx<<1]%5>=0?mod-num[idx<<1]%5:mod-num[idx<<1]%5+5];
}
int main()
{
int i;
long long ans;
while(~scanf("%d",&n))
{
map<int,int>mp;//用于离散化过程中判重,有可能存在先add a,再del a,再 add a的情况
for(i=0;i<n;i++)
{
scanf("%s",e[i].op);
if(e[i].op[0]=='s') e[i].x=0;
else scanf("%d",&e[i].x);
e[i].id=i;
}
sort(e,e+n,cmpval);
cnt=1;
for(i=0;i<n;i++)//离散化
{
if(e[i].op[0]=='a')
{
if(!mp[e[i].x]) mp[e[i].x]=cnt++;
val[mp[e[i].x]]=e[i].x;
e[i].x=mp[e[i].x];
}
else if(e[i].op[0]=='d')
{
e[i].x=mp[e[i].x];
}
}
sort(e,e+n,cmpid);
cnt--;
if(!cnt)//特判
{
for(i=0;i<n;i++)
{
if(e[i].op[0]=='s')
{
printf("0\n");
}
}
continue;
}
build(1,1,cnt);
for(i=0;i<n;i++)
{
if(e[i].op[0]=='a') update(1,1,cnt,e[i].x,1);
else if(e[i].op[0]=='d') update(1,1,cnt,e[i].x,-1);
else printf("%I64d\n",query(1,1,n,3));
}
}
}
相关文章推荐
- Coderforces 85 D. Sum of Medians(线段树单点修改)
- hdu 4288 线段树 Sum of Medians 像lazysales girl Coder
- Yandex.Algorithm 2011 Round 1 D. Sum of Medians 线段树
- CodeForces 85 D.Sum of Medians(线段树)
- Yandex-R1 Sum of Medians [线段树]
- codeforces 85D D. Sum of Medians Vector的妙用
- 【线段树】 HDOJ 5381 The sum of gcd
- CF85D:Sum of Medians(STL)
- hdu 5381 The sum of gcd(线段树等差数列区间修改+单点查询)
- hdu 5381 The sum of gcd (线段树x树状数组x区间和维护进阶x离线处理)
- CF 85D Sum of Medians (五颗线段树)
- hdu 4288 Coder & CF85-D Sum of Medians (单点更新)
- Given Length and Sum of Digits...---------cf水题
- CF-edu#5 E - Sum of Remainders -数学-枚举
- cf D. On Sum of Fractions
- HDOJ 3415 Max Sum of Max-K-sub-sequence(线段树优化DP)
- hdu 5381 The sum of gcd(线段树+gcd)
- CF 276C - Little Girl and Maximum Sum 差分数列,线段树,splay
- hdu 5381 The sum of gcd(线段树)
- hdu 5381 The sum of gcd(线段树+gcd)