-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added more square root decomposition problems
- Loading branch information
Showing
1 changed file
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#include<bits/stdc++.h> | ||
using namespace std; | ||
#define int long long | ||
const int N = 1e5+2, MOD = 1e9+7; | ||
int rootN; | ||
|
||
struct Q { | ||
int idx, l, r; | ||
}; | ||
|
||
Q q[N]; | ||
|
||
bool compare(Q q1, Q q2) { | ||
if(q1.l/rootN == q2.l/rootN) | ||
return q1.r > q2.r; | ||
|
||
return q1.l/rootN < q2.l/rootN; | ||
} | ||
|
||
signed main() { | ||
int n; | ||
cin>>n; | ||
|
||
vector<int> a(n); | ||
for(int i=0; i<n; i++) | ||
cin>>a[i]; | ||
|
||
rootN = sqrtl(n); | ||
int queries; | ||
cin>>queries; | ||
for(int i=0; i<queries; i++) { | ||
int l, r; | ||
cin>>l>>r; | ||
q[i].l = l; | ||
q[i].r = r; | ||
q[i].idx = i; | ||
} | ||
|
||
sort(q, q+queries, compare); | ||
vector<int> ans(queries); | ||
int curr_l = 0, curr_r = -1, l, r; | ||
int curr_ans = 0; | ||
|
||
for(int i=0; i<queries; i++) { | ||
l = q[i].l; | ||
r = q[i].r; | ||
l--; r--; | ||
|
||
while(curr_r < r) | ||
curr_ans += a[++curr_r]; | ||
|
||
while(curr_l > l) | ||
curr_ans += a[--curr_l]; | ||
|
||
while(curr_l < l) | ||
curr_ans -= a[curr_l++]; | ||
|
||
while(curr_r > r) | ||
curr_ans -= a[curr_r--]; | ||
|
||
ans[q[i].idx] = curr_ans; | ||
} | ||
|
||
for(int i=0; i<queries; i++) | ||
cout<<ans[i]<<endl; | ||
|
||
return 0; | ||
} |