#include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define int ll
const int mod = 1e9 + 7;
struct SegmentTree {
vector<ll> tree;
int n;
SegmentTree(vector<int>& a) {
n = a.size();
tree.assign(2*n, 0);
for(int i = n; i < 2*n; i++) {
tree[i] = a[i-n];
}
for(int idx = n-1; idx > 0; idx--) {
tree[idx] = tree[2*idx] + tree[2*idx+1];
}
}
void update(int idx, ll val) {
idx += n;
tree[idx] = val;
while(idx > 1) {
idx /= 2;
tree[idx] = tree[2*idx] + tree[2*idx+1];
tree[idx] %= mod;
}
}
ll getSum(int L, int R) {
L += n;
R += n;
ll sum = 0;
while(L < R) {
if (L % 2) {
sum += tree[L];
sum %= mod;
L++;
}
if (R % 2) {
R--;
sum += tree[R];
sum %= mod;
}
L /= 2, R /= 2;
}
return sum;
}
};
const int maxN = 200005;
vector<int> adj[maxN];
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
// freopen("test.in", "r", stdin);
int n, m;
cin >> n >> m;
vector<ll> add(n+1);
for(int i = 0; i < n; i++) {
cin >> add[i];
}
vector<ll> pref(n+1, 0);
pref[0] = 0;
for(int i = 1; i <= n; i++) {
pref[i] = pref[i-1];
pref[i] += add[i-1];
}
for(int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
a--, b--;
adj[b].push_back(a);
}
vector<int> a(n+1, 0);
SegmentTree cnt(a), sum(a);
for(int i = 0; i < n; i++) {
cnt.update(i, cnt.getSum(0, i) + i);
sum.update(i, sum.getSum(i, i+1) + sum.getSum(0, i) + pref[i]);
for(int j : adj[i]) {
cnt.update(i, cnt.getSum(i, i+1) - cnt.getSum(j, j+1) - 1);
sum.update(i, sum.getSum(i, i+1) - add[j] - sum.getSum(j, j+1));
}
sum.update(i, sum.getSum(i, i+1) + (add[i] * cnt.getSum(i, i+1)) % mod);
}
cout << (sum.getSum(0, n) + mod) % mod;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5120 KB |
Output is correct |
2 |
Correct |
5 ms |
4972 KB |
Output is correct |
3 |
Correct |
4 ms |
4972 KB |
Output is correct |
4 |
Correct |
4 ms |
4972 KB |
Output is correct |
5 |
Correct |
4 ms |
4972 KB |
Output is correct |
6 |
Correct |
4 ms |
4972 KB |
Output is correct |
7 |
Correct |
4 ms |
4972 KB |
Output is correct |
8 |
Correct |
4 ms |
4972 KB |
Output is correct |
9 |
Correct |
4 ms |
4972 KB |
Output is correct |
10 |
Correct |
4 ms |
4972 KB |
Output is correct |
11 |
Correct |
5 ms |
4972 KB |
Output is correct |
12 |
Correct |
4 ms |
4972 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5120 KB |
Output is correct |
2 |
Correct |
5 ms |
4972 KB |
Output is correct |
3 |
Correct |
4 ms |
4972 KB |
Output is correct |
4 |
Correct |
4 ms |
4972 KB |
Output is correct |
5 |
Correct |
4 ms |
4972 KB |
Output is correct |
6 |
Correct |
4 ms |
4972 KB |
Output is correct |
7 |
Correct |
4 ms |
4972 KB |
Output is correct |
8 |
Correct |
4 ms |
4972 KB |
Output is correct |
9 |
Correct |
4 ms |
4972 KB |
Output is correct |
10 |
Correct |
4 ms |
4972 KB |
Output is correct |
11 |
Correct |
5 ms |
4972 KB |
Output is correct |
12 |
Correct |
4 ms |
4972 KB |
Output is correct |
13 |
Correct |
28 ms |
6252 KB |
Output is correct |
14 |
Correct |
30 ms |
6272 KB |
Output is correct |
15 |
Correct |
33 ms |
6124 KB |
Output is correct |
16 |
Correct |
12 ms |
5356 KB |
Output is correct |
17 |
Correct |
27 ms |
6252 KB |
Output is correct |
18 |
Correct |
18 ms |
5996 KB |
Output is correct |
19 |
Correct |
6 ms |
5100 KB |
Output is correct |
20 |
Correct |
4 ms |
5100 KB |
Output is correct |
21 |
Correct |
4 ms |
5096 KB |
Output is correct |
22 |
Correct |
4 ms |
5132 KB |
Output is correct |
23 |
Correct |
5 ms |
5100 KB |
Output is correct |
24 |
Correct |
5 ms |
5100 KB |
Output is correct |
25 |
Correct |
4 ms |
5100 KB |
Output is correct |
26 |
Correct |
4 ms |
5100 KB |
Output is correct |
27 |
Correct |
7 ms |
5228 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
6252 KB |
Output is correct |
2 |
Correct |
30 ms |
6272 KB |
Output is correct |
3 |
Correct |
33 ms |
6124 KB |
Output is correct |
4 |
Correct |
12 ms |
5356 KB |
Output is correct |
5 |
Correct |
27 ms |
6252 KB |
Output is correct |
6 |
Correct |
18 ms |
5996 KB |
Output is correct |
7 |
Correct |
6 ms |
5100 KB |
Output is correct |
8 |
Correct |
4 ms |
5100 KB |
Output is correct |
9 |
Correct |
4 ms |
5096 KB |
Output is correct |
10 |
Correct |
4 ms |
5132 KB |
Output is correct |
11 |
Correct |
5 ms |
5100 KB |
Output is correct |
12 |
Correct |
5 ms |
5100 KB |
Output is correct |
13 |
Correct |
4 ms |
5100 KB |
Output is correct |
14 |
Correct |
4 ms |
5100 KB |
Output is correct |
15 |
Correct |
7 ms |
5228 KB |
Output is correct |
16 |
Correct |
4 ms |
5120 KB |
Output is correct |
17 |
Correct |
5 ms |
4972 KB |
Output is correct |
18 |
Correct |
4 ms |
4972 KB |
Output is correct |
19 |
Correct |
4 ms |
4972 KB |
Output is correct |
20 |
Correct |
4 ms |
4972 KB |
Output is correct |
21 |
Correct |
4 ms |
4972 KB |
Output is correct |
22 |
Correct |
4 ms |
4972 KB |
Output is correct |
23 |
Correct |
4 ms |
4972 KB |
Output is correct |
24 |
Correct |
4 ms |
4972 KB |
Output is correct |
25 |
Correct |
4 ms |
4972 KB |
Output is correct |
26 |
Correct |
5 ms |
4972 KB |
Output is correct |
27 |
Correct |
4 ms |
4972 KB |
Output is correct |
28 |
Correct |
42 ms |
6636 KB |
Output is correct |
29 |
Correct |
14 ms |
5612 KB |
Output is correct |
30 |
Correct |
28 ms |
5980 KB |
Output is correct |
31 |
Correct |
13 ms |
5612 KB |
Output is correct |
32 |
Correct |
7 ms |
5356 KB |
Output is correct |
33 |
Correct |
8 ms |
5484 KB |
Output is correct |
34 |
Correct |
30 ms |
6636 KB |
Output is correct |
35 |
Correct |
49 ms |
7404 KB |
Output is correct |
36 |
Correct |
48 ms |
7404 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
6252 KB |
Output is correct |
2 |
Correct |
30 ms |
6272 KB |
Output is correct |
3 |
Correct |
33 ms |
6124 KB |
Output is correct |
4 |
Correct |
12 ms |
5356 KB |
Output is correct |
5 |
Correct |
27 ms |
6252 KB |
Output is correct |
6 |
Correct |
18 ms |
5996 KB |
Output is correct |
7 |
Correct |
6 ms |
5100 KB |
Output is correct |
8 |
Correct |
4 ms |
5100 KB |
Output is correct |
9 |
Correct |
4 ms |
5096 KB |
Output is correct |
10 |
Correct |
4 ms |
5132 KB |
Output is correct |
11 |
Correct |
5 ms |
5100 KB |
Output is correct |
12 |
Correct |
5 ms |
5100 KB |
Output is correct |
13 |
Correct |
4 ms |
5100 KB |
Output is correct |
14 |
Correct |
4 ms |
5100 KB |
Output is correct |
15 |
Correct |
7 ms |
5228 KB |
Output is correct |
16 |
Correct |
42 ms |
6636 KB |
Output is correct |
17 |
Correct |
14 ms |
5612 KB |
Output is correct |
18 |
Correct |
28 ms |
5980 KB |
Output is correct |
19 |
Correct |
13 ms |
5612 KB |
Output is correct |
20 |
Correct |
7 ms |
5356 KB |
Output is correct |
21 |
Correct |
8 ms |
5484 KB |
Output is correct |
22 |
Correct |
30 ms |
6636 KB |
Output is correct |
23 |
Correct |
49 ms |
7404 KB |
Output is correct |
24 |
Correct |
48 ms |
7404 KB |
Output is correct |
25 |
Correct |
4 ms |
5120 KB |
Output is correct |
26 |
Correct |
5 ms |
4972 KB |
Output is correct |
27 |
Correct |
4 ms |
4972 KB |
Output is correct |
28 |
Correct |
4 ms |
4972 KB |
Output is correct |
29 |
Correct |
4 ms |
4972 KB |
Output is correct |
30 |
Correct |
4 ms |
4972 KB |
Output is correct |
31 |
Correct |
4 ms |
4972 KB |
Output is correct |
32 |
Correct |
4 ms |
4972 KB |
Output is correct |
33 |
Correct |
4 ms |
4972 KB |
Output is correct |
34 |
Correct |
4 ms |
4972 KB |
Output is correct |
35 |
Correct |
5 ms |
4972 KB |
Output is correct |
36 |
Correct |
4 ms |
4972 KB |
Output is correct |
37 |
Correct |
195 ms |
17004 KB |
Output is correct |
38 |
Correct |
156 ms |
17524 KB |
Output is correct |
39 |
Correct |
177 ms |
19052 KB |
Output is correct |
40 |
Correct |
291 ms |
20716 KB |
Output is correct |
41 |
Correct |
163 ms |
17772 KB |
Output is correct |
42 |
Correct |
230 ms |
20588 KB |
Output is correct |
43 |
Correct |
163 ms |
16192 KB |
Output is correct |
44 |
Correct |
194 ms |
17388 KB |
Output is correct |
45 |
Correct |
233 ms |
17528 KB |
Output is correct |