Sunday, January 6, 2019

[Spoj] METEORS - Meteors

Author            : Dipu Kumar Mohanto 
                    CSE, Batch - 6
                    BRUR.
Problem Statement : METEORS - Meteors
Source            : Spoj
Category          : Daya Structure, Search
Algorithm         : Parallel Binary Search
Verdict           : Accepted
#include <bits/stdc++.h>
 
using namespace std;
 
#define ll          long long
 
static const int maxn = 3e5 + 5;
 
ll res[maxn], reqd[maxn];
int ql[maxn], qr[maxn];
ll qa[maxn];
int lo[maxn], hi[maxn];
 
vector <int> owner[maxn], tocheck[maxn];
 
int n, m, q;
 
inline void update(int pos, ll val)
{
    while (pos <= m)
    {
        res[pos] += val;
        pos += (pos & -pos);
    }
}
 
inline ll read(int pos)
{
    ll sum = 0;
    while (pos > 0)
    {
        sum += res[pos];
        pos -= (pos & -pos);
    }
    return sum;
}
 
inline void rangeUpdate(int id)
{
    if (ql[id] <= qr[id])
    {
        update(ql[id], qa[id]);
        update(qr[id]+1, -qa[id]);
    }
    else
    {
        update(1, qa[id]);
        update(qr[id]+1, -qa[id]);
        update(ql[id], qa[id]);
    }
}
 
int main()
{
    scanf("%d %d", &n, &m);
    for (int i = 1; i <= m; i++)
    {
        int own;
        scanf("%d", &own);
        owner[own].push_back(i);
    }
    for (int i = 1; i <= n; i++) scanf("%lld", reqd+i);
    scanf("%d", &q);
    for (int i = 1; i <= q; i++)
    {
        scanf("%d %d %lld", &ql[i], &qr[i], &qa[i]);
    }
    for (int i = 1; i <= n; i++)
    {
        lo[i] = 1;
        hi[i] = q+1;
    }
    while (true)
    {
        // clean up tree
        for (int i = 0; i <= m; i++) res[i] = 0;
        bool updateReq = false;
        for (int i = 1; i <= n; i++)
        {
            if (lo[i] != hi[i])
            {
                updateReq = true;
                tocheck[ (lo[i]+hi[i]) / 2 ].push_back(i);
            }
        }
        if (updateReq == false) break;
        for (int qq = 1; qq <= q; qq++)
        {
            rangeUpdate(qq);
            for (auto &id : tocheck[qq])
            {
                ll sum = 0;
                for (auto &sectors : owner[id])
                {
                    sum += read(sectors);
                    if (sum >= reqd[id]) break;
                }
                if (sum >= reqd[id]) hi[id] = qq;
                else lo[id] = qq + 1;
            }
            //if (tocheck[qq].size() > 0)
                tocheck[qq].clear();
        }
    }
    for (int i = 1; i <= n; i++)
    {
        if (lo[i] <= q) printf("%d\n", lo[i]);
        else printf("NIE\n");
    }
}

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.