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

D. Sum of Medians

Time Limit: 1 Sec  Memory Limit: 256 MB

题目连接

http://codeforces.com/problemset/problem/85/D

Description

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 Input

6
add 4
add 5
add 1
add 2
add 3
sum

Sample Output

3

HINT

题意

给你一堆数,然后排序,然后让你输出下标mod5=3的和

题解:

用vector来做,虽然感觉有很多数据结构都可以把这道题秒了

代码:

//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 200001
#define mod 10007
#define eps 1e-9
int Num;
char CH[20];
//const int inf=0x7fffffff;   //нчоч╢С
const int inf=0x3f3f3f3f;
/*

inline void P(int x)
{
    Num=0;if(!x){putchar(‘0‘);puts("");return;}
    while(x>0)CH[++Num]=x%10,x/=10;
    while(Num)putchar(CH[Num--]+48);
    puts("");
}
*/
//**************************************************************************************
inline ll read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
    while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}
    return x*f;
}
inline void P(int x)
{
    Num=0;if(!x){putchar(‘0‘);puts("");return;}
    while(x>0)CH[++Num]=x%10,x/=10;
    while(Num)putchar(CH[Num--]+48);
    puts("");
}

vector<int> s;
int main()
{
    char a[10];
    int b;
    int n=read();
    for(int i=0;i<n;i++)
    {
        scanf("%s",a);
        if(a[0]==‘a‘)
        {
            scanf("%d",&b);
            s.insert(lower_bound(s.begin(),s.end(),b),b);
        }
        if(a[0]==‘d‘)
        {
            scanf("%d",&b);
            s.erase(lower_bound(s.begin(),s.end(),b));
        }
        if(a[0]==‘s‘)
        {
            ll sum=0;
            for(int j=2;j<s.size();j+=5)
                sum+=s[j];
            printf("%lld\n",sum);
        }
    }
}
时间: 2024-10-05 14:05:19

codeforces 85D D. Sum of Medians Vector的妙用的相关文章

codeforces 85D. Sum of Medians

二次联通门 : codeforces 85D. Sum of Medians /* codeforces 85D. Sum of Medians 正解线段树或是平衡树 结果用vector暴力卡过去了 */ #include <algorithm> #include <iostream> #include <cstdio> #include <vector> using namespace std; void read (int &now) { reg

Codeforces 85D Sum of Medians(线段树)

85D Sum of Medians 题目链接 题意:一个集合有添加,删除元素,每次查询输出集合位置为i % 5 == 3的位置和 思路:线段树,线段树记录下% 5 == 0, 1, 2, 3, 4的和,并且记录一个mov表示右移多少,每次添加一个值的时候,就当前位置之后的一整段位置都要右移一个单位,这样去搞线段树维护一下即可 代码: #include <cstdio> #include <cstring> #include <cstdlib> #include <

Codeforces 396B On Sum of Fractions 规律题

题目链接:点击打开链接 我们把 1 / { u(i)*v(i) }拆开->  (1/(u(i)-v(i)) * ( 1/v(i) - 1/u(i) ) 若n +1  是素数,则显然(1/(u(i)-v(i)) * ( 1/v(i) - 1/u(i) ) 这样完全相同的式子有 u(i)-v(i) 个 那么就可以把前面系数约掉,那么剩下的式子就是 1/2 - 1/(n+1) 若不是,则我们找到第一个<=n的素数,即v(n) 和第一个>n的素数,即 u(n) 然后前面的 2-v(n)求和,即

Codeforces 396B On Sum of Fractions 数论

题目链接:Codeforces 396B On Sum of Fractions 题解来自:http://blog.csdn.net/keshuai19940722/article/details/20076297 题目大意:给出一个n,ans = ∑(2≤i≤n)1/(v(i)*u(i)), v(i)为不大于i的最大素数,u(i)为大于i的最小素数, 求ans,输出以分式形式. 解题思路:一开始看到这道题1e9,暴力是不可能了,没什么思路,后来在纸上列了几项,突然想到高中时候求等差数列时候用到

【Codeforces 85 D】Sum of Medians

Codeforces 85 D 题意:维护一个有序集合,每次问编号\(mod\ 5\)余\(3\)的所有数的和. 思路:线段树维护\(mod\ 5\)余\(x\)的数的和,然后上推的时候根据左节点的值改一下就好了. Codeforces 718 A 题意:给一个小数,问最多取\(t\)次四舍五入到某一个小数点后的位后这个数最大能到多少. 思路:首先肯定贪心.(但不知道为什么tag上是\(dp\) 首先我们找到最靠左的一个大于等于5的数,把它四舍五入到上一位, 然后再不断地往前找到下一个大于等于5

Codeforces 121A Lucky Sum

Lucky Sum Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Original ID: 121A64-bit integer IO format: %I64d      Java class name: (Any) Petya loves lucky numbers. Everybody knows that lucky numbers are positive int

Codeforces 577B Modulo Sum:数学 结论【选数之和为m的倍数】

题目链接:http://codeforces.com/problemset/problem/448/C 题意: 给你n个数字,给定m. 问你是否能从中选出若干个数字,使得这些数字之和为m的倍数. 题解: 其实就是要找一些数字,使得之和mod m为0. 开一个vector,存当前已经能够构成的数字之和mod m之后的值. 一开始vector为空,然后枚举n个数字a[i],对于每个数字枚举当前vector中的值v[i],将没有出现过的(a[i]+v[i])%m值加入vector中. 最后判断下vec

Educational Codeforces Round 37-F.SUM and REPLACE (线段树,线性筛,收敛函数)

F. SUM and REPLACE time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard output Let D(x) be the number of positive divisors of a positive integer x. For example, D(2)?=?2 (2 is divisible by 1 and 2), D(6)?

CodeForces 797B Odd sum

排序. 正的偶数肯定都是可以加进去的,因为加偶数不改变奇偶性.奇数从大到小排序,取个最大的前缀和. #include <iostream> #include <cstdio> #include <cmath> #include <cstring> #include <string> #include <queue> #include <stack> #include <vector> #include <