/* -----------------------
Autor: Tomasz Boguslawski
-------------------------- */
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>
#include<sstream>
#include<cstring>
#include<map>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include <fstream>
#include<math.h>
#define LL long long
#define FOR(x, b, e) for(LL x = b; x <= (e); x++)
#define FORS(x, b, e, s) for(LL x = b; x <= (e); x+=s)
#define FORD(x, b, e) for(LL x = b; x >= (e); x--)
#define VAR(v, n) __typeof(n) v = (n)
#define ALL(c) (c).begin(), (c).end()
#define FOREACH(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)
#define DEBUG if (debug)
#define MIN(a,b) ((a>b)?b:a)
#define MAX(a,b) ((a>b)?a:b)
using namespace std;
#define M 1000000007
class Node
{
public:
LL mu, ad;
};
LL n,q;
LL a[501000];
LL b[501000];
LL g[501000];
LL ps[501000];
Node pm[501000];
LL endOfBlock[501000];
LL maxG;
LL expMod(LL a, LL pow)
{
LL res=1;
LL p = a;
while (pow!=0)
{
if (pow%2==1) res = (res * p) %M;
p = (p*p) %M;
pow = pow / 2;
}
return res;
}
void readData()
{
cin >>n; cin >>q;
FOR(i,1,n) { cin >> a[i]; cin >> b[i]; }
}
void prepare()
{
maxG=0;
LL summ = 0; ps[0]=0;
LL mm=1; LL aa=0; pm[0].mu=mm; pm[0].ad=aa;
FOR(i,1,n)
{
if (b[i]==1)
{
g[i]=M+3;
aa = (aa + a[i]) %M;
}
else
{
g[i]=a[i] / (b[i]-1);
if (g[i]>maxG) maxG=g[i];
mm = (mm * b[i]) %M;
aa = (aa * b[i]) %M;
}
endOfBlock[i]=0;
summ+=a[i];
ps[i]=summ;
pm[i].mu=mm; pm[i].ad=aa;
}
LL start=0;
b[n+1]=2;
FOR(i,1,n+1)
{
if (b[i]==1)
{
if (start==0) start=i;
}
else
{
if (start!=0)
{
LL ends = i-1;
//cout << "block: " << start << ".." << ends << "\n";
FOR(j,start,ends) endOfBlock[j]=ends;
start=0;
}
}
}
}
LL answerFor(LL x, LL i1, LL i2)
{
LL index = i1+1; // next index to handle
// part1: when x <= maxG
LL ends;
LL blockSum=0;
while ((index<=i2) && (x<=maxG))
{
if (b[index]==1)
{
// we are in the block of "1"s
ends=MIN(i2, endOfBlock[index]);
blockSum=ps[ends]-ps[index-1];
x+=blockSum;
index=ends+1;
}
else
{
// b[i] is at least 2
if (x<=g[index]) { x+=a[index]; } else { x*=b[index]; }
index++;
}
}
x = x % M;
//cout << "-- After part 1: index=" << index << " x=" << x << "\n";
if (index<=i2)
{
//cout << "multsum for " << index << ".." << i2 << "\n";
LL m2=pm[i2].mu; LL a2=pm[i2].ad;
LL m1=pm[index-1].mu; LL a1=pm[index-1].ad;
LL m3 = (m2 * expMod(m1, M-2)) %M;
LL a3 = (a2 - (m3 * a1)%M)%M; if (a3<0) a3+=M;
x = (x * m3) %M;
x = (x + a3) %M;
}
return x;
}
LL simpleAnswerFor(LL x, LL i1, LL i2)
{
bool small=true;
FOR(i,i1+1,i2)
{
if ( (small && (x<=g[i])) || (b[i]==1) )
{
x = x + a[i];
if (x >= M)
{
x = x - M;
small=false;
}
}
else
{
x = (x * b[i]);
if (x >= M)
{
small=false;
x = x%M;
}
}
}
return x;
}
void handleQueries()
{
LL x, i1, i2;
FOR(query,1,q)
{
cin >> x; cin >> i1; cin >> i2;
cout << answerFor(x,i1,i2) << "\n";
}
}
/// MAIN
int main(int argc, char* argv[])
{
// magic formula, which makes streams work faster:
ios_base::sync_with_stdio(0);
readData();
prepare();
handleQueries();
return 0;
}
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 | /* ----------------------- Autor: Tomasz Boguslawski -------------------------- */ #include<cstdio> #include<cstdlib> #include<iostream> #include<fstream> #include<iomanip> #include<string> #include<sstream> #include<cstring> #include<map> #include<vector> #include<set> #include<queue> #include<algorithm> #include <fstream> #include<math.h> #define LL long long #define FOR(x, b, e) for(LL x = b; x <= (e); x++) #define FORS(x, b, e, s) for(LL x = b; x <= (e); x+=s) #define FORD(x, b, e) for(LL x = b; x >= (e); x--) #define VAR(v, n) __typeof(n) v = (n) #define ALL(c) (c).begin(), (c).end() #define FOREACH(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define DEBUG if (debug) #define MIN(a,b) ((a>b)?b:a) #define MAX(a,b) ((a>b)?a:b) using namespace std; #define M 1000000007 class Node { public: LL mu, ad; }; LL n,q; LL a[501000]; LL b[501000]; LL g[501000]; LL ps[501000]; Node pm[501000]; LL endOfBlock[501000]; LL maxG; LL expMod(LL a, LL pow) { LL res=1; LL p = a; while (pow!=0) { if (pow%2==1) res = (res * p) %M; p = (p*p) %M; pow = pow / 2; } return res; } void readData() { cin >>n; cin >>q; FOR(i,1,n) { cin >> a[i]; cin >> b[i]; } } void prepare() { maxG=0; LL summ = 0; ps[0]=0; LL mm=1; LL aa=0; pm[0].mu=mm; pm[0].ad=aa; FOR(i,1,n) { if (b[i]==1) { g[i]=M+3; aa = (aa + a[i]) %M; } else { g[i]=a[i] / (b[i]-1); if (g[i]>maxG) maxG=g[i]; mm = (mm * b[i]) %M; aa = (aa * b[i]) %M; } endOfBlock[i]=0; summ+=a[i]; ps[i]=summ; pm[i].mu=mm; pm[i].ad=aa; } LL start=0; b[n+1]=2; FOR(i,1,n+1) { if (b[i]==1) { if (start==0) start=i; } else { if (start!=0) { LL ends = i-1; //cout << "block: " << start << ".." << ends << "\n"; FOR(j,start,ends) endOfBlock[j]=ends; start=0; } } } } LL answerFor(LL x, LL i1, LL i2) { LL index = i1+1; // next index to handle // part1: when x <= maxG LL ends; LL blockSum=0; while ((index<=i2) && (x<=maxG)) { if (b[index]==1) { // we are in the block of "1"s ends=MIN(i2, endOfBlock[index]); blockSum=ps[ends]-ps[index-1]; x+=blockSum; index=ends+1; } else { // b[i] is at least 2 if (x<=g[index]) { x+=a[index]; } else { x*=b[index]; } index++; } } x = x % M; //cout << "-- After part 1: index=" << index << " x=" << x << "\n"; if (index<=i2) { //cout << "multsum for " << index << ".." << i2 << "\n"; LL m2=pm[i2].mu; LL a2=pm[i2].ad; LL m1=pm[index-1].mu; LL a1=pm[index-1].ad; LL m3 = (m2 * expMod(m1, M-2)) %M; LL a3 = (a2 - (m3 * a1)%M)%M; if (a3<0) a3+=M; x = (x * m3) %M; x = (x + a3) %M; } return x; } LL simpleAnswerFor(LL x, LL i1, LL i2) { bool small=true; FOR(i,i1+1,i2) { if ( (small && (x<=g[i])) || (b[i]==1) ) { x = x + a[i]; if (x >= M) { x = x - M; small=false; } } else { x = (x * b[i]); if (x >= M) { small=false; x = x%M; } } } return x; } void handleQueries() { LL x, i1, i2; FOR(query,1,q) { cin >> x; cin >> i1; cin >> i2; cout << answerFor(x,i1,i2) << "\n"; } } /// MAIN int main(int argc, char* argv[]) { // magic formula, which makes streams work faster: ios_base::sync_with_stdio(0); readData(); prepare(); handleQueries(); return 0; } |
English