結果

問題 No.1568 Sushi
ユーザー blackyukiblackyuki
提出日時 2021-04-17 14:28:14
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,460 ms / 2,000 ms
コード長 3,927 bytes
コンパイル時間 3,123 ms
コンパイル使用メモリ 214,160 KB
実行使用メモリ 179,016 KB
最終ジャッジ日時 2023-09-07 16:08:04
合計ジャッジ時間 32,380 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 24 ms
6,908 KB
testcase_05 AC 18 ms
4,524 KB
testcase_06 AC 21 ms
5,632 KB
testcase_07 AC 20 ms
4,628 KB
testcase_08 AC 24 ms
6,636 KB
testcase_09 AC 1,271 ms
138,640 KB
testcase_10 AC 1,157 ms
99,744 KB
testcase_11 AC 1,150 ms
104,420 KB
testcase_12 AC 1,142 ms
101,840 KB
testcase_13 AC 1,225 ms
137,356 KB
testcase_14 AC 1,083 ms
94,964 KB
testcase_15 AC 1,327 ms
170,344 KB
testcase_16 AC 1,146 ms
97,836 KB
testcase_17 AC 1,460 ms
177,724 KB
testcase_18 AC 1,276 ms
132,428 KB
testcase_19 AC 1,225 ms
138,772 KB
testcase_20 AC 1,124 ms
98,428 KB
testcase_21 AC 1,237 ms
132,052 KB
testcase_22 AC 1,234 ms
121,932 KB
testcase_23 AC 1,301 ms
164,944 KB
testcase_24 AC 1,227 ms
123,084 KB
testcase_25 AC 1,314 ms
150,816 KB
testcase_26 AC 1,355 ms
167,360 KB
testcase_27 AC 1,440 ms
179,016 KB
testcase_28 AC 1,167 ms
116,096 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#define rep(i, n)  for(long long i=0;i<(long long)(n);i++)
#define REP(i,k,n) for(long long i=k;i<(long long)(n);i++)
typedef long long ll;
#define all(a) a.begin(),a.end()
#define lb(v,k) (lower_bound(all(v),(k))-v.begin())
ll cons=1000000000000000;
ll inf=1001001001001001001;
ll lg=18;
using vi=vector<ll>;
using vvi=vector<vi>;
typedef pair<ll,ll> P;
template<class T> bool chmin(T&a,T b){if(a>b){a=b;return true;}return false;}
template<class T> bool chmax(T&a,T b){if(a<b){a=b;return true;}return false;}
template<class T> void out(T a){cout<<a<<'\n';}
template<class T> void outp(T a){cout<<'('<<a.first<<','<<a.second<<')'<<'\n';}
class sparsetable{
    vi tmp;
    vvi Lmi,Rmi,Lma,Rma;
    vi rng;
public:
    sparsetable(vi v){
        rng.resize(v.size()+1);
        rep(c,lg)REP(i,1<<c,1<<(c+1))if(i<=v.size())rng[i]=c;
        Lmi=vvi(lg,vi(v.size()));
        Lma=vvi(lg,vi(v.size()));
        Rmi=vvi(lg,vi(v.size()));
        Rma=vvi(lg,vi(v.size()));
        rep(i,v.size()){
            Lmi[0][i]=v[i];
            Lma[0][i]=v[i];
            Rmi[0][i]=v[i];
            Rma[0][i]=v[i];
        }
        rep(c,lg-1)rep(i,v.size()){
            ll ni=i+(1<<c);
            if(ni<v.size()){
                Lmi[c+1][i]=min(Lmi[c][i],Lmi[c][ni]);
                Lma[c+1][i]=max(Lma[c][i],Lma[c][ni]);
            }
            ni=i-(1<<c);
            if(ni>=0){
                Rmi[c+1][i]=min(Rmi[c][i],Rmi[c][ni]);
                Rma[c+1][i]=max(Rma[c][i],Rma[c][ni]);
            }
        }
    }
    ll mi(ll l,ll r){
        if(l>=r)return inf;
        return min(Lmi[rng[r-l]][l],Rmi[rng[r-l]][r-1]);
    }
    ll ma(ll l,ll r){
        if(l>=r)return -inf;
        return max(Lma[rng[r-l]][l],Rma[rng[r-l]][r-1]);
    }
};
inline ll readll() {
    ll x = 0;
    char ch = getchar_unlocked();
    while (ch < '0' || ch > '9') ch = getchar_unlocked();
    while (ch >= '0' && ch <= '9'){
		x = (x << 3) + (x << 1) + ch - '0';
		ch = getchar_unlocked();
	}
    return x;
}
int main(){
    ll L=readll(),n=readll(),q=readll();
    vi difpoint(n+3);rep(i,n)difpoint[i+1]=readll();difpoint[n+1]=inf/2;difpoint[n+2]=inf;
    n+=3;
    vvi tablema(lg,vi(n)),tablemi(lg,vi(n));
    rep(i,n)tablema[0][i]=tablemi[0][i]=0;
    vi v(n);
    rep(i,n-1){
        if(i%2)v[i+1]=v[i]-difpoint[i+1]+difpoint[i];
        else v[i+1]=v[i]+difpoint[i+1]-difpoint[i];
    }
    sparsetable sp(v);
    rep(c,lg-1)rep(i,n){
        ll ni=i+(1<<c);
        tablema[c+1][i]=tablema[c][i];
        tablemi[c+1][i]=tablemi[c][i];
        if(ni<n){
            chmax(tablema[c+1][i],tablema[c][ni]);
            chmin(tablemi[c+1][i],tablemi[c][ni]);
            ll nni=min(ni+(1<<c),n);
            chmax(tablema[c+1][i],sp.ma(ni,nni)-sp.mi(i,ni));
            chmin(tablemi[c+1][i],sp.mi(ni,nni)-sp.ma(i,ni));
        }
    }
    auto val=[&](ll t){
        ll l=lb(difpoint,t);
        if(l%2)return v[l]-difpoint[l]+t;
        return v[l]+difpoint[l]-t;
    };
    ll ans=0;
    rep(qq,q){
        ll x,t;cin>>x>>t;
        x+=ans;t+=ans;x%=L;t%=cons;
        ll idx=lb(difpoint,t);
        ll cur=val(t);
        ll ok=n,ng=idx-1;
        while(ok-ng>1){
            ll md=(ok+ng)/2;
            if(sp.ma(idx,md+1)-cur>=x||sp.mi(idx,md+1)-cur<=x-L)ok=md;
            else ng=md;
        }
        ll d=max(v[ok]-cur-x,x-L-v[ok]+cur);
        ans=difpoint[ok]-d;
        ll r=idx;
        for(int c=lg-1;c>=0;c--){
            ll nr=r+(1<<c);
            ll ma=max(tablema[c][r],sp.ma(r,min(nr,n))-sp.mi(idx,r));
            ll mi=min(tablemi[c][r],sp.mi(r,min(nr,n))-sp.ma(idx,r));
            if(ma>=x||mi<=x-L)continue;
            r=nr;
        }
        d=max(v[r]-sp.mi(idx,r+1)-x,x-L-v[r]+sp.ma(idx,r+1));
        chmin(ans,difpoint[r]-d);
        ans-=t;
        out(ans);
    }
}
0