码迷,mamicode.com
首页 > 编程语言 > 详细

Codeforces Round #220 (Div. 2)D. Inna and Sequence 树状数组+二分

时间:2018-02-06 21:42:55      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:ret   ==   lower   .com   void   vector   个数   链接   while   

题目链接:D. Inna and Sequence

题意:三种操作,1往序列后面加个1,0往序列后面加个0,-1,把给定位置上的数删除。

题解:用树状数组保存的数没被删的个数,每次二分找到位置,然后用数组标记这里被删除。

#include<bits/stdc++.h>
#include<set>
#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#define pb push_back
#define ll long long
#define PI 3.14159265
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#define eps 1e-7
typedef unsigned long long ull;
const int mod=1e9+9;
const int inf=0x3f3f3f3f;
const int maxn=1e6+5;
const int root=1e6+7;
using namespace std;
ll t,n,m,k,len;
int tot,ed;
int a[maxn],b[maxn],v[maxn];
int p[maxn];
vector<int>st;
int  lowbit(int x)
{
    return x&(-x);
}
void add(int x,int y)
{
    for(int i=x;i<maxn;i+=lowbit(i))
    {
        b[i]+=y;
    }
}
int get_sum(int x)
{
    int ans=0;
    while(x>0)
    {
        ans+=b[x];
        x-=lowbit(x);
    }
    return ans;
}
int sec(int x)
{
    int l=1,r=ed;
    while(l<=r)
    {
        int mid=(l+r)>>1;
        if(get_sum(mid)>=x)r=mid-1;
        else l=mid+1;
    }
    return l;
}
void view()
{
     for(int i=1;i<=ed;i++)
    {
        if(!v[i])cout<<a[i];
    }
    cout<<endl;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin>>n>>m;
    for(int i=0;i<m;i++)
    {
        cin>>p[i];
    }
    int op;
    while(n--)
    {
        cin>>op;
        if(op==1)
        {
            ++tot;
            a[++ed]=1;
            add(ed,1);
        }
        else if(op==0)
        {
            ++tot;
            ++ed;
            add(ed,1);
        }
        else
        {
            if(tot<=0)continue;
            int pp=lower_bound(p,p+m,tot)-p;
            if(p[pp]==tot)pp++;
            for(int i=0;i<pp;i++)
            {
                int l=sec(p[i]);
                st.pb(l);
                tot--;
                v[l]=true;
            }
            for(int i=0;i<st.size();i++)
            {
                add(st[i],-1);
            }
            st.clear();
        }
     //   view();
    }
    if(tot>0)
    {
        view();
    }
    else{
        cout<<"Poor stack!"<<endl;
    }
    return 0;
}

 

Codeforces Round #220 (Div. 2)D. Inna and Sequence 树状数组+二分

标签:ret   ==   lower   .com   void   vector   个数   链接   while   

原文地址:https://www.cnblogs.com/lhclqslove/p/8424180.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!