-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathADATAXES.cpp
99 lines (99 loc) · 2.5 KB
/
ADATAXES.cpp
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
// Ivan Carvalho
// Solution to https://www.spoj.com/problems/ADATAXES/
#include <algorithm>
#include <cstdio>
#include <vector>
#define LSOne(S) (S & (-S))
#define MP make_pair
#define gc getchar_unlocked
inline void getint(int &x) {
register int c = gc();
x = 0;
for (; (c < 48 || c > 57); c = gc())
;
for (; c > 47 && c < 58; c = gc()) {
x = (x << 1) + (x << 3) + c - 48;
}
}
inline void print(int n) {
char buf[11];
buf[10] = '\n';
int i = 9;
while (n) {
buf[i--] = n % 10 + '0';
n /= 10;
}
while (buf[i] != '\n') putchar_unlocked(buf[++i]);
}
using namespace std;
typedef pair<int, int> i2;
typedef pair<int, i2> i3;
typedef pair<int, i3> i4;
typedef long long ll;
const int MAXN = 3 * 1e5 + 10;
const ll MOD = 1e9 + 7;
ll bit[MAXN], exibir[MAXN];
vector<i2> atualizacoes;
vector<i4> perguntas;
int N, Q;
ll fast_expo(ll z, ll expo) {
if (expo == 0) return 1LL;
if (expo == 1) return z;
if (expo % 2 == 0) {
ll temp = fast_expo(z, expo / 2);
return (temp * temp) % MOD;
}
return (z * fast_expo(z, expo - 1)) % MOD;
}
inline ll inv(ll z) {
if (z == 1) return z;
return fast_expo(z, MOD - 2);
}
inline void update(int pos, ll delta) {
while (pos <= N) {
bit[pos] = (bit[pos] * delta) % MOD;
pos += LSOne(pos);
}
}
inline ll read(int pos) {
ll ans = 1;
while (pos > 0) {
ans = (ans * bit[pos]) % MOD;
pos -= LSOne(pos);
}
return ans;
}
inline ll query(int a, int b) { return (read(b) * inv(read(a - 1))) % MOD; }
int main() {
getint(N);
getint(Q);
for (int i = 1; i <= N; i++) {
int val;
getint(val);
atualizacoes.push_back(MP(val, i));
bit[i] = 1;
}
sort(atualizacoes.begin(), atualizacoes.end());
for (int i = 1; i <= Q; i++) {
int l, r, h;
getint(l);
getint(r);
getint(h);
perguntas.push_back(MP(h, MP(i, MP(l + 1, r + 1))));
}
sort(perguntas.begin(), perguntas.end());
int ptr = 0;
for (int i = 0; i < Q; i++) {
int h = perguntas[i].first;
int idx = perguntas[i].second.first;
int l = perguntas[i].second.second.first;
int r = perguntas[i].second.second.second;
while (ptr < N && atualizacoes[ptr].first <= h) {
update(atualizacoes[ptr].second, (ll)atualizacoes[ptr].first);
ptr++;
}
exibir[idx] = query(l, r);
}
for (int i = 1; i <= Q; i++) print((int)exibir[i]);
return 0;
}