• 数据结构(线段树):CodeForces 85D Sum of Medians


    D. Sum of Medians
    time limit per test
    3 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    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).

    Examples
    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 3
    add 4
    add 10
    sum
    del 1
    sum
    Output
    5
    11
    13

      这道题目不难,注意去重,还要防止爆int。
     1 #include <algorithm>
     2 #include <iostream>
     3 #include <cstring>
     4 #include <cstdio>
     5 using namespace std;
     6 const int maxn=100010;
     7 int hsh[maxn],tot,Q;
     8 long long ans[maxn<<2][5];
     9 int sum[maxn<<2],tp[maxn],num[maxn];
    10 
    11 void Push_up(int x){
    12     int l=x<<1,r=x<<1|1;
    13     sum[x]=sum[l]+sum[r];
    14     for(int i=0;i<=4;i++)
    15         ans[x][i]=ans[l][i]+ans[r][((i-sum[l])%5+5)%5];
    16 }
    17 
    18 void Insert(int x,int l,int r,int g,int d){
    19     if(l==r){
    20         ans[x][0]+=hsh[l]*d;
    21         sum[x]+=d;
    22         return;
    23     }
    24     int mid=(l+r)>>1;
    25     if(mid>=g)Insert(x<<1,l,mid,g,d);
    26     else Insert(x<<1|1,mid+1,r,g,d);
    27     Push_up(x);
    28 }
    29 
    30 char op[10];
    31 int main(){
    32     scanf("%d",&Q);
    33     for(int q=1;q<=Q;q++){
    34         scanf("%s",op);
    35         if(op[0]=='a')tp[q]=1;
    36         else if(op[0]=='d')tp[q]=-1;
    37         else continue;
    38         scanf("%d",&num[q]);
    39         if(tp[q]==1){++tot;hsh[tot]=num[q];}
    40     }
    41     
    42     sort(hsh+1,hsh+tot+1);
    43     tot=unique(hsh+1,hsh+tot+1)-hsh-1;
    44 
    45     for(int q=1;q<=Q;q++){
    46         if(tp[q]==1){
    47             int p=lower_bound(hsh+1,hsh+tot+1,num[q])-hsh;
    48             Insert(1,1,tot,p,1);
    49         }
    50         else if(tp[q]==-1){
    51             int p=lower_bound(hsh+1,hsh+tot+1,num[q])-hsh;
    52             Insert(1,1,tot,p,-1);
    53         }
    54         else
    55             printf("%I64d
    ",ans[1][2]);
    56     }
    57     return 0;
    58 }
    尽最大的努力,做最好的自己!
  • 相关阅读:
    Napa简介——基于Web用于Outlook/SharePoint的IDE
    Azul开源Zing Jvm
    如何控制单元测试的粒度?
    存储过程与Entity Framework
    Java增强的for循环和普通for循环对比
    Java中的static变量、方法、代码块初始化执行顺序
    推荐一款好用的博客软件——菊子曰
    Android进程与线程
    OPhone SDK
    开始学习Android
  • 原文地址:https://www.cnblogs.com/TenderRun/p/5581621.html
Copyright © 2020-2023  润新知