Submission #14172544


Source Code Expand

Copy
#line 1 "main.cpp"
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cstdint>
#include <cstdlib>
#include <cmath>
#include <complex>
#include <chrono>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <type_traits>
#include <unordered_map>
#include <vector>
#include <random>
#include <utility>
#include <limits>
#include <list>

/* template start */
 
#define rep(i, a, b) for (long long i = (a); (i) < (b); (i)++)
#define all(i) i.begin(), i.end()

#ifdef LOCAL
#define debug(...) std::cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif

void debug_out(){std::cerr<<std::endl;}

template<typename Head,typename... Tail>
void debug_out(Head h,Tail... t){
  std::cerr<<" "<<h;
  if(sizeof...(t)>0)std::cout<<" :";
  debug_out(t...);
}
 
template <typename T1, typename T2>
std::ostream& operator<<(std::ostream& os, std::pair<T1, T2> pa) {
  return os << pa.first << " " << pa.second;
}
 
template <typename T>
std::ostream& operator<<(std::ostream& os, std::vector<T> vec) {
  for (std::size_t i = 0; i < vec.size(); i++)os << vec[i] << (i + 1 == vec.size() ? "" : " ");
  return os;
}
 
template<typename T1,typename T2>
inline bool chmax(T1& a,T2 b){return a<b && (a=b,true);}
 
template<typename T1,typename T2>
inline bool chmin(T1& a,T2 b){return a>b && (a=b,true);}

template<typename Num>
constexpr Num mypow(Num a, unsigned long long b) {
  if(b==0)return 1;
  if (a==0)return 0;
  Num x = 1;
  while (b > 0) {
    if(b & 1)x*=a;
    a*=a;
    b >>= 1;
  }
  return x;
}

/* template end */

using ll = long long;

template<typename Monoid>
class RBST_Seq{
  public:
  using value_t=typename Monoid::value_t;
  using size_t=std::size_t;
  private:
  unsigned int rnd(){
    static unsigned int x(123456789),y(362436069),z(521288629),w(88675123);
    unsigned int t=(x^(x<<11));
    x=y;y=z;z=w;
    return w=(w^(w>>19))^(t^(t>>8));
  }

  struct Node{
    value_t val,sum;
    size_t cnt;
    std::shared_ptr<Node> left,right;
    Node(value_t val_):val(val_),sum(val_),cnt(1),left(),right(){}
  };

  using node_ptr=std::shared_ptr<Node>;
  node_ptr root;

  value_t calc(const node_ptr& t){return t?t->sum:Monoid::id;}
  size_t count(const node_ptr& t){return t?t->cnt:0;}

  node_ptr update(node_ptr t){
    if(!t)return t;
    t->sum=Monoid::op(Monoid::op(calc(t->left),t->val),calc(t->right));
    t->cnt=count(t->left)+count(t->right)+1;
    return t;
  }

  node_ptr merge(node_ptr l,node_ptr r){
    if(!l||!r)return l?l:r;

    if(rnd()%(count(l)+count(r)) < count(l)){
      l->right=merge(l->right,r);
      return update(l);
    }else{
      r->left=merge(l,r->left);
      return update(r);
    }
  }

  std::pair<node_ptr,node_ptr> split(node_ptr t,size_t k){
    if(!t)return std::make_pair(t,t);

    if(k<=count(t->left)){
      auto temp=split(t->left,k);
      t->left=temp.second;
      return std::make_pair(temp.first,update(t));
    }else{
      auto temp=split(t->right,k-count(t->left)-1);
      t->right=temp.first;
      return std::make_pair(update(t),temp.second);
    }
  }

  void insert(node_ptr& t,node_ptr newnode,size_t k){
    auto temp=split(t,k);
    t=merge(merge(temp.first,newnode),temp.second);
  }

  void erase(node_ptr& t,size_t k){
    auto temp=split(t,k+1);
    auto temp2=split(temp.first,k);
    t=merge(temp2.first,temp.second);
  }

  void build(node_ptr& t,const std::vector<value_t>& val_,size_t l,size_t r){
    if(l==r){
      t=nullptr;
      return;
    }
    if(r==l+1){
      t=std::make_shared<Node>(val_[l]);
      return;
    }

    size_t mid=l+(r-l)/2;
    t=std::make_shared<Node>(val_[mid]);
    build(t->left,val_,l,mid);
    build(t->right,val_,mid+1,r);
    update(t);
  }

  RBST_Seq(const node_ptr& t):root(t){}
  public:
  RBST_Seq():root(){}
  RBST_Seq(const std::vector<value_t>& seq_):root(){
    build(root,seq_,0,seq_.size());
  }

  size_t size(){return count(root);}
  void insert(size_t k,const value_t& value){insert(root,std::make_shared<Node>(value),k);}
  void erase(size_t k){erase(root,k);}

  RBST_Seq split(size_t l,size_t r){
    assert(0<=l&&l<=r&&r<=size());
    auto tmp=split(root,r);
    auto tmp2=split(tmp.first,l);
    root=merge(tmp2.first,tmp.second);
    return RBST_Seq(tmp2.second);
  }

  void insert(size_t pos,RBST_Seq seq){
    assert(0<=pos&&pos<=size());
    auto tmp=split(root,pos);
    tmp.first=merge(tmp.first,seq.root);
    root=merge(tmp.first,tmp.second);
  }

  value_t fold_all(){return calc(root);}
  value_t fold(size_t l,size_t r){
    auto temp=split(root,r);
    auto temp2=split(temp.first,l);
    value_t ret=calc(temp2.second);
    root=merge(merge(temp2.first,temp2.second),temp.second);

    return ret;
  }
};

#line 2 "/home/imperi/Documents/cp-library/lib/local/modint.cpp"

template <std::uint_fast64_t Modulus> class modint {
  using u64 = std::uint_fast64_t;

public:
  u64 a;

  constexpr modint(const u64 x = 0) noexcept : a(x % Modulus) {}
  constexpr u64 &value() noexcept { return a; }
  constexpr const u64 &value() const noexcept { return a; }
  constexpr bool operator==(const modint rhs) const noexcept {return a==rhs.a;}
  constexpr bool operator!=(const modint rhs) const noexcept {return !(*this==rhs);}
  constexpr modint operator+(const modint rhs) const noexcept {
    return modint(*this) += rhs;
  }
  constexpr modint operator-(const modint rhs) const noexcept {
    return modint(*this) -= rhs;
  }
  constexpr modint operator*(const modint rhs) const noexcept {
    return modint(*this) *= rhs;
  }
  constexpr modint operator/(const modint rhs) const noexcept {
    return modint(*this) /= rhs;
  }
  constexpr modint &operator+=(const modint rhs) noexcept {
    a += rhs.a;
    if (a >= Modulus) {
      a -= Modulus;
    }
    return *this;
  }
  constexpr modint &operator-=(const modint rhs) noexcept {
    if (a < rhs.a) {
      a += Modulus;
    }
    a -= rhs.a;
    return *this;
  }
  constexpr modint &operator*=(const modint rhs) noexcept {
    a = a * rhs.a % Modulus;
    return *this;
  }
  constexpr modint &operator/=(modint rhs) noexcept {
    u64 exp = Modulus - 2;
    while (exp) {
      if (exp % 2) {
        *this *= rhs;
      }
      rhs *= rhs;
      exp /= 2;
    }
    return *this;
  }
};
#line 209 "main.cpp"

constexpr ll MOD=1e9+7;
using mint=modint<MOD>;

struct Plus{
  using value_t=mint;
  static value_t id;
  static constexpr value_t op(value_t a,value_t b){
    return a+b;
  }
};

mint Plus::id=0;

using Seq=RBST_Seq<Plus>;

int main() {
  std::cin.tie(nullptr);
  std::ios::sync_with_stdio(false);

  ll n,m;
  std::cin>>n>>m;

  std::vector<Seq> seq;

  constexpr mint HASHPOW=1e6;
  std::vector<mint> pow(n,1),pow_inv(n);
  rep(i,1,n)pow[i]=pow[i-1]*HASHPOW;
  pow_inv[n-1]=mint(1)/pow[n-1];
  for(ll i=n-2;i>=0;i--)pow_inv[i]=pow_inv[i+1]*HASHPOW;

  rep(i,0,n)debug(i,pow[i].value());

  rep(i,0,m){
    std::string s;
    std::cin>>s;
    std::vector<mint> tmp(n);
    rep(j,0,n)tmp[j]=pow[j]*(s[j]-'a'+1);
    seq.emplace_back(tmp);
  }

  ll q;
  std::cin>>q;

  while(q--){
    ll type;
    std::cin>>type;

    if(type==1){
      ll x,y,l,r;
      std::cin>>x>>y>>l>>r;
      x--;y--;l--;
      Seq f=seq[x].split(l,r);
      Seq s=seq[y].split(l,r);
      seq[x].insert(l,s);
      seq[y].insert(l,f);
    }else{
      ll x,y,l,r;
      std::cin>>x>>y>>l>>r;
      x--;y--;l--;
      std::cout<<(seq[x].fold(l,r)*pow_inv[l]).value()<<"\n";
    }
  }

  return 0;
}

Submission Info

Submission Time
Task E - Hash Swapping
User Imperi
Language C++14 (GCC 5.4.1)
Score 800
Code Size 7962 Byte
Status
Exec Time 3118 ms
Memory 315456 KB

Judge Result

Set Name Score / Max Score Test Cases
Sample 0 / 0 example_0, example_1
All 800 / 800 bigrand_0, bigrand_1, bigrand_2, bigrand_3, bigrand_4, bigrand_5, example_0, example_1, rand_0, rand_1, rand_2, rand_3, rand_4, rand_5, rand_6, rand_7, rand_8, rand_9
Case Name Status Exec Time Memory
bigrand_0 2279 ms 315076 KB
bigrand_1 3118 ms 315456 KB
bigrand_2 1666 ms 312468 KB
bigrand_3 2360 ms 301020 KB
bigrand_4 2859 ms 308640 KB
bigrand_5 1551 ms 291084 KB
example_0 1 ms 256 KB
example_1 1 ms 256 KB
rand_0 1 ms 256 KB
rand_1 2 ms 384 KB
rand_2 1 ms 384 KB
rand_3 1 ms 256 KB
rand_4 1 ms 256 KB
rand_5 1 ms 384 KB
rand_6 1 ms 384 KB
rand_7 1 ms 384 KB
rand_8 2 ms 384 KB
rand_9 1 ms 256 KB