CF558E A Simple Task

本文介绍了一种解决CF558E问题的方法,通过使用线段树和桶排序思想来实现区间排序操作,最终输出排序后的字符序列。详细解析了如何利用线段树进行区间更新和查询,并附带完整的代码实现。

CF558E A Simple Task    

WOC怎么又一个simple task?     

操作就是区间排序+最终询问     

第一反应是Splay(不对呀,我明明不会Splay的......?)    

后来看了看,感觉Splay不可做(我连Splay都不会,怎么就觉得不能做了)    

感觉线段树比较靠谱     

观察题目发现小写字母只有26个(常识)     

往元素上去做文章     

将排序用另外一种方式呈现      

用桶排序的思想    

开一个桶记录区间1~26字母出现的个数    

然后区间赋值,达到排序目的    

输出只要遍历一下线段树的叶子节点即可    

代码:    

#include<bits/stdc++.h>
using namespace std;
const int N=100005;
int n,m;
int a[N];
int cal[30];
struct Sugment_Tree{
    int LZT[N<<2];
    int t[N<<2][30];
    #define il inline
    #define mid (l+r)/2
    Sugment_Tree(){memset(t,0,sizeof(t));memset(LZT,0,sizeof(LZT));}
    il void push_up(int num){
        for(int i=1;i<=26;i++){t[num][i]=t[num<<1][i]+t[num<<1|1][i];}
    }
    il void push_down(int l,int r,int num){
        if(!LZT[num]) return;
        LZT[num<<1]=LZT[num];
        LZT[num<<1|1]=LZT[num];
        memset(t[num<<1],0,sizeof(t[num<<1]));
        memset(t[num<<1|1],0,sizeof(t[num<<1|1]));
        t[num<<1][LZT[num]]=(mid-l+1);
        t[num<<1|1][LZT[num]]=(r-(mid+1)+1);
        LZT[num]=0;
    }
    il void build(int l,int r,int num){
        if(l==r){
            t[num][a[l]]=1;
            return;
        }
        build(l,mid,num<<1);
        build(mid+1,r,num<<1|1);
        push_up(num);
    }
    il void upt(int l,int r,int num,int L,int R,int SUM){
        if(l>R||r<L) return;
        if(l>=L&&r<=R){
            LZT[num]=SUM;
            memset(t[num],0,sizeof(t[num]));
            t[num][SUM]=r-l+1;
            return;
        }
        push_down(l,r,num);
        upt(l,mid,num<<1,L,R,SUM);
        upt(mid+1,r,num<<1|1,L,R,SUM);
        push_up(num);
    }
    il int ask(int l,int r,int num,int L,int R,int SUM){
        if(l>R||r<L) return 0;
        if(l>=L&&r<=R){
            return t[num][SUM];
        }
        push_down(l,r,num);
        return ask(l,mid,num<<1,L,R,SUM)+ask(mid+1,r,num<<1|1,L,R,SUM);
    }
    il void output(int l,int r,int num){
        if(l==r){
            for(int i=1;i<=26;i++){
                if(t[num][i]) printf("%c",'a'+i-1);
            }
            return;
        }
        push_down(l,r,num); 
        output(l,mid,num<<1);
        output(mid+1,r,num<<1|1);
    }
}T;
int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++){
        char c;
        cin>>c;
        a[i]=c-'a'+1;
    }
    T.build(1,n,1);
    //T.output(1,n,1);
    while(m--){
        int l,r,x;
        scanf("%d%d%d",&l,&r,&x);
        memset(cal,0,sizeof(cal));
        for(int i=1;i<=26;i++){
            cal[i]=T.ask(1,n,1,l,r,i);
        }
        int L=l;
        if(x==1){
            for(int i=1;i<=26;i++){
                T.upt(1,n,1,L,L+cal[i]-1,i);
                L+=cal[i];
            }
        } 
        else{
            for(int i=26;i>=1;i--){
                T.upt(1,n,1,L,L+cal[i]-1,i);
                L+=cal[i];
            }
        } 
    }
    T.output(1,n,1);
    return 0;
} 

 

转载于:https://www.cnblogs.com/QYJ060604/p/11428382.html

### Codeforces Problem 1978A Explanation The provided references pertain specifically to problem 742B on the Codeforces platform, which involves finding pairs of numbers that XOR to a given value \( x \)[^3]. However, regarding **Codeforces Problem 1978A**, this specific question has not been addressed within the supplied references. To provide an accurate response about **Problem 1978A**, one must first understand its requirements: #### Understanding Problem Statement Typically, each Codeforces problem includes: - A detailed description of the task. - Input format specifications. - Output format expectations. - Constraints and examples illustrating how solutions should behave under various conditions. Since direct information is unavailable from the current citations, here's a general approach based on typical competitive programming practices for solving problems similar to those found in recent contests like CF Round #1978 (Div. 2). #### General Approach for Solving Competitive Programming Problems When tackling any coding challenge, especially on platforms such as Codeforces, consider these strategies: - Carefully read through all parts of the problem statement multiple times until fully understood. - Identify key elements including inputs, outputs, constraints, and special cases. - Develop test cases beyond those provided by the contest organizers to ensure comprehensive testing before submission. For instance, if Problem 1978A were related to string manipulation or simple arithmetic operations common in beginner-level tasks, approaches might include iterating over characters/numbers efficiently while applying necessary transformations according to specified rules. ```cpp #include <iostream> using namespace std; int main() { int n; cin >> n; // Example input handling // Placeholder logic since exact details are unknown cout << "Example output"; return 0; } ``` This template serves merely illustrative purposes without reflecting actual implementation steps tailored explicitly towards Problem 1978A due to lack of concrete details. --related questions-- 1. How does understanding bitwise operators help solve certain types of algorithmic challenges? 2. What techniques can be employed when optimizing time complexity for large datasets in competitive programming? 3. Can you explain methods used to debug code during live competitions effectively? 4. In what ways do practice sessions contribute to improving performance in online coding contests?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值