1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
| #include<bits/stdc++.h> #define I inline #define LL long long #define It set<node>::iterator using namespace std; inline LL read(){ LL res=0,f=1;char ch=getchar(); while(ch<'0'ch>'9'){if(ch=='-') f=-1;ch=getchar();} while(ch>='0'&&ch<='9') res=res*10+ch-'0',ch=getchar(); return res*f; } inline void write(LL x){ if(x<0) putchar('-'),x=-x; if(x<10) putchar(x+'0'); else{ write(x/10); putchar(x%10+'0'); } } struct node{ int l,r; mutable LL val; node(int L,int R=-1,LL V=0):l(L),r(R),val(V){} bool operator<(const node& q)const{return l<q.l;} }; set<node> s; inline LL fpow(LL a,LL b,LL mod){ LL s=1;a%=mod; while(b){ if(b&1) s*=a,s%=mod; a*=a,a%=mod; b>>=1; } return s; } I It split(int x){ It it=s.lower_bound(node(x)); if(it!=s.end()&&it->l==x) return it; it--; int L=it->l,R=it->r;LL V=it->val; s.erase(it); s.insert(node(L,x-1,V)); return s.insert(node(x,R,V)).first; } I void assign(int l,int r,LL val){ It it2=split(r+1),it1=split(l); s.erase(it1,it2); s.insert(node(l,r,val)); } I void add(int l,int r,LL val){ It it2=split(r+1),it1=split(l); for(;it1!=it2;it1++) it1->val+=val; } I LL grank(int l,int r,int x){ It it2=split(r+1),it1=split(l); vector<pair<LL,int> > tmp;tmp.clear(); for(;it1!=it2;it1++) tmp.push_back(make_pair(it1->val,it1->r-it1->l+1)); sort(tmp.begin(),tmp.end()); for(vector<pair<LL,int> >::iterator it=tmp.begin();it!=tmp.end();it++){ x-=it->second; if(x<=0) return it->first; } } I LL sum(int l,int r,LL x,LL y){ It it2=split(r+1),it1=split(l); LL s=0; for(;it1!=it2;it1++) s+=(LL)(it1->r-it1->l+1)*fpow(it1->val,x,y)%y,s%=y; return s; } int n,m; LL seed,vmax,a[100010]; I LL rnd(){ LL res=seed; seed=(seed*7+13)%1000000007; return res; } int main(){ n=read(),m=read(),seed=read(),vmax=read(); for(int i=1;i<=n;i++){ a[i]=rnd()%vmax+1; s.insert(node(i,i,a[i])); } s.insert(node(n+1,n+1,0)); for(int i=1;i<=m;i++){ LL op,l,r,x,y; op=rnd()%4+1,l=rnd()%n+1,r=rnd()%n+1; if(l>r) swap(l,r); if(op==3) x=rnd()%(r-l+1)+1; else x=rnd()%vmax+1; if(op==4) y=rnd()%vmax+1; if(op==1) add(l,r,x); else if(op==2) assign(l,r,x); else if(op==3) write(grank(l,r,x)),putchar('\n'); else if(op==4) write(sum(l,r,x,y)),putchar('\n'); } return 0; }
|