Cattle passenger Challenge 38 B substring reversal

The meaning of problems

Fixed length m m subinterval flip, single query.
Ensure that the reversing operation of the starting index is incremented.

answer

can S p l a Y Splay , may be THE ( n ) O (n)

First we fix a window, with a mark indicating whether the flip.
If overturned, add elements on the increase in the head, or added to the tail.
Time of the visit, marking the positive decision to find or look backwards.

If the window does not directly answer.
Prior to the current operation will not be re-index operation (increment), so direct assignment in the sliding window of time.

#include<bits/stdc++.h>
using namespace std;

const int maxn = 1e6 + 500;

int n,m,q,swq,now;
deque<char>dq;
char s[maxn],str[maxn];

int main(){
    cin>>n>>m>>q;
    scanf("%s",str);
    strcpy(s+1,str);
    for(int i=1;i<=m;i++)dq.push_back(str[i-1]);
    now=1;
    for(int i=1;i<=q;i++){
        int op,id;scanf("%d%d",&op,&id);
        if(op==2){
            if(id<now||id>now+m-1)putchar(s[id]);
            else{
                id-=now;
                if(swq)putchar(dq[m-1-id]);
                else putchar(dq[id]);
            }
        }
        else{
            while(now<id){
                if(swq){
                    s[now]=dq.back();
                    dq.pop_back();
                    dq.push_front(str[now+m-1]);
                }
                else{
                    s[now]=dq.front();
                    dq.pop_front();
                    dq.push_back(str[now+m-1]);
                }
                now++;
//                for(auto x:dq)cout<<x<<" ";puts("");
            }
            swq^=1;
        }
    }

}

S p l a Y Splay did not learn

Published 203 original articles · won praise 17 · views 20000 +

Guess you like

Origin blog.csdn.net/mxYlulu/article/details/105064452