- #include <bits/stdc++.h>
- using namespace std;
- #define mx 100001
- int arr[mx];
- #define ll long long unsigned
- struct info {
- ll prop, sum;
- } tree[mx * 3];
- void init(int node, int b, int e)
- {
- if (b == e) {
- tree[node].sum = arr[b];
- tree[node].prop = 0;
- return;
- }
- int Left = node * 2;
- int Right = node * 2 + 1;
- int mid = (b + e) / 2;
- init(Left, b, mid);
- init(Right, mid + 1, e);
- tree[node].sum = (tree[Left].sum+ tree[Right].sum);
- tree[node].prop = 0;
- }
- ll query(int node, int b, int e, int i, int j, ll carry = 0)
- {
- if (i > e || j < b)
- return 0;
- if (b >= i and e <= j)
- return tree[node].sum + carry * (e - b + 1); //সাম এর সাথে যোগ হবে সেই রেঞ্জের সাথে অতিরিক্ত যত যোগ করতে বলেছে সেটা
- int Left = node << 1;
- int Right = (node << 1) + 1;
- int mid = (b + e) >> 1;
- ll p1 = query(Left, b, mid, i, j, carry + tree[node].prop); //প্রপাগেট ভ্যালু বয়ে নিয়ে যাচ্ছে carry ভ্যারিয়েবল
- ll p2 = query(Right, mid + 1, e, i, j, carry + tree[node].prop);
- return p1 + p2;
- }
- void update(int node, int b, int e, int i, int j, ll x)
- {
- if (i > e || j < b)
- return;
- if (b >= i && e <= j) //নোডের রেঞ্জ আপডেটের রেঞ্জের ভিতরে
- {
- tree[node].sum += ((e - b + 1) * x); //নিচে নোড আছে e-b+1 টি, তাই e-b+1 বার x যোগ হবে এই রেঞ্জে
- tree[node].prop += x; //নিচের নোডগুলোর সাথে x যোগ হবে
- return;
- }
- int Left = node * 2;
- int Right = (node * 2) + 1;
- int mid = (b + e) / 2;
- update(Left, b, mid, i, j, x);
- update(Right, mid + 1, e, i, j, x);
- tree[node].sum = tree[Left].sum + tree[Right].sum + (e - b + 1) * tree[node].prop;
- //উপরে উঠার সময় পথের নোডগুলো আপডেট হবে
- //বাম আর ডান পাশের সাম ছাড়াও যোগ হবে নিচে অতিরিক্ত যোগ হওয়া মান
- }
- int main()
- {
- int ts;
- scanf("%d", &ts);
- for(int p = 1; p <=ts; p++){
- memset(arr, 0, sizeof(arr));
- memset(tree, 0, sizeof(tree));
- printf("Case %d:\n", p);
- int n, q;
- scanf("%d%d", &n, &q);
- //for(int i = 1; i <= n; i++) scanf("%d", &arr[i]);
- //init(1, 1, n);
- for(int i = 0; i<q ; i++){
- int num;
- scanf("%d", &num);
- if(num == 0){
- int x, y, v;
- scanf("%d%d%d", &x, &y, &v);
- update(1, 1, n, x+1, y+1, v);
- }
- else if(num == 1){
- int x, y;
- scanf("%d%d", &x, &y);
- printf("%lld\n", query(1, 1, n, x+1, y+1, 0));
- }
- }
- }
- return 0;
- }
Saturday, 29 July 2017
Lightoj 1164 - Horrible Queries
Subscribe to:
Post Comments (Atom)
Most Featured Post
Lightoj 1159 - Batman
http://lightoj.com/volume_showproblem.php?problem=1159 problem analysis: First i thought of this as if s1, s2 and s3 are those three str...
-
http://lightoj.com/volume_showproblem.php?problem=1382 Problem analysis: This is a rare problem i wrote about so far. After much strugg...
-
http://lightoj.com/volume_showproblem.php?problem=1159 problem analysis: First i thought of this as if s1, s2 and s3 are those three str...
No comments:
Post a Comment