aboutsummaryrefslogtreecommitdiff
path: root/18/day1/tastyhay.cpp
diff options
context:
space:
mode:
authorAnthony Wang2020-10-06 17:25:38 -0500
committerAnthony Wang2020-10-06 17:25:38 -0500
commitd1791b90ae00a4f688218a9810e55d1db5311cdc (patch)
tree943c8bba67605bfdfae952e5c978eb4cab2dec4a /18/day1/tastyhay.cpp
parentb356f63f0ba062096adf52ca69cfc366001c68a4 (diff)
Restructured directoriesHEADmaster
Diffstat (limited to '18/day1/tastyhay.cpp')
-rw-r--r--18/day1/tastyhay.cpp32
1 files changed, 32 insertions, 0 deletions
diff --git a/18/day1/tastyhay.cpp b/18/day1/tastyhay.cpp
new file mode 100644
index 0000000..8ef6d42
--- /dev/null
+++ b/18/day1/tastyhay.cpp
@@ -0,0 +1,32 @@
+#include <bits/stdc++.h>
+#define f first
+#define s second
+using namespace std;
+using ll = long long;
+using ii = pair<int, int>;
+
+int t[100001];
+map<int, ll> cnt, pre;
+
+int main() {
+ int N, Q;
+ cin >> N >> Q;
+ for (int i = 0; i < N; ++i) cin >> t[i];
+
+ unordered_map<int, int> m;
+ for (int i = N - 1; i >= 0; --i) {
+ unordered_map<int, int> tmp;
+ for (auto x : m) tmp[__gcd(t[i], x.f)] += x.s;
+ ++tmp[t[i]];
+ swap(tmp, m);
+ for (auto& x : m) cnt[x.f] += x.s;
+ }
+ for (auto& x : cnt) pre[x.f] = x.s + (pre.size() ? pre.rbegin()->s : 0);
+
+ while (Q--) {
+ int a, b;
+ cin >> a >> b;
+ auto ia = pre.upper_bound(a - 1), ib = pre.upper_bound(b);
+ cout << (ib != pre.begin() ? prev(ib)->s : 0) - (ia != pre.begin() ? prev(ia)->s : 0) << '\n';
+ }
+} \ No newline at end of file