Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <cstring>
- #include <algorithm>
- #include <cmath>
- #include <vector>
- #include <set>
- #include <map>
- #include <stack>
- #include <queue>
- #include <deque>
- #include <unordered_map>
- #include <numeric>
- #include <iomanip>
- using namespace std;
- #define pii pair<long long, long long>
- #define ll long long
- #define FAST ios_base::sync_with_stdio(false); cin.tie(NULL)
- const long long dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
- const long long dl[2] = {1, -1};
- const long long MOD = 1000000007;
- const long long MAXN = 100005;
- int N, M;
- int arr[MAXN];
- long long seg[MAXN * 4], lazy[MAXN * 4];
- void prop(int x, int s, int e){
- if(lazy[x] == 0) return;
- seg[x] += lazy[x] * (e + 1 - s);
- if(s == e){
- lazy[x] = 0;
- }
- else{
- lazy[x * 2] += lazy[x];
- lazy[x * 2 + 1] += lazy[x];
- lazy[x] = 0;
- }
- }
- void build(int x, int s, int e){
- if(s == e){
- seg[x] = arr[s];
- return;
- }
- int mid = (s + e)/2;
- build(x * 2, s, mid);
- build(x * 2 + 1, mid + 1, e);
- seg[x] = seg[x * 2] + seg[x * 2 + 1];
- }
- void update(int x, int s, int e, int a, int b, int add){
- prop(x, s, e);
- if(e < a or b < s) return;
- if(a <= s and e <= b){
- lazy[x] += add;
- prop(x, s, e);
- return;
- }
- int mid = (s + e)/2;
- update(x * 2, s, mid, a, b, add);
- update(x * 2 + 1, mid + 1, e, a, b, add);
- seg[x] = seg[x * 2] + seg[x * 2] + 1;
- }
- long long query(int x, int s, int e, int idx){
- prop(x, s, e);
- if(idx < s or idx > e) return 0;
- if(s == e){
- return seg[x];
- }
- int mid = (s + e)/2;
- return query(x * 2, s, mid, idx) + query(x * 2 + 1, mid + 1, e, idx);
- }
- int main() {
- FAST;
- cin >> N;
- for(int i = 1; i <= N; i++){
- cin >> arr[i];
- }
- build(1, 1, N);
- cin >> M;
- for(int a, i = 1; i <= M; i++){
- cin >> a;
- if(a == 1){
- int b, c, d;
- cin >> b >> c >> d;
- update(1, 1, N, b, c, d);
- }
- else{
- int b;
- cin >> b;
- cout << query(1, 1, N, b) << "\n";
- }
- }
- }
- /*
- 5
- 1 2 3 4 5
- 4
- 1 3 4 6
- 2 3
- 1 1 3 -2
- 2 3
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement