【ACMICPC Chengdu Online】1001

题意:

一个序列,Add 操作加入一个数,保持有序

    Del 操作删除一个数,保持有序

                    Sum操作求 i % 5 = 3 的和

维护线段树,move数组代表偏移了几位。PushUp那里是重点

/*
 * =====================================================================================
 *
 *         Author:  KissBuaa.DS(AC)
 *        Company:  BUAA-ACMICPC-Group
 *
 * =====================================================================================
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//#include <stdbool.h>
#include <math.h>
#define LL __int64
#define CLR(x) memset(x,0,sizeof(x))
#define typec double
#define sqr(x) ((x)*(x))
#define abs(x) ((x)<0?(-(x)):(x))
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define PI acos(-1.0)
#define lowbit(x) ((x)&(-(x)))
#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1
#define inf 100000000
//For C++
#include <cctype>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <bitset>
#include <list>
#include <iostream>
using namespace std;
const double eps=1e-10;
int dblcmp(typec d) {
    if (fabs(d)<eps)
       return 0;
    return (d>0)?1:-1;
}
int n;
const int N =  111111;
LL tree[N << 2][5] ;
LL move[N << 2];
char str[10];
struct OP{
    LL op ;
    LL x;
    void input(){
        scanf("%s",str);
        if (str[0]=='s') {op=0,x=0;}
        else if (str[0]=='a'){
            op = 1;
            scanf("%I64d",&x);
        }
        else if (str[0] == 'd'){
            op = -1;
            scanf("%I64d",&x);
        }
    }
}io[N];
LL num[N];
int tot;
void PushUp(int rt){
    for (int i = 0 ; i < 5 ; ++ i){
        tree[rt][i] = tree[rt << 1][i] + tree[rt << 1 | 1][(i - move[rt << 1] + 15)%5];
    }
}
void build(int l ,int r ,int rt){
    move[rt]=0;
    if (l == r){
        for (int i = 0 ; i < 5 ; ++i)
            tree[rt][i] = 0;
        return;
    }
    int m = (l + r) >> 1;
    build(lson);
    build(rson);
    PushUp(rt);
}
void update(int p , LL c , int l , int r, int rt){
    move[rt] = (move[rt] + c + 10) % 5;
    if (l == r){
        if (c == -1) tree[rt][0] = 0;
        else tree[rt][0] =  num[p - 1];
        return ;
    }
    int m = (l + r) >> 1;
    if (p <= m) update(p , c , lson);
    else update(p , c , rson);
    PushUp(rt);
}
void solve(){
    tot = 0;
    for (int i = 0 ; i < n ; ++i){
        io[i].input();
        if (io[i].op){
            num[tot ++] = io[i].x;
        }
    }
    sort(num , num + tot);
    tot = unique(num , num + tot ) - num;
    build(1 , tot , 1);
    for (int i = 0 ; i < n ; ++i){
        if (io[i].op == 0) printf("%I64d\n",tree[1][2]);
        else{
            int pos = lower_bound(num , num + tot , io[i].x) - num + 1 ;
            //printf("POS: %d\n",pos);
            update(pos , io[i].op , 1 , tot , 1);
        }
    }
}
int main(){
    while (~scanf("%d",&n)) solve();
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值