Alex_tz307

monede2 100p

Dec 3rd, 2020
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.51 KB | None | 0 0
  1.  
  2. #include <bits/stdc++.h>
  3.  
  4. using namespace std;
  5.  
  6. struct Raport {
  7.     int a, b;
  8.     Raport(int, int);
  9.     Raport();
  10.  
  11.     Raport& operator+=(const Raport&);
  12.     Raport& operator-=(const Raport&);
  13.     Raport& operator*=(const Raport&);
  14.     Raport& operator/=(const Raport&);
  15.  
  16.     bool operator==(const Raport& rhs) const;
  17.     bool operator!=(const Raport& rhs) const;
  18. };
  19.  
  20. Raport operator+(Raport, const Raport&);
  21. Raport operator-(Raport, const Raport&);
  22. Raport operator*(Raport, const Raport&);
  23. Raport operator/(Raport, const Raport&);
  24.  
  25. Raport::Raport(int A, int B) : a(A), b(B) {}
  26.  
  27. Raport::Raport() : Raport(0, 1) {}
  28.  
  29. Raport& Raport::operator+=(const Raport& rhs) {
  30.     a = ((long long)a * rhs.b + (long long)b * rhs.a) % 1000000007;
  31.     b = ((long long)b * rhs.b) % 1000000007;
  32.     return *this;
  33. }
  34.  
  35. Raport& Raport::operator-=(const Raport& rhs) {
  36.     a = ((long long)a * rhs.b - (long long)b * rhs.a) % 1000000007;
  37.     if (a < 0)
  38.         a += 1000000007;
  39.     b = ((long long)b * rhs.b) % 1000000007;
  40.     return *this;
  41. }
  42.  
  43. Raport& Raport::operator*=(const Raport& rhs) {
  44.     a = ((long long)a * rhs.a) % 1000000007;
  45.     b = ((long long)b * rhs.b) % 1000000007;
  46.     return *this;
  47. }
  48.  
  49. Raport& Raport::operator/=(const Raport& rhs) {
  50.     a = ((long long)a * rhs.b) % 1000000007;
  51.     b = ((long long)b * rhs.a) % 1000000007;
  52.     return *this;
  53. }
  54.  
  55. bool Raport::operator==(const Raport& rhs) const {
  56.     return ((long long)a * rhs.b - (long long)b * rhs.a) % 1000000007 == 0;
  57. }
  58.  
  59. bool Raport::operator!=(const Raport& rhs) const { return !(*this == rhs); }
  60.  
  61. Raport operator+(Raport a, const Raport& b) { return a += b; }
  62.  
  63. Raport operator-(Raport a, const Raport& b) { return a -= b; }
  64.  
  65. Raport operator*(Raport a, const Raport& b) { return a *= b; }
  66.  
  67. Raport operator/(Raport a, const Raport& b) { return a /= b; }
  68.  
  69.  
  70. class InParser {
  71. private:
  72.     FILE* fin;
  73.     char* buff;
  74.     int sp;
  75.     char read_ch() {
  76.         ++sp;
  77.         if (sp == 16384) {
  78.             sp = 0;
  79.             fread(buff, 1, 16384, fin);
  80.         }
  81.         return buff[sp];
  82.     }
  83. public:
  84.     InParser(const char* nume) {
  85.         fin = fopen(nume, "r");
  86.         buff = new char[16384]();
  87.         sp = 16383;
  88.     }
  89.  
  90.     InParser& operator >> (int& n) {
  91.         char c;
  92.         while (!isdigit(c = read_ch()));
  93.         n = c - '0';
  94.         while (isdigit(c = read_ch()))
  95.             n = 10 * n + c - '0';
  96.         return *this;
  97.     }
  98. };
  99. class OutParser {
  100. private:
  101.     FILE* fout;
  102.     char* buff;
  103.     int sp;
  104.     void write_ch(char ch) {
  105.         if (sp == 50000) {
  106.             fwrite(buff, 1, 50000, fout);
  107.             sp = 0;
  108.         }
  109.         buff[sp++] = ch;
  110.     }
  111. public:
  112.     OutParser(const char* name) {
  113.         fout = fopen(name, "w");
  114.         buff = new char[50000]();
  115.         sp = 0;
  116.     }
  117.     ~OutParser() {
  118.         fwrite(buff, 1, sp, fout);
  119.         fclose(fout);
  120.     }
  121.     OutParser& operator << (int n) {
  122.         if(n < 10)
  123.             write_ch(n + '0');
  124.         else {
  125.             (*this) << (n / 10);
  126.             write_ch(n % 10 + '0');
  127.         }
  128.         return *this;
  129.  
  130.     }
  131.     OutParser& operator << (const char& ch) {
  132.         write_ch(ch);
  133.         return *this;
  134.     }
  135. };
  136.  
  137. inline Raport Merge(const Raport& a, const Raport& b) {
  138.     return a + b - Raport(2, 1) * a * b;
  139. }
  140.  
  141. int main() {
  142.     InParser fin("monede2.in");
  143.     OutParser fout("monede2.out");
  144.     Raport Tree[1 << 21];
  145.     int N, Q, x, y;
  146.     fin >> N >> Q;
  147.     for(int i = 0; i < N; i++) {
  148.         fin >> x >> y;
  149.         Tree[N + i] = {x, y};
  150.     }
  151.     for(int i = N - 1; i; i--)
  152.         Tree[i] = Merge(Tree[i << 1], Tree[i << 1 | 1]);
  153.     while(Q--) {
  154.         fin >> x >> y;
  155.         x += N - 1, y += N - 1;
  156.         Raport R(0, 1);
  157.         while(x <= y) {
  158.             if(x & 1) {
  159.                 R = Merge(R, Tree[x]);
  160.                 ++x;
  161.             }
  162.             if(!(y & 1)) {
  163.                 R = Merge(R, Tree[y]);
  164.                 --y;
  165.             }
  166.             x >>= 1, y >>= 1;
  167.         }
  168.         fout << R.a << ' ' << R.b << '\n';
  169.     }
  170. }
  171.  
Advertisement
Add Comment
Please, Sign In to add comment