Submission #1333413

#TimeUsernameProblemLanguageResultExecution timeMemory
1333413tudor_costinMagija (COCI26_magija)C++20
73 / 110
1095 ms20140 KiB
#include <iostream>
#include <vector>
#include <cstdio>
#pragma GCC optimize("Ofast")
#pragma GCC target "avx2"
using namespace std;
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;
    int left_child = aint[2 * nod];
    int right_child = aint[2 * nod + 1];
    int power = pw[siz];
    long long product = (1LL * power * right_child) % mod;
    int res = left_child + product;
    if (res >= mod) res -= mod;
    aint[nod] = res;
    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;
    int left_child = aint[2 * nod];
    int right_child = aint[2 * nod + 1];
    int power = pw[siz];
    long long product = (1LL * power * right_child) % mod;
    int res = left_child + product;
    if (res >= mod) res -= mod;
    aint[nod] = res;
    return;
}
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 power=pw[siz];
        long long product=(1LL*pw[siz]*R%mod);
        int res=L+product;
        if(res>=mod) res-=mod;
        return res;
    }
}
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;
            cout<<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;
            int last=1;
            while(true)
            {
                int st=last,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;
                last=ans+1;
                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...