线段树 + 离线查询 HDU 4288 Coder

Hint 是因为 scanf 的问题吗?我用 cin,关闭了同步,用 G++ 更快
pushup() 看了好久别人的代码才看懂,我和你们比,真的简直是个弱智
我懒,所以用 vector,用时间换时间

#include<bits/stdc++.h>
using namespace std;
using ll=long long;
const int maxn=1e5+100;
vector<ll> array_main,leaves;
vector<char> op;
struct node
{
    int cnt=0;
    ll sum[5];
};
node tree[maxn<<2];
void pushup(int nt)
{
    for(int i=0;i<5;i++)
    {
        tree[nt].cnt=tree[(nt<<1)].cnt+tree[(nt<<1)+1].cnt; 
        tree[nt].sum[i]=tree[(nt<<1)].sum[i]+tree[(nt<<1)+1].sum[(((i-(tree[(nt<<1)].cnt%5))%5)+5)%5];
    }
}
void update(int p,int nt,int nl,int nr,int c)
{
    if(nl==nr)
    {
        tree[nt].cnt+=c; 
        if(c==-1) tree[nt].sum[1]=0;
        else tree[nt].sum[1]=leaves[p];
        return;
    }
    int m=((nl+nr)>>1);
    if(p<=m) update(p,(nt<<1),nl,m,c);
    else update(p,(nt<<1)+1,m+1,nr,c);
    pushup(nt);
}
int main()
{
    ios::sync_with_stdio(false);
    int n;
    while(cin>>n)
    { 
        memset(tree,0,sizeof(tree));
        array_main.clear();
        op.clear();
        leaves.clear();
        for(int i=0,x;i<n;i++)
        {
            string s;
            cin>>s;
            if(s[0]=='s')
            {
                array_main.push_back(0);
                op.push_back(s[0]);
                continue;
            }
            cin>>x;
            array_main.push_back(x);
            op.push_back(s[0]);
        }
        leaves=array_main;
        leaves.push_back(0);
        sort(leaves.begin(),leaves.end());
        leaves.erase(unique(leaves.begin(),leaves.end()),leaves.end()); 
        for(int i=0;i<op.size();i++)
        {

            if(op[i]=='s') cout<<tree[1].sum[3]<<'\n'; 
            else if(op[i]=='a') 
                update(lower_bound(leaves.begin(),leaves.end(),array_main[i])-leaves.begin(),1,1,leaves.size()-1,1);
            else if(op[i]=='d') 
                update(lower_bound(leaves.begin(),leaves.end(),array_main[i])-leaves.begin(),1,1,leaves.size()-1,-1);
        } 
    }
    return 0;
}

版权声明:本文为qq_41246972原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。