Advertisement
Guest User

segment tree string

a guest
Aug 2nd, 2021
344
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.67 KB | None | 0 0
  1.     /**************************************
  2.     Author : Susanka Majumder ( bingobong )
  3.     ***************************************/
  4.    #include<bits/stdc++.h>  
  5.   #ifndef ONLINE_JUDGE
  6.    #include<sys/resource.h>
  7.     #include <sys/time.h>
  8.     #endif
  9.  
  10.     using namespace std;
  11.      
  12.     /* Template file for Online Algorithmic Competitions */
  13.     /* Typedefs */
  14.         /* System Controllers */
  15.         const long long CPU_TIME = 3 ; // CPU time program has acess to ( in seconds )
  16.         const long long MAX_FILE_SIZE = 1024*1024 ; // Maximum File size that the program can create
  17.         /* Basic types */
  18.         typedef long long           ll;
  19.         typedef long double         ld;
  20.         typedef unsigned long long ull;
  21.         /* STL containers */
  22.         typedef vector <int>    vi;
  23.         typedef vector <ll>     vll;
  24.         typedef pair <int, int> pii;
  25.         typedef pair <ll, ll>   pll;
  26.         typedef vector < pii >  vpii;
  27.         typedef vector < pll >  vpll;
  28.         typedef vector <string> vs;
  29.         typedef vector < vi >   vvi;
  30.         typedef vector < vll >  vvll;
  31.         typedef vector < vpii > vvpii;
  32.         typedef set <int>       si;
  33.     /* Macros */
  34.         /* Loops */
  35.         #define fl(i, a, b)     for(int i(a); i <= (b); i ++)
  36.         #define rep(i, n)       fl(i, 1, (n))
  37.         #define loop(i, n)      fl(i, 0, (n) - 1)
  38.         #define rfl(i, a, b)    for(int i(a); i >= (b); i --)
  39.         #define rrep(i, n)      rfl(i, (n), 1)
  40.         /* Algorithmic functions */
  41.         #define pow2(x) ((x)*(x))
  42.         #define mod(x, m) ((((x) % (m)) + (m)) % (m))
  43.         #define max3(a, b, c) max(a, max(b, c))
  44.         #define min3(a, b, c) min(a, min(b, c))
  45.         #define srt(v)          sort((v).begin(), (v).end())
  46.         /* STL container methods */
  47.         #define pb              push_back
  48.         #define mp              make_pair
  49.         #define eb              emplace_back
  50.         /* String methods */
  51.         #define dig(i)          (s[i] - '0')
  52.         #define slen(s)         s.length()
  53.         /* Shorthand notations */
  54.         #define fr              first
  55.         #define sc              second
  56.         #define re              return
  57.         #define sz(x)           ((int) (x).size())
  58.         #define all(x)          (x).begin(), (x).end()
  59.         #define sqr(x)          ((x) * (x))
  60.         #define fill(x, y)      memset(x, y, sizeof(x))
  61.         #define clr(a)          fill(a, 0)
  62.         #define endl            '\n'
  63.         /* Mathematical */
  64.         #define IINF            0x3f3f3f3f
  65.         #define LLINF           1000111000111000111LL
  66.         #define PI              3.14159265358979323
  67.         #define NIL             -1
  68.         /* Debugging purpose */
  69.        #define db(...) ZZ(#__VA_ARGS__, __VA_ARGS__)
  70. template <typename Arg1> void ZZ(const char* name, Arg1&& arg1){std::cerr << name << " = " << arg1 << endl;}
  71. template <typename Arg1, typename... Args>void ZZ(const char* names, Arg1&& arg1, Args&&... args)
  72. {
  73.     const char* comma = strchr(names + 1, ',');
  74.     std::cerr.write(names, comma - names) << " = " << arg1;
  75.     ZZ(comma, args...);
  76. }
  77.         /* Fast Input Output */
  78.         #define FAST_IO                  ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
  79.     /* Constants */
  80.         const ll MOD = 1000000007LL;
  81.         const ll MAX = 100010LL;
  82.     /* Templates */
  83.     template<class T> T abs(T x) { re x > 0 ? x : -x; }
  84.     template<typename T> T gcd(T a, T b){ if(b == 0) return a; return gcd(b, a % b); }
  85.     template<typename T> T power(T x, T y, ll m = MOD){T ans = 1; x %= m; while(y > 0){ if(y & 1LL) ans = (ans * x)%m; y >>= 1LL; x = (x*x)%m; } return ans%m; }
  86.     template<typename T> T ceiling(T numerator ,T denominator){ return (numerator+denominator-1)/denominator; }
  87.     template<typename T> T inverse(T num,ll m=MOD){ return ((num==1)?1:(MOD - ((MOD/num)*inverse(MOD%num))%MOD+MOD)%MOD) ;  }
  88.     /* input and output of non standard data types */
  89.     template<typename T> istream& operator >> (istream& cin,vector<T> &a){
  90.         for(auto &i:a)cin>>i;
  91.         return cin;
  92.     }
  93.     template<typename T> ostream& operator << (ostream& cout,vector<T> &a){
  94.         for(auto &i:a)cout<<i<<" ";
  95.         return cout;
  96.     }
  97.     template<typename T,typename A> ostream& operator << (ostream& cout,pair<T,A> &a){
  98.         cout<<"("<<a.first<<","<<a.second<<")" ;
  99.         return cout;
  100.     }
  101.     template<typename T> ostream& operator << (ostream& cout,set<T> &a){
  102.         for(auto &i:a)cout<<i<<" ";
  103.         return cout;
  104.     }
  105.     template<typename T,typename A> ostream& operator << (ostream& cout,map<T,A> &a){
  106.         for(auto &i:a) cout<<"("<<i.first<<","<<i.second<<") " ;
  107.         return cout;
  108.     }
  109.     /* additional*/
  110.     #define tr(c,i) for(auto i = (c).begin(); i != (c).end(); i++)
  111.     #define present(c,x) ((c).find(x) != (c).end())
  112.     #define cpresent(c,x) (find(all(c),x) != (c).end())
  113.     /* random genartor */
  114.     // mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  115.     // int k = uniform_int_distribution<int>(83, 32832932)(rng) ;
  116.     //double inf = 1.0/0.0;
  117.  
  118.     #define int long long
  119.  
  120.  
  121.     struct segtree
  122. {
  123.     int size ;
  124.     vector<string> operations ;
  125.     vector<string> values ;
  126.  
  127.     string NO_OPERATION = "" ;
  128.     string NEUTRAL_ELEMENT = "" ;
  129.  
  130.     string modify_operation(string a,string b,int len)
  131.     {
  132.       if(b == NO_OPERATION ) return a ;
  133.       srt(b) ;
  134.         return b ;
  135.     }
  136.  
  137.     string calc_operation(string a,string b){
  138.       string s = a+b ;
  139.       srt(s) ;
  140.       return s  ;
  141.     }
  142.  
  143.     void apply_mod_op(string &a,string b,int len)
  144.     {
  145.       a = modify_operation(a,b,len ) ;
  146.     }
  147.  
  148.  
  149.  
  150.     void init(int n)
  151.     {
  152.       size = 1 ;
  153.       while(size < n ) size *= 2 ;
  154.       operations.assign(2*size , "") ;
  155.       values.assign(2*size , "") ;
  156.     }
  157.  
  158.     void propagate(int x ,int lx ,int rx)
  159.     {
  160.       if(rx - lx ==1) return ;
  161.       int m = (lx + rx) /2 ;
  162.       apply_mod_op(operations[2*x +1 ] , operations[x] , 1) ;
  163.       apply_mod_op( values[2*x + 1] , operations[x] , m - lx ) ;
  164.       apply_mod_op(operations[2*x+ 2] , operations[x] , 1 ) ;
  165.       apply_mod_op(values[2*x + 2] , operations[x] ,rx - m ) ;
  166.       operations[x] = NO_OPERATION ;
  167.     }
  168.  
  169.     void modify(int l ,int r ,string v,int x,int lx,int rx)
  170.     {
  171.       propagate(x,lx,rx) ;
  172.       if(lx >= r || l>= rx) return ;
  173.       if(lx >= l && rx<= r )
  174.       {
  175.         apply_mod_op(operations[x] , v, 1) ;
  176.         apply_mod_op(values[x] , v , rx - lx ) ;
  177.         return ;
  178.       }
  179.       int m = (lx+ rx)/2 ;
  180.       modify(l ,r , v , 2*x+1 , lx , m ) ;
  181.       modify( l , r, v , 2*x + 2 , m ,rx ) ;
  182.       values[x] = calc_operation(values[2*x + 1 ], values[2*x + 2] ) ;
  183.  
  184.     }
  185.  
  186.     void modify(int l , int r , string v)
  187.     {
  188.       modify(l , r, v , 0 , 0 , size) ;
  189.  
  190.     }
  191.  
  192.     string calc(int l ,int r , int x , int lx , int rx)
  193.     {
  194.       propagate(x, lx, rx ) ;
  195.       if( lx>=r || l>= rx ) return NEUTRAL_ELEMENT ;
  196.       if( lx >= l && rx <= r ) return values[x] ;
  197.  
  198.       int m = ( lx + rx )/2 ;
  199.       auto m1 = calc(l , r , 2*x + 1 , lx , m ) ;
  200.       auto m2 = calc( l , r , 2*x + 2 , m , rx ) ;
  201.  
  202.       return calc_operation(m1 , m2 ) ;
  203.  
  204.     }
  205.  
  206.  
  207.     string calc(int l,int r){
  208.       return calc(l , r, 0 , 0 , size ) ;
  209.     }
  210. };
  211.  
  212.      void solve()
  213.     {
  214.        int n , q ;
  215.        cin>>n>>q ;
  216.        segtree st ;
  217.        st.init(n) ;
  218.        string s ;
  219.        loop(i, n )
  220.        {
  221.           cin>>s ;
  222.           st.modify(i , i + 1 , s) ;
  223.        }
  224.      
  225.        while(q--)
  226.        {
  227.           int  l , r , k  ;
  228.           cin>>l>>r>>k ;
  229.  
  230.           string ans_string  = st.calc(l-1 , r) ;
  231.           cout<<ans_string[k-1]<<endl ;  
  232.  
  233.        }
  234.     }
  235.    
  236.     signed main(){
  237.  
  238.  
  239.         #ifndef ONLINE_JUDGE
  240.         (void)!freopen("input.txt","r",stdin);
  241.         (void)!freopen("output.txt","w",stdout);
  242.         (void)!freopen("error.txt","w",stderr);
  243.         /* setting up soft limit on resources */
  244.         rlimit rlim , rlim_time ;
  245.         if(getrlimit( RLIMIT_FSIZE , &rlim) || getrlimit(RLIMIT_CPU , &rlim_time) )  
  246.             return 1 ;
  247.         rlim.rlim_cur = MAX_FILE_SIZE ;
  248.         rlim_time.rlim_cur = CPU_TIME ;
  249.         if(setrlimit( RLIMIT_FSIZE , &rlim ) || setrlimit(RLIMIT_CPU , &rlim_time))
  250.             return 2 ;
  251.  
  252.         #endif
  253.        
  254.         FAST_IO;
  255.  
  256.          int t = 1 ;
  257.         //cin>>t ;
  258.         while(t--)
  259.         {
  260.             solve() ;
  261.         }
  262.  
  263.      
  264.        
  265.         #ifndef ONLINE_JUDGE
  266.         cout<<"\nTime Elapsed: " << 1.0*clock() / CLOCKS_PER_SEC << " sec\n";
  267.         #endif
  268.         return 0;
  269.     }
  270.    
  271.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement