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
using namespace std; const int maxn = 2e5 + 10; typedef long long ll; ll segment_tree[3 * maxn], lazy[3 * maxn], a[maxn]; int n; void build_tree(int L, int R, int pos) { if(L == R) { segment_tree[pos] = a[L]; } else { int middle = (L + R) / 2; build_tree(L, middle, 2 * pos); build_tree(middle + 1, R, 2 * pos + 1); segment_tree[pos] = segment_tree[2 * pos] + segment_tree[2 * pos + 1]; } } void update(int L, int R, int pos, int i, int j, ll value) { if(lazy[pos] > 0) { segment_tree[pos] += lazy[pos] * (R - L + 1); if(L != R) { lazy[2 * pos] += lazy[pos]; lazy[2 * pos + 1] += lazy[pos]; } lazy[pos] = 0; } // L R i L R j L R if(R < i or j < L) { return; } if(i <= L and R <= j) { segment_tree[pos] += value * (R - L + 1); if(L != R) { lazy[2 * pos] += value; lazy[2 * pos + 1] += value; } } else { int middle = (L + R) / 2; update(L, middle, 2 * pos, i, j, value); update(middle + 1, R, 2 * pos + 1, i, j, value); segment_tree[pos] = segment_tree[2 * pos] + segment_tree[2 * pos + 1]; } } ll query(int L, int R, int pos, int i, int j) { if(lazy[pos] > 0) { segment_tree[pos] += lazy[pos] * (R - L + 1); if(L != R) { lazy[2 * pos] += lazy[pos]; lazy[2 * pos + 1] += lazy[pos]; } lazy[pos] = 0; } // L R i L R j L R if(R < i or j < L) { return 0; } if(i <= L and R <= j) { return segment_tree[pos]; } int middle = (L + R) / 2; return query(L, middle, 2 * pos, i, j) + query(middle + 1, R, 2 * pos + 1, i, j); } int main() { ios_base::sync_with_stdio(false); int q; cin >> n >> q; for(int i = 0; i < n; i++) { cin >> a[i]; } build_tree(0, n - 1, 1); for(int i = 0; i < q; i++) { int type; cin >> type; if(type == 1) { int x, y, u; cin >> x >> y >> u; update(0, n - 1, 1, x - 1, y - 1, u); } else { int x; cin >> x; cout << query(0, n - 1, 1, x - 1, x - 1) << 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)