Cod sursa(job #3169949)

Utilizator hhhhhhhAndrei Boaca hhhhhhh Data 16 noiembrie 2023 13:07:05
Problema Walls Scor 100
Compilator cpp-64 Status done
Runda Meduzele când plimbă sub clopotele verzi. Marime 2.7 kb
#include <bits/stdc++.h>
#pragma GCC optimize ("Ofast")
#pragma GCC optimize ("fast-math")
#pragma GCC optimize ("unroll-loops")
using namespace std;
ifstream fin("walls.in");
ofstream fout("walls.out");
const int bucksize=320;
typedef pair<int,int> pii;
int n,h[100005],w[100005],arb[4*100005];
vector<int> poz;
map<pii,int> f;
int getbuck(int x)
{
    return x/bucksize+(x%bucksize!=0);
}
int buck[505];
void build()
{
    for(int i=1;i<=n;i++)
    {
        int b=getbuck(i);
        buck[b]=max(buck[b],h[i]);
    }
}
void update(int poz)
{
    int b=getbuck(poz);
    buck[b]=0;
    for(int i=(b-1)*bucksize+1;i<=b*bucksize;i++)
        buck[b]=max(buck[b],h[i]);
}
int query(int poz)
{
    int rez=0;
    int b=getbuck(poz);
    for(int i=poz;i>=1&&getbuck(i)==b;i--)
        rez=max(rez,h[i]);
    for(int i=b-1;i>=1;i--)
        rez=max(rez,buck[i]);
    return rez;
}
int main()
{
    ios_base::sync_with_stdio(false);
    fin.tie(0);
    fin>>n;
    poz.push_back(0);
    for(int i=1;i<=n;i++)
    {
        fin>>w[i]>>h[i];
        int r=w[i];
        if(i==1)
            poz.push_back(r);
        else
        {
            r=poz.back()+1+w[i];
            poz.push_back(r);
        }
    }
    build();
    int q;
    fin>>q;
    while(q--)
    {
        int x,y;
        fin>>x>>y;
        int last=0;
        int st=1;
        int dr=n;
        while(st<=dr)
        {
            int mij=(st+dr)/2;
            if(poz[mij]<x)
            {
                last=mij;
                st=mij+1;
            }
            else
                dr=mij-1;
        }
        if(last==0)
        {
            fout<<"MISS\n";
            continue;
        }
        if(query(last)<y)
        {
            fout<<"MISS\n";
            continue;
        }
        int ind=-1;
        int b=getbuck(last);
        for(int i=last;i>=1&&getbuck(i)==b;i--)
            if(h[i]>=y)
            {
                ind=i;
                break;
            }
        if(ind==-1)
        {
            for(int i=b-1;i>=1;i--)
                if(buck[i]>=y)
                {
                    for(int j=bucksize*i;j>bucksize*(i-1);j--)
                        if(h[j]>=y)
                        {
                            ind=j;
                            break;
                        }
                    break;
                }
        }
        fout<<"HIT "<<poz[ind]-f[{ind,y}]<<' '<<ind<<' ';
        f[{ind,y}]++;
        if(f[{ind,y}]==w[ind])
            {
                fout<<"YES\n";
                h[ind]=y-1;
                update(ind);
            }
        else
            fout<<"NO\n";
    }
    return 0;
}