제출 #1328298

#제출 시각아이디문제언어결과실행 시간메모리
1328298Zbyszek99Collecting Stamps 4 (JOI25_stamps4)C++20
100 / 100
2643 ms148200 KiB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#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;

const int tree_siz = 2048*2048-1;

struct segtree
{
    int drzewo[tree_siz];
    segtree()
    {
        rep(i,tree_siz) drzewo[i] = 0;
    }
    int get_sum(int akt, int p1, int p2, int s1, int s2)
    {
        if(p2 < s1 || p1 > s2) return 0;
        if(p1 >= s1 && p2 <= s2) return drzewo[akt-1];
        return get_sum(akt*2,p1,(p1+p2)/2,s1,s2) + get_sum(akt*2+1,(p1+p2)/2+1,p2,s1,s2);
    }
    void upd(int v)
    {
        drzewo[v-1] = drzewo[v*2-1] + drzewo[v*2];
        if(v != 1) upd(v/2);
    }
    void change(int ind, int x)
    {
        drzewo[tree_siz/2+ind] = x;
        upd((tree_siz/2+ind+1)/2);
    }
};

segtree tree_end;
ll ans[1000'001];
ll C[1000'001];
vi road_poz[500'001];
int road[1000'001];
ll cur_ans = 0;
ll query_ans[500'001];

int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    //random_start();
    int n;
    ll X;
    cin >> n >> X;
    ll total_ends = 0;
    rep(i,n*2)
    {   
        int x;
        cin >> x;
        road[i] = x;
        road_poz[x].pb(i);
        if(siz(road_poz[x]) == 1)
        {
            cur_ans += total_ends;
        }
        else
        {
            total_ends++;
            tree_end.change(i,1);
        }
    }   
    rep(i,2*n) cin >> C[i];
    rep(i,n*2)
    {
        ans[i] = (ll)n*(ll)n - cur_ans;
        cur_ans -= tree_end.get_sum(1,0,tree_siz/2,0,road_poz[road[i]][0]);
        cur_ans -= (i+n*2 - 1) - (road_poz[road[i]][1]) + 1 - tree_end.get_sum(1,0,tree_siz/2,road_poz[road[i]][1],tree_siz/2);
        tree_end.change(road_poz[road[i]][1],0);
        cur_ans += tree_end.get_sum(1,0,tree_siz/2,0,road_poz[road[i]][1]);
        road_poz[road[i]] = {road_poz[road[i]][1],i+n*2};
        tree_end.change(road_poz[road[i]][1],1);
    }
    vector<pll> anses;
    rep(i,n*2)
    {
        anses.pb({ans[i],C[i]});
    }
    sort(all(anses));
    vector<pll> queries;
    int q;
    cin >> q;
    rep(i,q)
    {
        ll x;
        cin >> x;
        queries.pb({x,i});
    }
    sort(all(queries));
    ll c1 = 3e18;
    multiset<ll> c2;
    forall(it,anses)
    {
        c2.insert(it.ss);
    }
    int cur_a = 0;
    forall(it,queries)
    {
        while(cur_a != siz(anses) && anses[cur_a].ff < it.ff)
        {
            pll it2 = anses[cur_a];
            c2.erase(c2.find(it2.ss));
            c1 = min(c1,it2.ss - X * it2.ff);
            cur_a++;
        }
        ll a = 3e18;
        if(siz(c2) != 0)
        {
            a = min(a,*c2.begin());
        }
        if(c1 != 3e18)
        {
            a = min(a,c1 + it.ff * X);
        }
        query_ans[it.ss] = a;
    }
    rep(i,q) cout << query_ans[i] << "\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...