15145641

  当我看到题目是5秒的时候,压根没有想树状数组,一直奔着模拟队列去想了,最后也没搞定,赛后看到大神的题解才恍然大悟,原来如此,题目中有明显的暗示,求前n项和,骤然感叹,自己太low...

果然还是要多做多研究啊,下面是代码,还有一处脑残错误在代码里...当时真的是找了好久才发现...

#include <cstdio>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <queue>
#include <map>
#include <cstring>
using namespace std;
#define maxn 100005
int c[maxn],mark[maxn],n;
int lowbit(int k)
{
    return k&(-k);
}
void update(int x,int v)///把树状数组的上界写了变量num,wa了无数次
{
    while(x <= n)
    {
        c[x] += v;
        x += lowbit(x);
    }
}
int cal(int x)
{
    int ans = 0;
    while(x > 0)
    {
        ans += c[x];
        x -= lowbit(x);
    }
    return ans;
}
struct Node
{
    int n,y;
    friend bool operator < (Node a,Node b)
    {
        return a.y > b.y;
    }
};
int main()
{
    int t,top,num,x,y;
    Node tmp;
    char op[20];
    while(~scanf("%d",&t))
    {
        n = t;
        top = 1,num = 1;
        memset(c,0,sizeof(c));
        memset(mark,0,sizeof(mark));
        map<int,int> pos;
        priority_queue<Node>que;
        while(!que.empty()) que.pop();
        while(t--)
        {
            scanf("%s",op);
            if(op[0] == 'a')
            {
                scanf("%d%d",&x,&y);
                tmp.n = num,tmp.y = y;
                que.push(tmp);
                pos[x] = num;
                mark[num] = 1;
                num++;
            }
            else if(op[0] == 'p')
            {
                while(mark[top] == 0 && top <= num) top++;
                if(top > num)continue;
                mark[top] = 0;
                update(top,-1);
            }
            else if(op[0] == 'l')
            {
                while(!que.empty() && mark[que.top().n] == 0)
                {
                    que.pop();
                }
                if(!que.empty())
                {
                    Node now;
                    now = que.top();
                    que.pop();
                    mark[now.n] = 0;
                    update(now.n,-1);
                }
            }
            else if(op[0] == 'c')
            {
                scanf("%d%d",&x,&y);
                int m = pos[x];
                if(pos[x] == 0) continue;
                if(mark[m] == 0) continue;
                int tot = m - 1 + cal(pos[x]-1);
                printf("%d\n",tot);
                if(tot > y)
                {
                    mark[m] = 0;
                    update(m,-1);
                }
            }
        }
    }
    return 0;
}

 

posted on 2016-04-30 15:46  icode-xiaohu  阅读(241)  评论(0编辑  收藏  举报