Submission #1349246

#TimeUsernameProblemLanguageResultExecution timeMemory
1349246vjudge1Legendary Dango Eater (JOI26_dango)C++17
100 / 100
469 ms185272 KiB
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#include <cstring>
#include <set>
#ifdef _WIN32
#define getchar _getchar_nolock
#define putchar _putchar_nolock
#else
#define getchar getchar_unlocked
#define putchar putchar_unlocked
#endif
#define pll pair<ll,ll>
#define pld pair<ld,ld>
typedef long long ll;
typedef long double ld;
typedef __int128 i128;
namespace io {
    using namespace std;
    template <typename T> void debug (T x) {
        cerr<<x<<'\n';
    }
    template <typename T> void debuglen (T x) {
        cerr<<x<<' ';
    }
    template <typename T,typename...Args> void debug (T x,Args...args) {
        cerr<<x<<' ';
        debug(args...);
    }
    template <typename T> void debug (T*lt,T*rt) {
        ll len=rt-lt;
        for (ll i=0;i<len;i++) {
            debuglen(*(lt+i));
        }
        cerr<<'\n';
    }
    inline ll read () {
        char x=getchar();
        ll ans=0,f=1;
        while (x<'0'||x>'9') {
            if (x=='-') {
                f=-1;
            }
            x=getchar();
        }
        while (x>='0'&&x<='9') {
            ans=(ans<<1)+(ans<<3);
            ans+=(x^'0');
            x=getchar();
        }
        return ans*f;
    }
    void print (ll x) {
        if (x<0) {
            x=-x;
            putchar('-');
        }
        if (x>=10) {
            print(x/10);
        }
        putchar(x%10+'0');
    }
}
using namespace io;
const ll N=5e5+5,mod=1e9+7,inf=2e18;
const ld eps=1e-6;
ll n,q,k,a[N],sum[N],b[N],f[N][20],g[N][20];
set<pll> st;
inline void calc (ll l,ll r,ll num) {
	auto it=st.lower_bound({l,0});
	while (it!=st.end()&&(*it).first<=r) {
		f[(*it).second][0]=num+1;
		it=st.erase(it);
	}
}
inline void solve () {
	n=read(),q=read(),k=read();
	for (ll i=1;i<=n;i++) {
		a[i]=read();
		if (!(i&1)) {
			a[i]=-a[i];
		}
		sum[i]=sum[i-1]+a[i];
		b[i]=(sum[i]%k+k)%k;
	}
	for (ll i=1;i<=n;i+=2) {
		f[i][0]=n+1;
	}
	for (ll i=2;i<=n;i+=2) {
		f[i][0]=i+1;
		st.insert({b[i-2],i-1});
		calc(b[i-1]+a[i]+1,b[i-1],i);
		calc(max(b[i-1]+a[i]+k,b[i-1])+1,k-1,i);
	}
	for (ll i=1;i<=n;i+=2) {
		ll num=sum[f[i][0]-2]-sum[i-1];
		if (f[i][0]==n+1&&n&1) {
			num+=a[n];
		}
		g[i][0]=num/k;
	}
	ll kl=-1,pl=n;
	while (pl) {
		pl>>=1;
		kl++;
	}
	for (ll i=1;i<=kl;i++) {
		for (ll j=1;j<=n-(1<<i)+1;j++) {
			f[j][i]=f[f[j][i-1]][i-1];
			g[j][i]=g[j][i-1]+g[f[j][i-1]][i-1];
		}
	}
	while(q--) {
		ll x=read(),y=read();
		ll ans=0;
		kl=-1,pl=y-x+1;
		while (pl) {
			pl>>=1;
			kl++;
		}
		for(ll i=kl;i>=0;i--) {
			if(f[x][i]>y||!f[x][i]) {
				continue;
			}
			ans+=g[x][i];
			x=f[x][i];
		}
		ll num=sum[y]-sum[x-1];
		if (!(x&1)) {
			num-=a[x];
		}
		if (x!=y&&!(y&1)) {
			num-=a[y];
		}
		ans+=num/k;
		print(ans);
		puts("");
	}
}
int main () {
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
    ll T=1;
//	T=read();
    while (T--) {
        solve();
    }
    return 0;
}
#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...