Advertisement
ismaeil

B. Cryptography

Jul 24th, 2020
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.63 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define Int long long
  3. using namespace std;
  4. #define rg (p << 1) | 1
  5. #define lf (p << 1)
  6.  
  7. vector< vector<Int> > Ans;
  8. int MOD ,n ,m;
  9.  
  10. class SegTree {
  11.     private:
  12.         vector< vector<Int> > Identity ;
  13.         vector< vector< vector<Int> > > Seg;
  14.  
  15.         Int MulMod(Int a ,Int b){
  16.             return 1ll * ( 1ll * (a % MOD) * (b % MOD) ) % MOD;
  17.         }
  18.  
  19.         vector< vector< Int > > Compain(vector< vector< Int > > a ,vector< vector< Int > > b) {
  20.  
  21.             vector< vector< Int > > Res(2 ,vector< Int >(2 ,0));
  22.             for(int i = 0 ; i < 2 ; i++)
  23.                 for(int j = 0 ; j < 2 ; j++)
  24.                     for(int k = 0 ; k < 2 ; k++)
  25.                         Res[i][j] += 1ll * MulMod(a[i][k] ,b[k][j]);
  26.  
  27.             return Res;
  28.         }
  29.  
  30.         void DoIdentity (){
  31.             Identity.assign(2 ,vector< Int >(2 ,0));
  32.             for(int i = 0 ; i < 2 ; i++)
  33.                 Identity[i][i] = 1ll;
  34.         }
  35.  
  36.         void UPD(int i ,vector< vector< Int > > v ,int p ,int L ,int R){
  37.             if( i >  R || i <  L ) return;
  38.             if( L == R ) {
  39.                 Seg[p] = v; return;}
  40.  
  41.             int Mid = (L + R) >> 1;
  42.             UPD(i ,v ,lf ,L ,Mid);
  43.             UPD(i ,v ,rg ,Mid + 1 ,R);
  44.             Seg[p] = Compain(Seg[lf] ,Seg[rg]);
  45.         }
  46.  
  47.         vector< vector< Int > > QRY(int i ,int j ,int p ,int L ,int R){
  48.             if( i >  R || L >  j ) return Identity;
  49.             if( i <= L && R <= j ) return Seg[p];
  50.  
  51.             int Mid = (L + R) >> 1;
  52.             vector< vector< Int > > LF = QRY(i ,j ,lf ,L ,Mid);
  53.             vector< vector< Int > > RG = QRY(i ,j ,rg ,Mid + 1 ,R);
  54.             return Compain(LF ,RG);
  55.         }
  56.  
  57.     public:
  58.         SegTree(int n){
  59.             DoIdentity();
  60.             Seg.assign(4 * n ,Identity );
  61.         }
  62.         void UPD(int i ,vector< vector< Int > > v){
  63.             UPD(i ,v ,1 ,1 ,n);
  64.         }
  65.         vector< vector< Int > > QRY(int L ,int R){
  66.             return QRY(L ,R ,1 ,1 ,n);
  67.         }
  68. };
  69.  
  70. int main() {
  71.     scanf("%d%d%d" ,&MOD ,&n ,&m);
  72.     SegTree ST(n + 1);
  73.  
  74.     vector< vector< Int > > V(2 ,vector< Int >(2 ,0));
  75.     for(int idx = 1 ; idx <= n ; idx++){
  76.         for(int i = 0 ; i < 2 ; i++)
  77.             for(int j = 0 ; j < 2 ; j++)
  78.                 scanf("%lld" ,&V[i][j]);
  79.         ST.UPD(idx ,V);
  80.     }
  81.  
  82.     while( m-- ){
  83.             int L ,R; scanf("%d%d" ,&L ,&R);
  84.             Ans = ST.QRY(L ,R);
  85.             for(int i = 0 ; i < 2 ; i++ ,puts(""))
  86.                 for(int j = 0 ; j < 2 ; j++)
  87.                     printf("%lld " ,Ans[i][j]);
  88.             puts("");
  89.     }
  90.     return 0;
  91. }
  92.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement