Archives
Trending
Support
Login
clear text
XML
Django
JavaScript
MATLAB
C
C++
C#
Python
SQL
Shell
Bash
Markdown
YAML
JSON
HTML
CSS
PHP
Java
Ruby
Go
Rust
Swift
Kotlin
Arduino
TypeScript
Perl
Autohotkey
Lua
SQF
R
Scala
Haskell
Groovy
Dart
Clojure
VB.NET
Objective-C
PowerShell
Bash
CoffeeScript
Verilog
#include
#include
#include
#include
#include
#include
#include
using namespace std; const int maxn = 2e5 + 10; int arr[maxn]; long long segment_tree[3 * maxn]; void build_tree(int L, int R, int position) { if(L == R) { segment_tree[position] = arr[L]; } else { int middle = (L + R) / 2; build_tree(L, middle , 2 * position); build_tree(middle + 1, R, 2 * position + 1); segment_tree[position] = segment_tree[2 * position] + segment_tree[2 * position + 1]; } } long long query(int L, int R, int position, int i, int j) { if(i <= L and R <= j) { return segment_tree[position]; } if(R < i or j < L) { return 0; } int middle = (L + R) / 2; return query(L, middle, 2 * position, i, j) + query(middle + 1, R, 2 * position + 1, i, j); } int main() { int n, q; cin >> n >> q; for(int i = 0; i < n; i++) { cin >> arr[i]; } build_tree(0, n - 1, 1); // building the tree O(N log(N)) for(int i = 0; i < q; i++) { int a, b; cin >> a >> b; a--; b--; cout << query(0, n - 1, 1, a, b) << endl; } return 0; }
Mark as private (unlisted)
for 30 minutes
for 6 hours
for 1 day
for 1 week
for 1 month
for 1 year
everlasting (like CCCP)