-
Notifications
You must be signed in to change notification settings - Fork 0
/
GSS1.cpp
executable file
·50 lines (50 loc) · 997 Bytes
/
GSS1.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
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define ll long long
#define MAX 5000010
ll t[4*MAX], a[MAX];
ll m, n, qs, qe;
ll buildSegTree(ll arr[], ll start, ll end, ll node)
{
if(start == end)
{
t[node] = arr[start];
return t[node];
}
ll mid = (start + end)/2;
t[node] = buildSegTree(arr, start, mid, 2*node) + buildSegTree(arr, mid + 1, end, 2*node + 1);
return t[node];
}
ll query(ll start, ll end, ll node, ll qs, ll qe)
{
if(qs <= start && qe >= end)
{
return t[node];
}
if(end < qs || start > qe)
{
return 0;
}
ll mid = (start + end)/2;
ll x = query(start, mid, 2*node, qs, qe);
ll y = query(mid + 1, end, 2*node + 1, qs , qe);
return x + y;
}
int main()
{
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> n;
for(ll i = 1; i <= n; i++)
{
cin >> a[i];
}
buildSegTree(a, 1, n, 1);
cin >> m;
while(m--)
{
cin >> qs >> qe;
cout << query(1, n, 1, qs, qe) << endl;
}
return 0;
}