Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int main()
- {
- int n;
- cin >> n;
- int niza[n];
- for(int i = 0; i < n; i++) {
- cin >> niza[i];
- }
- int Q;
- cin >> Q;
- int zadaci[Q][2];
- for(int i = 0; i < Q; i++) {
- cin >> zadaci[i][0] >> zadaci[i][1];
- }
- // kako se pravi prefix sum?
- // prvo pravime nova niza, so ista golemina ko nizata na koja sakame
- // da presmetuvame zbir
- int prefix_sum[n];
- int zbir = 0;
- // treba da ja pomineme sega nizata
- for(int i = 0; i < n; i++) {
- zbir += niza[i];
- prefix_sum[i] = zbir;
- }
- for(int i = 0; i < Q; i++) {
- int S = zadaci[i][0]; // poceten indeks
- int E = zadaci[i][1]; // kraen indeks
- if(S == 0) {
- cout << prefix_sum[E] << endl;
- }
- else {
- cout << (prefix_sum[E] - prefix_sum[S - 1]) << endl;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement