Submission #1356318

#TimeUsernameProblemLanguageResultExecution timeMemory
1356318Zbyszek99Baker (JOI26_baker)C++20
100 / 100
1370 ms673956 KiB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define lll __int128_t
#define ll long long
#define ld long double
#define ull unsigned long long
#define ff first
#define ss second
#define pii pair<int,int>
#define pll pair<long long, long long>
#define vi vector<int>
#define vl vector<long long>
#define pb push_back
#define rep(i, b) for(int i = 0; i < (b); ++i)
#define rep2(i,a,b) for(int i = a; i <= (b); ++i)
#define rep3(i,a,b,c) for(int i = a; i <= (b); i+=c)
#define count_bits(x) __builtin_popcountll((x))
#define all(x) (x).begin(),(x).end()
#define siz(x) (int)(x).size()
#define forall(it,x) for(auto& it:(x))
using namespace __gnu_pbds;
using namespace std;
typedef tree<int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update> ordered_set;
//mt19937 mt;void random_start(){mt.seed(chrono::time_point_cast<chrono::milliseconds>(chrono::high_resolution_clock::now()).time_since_epoch().count());}
//ll los(ll a, ll b) {return a + (mt() % (b-a+1));}
const int INF = 1e9+50;
const ll INF_L = 1e18+40;
const ll MOD = 1e9+7;

struct line
{
    ll a,b;
};

const int tree_siz = 1024*4096-1;
vector<line> lines[tree_siz+1];
int ptr[tree_siz+1];
vi queries[tree_siz+1];

void add_query(int akt, int p1, int p2, int s1, int s2, int qq)
{
    if(p2 < s1 || p1 > s2) return;
    if(p1 >= s1 && p2 <= s2)
    {
        queries[akt].pb(qq);
        return;
    }
    add_query(akt*2,p1,(p1+p2)/2,s1,s2,qq);
    add_query(akt*2+1,(p1+p2)/2+1,p2,s1,s2,qq);
}

pair<lll,lll> y1_,y2_;

void make_hull(vector<line>& h1, vector<line>& h2)
{
    forall(it,h2)
    {
        while(siz(h1) >= 2)
        {
            line l1 = h1[siz(h1)-2];
            line l2 = h1.back();
            y1_ = {l1.b-l2.b,l2.a-l1.a};
            y2_ = {l2.b-it.b,it.a-l2.a};
            if(y1_.ff*y2_.ss <= y2_.ff*y1_.ss) break;
            else h1.pop_back();
        }
        h1.pb(it);
    }
}

pll query_seg[400001];
ll query_t[400001];
ll query_k[400001];
ll query_ans[400001];

void build(int akt)
{
    if(akt < tree_siz/2+1)
    {
        build(akt*2);
        build(akt*2+1);
        make_hull(lines[ptr[akt*2]],lines[ptr[akt*2+1]]);
        ptr[akt] = ptr[akt*2];
        lines[ptr[akt*2+1]].clear();
    }
    int p = 0;
    forall(it,queries[akt])
    {
        while(p+1 < siz(lines[ptr[akt]]) && lines[ptr[akt]][p].a*query_k[it]+lines[ptr[akt]][p].b <= lines[ptr[akt]][p+1].a*query_k[it]+lines[ptr[akt]][p+1].b) p++;
        query_ans[it] = max(query_ans[it],lines[ptr[akt]][p].a*query_k[it]+lines[ptr[akt]][p].b);
    }
}

ll X,L2;
int n,q;
vl T;

int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    //random_start();
    cin >> X >> n >> L2 >> q;
    T.resize(n);
    rep(i,tree_siz+1) ptr[i] = i;
    rep(i,n) 
    {
        cin >> T[i];
        lines[tree_siz/2+1+i].pb({(i+1),-(T[i]+L2)});
    }
    vector<pll> q2;
    rep(qq,q)
    {
        ll k,t;
        cin >> k >> t;
        ll L = -1;
        ll R = -2;
        int l = 0;
        int r = n-1;
        while(l <= r)
        {
            int mid = (l+r)/2;
            if(T[mid] <= t)
            {
                R = mid;
                l = mid+1;
            }
            else r = mid-1;
        }
        l = 0;
        r = n-1;
        while(l <= r)
        {
            int mid = (l+r)/2;
            if(T[mid]+L2 >= t)
            {
                L = mid;
                r = mid-1;
            }
            else l = mid+1;
        }
        query_seg[qq] = {L,R};
        query_k[qq] = k;
        query_t[qq] = t;
        query_ans[qq] = -1e18;
        if(L <= R && L != -1 && R != -2) q2.pb({k,qq});
    }
    sort(all(q2));
    forall(it,q2) add_query(1,0,tree_siz/2,query_seg[it.ss].ff,query_seg[it.ss].ss,it.ss);
    build(1);
    rep(qq,q)
    {
        if(query_seg[qq].ff <= query_seg[qq].ss && query_seg[qq].ff != -1 && query_seg[qq].ss != -2) cout << min(query_seg[qq].ss-query_seg[qq].ff+1,query_seg[qq].ss+1-(query_ans[qq]+query_t[qq]+query_k[qq]-1)/query_k[qq]) << "\n";
        else cout << "0\n";
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...