-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathequi.cpp
67 lines (51 loc) · 1.11 KB
/
equi.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
#include <bits/stdc++.h>
#include <atcoder/segtree>
using namespace atcoder;
using namespace std;
using ll = long long;
using pi = pair<ll, ll>;
using vi = vector<ll>;
using vpi = vector<pi>;
using vb = vector<bool>;
ll mapping(ll x, ll y) {
return min(x, y);
}
ll mapping2(ll x, ll y) {
return max(x, y);
}
ll e() {
return 0;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll n, q;
cin >> n >> q;
vi a(n);
for (ll i = 0; i < n; i++) {
cin >> a[i];
}
for (ll i = 0; i < n; i++) {
ll b;
cin >> b;
a[i] -= b;
}
vi psum(n + 1);
for (ll i = 1; i <= n; i++) {
psum[i] = psum[i - 1] + a[i];
}
segtree<ll, mapping, e> st(psum);
segtree<ll, mapping2, e> st2(psum);
for (ll i = 0; i < q; i++) {
ll l, r;
cin >> l >> r;
ll mn = st.prod(l, r) - psum[l - 1];
ll mx = st2.prod(l, r) - psum[l - 1];
ll sum = psum[r] - psum[l - 1];
if (sum == 0 || mx > 0) cout << -1 << endl;
else {
cout << -mn << endl;
}
}
return 0;
}