#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int mod = 1e9 + 7;
#define int ll
struct Node {
ll sum;
ll delta;
Node() {
sum = 0;
delta = 0;
}
void Reset() {
delta = 0;
}
};
struct SegTree {
int n;
vector<Node> tree;
SegTree(vector<int>& a) {
n = a.size();
tree.assign(4*n, Node());
}
void build_r(vector<int>& a, int l, int r, int idx) {
if (l == r) {
if (l < n)
tree[idx].sum = a[l];
else
tree[idx].sum = 0;
return;
}
int mid = (l + r) / 2;
build_r(a, l, mid, 2*idx+1);
build_r(a, mid+1, r, 2*idx+2);
tree[idx].sum = (tree[2*idx+1].sum + tree[2*idx+2].sum) % mod;
}
void applyAggr(int idx, int l, int r) {
tree[idx].sum = (tree[idx].sum + ((r-l+1) * tree[idx].delta) % mod) % mod;
tree[idx].sum += mod;
tree[idx].sum %= mod;
if (l != r) {
compose(idx, 2*idx+1);
compose(idx, 2*idx+2);
}
tree[idx].Reset();
}
void compose(int par, int child) {
tree[child].delta = (((tree[child].delta + tree[par].delta) % mod) + mod) % mod;
}
void updateSum(int L, int R, int val) {
updateSum_r(0, 0, n-1, L, R, val);
}
void updateSum_r(int idx, int l, int r, int& L, int& R, int& val) {
if (r < L || R < l || l >= n) {
return;
}
if (L <= l && R >= r) {
tree[idx].delta += val;
tree[idx].delta += mod;
tree[idx].delta %= mod;
return;
}
applyAggr(idx, l, r);
int mid = (l + r) / 2;
updateSum_r(2*idx+1, l, mid, L, R, val);
updateSum_r(2*idx+2, mid+1, r, L, R, val);
applyAggr(2*idx+1, l, mid);
applyAggr(2*idx+2, mid+1, r);
tree[idx].sum = (((tree[2*idx+1].sum + tree[2*idx+2].sum) % mod) + mod) % mod;
}
ll getSum(int L, int R) {
return getSum_r(0, 0, n-1, L, R);
}
ll getSum_r(int idx, int l, int r, int& L, int& R) {
if (r < L || R < l || l >= n)
return 0;
applyAggr(idx, l, r);
if (L <= l && R >= r)
return ((tree[idx].sum % mod) + mod) % mod;
int mid = (l + r) / 2;
return ((getSum_r(2*idx+1, l, mid, L, R) + getSum_r(2*idx+2, mid+1, r, L, R) % mod) + mod) % mod;
}
};
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
// freopen("test.in", "r", stdin);
int n, m;
cin >> n >> m;
vector<int> adj[n+2], add(n);
for(int i = 0; i < n; i++) {
cin >> add[i];
}
for(int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
a--, b--;
adj[a].push_back(b);
}
vector<int> a(n+1, 0);
SegTree sum(a), cnt(a);
for(int i = 0; i < n; i++) {
sum.updateSum(i, i, cnt.getSum(i, i) * add[i]);
cnt.updateSum(i+1, n-1, cnt.getSum(i, i) + 1);
sum.updateSum(i+1, n-1, sum.getSum(i, i) + add[i]);
for(int j : adj[i]) {
cnt.updateSum(j, j, -cnt.getSum(i, i) - 1);
sum.updateSum(j, j, -sum.getSum(i, i) - add[i]);
}
}
cout << sum.getSum(0, n-1) << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
512 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
512 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
154 ms |
1644 KB |
Output is correct |
14 |
Correct |
151 ms |
1644 KB |
Output is correct |
15 |
Correct |
126 ms |
1388 KB |
Output is correct |
16 |
Correct |
50 ms |
720 KB |
Output is correct |
17 |
Correct |
136 ms |
1260 KB |
Output is correct |
18 |
Correct |
95 ms |
876 KB |
Output is correct |
19 |
Correct |
7 ms |
492 KB |
Output is correct |
20 |
Correct |
3 ms |
364 KB |
Output is correct |
21 |
Correct |
3 ms |
364 KB |
Output is correct |
22 |
Correct |
2 ms |
364 KB |
Output is correct |
23 |
Correct |
6 ms |
364 KB |
Output is correct |
24 |
Correct |
2 ms |
364 KB |
Output is correct |
25 |
Correct |
2 ms |
364 KB |
Output is correct |
26 |
Correct |
3 ms |
364 KB |
Output is correct |
27 |
Correct |
17 ms |
492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
154 ms |
1644 KB |
Output is correct |
2 |
Correct |
151 ms |
1644 KB |
Output is correct |
3 |
Correct |
126 ms |
1388 KB |
Output is correct |
4 |
Correct |
50 ms |
720 KB |
Output is correct |
5 |
Correct |
136 ms |
1260 KB |
Output is correct |
6 |
Correct |
95 ms |
876 KB |
Output is correct |
7 |
Correct |
7 ms |
492 KB |
Output is correct |
8 |
Correct |
3 ms |
364 KB |
Output is correct |
9 |
Correct |
3 ms |
364 KB |
Output is correct |
10 |
Correct |
2 ms |
364 KB |
Output is correct |
11 |
Correct |
6 ms |
364 KB |
Output is correct |
12 |
Correct |
2 ms |
364 KB |
Output is correct |
13 |
Correct |
2 ms |
364 KB |
Output is correct |
14 |
Correct |
3 ms |
364 KB |
Output is correct |
15 |
Correct |
17 ms |
492 KB |
Output is correct |
16 |
Correct |
1 ms |
512 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
384 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
1 ms |
364 KB |
Output is correct |
27 |
Correct |
1 ms |
364 KB |
Output is correct |
28 |
Correct |
238 ms |
2540 KB |
Output is correct |
29 |
Correct |
65 ms |
1388 KB |
Output is correct |
30 |
Correct |
147 ms |
1900 KB |
Output is correct |
31 |
Correct |
62 ms |
1516 KB |
Output is correct |
32 |
Correct |
25 ms |
1132 KB |
Output is correct |
33 |
Correct |
32 ms |
1260 KB |
Output is correct |
34 |
Correct |
156 ms |
1900 KB |
Output is correct |
35 |
Correct |
246 ms |
2668 KB |
Output is correct |
36 |
Correct |
267 ms |
2796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
154 ms |
1644 KB |
Output is correct |
2 |
Correct |
151 ms |
1644 KB |
Output is correct |
3 |
Correct |
126 ms |
1388 KB |
Output is correct |
4 |
Correct |
50 ms |
720 KB |
Output is correct |
5 |
Correct |
136 ms |
1260 KB |
Output is correct |
6 |
Correct |
95 ms |
876 KB |
Output is correct |
7 |
Correct |
7 ms |
492 KB |
Output is correct |
8 |
Correct |
3 ms |
364 KB |
Output is correct |
9 |
Correct |
3 ms |
364 KB |
Output is correct |
10 |
Correct |
2 ms |
364 KB |
Output is correct |
11 |
Correct |
6 ms |
364 KB |
Output is correct |
12 |
Correct |
2 ms |
364 KB |
Output is correct |
13 |
Correct |
2 ms |
364 KB |
Output is correct |
14 |
Correct |
3 ms |
364 KB |
Output is correct |
15 |
Correct |
17 ms |
492 KB |
Output is correct |
16 |
Correct |
238 ms |
2540 KB |
Output is correct |
17 |
Correct |
65 ms |
1388 KB |
Output is correct |
18 |
Correct |
147 ms |
1900 KB |
Output is correct |
19 |
Correct |
62 ms |
1516 KB |
Output is correct |
20 |
Correct |
25 ms |
1132 KB |
Output is correct |
21 |
Correct |
32 ms |
1260 KB |
Output is correct |
22 |
Correct |
156 ms |
1900 KB |
Output is correct |
23 |
Correct |
246 ms |
2668 KB |
Output is correct |
24 |
Correct |
267 ms |
2796 KB |
Output is correct |
25 |
Correct |
1 ms |
512 KB |
Output is correct |
26 |
Correct |
1 ms |
364 KB |
Output is correct |
27 |
Correct |
1 ms |
364 KB |
Output is correct |
28 |
Correct |
1 ms |
364 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
30 |
Correct |
1 ms |
364 KB |
Output is correct |
31 |
Correct |
1 ms |
384 KB |
Output is correct |
32 |
Correct |
1 ms |
364 KB |
Output is correct |
33 |
Correct |
1 ms |
364 KB |
Output is correct |
34 |
Correct |
1 ms |
364 KB |
Output is correct |
35 |
Correct |
1 ms |
364 KB |
Output is correct |
36 |
Correct |
1 ms |
364 KB |
Output is correct |
37 |
Execution timed out |
1090 ms |
34500 KB |
Time limit exceeded |
38 |
Halted |
0 ms |
0 KB |
- |