Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <cstring>
- using namespace std;
- typedef long long ll;
- const int maxn = 2e5 + 10;
- int a[maxn];
- ll segment_tree[3 * maxn];
- ll lazy_propagation[3 * maxn];
- void build_tree(int L, int R, int position) {
- if(L == R) {
- segment_tree[position] = a[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];
- }
- }
- void update_range(int L, int R, int position, ll i, ll j, int new_value) {
- if(lazy_propagation[position] > 0) {
- segment_tree[position] += lazy_propagation[position] * (R - L + 1);
- if(L != R) {
- lazy_propagation[2 * position] += lazy_propagation[position];
- lazy_propagation[2 * position + 1] += lazy_propagation[position];
- }
- lazy_propagation[position] = 0;
- }
- // L R i L R j L R
- if(R < i or j < L) {
- return;
- }
- if(i <= L and R <= j) {
- segment_tree[position] += new_value * (R - L + 1);
- if(L != R) {
- lazy_propagation[2 * position] += new_value;
- lazy_propagation[2 * position + 1] += new_value;
- }
- return;
- }
- int middle = (L + R) / 2;
- update_range(L, middle, 2 * position, i, j, new_value);
- update_range(middle + 1, R, 2 * position + 1, i, j, new_value);
- segment_tree[position] = segment_tree[2 * position] + segment_tree[2 * position + 1];
- }
- ll query(int L, int R, int position, ll i, ll j) {
- if(lazy_propagation[position] > 0) {
- segment_tree[position] += lazy_propagation[position] * (R - L + 1);
- if(L != R) {
- lazy_propagation[2 * position] += lazy_propagation[position];
- lazy_propagation[2 * position + 1] += lazy_propagation[position];
- }
- lazy_propagation[position] = 0;
- }
- // L R i L R j L R
- 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() {
- ios_base::sync_with_stdio(false);
- int n;
- cin >> n;
- int q;
- cin >> q;
- for(int i = 0; i < n; i++) {
- cin >> a[i];
- }
- build_tree(0, n - 1, 1);
- memset(lazy_propagation, 0, sizeof lazy_propagation);
- for(int i = 0; i < q; i++) {
- int c;
- cin >> c;
- if(c == 1) {
- int a, b, x;
- cin >> a >> b >> x;
- update_range(0, n - 1, 1, a - 1, b - 1, x);
- }
- else {
- int k;
- cin >> k;
- cout << query(0, n - 1, 1, k - 1, k - 1) << "\n";
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement