Cod sursa(job #3169913)

Utilizator hhhhhhhAndrei Boaca hhhhhhh Data 16 noiembrie 2023 12:02:09
Problema Walls Scor 90
Compilator cpp-64 Status done
Runda Meduzele când plimbă sub clopotele verzi. Marime 2.89 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");
typedef pair<int,int> pii;
int n,h[100005],w[100005],arb[4*100005];
vector<int> poz;
map<pii,int> f;
void build(int nod,int st,int dr)
{
    if(st==dr)
    {
        arb[nod]=h[st];
        return;
    }
    int mij=(st+dr)/2;
    build(nod*2,st,mij);
    build(nod*2+1,mij+1,dr);
    arb[nod]=max(arb[nod*2],arb[nod*2+1]);
}
void update(int nod,int st,int dr,int poz,int val)
{
    if(st==dr)
    {
        arb[nod]=val;
        return;
    }
    int mij=(st+dr)/2;
    if(poz<=mij)
        update(nod*2,st,mij,poz,val);
    else
        update(nod*2+1,mij+1,dr,poz,val);
    arb[nod]=max(arb[nod*2],arb[nod*2+1]);
}
int query(int nod,int st,int dr,int a,int b)
{
    if(st>=a&&dr<=b)
        return arb[nod];
    int rez=0;
    int mij=(st+dr)/2;
    if(a<=mij)
        rez=max(rez,query(nod*2,st,mij,a,b));
    if(b>mij)
        rez=max(rez,query(nod*2+1,mij+1,dr,a,b));
    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(1,1,n);
    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(1,1,n,1,last)<y)
        {
            fout<<"MISS\n";
            continue;
        }
        int ind=1;
        st=1;
        dr=last;
        while(st<=dr)
        {
            if(dr-st<=200)
            {
                for(int i=dr;i>=st;i--)
                    if(h[i]>=y)
                    {
                        ind=max(ind,i);
                        break;
                    }
            }
            int mij=(st+dr)/2;
            if(query(1,1,n,mij,last)>=y)
            {
                ind=mij;
                st=mij+1;
            }
            else
                dr=mij-1;
        }
        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(1,1,n,ind,h[ind]);
            }
        else
            fout<<"NO\n";
    }
    return 0;
}