Algorithm


Problem link- https://www.spoj.com/problems/UPDATEIT/

UPDATEIT - Update the array !

 

You have an array containing n elements initially all 0. You need to do a number of update operations on it. In each update you specify l, r and val which are the starting index, ending index and value to be added. After each update, you add the 'val' to all elements from index l to r. After 'u' updates are over, there will be q queries each containing an index for which you have to print the element at that index.

Input

First line consists of t, the number of test cases. (1 <= t <= 10)

Each test case consists of "n u",number of elements in the array and the number of update operations, in the first line (1 <= n <= 10000 and 1 <= u <= 100000)

Then follow u lines each of the format "l r val" (0 <= l,r < n, 0 <= val <=10000)

Next line contains q, the number of queries. (1 <= q <= 10000)

Next q lines contain an index (0 <= index < n)

Output

For each test case, output the answers to the corresponding queries in separate lines.

Example

Input:
1
5 3
0 1 7
2 4 6
1 3 2
3
0
3
4

Output:
7
8
6

 

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming

#include <bits/stdc++.h>
#include <limits.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
 
#define MOD (ll)1000000007
#define pb   push_back
#define EPS 1e-9
#define FOR(i,n)  for(int i = 0;i < n; i++)
#define FORE(i,a,b) for(int i = a;i <= b; i++)
#define pi(a)   printf("%d\n", a)
#define all(c)  c.begin(), c.end()
#define tr(container, it)   for(typeof(container.begin()) it = container.begin(); it != container.end(); it++)
#define gc getchar_unlocked
#define sdi(a, b)   si(a);si(b)
#define io ios_base::sync_with_stdio(false);cin.tie(NULL);
#define endl '\n'
#define F first
#define S second
#define FILL(arr, val)  memset(arr, val, sizeof(arr))
#define read(arr, n)    for(int i = 0;i < n; i++)cin>>arr[i];
#define sp ' '

template <typename T> T gcd(T a, T b){return (b==0)?a:gcd(b,a%b);}
template <typename T> T lcm(T a, T b){return a*(b/gcd(a,b));}
template <typename T> T mod_exp(T b, T p, T m){T x = 1;while(p){if(p&1)x=(x*b)%m;b=(b*b)%m;p=p>>1;}return x;}
template <typename T> T invFermat(T a, T p){return mod_exp(a, p-2, p);}
template <typename T> T exp(T b, T p){T x = 1;while(p){if(p&1)x=(x*b);b=(b*b);p=p<<1;}return x;}

void si(int &x){
    register int c = gc();
    x = 0;
    int neg = 0;
    for(;((c<48 || c>57) && c != '-');c = gc());
    if(c=='-') {neg=1;c=gc();}
    for(;c>47 && c<58;c = gc()) {x = (x<<1) + (x<<3) + c - 48;}
    if(neg) x=-x;
}

const int MAXN = (int)1e4+5;
ll BIT[MAXN];
int n;

void update(int idx, int val){
    while(idx <= MAXN){
        BIT[idx] += val;
        idx += idx&-idx;
    }
}

ll query(int idx>{
    ll sum = 0;
    while(idx > 0){
        sum += BIT[idx];
        idx -= idx&-idx;
    }
    return sum;
}

ll RSQ(int l, int r){
    return query(r) - query(l-1);
}

int main(){
    io;
    int t;
    cin >> t;
    while(t--){
       int u;
       cin >> n >> u;
       FOR(i, u){
        int l, r;
        cin >> l >> r;
        ++l;++r;
        int val;
        cin >> val;
        update(l, val);
        update(r+1, -val);
       }
       int q;
       cin >> q;
       while(q--){
        int idx;
        cin >> idx;
        ++idx;
        cout << query(idx) << endl;
       }
       memset(BIT, 0, sizeof BIT);
    }
    return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
1
5 3
0 1 7
2 4 6
1 3 2
3
0
3
4

Output

x
+
cmd
7
8
6
Advertisements

Demonstration


SPOJ Solution-Update the array !-Solution in C, C++, Java, Python

Previous
SPOJ Solution - Test Life, the Universe, and Everything - Solution in C, C++, Java, Python