1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#define NDEBUG

#include <algorithm>
#include <cstdio>
#include <cassert>
#include <vector>

const int M = 1000000007;
const long long MM = M;

int N, Q;
int A[500000], B[500000];

int mulimit(int x, int y) {
  return std::min(MM, static_cast<long long>(x) * static_cast<long long>(y));
}

int mulmod(int x, int y, bool* overfloat = nullptr) {
  long long xy = static_cast<long long>(x) * static_cast<long long>(y);
  if (xy >= MM) {
    xy %= MM;
    if (overfloat) *overfloat = true;
  }
  return xy;
}

int divrounddown(int x, int y) {
  return x / y;
}

struct Funkcja
{
  // y = a + b*x
  int a = 0;
  int b = 1;
  bool aOF = false, bOF = false;

  void operator*=(const int c)
  {
    a = mulmod(a, c, &aOF);
    b = mulmod(b, c, &bOF);
  }

  void operator+=(const int c)
  {
    a += c;
    if (a >= M) {
      a %= M;
      aOF = true;
    }
  }
  
  int evalmod(const int x, bool& overflow) const
  {
    long long bx = mulmod(b, x, &overflow);
    if (bOF) overflow = true;
    long long result = static_cast<long long>(a) + bx;
    if (result >= MM) overflow = true;
    return result % MM;
  }

  long long evalimit(const int x) const
  {
    if (aOF || (x > 0 && bOF)) return M;
    int bx = mulimit(b, x);
    return std::min(M, a + bx);
  }
};

struct Tree
{
  int min=0, max=M-1;
  int minval=0, maxval=M-1;
  Funkcja f;
  Tree* left = nullptr;
  Tree* right = nullptr;
  
  Tree() {
  }

  Tree(int min, int max, const Funkcja& f) : min(min), max(max), f(f) {
    minval = f.evalimit(min);
    maxval = f.evalimit(max);
  }
  
  void change(int a, int b)
  {
    // zawsze i tak przejedziemy całą dziedzinę funkcji
    // więc nie ma co się ograniczać
    if (left) {
      left->change(a, b);
      right->change(a, b);

      minval = left->minval;
      maxval = right->maxval;
    } else {

      int upto; // dodajemy do upto włącznie, mnożymy od upto+1
      if (b == 1) {
        upto = M;
      } else if (a == 0) {
        upto = -1;
      } else {
        upto = a / (b - 1);
      }

      if (maxval <= upto) {
        // tylko dodajemy
        f += a;
        minval = std::min(M, minval + a);
        maxval = std::min(M, maxval + a);

      } else if (upto < minval || f.aOF || f.bOF || upto < f.a) {
        // tylko mnożymy
        f *= b;
        minval = mulimit(minval, b);
        maxval = mulimit(maxval, b);

      } else {
        // dodajemy do upto włącznie
        // czyli musimy wyznaczyć odpowiedni x
        int upto_x = divrounddown(upto - f.a, f.b);
        assert(min <= upto_x);
        assert(upto_x+1 <= max);

        Funkcja fL(f), fR(f);
        fL += a;
        fR *= b;

        left = new Tree(min, upto_x, fL);
        right = new Tree(upto_x+1, max, fR);

        minval = left->minval;
        maxval = right->maxval;
      }
    }
  }

  int evalmod(const int x, bool& overflow) const
  {
    if (left) {
      if (x <= left->max && !overflow) {
        return left->evalmod(x, overflow);
      } else {
        return right->evalmod(x, overflow);
      }
    } else {
      return f.evalmod(x, overflow);
    }
  }

  void wypisz() const
  {
    if (left) {
      printf("[%d-%d]: (values %d-%d)\n", min, max, minval, maxval);
      left->wypisz();
      right->wypisz();
    } else {
      printf("[%d-%d]: %d + %d x (values %d-%d)\n", min, max, f.a, f.b, minval, maxval);
    }
  }
};

void fill(Tree& tree, int l, int r)
{
  for (int i=l; i<r; ++i) {
    tree.change(A[i], B[i]);
  }
}

#define FACTOR 16
#define MAX_L 20
std::vector<Tree> cache[MAX_L+1];

int dawaj(int L, int x, int l, int r, bool& overflow)
{
  int upl = (l + FACTOR - 1) / FACTOR;
  int upr = r / FACTOR;
  
  if (upl < upr) {
    int r1 = upl * FACTOR;
    for (int i=l; i<r1; ++i) {
      x = cache[L][i].evalmod(x, overflow);
    }
    x = dawaj(L+1, x, upl, upr, overflow);
    int l1 = upr * FACTOR;
    for (int i=l1; i<r; ++i) {
      x = cache[L][i].evalmod(x, overflow);
    }
  } else {
    for (int i=l; i<r; ++i) {
      x = cache[L][i].evalmod(x, overflow);
    }
  }
  return x;
}

int main()
{
  scanf("%d%d", &N, &Q);
  cache[0].resize(N);
  for (int i=0; i<N; ++i) {
    scanf("%d%d", &A[i], &B[i]);
    cache[0][i].change(A[i], B[i]);
  }
  
  int chunk_size = 1;
  for (int L=1; L<MAX_L; ++L) {
    chunk_size *= FACTOR;
    const int chunk_count = N / chunk_size;
    if (chunk_count == 0) break;

    cache[L].resize(chunk_count);
    for (int c=0; c<chunk_count; ++c) {
      fill(cache[L][c], c*chunk_size, (c+1)*chunk_size);
    }
  }

  for (int q=0; q<Q; ++q) {
    int x, l, r;
    scanf("%d%d%d", &x, &l, &r);

    bool overflow = false;
    x = dawaj(0, x, l, r, overflow);
    printf("%d\n", x);
  }
}