【codeforces 899 F . Letters Removing】 线段树

该博客主要介绍了Codeforces 899F问题的解决策略,内容涉及使用线段树进行字符串操作。题目要求在给定区间内删除特定字符并自动前补,解决方案中提到了用80个集合存储字符位置,并通过二分查找确定转换边界以通过题目。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

899F

题意 给你一个字符串 然后给你一个区间 让你删除区间内一种字符 并且自动向前补位

做法 我们开80个set存这些值的下标 然后暴力去删

注意用二分判断l r 转换的边界即可A掉这道题

#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
#include <random>
#include <ctime>
using namespace std;

#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))

typedef pair<int,int> pll;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;
const ll mod =  (int)1e9+7;

ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
//inv[1]=1;
//for(int i=2; i<M; ++i) inv[i]=1ll*(mod-mod/i)*inv[mod%i]%mod;
//for(int i=0; i<LIM; ++i) coef[i]=1ll*(P[i]-1)*inv[P[i]]%mod;
const int MAX_N = 200025;
int s[MAX_N<<2],len;
char str[MAX_N];
void up(int rt)
{
    s[rt] = s[rt<<1] + s[rt<<1|1];
}
void update(int rt,int l,int r,int x)
{
    if(l==r)
    {
        s[rt]++;
        return ;
    }
    int mid = (l+r)>>1;
    if(x<=mid) update(rt<<1,l,mid,x);
    else update(rt<<1|1,mid+1,r,x);
    up(rt);
}
int query(int rt,int l,int r,int x,int y)
{
    if(x<=l&&r<=y)
    {
        return s[rt];
    }
    int res = 0,mid = (l+r)>>1;
    if(x<=mid) res+=query(rt<<1,l,mid,x,y);
    if(mid<y) res+=query(rt<<1|1,mid+1,r,x,y);
    return res;
}
int Find_pos(int pos)
{
    int l = pos,r = len;
    while(l<=r)
    {
        int mid = (l+r)>>1;
        int num = query(1,1,len,1,mid);
        if(mid==pos+num&&str[mid]!='.')
        {
            return mid;
        }
        else if(mid<pos+num) l = mid + 1;
        else r = mid - 1;
    }
}
set<int > st[80];
char opt[15];
int main()
{
    //ios::sync_with_stdio(false);
    //freopen("a.txt","r",stdin);
    //freopen("b.txt","w",stdout);
    int n,m,x,y;
    scanf("%d%d",&n,&m);
    scanf("%s",str+1);
    len = strlen(str+1);
    for(int i = 1;i<=len;++i)
        st[str[i]-'0'].insert(i);
    while(m--)
    {
        scanf("%d%d%s",&x,&y,opt);
        if(x+query(1,1,len,1,x)>len) continue;
        else
        {
            if(x==y) x = y = Find_pos(x);
            else
            {
            x = Find_pos(x);
            if(query(1,1,len,1,y)+y>n) y = n;
            else y = Find_pos(y);
            }
            set<int>::iterator it;
            it = st[opt[0]-'0'].begin();
            while(it!=st[opt[0]-'0'].end())
            {
                int id = *it;
                if(id<=y&&id>=x&&str[id]!='.')
                {
                    update(1,1,len,id);
                    str[id] = '.';
                    set<int>::iterator tmp = it;
                    it++;
                    st[opt[0]-'0'].erase(tmp);
                }
                else it++;
                if(id>y) break;
            }
        }
    }
    for(int i = 1;i<=len;++i)
        if(str[i]!='.') printf("%c",str[i]);

    //fclose(stdin);
    //fclose(stdout);
    //cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值