Codeforces 558E 線段樹處理字串內排序,codeforces558e

來源:互聯網
上載者:User

Codeforces 558E 線段樹處理字串內排序,codeforces558e

給出長度為n的字串,m個操作。

每個操作有三個值 l,r,op。

op==1,表示將字串中[ l ,r ]的部分按照升序排列。

op==0,表示將字串中[ l ,r ]的部分按照降序排列。

輸出最終的字串


按小寫字母建26顆線段樹

對於每次修改,先記錄[l,r]區間內各個字母出現的次數,並對相應區間清空,然後按照升序或者降序從新更新


#include "stdio.h"#include "string.h"char str[100010];int n,temp;struct node{    int l,r,x,lazy;}data[30][400010];void build(int l,int r,int k,int op){    int mid;    data[op][k].l=l;    data[op][k].r=r;    data[op][k].x=0;    data[op][k].lazy=-1;    if (l==r)        return ;    mid=(l+r)/2;    build(l,mid,k*2,op);    build(mid+1,r,k*2+1,op);}void Pushdown(int k,int op){    if (data[op][k].lazy==-1) return ;    if (data[op][k].l==data[op][k].r) return ;    if (data[op][k].lazy==0)    {        data[op][k*2].x=data[op][k*2].lazy=0;        data[op][k*2+1].x=data[op][k*2+1].lazy=0;    }    else    {        data[op][k*2].x=data[op][k*2].r-data[op][k*2].l+1;        data[op][k*2+1].x=data[op][k*2+1].r-data[op][k*2+1].l+1;        data[op][k*2].lazy=data[op][k*2+1].lazy=1;    }    data[op][k].lazy=-1;}void updata(int l,int r,int k,int op){    int mid;    if (data[op][k].l==l && data[op][k].r==r)    {        data[op][k].x=data[op][k].r-data[op][k].l+1;        data[op][k].lazy=1;        return;    }    Pushdown(k,op);    mid=(data[op][k].l+data[op][k].r)/2;    if (r<=mid) updata(l,r,k*2,op);    else        if (l>mid) updata(l,r,k*2+1,op);    else    {        updata(l,mid,k*2,op);        updata(mid+1,r,k*2+1,op);    }    data[op][k].x=data[op][k*2].x+data[op][k*2+1].x;}void  search(int l,int r,int k,int op){    int mid;    if (data[op][k].l==l && data[op][k].r==r)    {        temp+=data[op][k].x;        data[op][k].x=0;        data[op][k].lazy=0;        return ;    }    Pushdown(k,op);    mid=(data[op][k].l+data[op][k].r)/2;    if (r<=mid) search(l,r,k*2,op);    else if (l>mid)  search(l,r,k*2+1,op);    else    {        search(l,mid,k*2,op);        search(mid+1,r,k*2+1,op);    }    data[op][k].x=data[op][k*2].x+data[op][k*2+1].x;}void init(){    int i;    scanf("%s",str);    for (i=0;i<26;i++)        build(1,n,1,i);    for (i=0;i<n;i++)        updata(i+1,i+1,1,str[i]-'a');}int main(){    int m,a,b,c,i,j,k;    int mark[30];    while (scanf("%d%d",&n,&m)!=EOF)    {        init();        while (m--)        {            scanf("%d%d%d",&a,&b,&c);            memset(mark,0,sizeof(mark));            for (i=0;i<26;i++)            {                temp=0;                search(a,b,1,i); // 尋找區間內i字母出現的次數,並清空                mark[i]+=temp;            }            if (c==0)            {                k=a;                for (i=25;i>=0;i--)                    if (mark[i]!=0)                    {                        updata(k,k+mark[i]-1,1,i); // 更新區間字母                        k+=mark[i];                    }            }            else            {                k=a;                for (i=0;i<26;i++)                    if (mark[i]!=0)                    {                        updata(k,k+mark[i]-1,1,i);                        k+=mark[i];                    }            }        }        for (i=1;i<=n;i++)        {            for (j=0;j<26;j++)            {                temp=0;                search(i,i,1,j);                if (temp!=0)                {                    printf("%c",j+'a');                    break;                }            }        }        printf("\n");    }    return 0;}


著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.