제출 #1333409

#제출 시각아이디문제언어결과실행 시간메모리
1333409tudor_costinMagija (COCI26_magija)C++20
73 / 110
1095 ms20212 KiB
#include <iostream>
#include <vector>
#include <cstdio>
#pragma GCC optimize("Ofast")
#pragma GCC target "avx2"
using namespace std;
class InParser {
private:
    static const int SIZE = (1 << 16);
    char buffer[SIZE];
    int len;
    int pos;

    inline char get_char() {
        if (pos == len) {
            pos = 0;
            // Read directly from standard input (stdin)
            len = fread(buffer, 1, SIZE, stdin);
            if (len == 0) return EOF;
        }
        return buffer[pos++];
    }

    inline bool is_digit(char c) {
        return (c >= '0' && c <= '9');
    }

public:
    InParser() {
        len = 0;
        pos = 0;
    }

    InParser& operator>>(int& n) {
        char c = get_char();
        while (!is_digit(c) && c != '-' && c != EOF) c = get_char();
        if (c == EOF) return *this; // Safety check for end of input

        bool sgn = (c == '-');
        if (sgn) c = get_char();

        n = c - '0';
        c = get_char();

        while (is_digit(c)) {
            n = n * 10 + (c - '0');
            c = get_char();
        }

        if (sgn) n = -n;
        return *this;
    }

    // Overload for long long
    InParser& operator>>(long long& n) {
        char c = get_char();
        while (!is_digit(c) && c != '-' && c != EOF) c = get_char();
        if (c == EOF) return *this;

        bool sgn = (c == '-');
        if (sgn) c = get_char();

        n = c - '0';
        c = get_char();

        while (is_digit(c)) {
            n = n * 10 + (c - '0');
            c = get_char();
        }

        if (sgn) n = -n;
        return *this;
    }
};

class OutParser {
private:
    static const int SIZE = (1 << 16);
    char buffer[SIZE];
    int len;

    inline void write_ch(char c) {
        if (len == SIZE) {
            // Write directly to standard output (stdout)
            fwrite(buffer, 1, SIZE, stdout);
            len = 0;
        }
        buffer[len++] = c;
    }

public:
    OutParser() {
        len = 0;
    }

    ~OutParser() {
        if (len > 0) {
            fwrite(buffer, 1, len, stdout);
        }
    }

    // Overload for single characters ('\n', ' ')
    OutParser& operator<<(char c) {
        write_ch(c);
        return *this;
    }

    // Overload for C-strings ("YES\n", "NO\n")
    OutParser& operator<<(const char* s) {
        while (*s) {
            write_ch(*s++);
        }
        return *this;
    }

    // Overload for long long (handles int automatically via casting)
    OutParser& operator<<(long long vu32) {
        if (vu32 < 0) {
            write_ch('-');
            vu32 = -vu32;
        }

        if (vu32 <= 9) {
            write_ch(vu32 + '0');
        } else {
            (*this) << (vu32 / 10);
            write_ch(vu32 % 10 + '0');
        }

        return *this;
    }

    // Explicit overload for int to route to long long
    OutParser& operator<<(int n) {
        return (*this) << (long long)n;
    }
};

// Global instantiation to replace cin and cout
InParser fin;
OutParser fout;
const int Nmax=2e5+5,mod=1e9+7,base=31;
int aint[4*Nmax],pw[Nmax];
int sef[Nmax];
vector<int> nodes[Nmax];
int n;
pair<int,int> d[Nmax];
void precalc(int n)
{
    pw[0]=1;
    for(int i=1;i<=n;i++)
    {
        pw[i]=1LL*pw[i-1]*base%mod;
    }
}
void build(int nod,int l,int r)
{
    if(l==r)
    {
        aint[nod]=sef[l];
        return;
    }
    int mid=(l+r)/2;
    build(2*nod,l,mid);
    build(2*nod+1,mid+1,r);
    int siz=mid-l+1;
    aint[nod]=(aint[2*nod]+1LL*pw[siz]*aint[2*nod+1]%mod);
    if(aint[nod]>=mod) aint[nod]-=mod;
    return;
}
void update(int nod,int l,int r,int poz,int val)
{
    if(l==r)
    {
        aint[nod]=val;
        return;
    }
    int mid=(l+r)/2;
    if(poz<=mid) update(2*nod,l,mid,poz,val);
    else update(2*nod+1,mid+1,r,poz,val);
    int siz=mid-l+1;
    aint[nod]=(aint[2*nod]+1LL*pw[siz]*aint[2*nod+1]%mod);
    if(aint[nod]>=mod) aint[nod]-=mod;
}
int query(int nod,int l,int r,int st,int dr)
{
    if(st<=l && r<=dr)
    {
        return aint[nod];
    }
    int mid=(l+r)/2;
    if(dr<=mid) return query(2*nod,l,mid,st,dr);
    else if(mid<st) return query(2*nod+1,mid+1,r,st,dr);
    else
    {
        int siz=mid-st+1;
        int L=query(2*nod,l,mid,st,mid);
        int R=query(2*nod+1,mid+1,r,mid+1,dr);
        int sol=(L+1LL*pw[siz]*R%mod);
        if(sol>=mod) sol-=mod;
        return sol;
    }
}
void unite(int a,int b)
{
    if(nodes[a].size()<nodes[b].size()) swap(a,b);
    d[a].first=min(d[a].first,d[b].first);
    d[a].second=max(d[a].second,d[b].second);
    for(int x:nodes[b])
    {
        sef[x]=a;
        nodes[a].push_back(x);
        update(1,1,n,x,a);
    }
    nodes[b].clear();
    return;
}
int main()
{
    ///ios_base::sync_with_stdio(0);
    ///cin.tie(0),cout.tie(0);
    int q;
    cin>>n>>q;
    for(int i=1;i<=n;i++)
    {
        sef[i]=i;
        nodes[i].push_back(i);
        d[i]={i,i};
    }
    precalc(n);
    build(1,1,n);
    while(q--)
    {
        int tip;
        cin>>tip;
        if(tip==1)
        {
            ///query
            int x;
            cin>>x;
            fout<<d[sef[x]].first<<' '<<d[sef[x]].second<<'\n';
            continue;
        }
        else
        {
            ///update
            int l1,r1,l2,r2,len;
            cin>>l1>>l2>>len;
            ///r1=l1+len-1;
            ///r2=l2+len-1;
            while(true)
            {
                int st=1,dr=len;
                int ans=len+1;
                while(st<=dr)
                {
                    int mid=(st+dr)/2;
                    r1=l1+mid-1;
                    r2=l2+mid-1;
                    ///cout<<mid<<'\n';
                    int hsh1=query(1,1,n,l1,r1);
                    int hsh2=query(1,1,n,l2,r2);
                    if(hsh1!=hsh2)
                    {
                        ans=mid;
                        dr=mid-1;
                    }
                    else st=mid+1;
                }
                if(ans>len) break;
                int poz1=l1+ans-1;
                int poz2=l2+ans-1;
                ///cout<<111<<'\n';
                unite(sef[poz1],sef[poz2]);
                ///cout<<111<<'\n';
            }
        }
    }
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...