Alex_tz307

monede2 70p - recursiv

Dec 3rd, 2020 (edited)
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.49 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. ifstream fin("monede2.in");
  6. ofstream fout("monede2.out");
  7.  
  8. class Raport {
  9.   public:
  10.     int a, b;
  11.     Raport(int, int);
  12.     Raport();
  13.  
  14.     Raport &operator+=(const Raport &);
  15.     Raport &operator-=(const Raport &);
  16.     Raport &operator*=(const Raport &);
  17.     Raport &operator/=(const Raport &);
  18.  
  19.     bool operator==(const Raport &rhs) const;
  20.     bool operator!=(const Raport &rhs) const;
  21. };
  22.  
  23. Raport operator+(Raport, const Raport &);
  24. Raport operator-(Raport, const Raport &);
  25. Raport operator*(Raport, const Raport &);
  26. Raport operator/(Raport, const Raport &);
  27.  
  28. Raport::Raport(int A, int B) : a(A), b(B) {}
  29.  
  30. Raport::Raport() : Raport(0, 1) {}
  31.  
  32. Raport &Raport::operator+=(const Raport &rhs) {
  33.     a = ((long long)a * rhs.b + (long long)b * rhs.a) % 1000000007;
  34.     b = ((long long)b * rhs.b) % 1000000007;
  35.     return *this;
  36. }
  37.  
  38. Raport &Raport::operator-=(const Raport &rhs) {
  39.     a = ((long long)a * rhs.b - (long long)b * rhs.a) % 1000000007;
  40.     if (a < 0)
  41.         a += 1000000007;
  42.     b = ((long long)b * rhs.b) % 1000000007;
  43.     return *this;
  44. }
  45.  
  46. Raport &Raport::operator*=(const Raport &rhs) {
  47.     a = ((long long)a * rhs.a) % 1000000007;
  48.     b = ((long long)b * rhs.b) % 1000000007;
  49.     return *this;
  50. }
  51.  
  52. Raport &Raport::operator/=(const Raport &rhs) {
  53.     a = ((long long)a * rhs.b) % 1000000007;
  54.     b = ((long long)b * rhs.a) % 1000000007;
  55.     return *this;
  56. }
  57.  
  58. bool Raport::operator==(const Raport &rhs) const {
  59.     return ((long long)a * rhs.b - (long long)b * rhs.a) % 1000000007 == 0;
  60. }
  61.  
  62. bool Raport::operator!=(const Raport &rhs) const { return !(*this == rhs); }
  63.  
  64. Raport operator+(Raport a, const Raport &b) { return a += b; }
  65.  
  66. Raport operator-(Raport a, const Raport &b) { return a -= b; }
  67.  
  68. Raport operator*(Raport a, const Raport &b) { return a *= b; }
  69.  
  70. Raport operator/(Raport a, const Raport &b) { return a /= b; }
  71.  
  72. class SegTree {
  73.     public:
  74.         int sz = 1;
  75.         vector<Raport> Tree;
  76.  
  77.         void init(int N) {
  78.             sz = 1;
  79.             while(sz < N)
  80.                 sz <<= 1;
  81.             Tree.resize((sz << 1) | 1);
  82.         }
  83.  
  84.         Raport Merge(Raport a, Raport b) {
  85.             return a * (Raport(1, 1) - b) + (Raport(1, 1) - a) * b;
  86.         }
  87.  
  88.         void build(int x, int lx, int rx) {
  89.             if(lx == rx) {
  90.                 int a, b;
  91.                 fin >> a >> b;
  92.                 Tree[x] = Raport(a, b);
  93.                 return;
  94.             }
  95.             int mid = (lx + rx) >> 1;
  96.             build(x << 1, lx, mid);
  97.             build((x << 1) | 1, mid + 1, rx);
  98.             Tree[x] = Merge(Tree[x << 1], Tree[(x << 1) | 1]);
  99.         }
  100.  
  101.         Raport query(int x, int lx, int rx, int st, int dr) {
  102.             if(st <= lx && rx <= dr)
  103.                 return Tree[x];
  104.             int mid = (lx + rx) >> 1;
  105.             Raport ans1(0, 1), ans2(0, 1);
  106.             if(st <= mid)
  107.                 ans1 = query(x << 1, lx, mid, st, dr);
  108.             if(mid < dr)
  109.                 ans2 = query((x << 1) | 1, mid + 1, rx, st, dr);
  110.             return Merge(ans1, ans2);
  111.         }
  112. };
  113.  
  114. int main() {
  115.     fin.sync_with_stdio(false);
  116.     fout.sync_with_stdio(false);
  117.     fin.tie(nullptr);
  118.     fout.tie(nullptr);
  119.     int N, Q;
  120.     fin >> N >> Q;
  121.     SegTree Tree;
  122.     Tree.init(N);
  123.     Tree.build(1, 1, N);
  124.     while(Q--) {
  125.         int st, dr;
  126.         fin >> st >> dr;
  127.         Raport R = Tree.query(1, 1, N, st, dr);
  128.         fout << R.a << ' ' << R.b << '\n';
  129.     }
  130. }
  131.  
Advertisement
Add Comment
Please, Sign In to add comment