#pragma GCC optimize("O3") #pragma GCC target("avx") #include #include #include #include using namespace std; #define INF (1<<29) class RMQ{ int size; vector minval; vector maxval; pair _query(int a,int b,int k,int l, int r){ if(r<=a||b<=l)return make_pair(INF,-INF); if(a<=l&&r<=b)return make_pair(minval[k],maxval[k]); else{ pair vl=_query(a,b,k*2+1,l,(l+r)/2); pair vr=_query(a,b,k*2+2,(l+r)/2,r); return make_pair(min(vl.first,vr.first),max(vl.second,vr.second)); } } public: RMQ(int k){init(k);} void init(int k){ for(size=1;size(2*size-1,INF); maxval=vector(2*size-1,-INF); } void update(int k,int t){ k+=size-1; minval[k]=maxval[k]=t; while(k>0){ k=(k-1)/2; minval[k]=min(minval[k*2+1],minval[k*2+2]); maxval[k]=max(maxval[k*2+1],maxval[k*2+2]); } } pair query(int a,int b){ return _query(a,b,0,0,size); } }; int N,D,I,J; long long X; void iterate(){ I=X/~-N; J=X%~-N; if(I>J)swap(I,J); else J++; X=(X+D)%((long long)N*~-N); } int calc(const string &s,const string &t){ int l=min(s.size(),t.size()),i=0; for(;i>N; vector>v(N); vectorrev(N); for(int i=0;i>v[i].first,v[i].second=i; cin>>M>>X>>D; sort(v.begin(),v.end()); for(int i=0;ij)swap(i,j); R+=rmq.query(i+1,j+1).first; } cout<