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
#include <cstdio>
#define MDL 1000000007
long long fts; //fighters
int a[500005];
int b[500005];
bool ovf = false;
int main(){
    int x,l,r,n,q;
    scanf("%d %d",&n,&q);
    for(int i=1; i<=n; i++){
        scanf("%d %d",&a[i],&b[i]);
    }
    for(int i=0; i<q; i++){
        scanf("%d %d %d",&x,&l,&r);
        fts = x;
        for(int j=l+1; j<=r; j++){
            if(ovf){
                if( b[j]==1 )
                    fts = (fts + a[j]) % MDL;
                else
                    fts = (fts * b[j]) % MDL;

            } else {

                if (fts + a[j] > fts * b[j])
                    fts = (fts + a[j]) % MDL;
                else{
                    if(ovf == false)
                        if(fts * b[j] > MDL)
                            ovf = true;
                    fts = (fts * b[j]) % MDL;
                }

            }
        }
        printf("%lld\n",fts);
    }
    return 0;
}