Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <cstdio>
- #include <cstring>
- const char IN [ ] = "j.in" ;
- const char OUT [ ] = "j.out" ;
- const int MAX = 100014 ;
- using namespace std;
- #define fin cin
- #define fout cout
- //ifstream fin ( IN ) ;
- //ofstream fout ( OUT ) ;
- long long AINTstanga [ MAX * 8 ] ;
- long long putb [ MAX ] ;
- long long val , P , B ; int pos ;
- inline void update ( int nod , int st , int dr )
- {
- if ( st == dr )
- {
- AINTstanga [ nod ] = val ;
- return ;
- }
- int mij = ( st + dr ) >> 1 ;
- if ( pos <= mij )
- update ( nod << 1 , st , mij ) ;
- else update ( nod << 1 | 1 , mij + 1 , dr ) ;
- int put = dr - st ;
- int put1 = mij - st ;
- AINTstanga [ nod ] = ( AINTstanga [ nod << 1 ] * putb [ put - put1 ] + AINTstanga [ nod << 1 | 1 ] ) % P ;
- }
- inline long long Query ( int nod , int st , int dr , int qst , int qdr )
- {
- if ( qst <= st and dr <= qdr )
- {
- int putmax = qdr - st ;
- int put_actual = dr - st ;
- if ( st == dr )
- putmax = qdr - st ;
- return ( AINTstanga [ nod ] * putb [ putmax - put_actual ] ) % P;
- }
- int mij = ( st + dr ) >> 1 ;
- long long SOL = 0 ;
- if ( qst <= mij )
- SOL = ( SOL + Query ( nod << 1 , st , mij , qst , qdr ) ) % P ;
- if ( qdr > mij )
- SOL = ( SOL + Query ( nod << 1 | 1, mij + 1 , dr , qst , qdr ) ) % P;
- return SOL % P ;
- }
- int main ( )
- {
- while ( 1 )
- {
- memset ( AINTstanga , 0 , sizeof ( AINTstanga ) ) ;
- long long L , N ;
- scanf ( "%I64d %I64d %I64d %I64d" , &B , &P , &L , &N ) ;
- if ( !B and !P and !L and !N )
- break ;
- putb [ 0 ] = 1 ;
- for ( int i = 1 ; i <= L ; ++ i )
- putb [ i ] = ( putb [ i - 1 ] * B ) % P ;
- while ( N -- )
- {
- char C ;
- scanf ( "%*c" ) ;
- scanf ( "%c" , &C ) ;
- if ( C == 'E' )
- {
- int x ;
- scanf ( "%d" ,&x );
- pos = x ;
- scanf ( "%I64d" , &val ) ;
- update ( 1 , 1 , L ) ;
- }
- else {
- int i , j ;
- scanf ( "%d %d" , &i , &j ) ;
- printf ( "%I64d\n" , Query ( 1 , 1 , L , i , j ) ) ;
- }
- }
- printf ( "-\n" ) ;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement