QOJ.ac

QOJ

ID提交记录ID题目HackerOwner结果提交时间测评时间
#1544#868067#9683. 士兵IvanZhang2009XY_ElevenFailed.2025-02-08 15:38:562025-02-08 15:38:57

详细

Extra Test:

Validator Runtime Error

input:

9 1
7 10
7 7 7 6 2 2 3

output:


result:


ID题目提交者结果用时内存语言文件大小提交时间测评时间
#868067#9683. 士兵XY_Eleven#100 ✓1357ms143484kbC++238.6kb2025-01-24 11:42:162025-01-24 11:42:17

answer

#include <bits/stdc++.h>
// #include <windows.h>
// #include <bits/extc++.h>
// using namespace __gnu_pbds;
using namespace std;
//#pragma GCC optimize(3)
#define DB double
#define LL long long
#define ULL unsigned long long
#define in128 __int128
#define cint const int
#define cLL const LL
#define For(z,e1,e2) for(int z=(e1);z<=(e2);z++)
#define Rof(z,e1,e2) for(int z=(e2);z>=(e1);z--)
#define For_(z,e1,e2) for(int z=(e1);z<(e2);z++)
#define Rof_(z,e1,e2) for(int z=(e2);z>(e1);z--)
#define inint(e) scanf("%d",&e)
#define inll(e) scanf("%lld",&e)
#define inpr(e1,e2) scanf("%d%d",&e1,&e2)
#define in3(e1,e2,e3) scanf("%d%d%d",&e1,&e2,&e3)
#define outint(e) printf("%d\n",e)
#define outint_(e) printf("%d%c",e," \n"[i==n])
#define outint2_(e,e1,e2) printf("%d%c",e," \n"[(e1)==(e2)])
#define outll(e) printf("%lld\n",e)
#define outll_(e) printf("%lld%c",e," \n"[i==n])
#define outll2_(e,e1,e2) printf("%lld%c",e," \n"[(e1)==(e2)])
#define exc(e) if(e) continue
#define stop(e) if(e) break
#define ret(e) if(e) return
#define ll(e) (1ll*(e))
#define pb push_back
#define ft first
#define sc second
#define pii pair<int,int> 
#define pli pair<long long,int> 
#define vct vector 
#define clean(e) while(!e.empty()) e.pop()
#define all(ev) ev.begin(),ev.end()
#define sz(ev) ((int)ev.size())
#define debug(x) printf("%s=%d\n",#x,x)
#define x0 __xx00__
#define y0 __yy00__
#define y1 __yy11__
#define ffo fflush(stdout)
cLL mod=998244353ll,G=404ll;
// cLL mod=1000000007ll;
// cLL mod[2]={1686688681ll,1666888681ll},base[2]={166686661ll,188868881ll};
template <typename Type> void get_min(Type &w1,const Type w2) { if(w2<w1) w1=w2; } template <typename Type> void get_max(Type &w1,const Type w2) { if(w2>w1) w1=w2; }
template <typename Type> Type up_div(Type w1,Type w2) { return (w1/w2+(w1%w2?1:0)); }
template <typename Type> Type gcd(Type X_,Type Y_) { Type R_=X_%Y_; while(R_) { X_=Y_; Y_=R_; R_=X_%Y_; } return Y_; } template <typename Type> Type lcm(Type X_,Type Y_) { return (X_/gcd(X_,Y_)*Y_); }
template <typename Type> Type md(Type w1,const Type w2=mod) { w1%=w2; if(w1<0) w1+=w2; return w1; } template <typename Type> Type md_(Type w1,const Type w2=mod) { w1%=w2; if(w1<=0) w1+=w2; return w1; }
void ex_gcd(LL &X_,LL &Y_,LL A_,LL B_) { if(!B_) { X_=1ll; Y_=0ll; return ; } ex_gcd(Y_,X_,B_,A_%B_); X_=md(X_,B_); Y_=(1ll-X_*A_)/B_; } LL inv(LL A_,LL B_=mod) { LL X_=0ll,Y_=0ll; ex_gcd(X_,Y_,A_,B_); return X_; }
template <typename Type> void add(Type &w1,const Type w2,const Type M_=mod) { w1=md(w1+w2,M_); } void mul(LL &w1,cLL w2,cLL M_=mod) { w1=md(w1*md(w2,M_),M_); } template <typename Type> Type pw(Type X_,Type Y_,Type M_=mod) { Type S_=1; while(Y_) { if(Y_&1) mul(S_,X_,M_); Y_>>=1; mul(X_,X_,M_); } return S_; }
template <typename Type> Type bk(vector <Type> &V_) { auto T_=V_.back(); V_.pop_back(); return T_; } template <typename Type> Type tp(stack <Type> &V_) { auto T_=V_.top(); V_.pop(); return T_; } template <typename Type> Type frt(queue <Type> &V_) { auto T_=V_.front(); V_.pop(); return T_; }
template <typename Type> Type bg(set <Type> &V_) { auto T_=*V_.begin(); V_.erase(V_.begin()); return T_; } template <typename Type> Type bk(set <Type> &V_) { auto T_=*prev(V_.end()); V_.erase(*prev(V_.end())); return T_; }
mt19937 gen(time(NULL)); int rd() { return abs((int)gen()); }
int rnd(int l,int r) { return rd()%(r-l+1)+l; }

void main_init()
{

}
cint N=5.01e5,M=N<<1;
int n,m;
cLL inf=2e18;
LL c;
LL a[N],b[N];
LL dp[M];
vct <LL> g,h;
struct Node
{
    int l,r,mid;
    LL w1,w2,ad,cv1,cv2;
};
struct SegTree
{
    Node t[M<<2];
    void bld(int p,int l,int r)
    {
        int mid=l+r>>1;
        t[p]={l,r,mid,-g[l],-g[r],0ll,inf,0ll};
        ret(l>=r);
        bld(p<<1,l,mid),bld(p<<1|1,mid+1,r);
    }
    void setf(int p,LL ad,LL cv1,LL cv2)
    {
        if(cv1!=inf)
        {
            t[p].ad=0ll;
            t[p].cv1=cv1;
            t[p].cv2=inf;
            t[p].w1=cv1;
            t[p].w2=cv1;
        }
        else if(cv2!=inf)
        {
            t[p].ad=0ll;
            t[p].cv1=inf;
            t[p].cv2=cv2;
            t[p].w1=cv2-g[t[p].l];
            t[p].w2=cv2-g[t[p].r];
        }
        else
        {
            t[p].ad+=ad;
            if(t[p].cv1!=inf) t[p].cv1+=ad;
            if(t[p].cv2!=inf) t[p].cv2+=ad;
            t[p].w1+=ad;
            t[p].w2+=ad;
        }
    }
    void pdn(int p)
    {
        setf(p<<1,t[p].ad,t[p].cv1,t[p].cv2);
        setf(p<<1|1,t[p].ad,t[p].cv1,t[p].cv2);
        t[p].ad=0ll,t[p].cv1=inf,t[p].cv2=inf;
    }
    void pup(int p)
    {
        t[p].w1=t[p<<1].w1;
        t[p].w2=t[p<<1|1].w2;
    }
    int K; LL W;
    void fin(int p)
    {
        if(t[p].l>=t[p].r)
            return (void)(W=t[p].w1);
        pdn(p);
        fin(p<<1|(K>t[p].mid));
        pup(p);
    }
    LL qry(int k)
    {
        K=k;
        fin(1);
        return W;
    }
    int L,R;
    void add(int p)
    {
        if(L<=t[p].l&&t[p].r<=R)
            return setf(p,W,inf,inf);
        pdn(p);
        if(L<=t[p].mid) add(p<<1);
        if(R>t[p].mid) add(p<<1|1);
        pup(p);
    }
    void add(int l,int r,LL w)
    {
        ret(l>r);
        L=l,R=r,W=w;
        add(1);
    }
    void cov1(int p)
    {
        if(L<=t[p].l&&t[p].r<=R)
            return setf(p,0ll,W,inf);
        pdn(p);
        if(L<=t[p].mid) cov1(p<<1);
        if(R>t[p].mid) cov1(p<<1|1);
        pup(p);
    }
    void cov1(int l,int r,LL w)
    {
        ret(l>r);
        L=l,R=r,W=w;
        cov1(1);
    }
    void cov2(int p)
    {
        if(L<=t[p].l&&t[p].r<=R)
            return setf(p,0ll,inf,W);
        pdn(p);
        if(L<=t[p].mid) cov2(p<<1);
        if(R>t[p].mid) cov2(p<<1|1);
        pup(p);
    }
    void cov2(int l,int r,LL w)
    {
        ret(l>r);
        L=l,R=r,W=w;
        cov2(1);
    }
    int Fin1(int p)
    {
        if(t[p].l>=t[p].r)
        {
            // printf("> %d\n",t[p].l);
            return t[p].l;
        }
        pdn(p);
        int re=Fin1(p<<1|(t[p<<1|1].w1+g[t[p<<1|1].l]<W));
        pup(p);
        return re;
    }
    int fin1(LL w) // right most
    {
        W=w;
        return Fin1(1);
    }
    int Fin2(int p)
    {
        if(t[p].l>=t[p].r)
        {
            // printf("> %d\n",t[p].l);
            return t[p].l;
        }
        pdn(p);
        // printf("fin2 %d: %lld\n",p,t[p<<1].w2);
        int re=Fin2(p<<1|(t[p<<1].w2>=W));
        pup(p);
        return re;
    }
    int fin2(LL w) // left most
    {
        W=w;
        return Fin2(1);
    }
}tr;
void main_solve(int _)
{
    scanf("%d%lld",&n,&c);
    g.clear();
    For(i,1,n)
    {
        scanf("%lld%lld",&a[i],&b[i]);
        g.pb(a[i]); if(a[i]!=1ll) g.pb(a[i]-1ll);
    }
    sort(all(g)); g.erase(unique(all(g)),g.end());
    m=sz(g); g.insert(g.begin(),0ll);
    for(auto &i:g) i*=c;
    h=g; Rof(i,1,m) h[i]-=h[i-1];
    a[0]=a[n+1]=inf/c;
    b[0]=b[n+1]=-inf/c;
    // For(i,0,m) dp[i]=-g[i];
    tr.bld(1,0,m);
    // For(j,0,m) printf("%lld ",tr.qry(j)); printf("\n");
    For(i,1,n+1)
    {
        int t=lower_bound(all(g),a[i]*c)-g.begin();
        // printf("t=%d\n",t);
        if((!t)||(t>m)||(!b[i]))
        {
            tr.add(t,m,b[i]);
            continue;
        }
        if(b[i]>0ll)
        {
            LL w=tr.qry(t)+b[i];
            // printf("w=%lld\n",w);
            tr.add(t,m,-inf);
            tr.cov1(tr.fin2(w),t-1,w);
            tr.add(t,m,inf+b[i]);
        }
        else
        {
            tr.add(t,m,b[i]);
            t--;
            LL w=tr.qry(t)+g[t];
            tr.add(1,t,-inf);
            // printf("w=%lld\n",w);
            tr.cov2(t+1,tr.fin1(w),w);
            tr.add(1,t,inf);
        }
        // For(j,0,m) printf("%lld ",tr.qry(j)); printf("\n");
        // printf("%d:%d\n",i,t);
        // For(j,t,m) dp[j]+=(b[i]<<1);
        // For(j,1,m) get_max(dp[j],dp[j-1]-h[j]);
        // Rof(j,0,m-1) get_max(dp[j],dp[j+1]-h[j+1]);
        // For(j,0,m) printf("%lld ",dp[j]); printf("\n");
        // For(j,1,m) printf("%lld ",dp[j-1]-dp[j]); printf("\n");
    }
    // outll(dp[0]>>1);
    outll(tr.qry(0));
}
int main()
{
    // ios::sync_with_stdio(0); cin.tie(0);
    // freopen("in.txt","r",stdin);
    // freopen("out.txt","w",stdout);
    // srand(time(NULL));
    main_init();
    int _; inint(_); For(__,1,_) // T>1 ?
        // printf("\n------------\n\n"),
        main_solve(__);
    // cerr<<clock()<<'\n';
    return 0;
}
/*
4 5 4 3 2 1 
12 13 14 13 12 11 
20 21 22 21 20 19 
24 25 26 27 26 25 
24 25 26 27 26 27 
24 25 26 27 26 27 
*/