-
Notifications
You must be signed in to change notification settings - Fork 31
/
code_2.cpp
76 lines (69 loc) · 1.67 KB
/
code_2.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
68
69
70
71
72
73
74
75
76
//
// code_2.cpp
// Algorithm
//
// Created by Mohd Shoaib Rayeen on 23/11/18.
// Copyright © 2018 Shoaib Rayeen. All rights reserved.
//
#include <iostream>
#include <vector>
#include <math.h>
using namespace std;
struct Query {
int L, R;
};
int block;
bool compare(Query x, Query y) {
if (x.L/block != y.L/block) {
return x.L/block < y.L/block;
}
return x.R < y.R;
}
void getQuerySums( vector<int> array , vector<Query> List ) {
int n = int(array.size());
int m = int(List.size());
block = (int)sqrt(n);
sort(List.begin(), List.end(), compare);
int currL = 0, currR = 0;
int currSum = 0;
for (int i = 0; i < m; i++) {
int L = List[i].L, R = List[i].R;
while (currL < L) {
currSum -= array[currL];
currL++;
}
while (currL > L) {
currSum += array[currL-1];
currL--;
}
while (currR <= R) {
currSum += array[currR];
currR++;
}
while (currR > R+1) {
currSum -= array[currR-1];
currR--;
}
cout << "Sum of [" << L << ", " << R << "]\t:\t" << currSum << endl;
}
}
int main() {
int size;
cout << "\nEnter Size of Array\t:\t";
cin >> size;
vector<int> array(size);
cout << "\nEnter Array Elements\n";
for ( int i = 0; i < size; i++ ) {
cin >> array[i];
}
cout << "\nEnter Number of Queries\t:\t";
cin >> size;
vector<Query> List(size);
cout << "\nEnter Queries (L,R) Form\n";
for ( int i = 0; i < size; i++ ) {
cin >> List[i].L;
cin >> List[i].R;
}
getQuerySums(array,List);
return 0;
}