Submission #14172508


Source Code Expand

Copy
#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;
  }
};

#include "../lib/local/modint.cpp"

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

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

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 0
Code Size 6358 Byte
Status

Compile Error

./Main.cpp:208:35: fatal error: ../lib/local/modint.cpp: No such file or directory
 #include "../lib/local/modint.cpp"
                                   ^
compilation terminated.