This commit is contained in:
Zengtudor 2024-10-08 09:51:15 +08:00
parent a3eb484f6e
commit 28220c49d2
5 changed files with 666 additions and 0 deletions

View File

@ -16,6 +16,8 @@ message("Build type is [${CMAKE_BUILD_TYPE}]")
project(algorithm_2024) project(algorithm_2024)
include_directories(${CMAKE_CURRENT_LIST_DIR}/src/include)
file(GLOB_RECURSE SRC_LIST CONFIGURE_DEPENDS ${CMAKE_CURRENT_LIST_DIR}/src/*.cpp) file(GLOB_RECURSE SRC_LIST CONFIGURE_DEPENDS ${CMAKE_CURRENT_LIST_DIR}/src/*.cpp)
foreach(SRC IN LISTS SRC_LIST) foreach(SRC IN LISTS SRC_LIST)

View File

@ -0,0 +1,179 @@
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=2e5+5;
struct node
{
ll mx,sum,t,sv,add,tag,len;
};
node tr[maxn<<2];
int n,q;
ll d[maxn<<2];
inline void mdf(int id,int k,ll w)
{
if(k==0)
{
tr[id].mx+=w;
tr[id].t+=w*w*tr[id].len+tr[id].sv*w+tr[id].sum*w;
tr[id].sum+=w*tr[id].len;
tr[id].sv+=w*tr[id].len;
tr[id].add+=w;
}
if(k==1)
{
tr[id].mx=w,tr[id].t=w*w*tr[id].len,tr[id].sum=w*tr[id].len,tr[id].sv=w*tr[id].len;
tr[id].add=0,tr[id].tag=w;
}
}
inline void pushdown(int id)
{
if(tr[id].tag)
{
mdf(id<<1,1,tr[id].tag);
mdf(id<<1|1,1,tr[id].tag);
}
if(tr[id].add)
{
mdf(id<<1,0,tr[id].add);
mdf(id<<1|1,0,tr[id].add);
}
tr[id].tag=tr[id].add=0;
}
node calc(node nd,int id)
{
node ret=nd;
if(tr[id].len==1)
{
ret.mx=max(ret.mx,tr[id].mx);
ret.sum+=tr[id].sum;
ret.t+=tr[id].sum*ret.mx;
ret.sv+=ret.mx;
ret.len+=tr[id].len;
}
else
{
pushdown(id);
if(tr[id<<1].mx>=nd.mx)
{
ret=calc(nd,id<<1);
ret.mx=max(ret.mx,tr[id<<1|1].mx);
ret.sum+=tr[id<<1|1].sum;
ret.t+=tr[id].t-tr[id<<1].t;
ret.sv+=tr[id].sv-tr[id<<1].sv;
ret.len+=tr[id<<1|1].len;
}
else
{
nd.sum+=tr[id<<1].sum;
nd.t+=tr[id<<1].sum*nd.mx;
nd.sv+=tr[id<<1].len*nd.mx;
nd.len+=tr[id<<1].len;
ret=calc(nd,id<<1|1);
}
}
return ret;
}
inline void pushup(int id)
{
tr[id]=calc(tr[id<<1],id<<1|1);
tr[id].tag=tr[id].add=0;
}
void build(int id,int l,int r)
{
if(l==r)
{
tr[id]=(node){d[l],d[l],d[l]*d[l],d[l],0,0,1};
return;
}
int mid=(l+r)>>1;
build(id<<1,l,mid);
build(id<<1|1,mid+1,r);
pushup(id);
}
void update(int id,int l,int r,int x,int y,int k,ll w)
{
if(l>=x && r<=y)
{
mdf(id,k,w);
return;
}
int mid=(l+r)>>1;
pushdown(id);
if(x<=mid) update(id<<1,l,mid,x,y,k,w);
if(y>mid) update(id<<1|1,mid+1,r,x,y,k,w);
pushup(id);
}
int num[maxn],cnt,L[maxn],R[maxn];
void get_num(int id,int l,int r,int x,int y)
{
if(l>=x && r<=y)
{
cnt++;
num[cnt]=id,L[cnt]=l,R[cnt]=r;
return;
}
int mid=(l+r)>>1;
pushdown(id);
if(x<=mid) get_num(id<<1,l,mid,x,y);
if(y>mid) get_num(id<<1|1,mid+1,r,x,y);
}
int get_pos(int id,int l,int r,node nd,ll t)
{
if(l==r) return l;
int mid=(l+r)>>1;
pushdown(id);
node dt=calc(nd,id<<1);
if(dt.t<=t) return get_pos(id<<1|1,mid+1,r,dt,t);
else return get_pos(id<<1,l,mid,nd,t);
}
int main()
{
//system("fc training.out training6.ans /W");
//return 0;
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
//freopen("training6.in","r",stdin);
//freopen("training.out","w",stdout);
int fl;
node nd,dt;
char opt;
ll s,t,w;
cin>>n>>q;
for(int i=0;i<n;i++) cin>>d[i];
build(1,0,n-1);
while(q--)
{
cin>>opt>>s>>t;
if(opt=='P')
{
cin>>w;
if(s<=t) update(1,0,n-1,s,t,0,w);
else update(1,0,n-1,0,t,0,w),update(1,0,n-1,s,n-1,0,w);
}
if(opt=='R')
{
cin>>w;
if(s<=t) update(1,0,n-1,s,t,1,w);
else update(1,0,n-1,0,t,1,w),update(1,0,n-1,s,n-1,1,w);
}
if(opt=='Q')
{
fl=0,cnt=0,nd=(node){0,0,0,0,0,0,0};
get_num(1,0,n-1,s,n-1);
get_num(1,0,n-1,0,n-1);
for(int i=1;i<=cnt;i++)
{
dt=calc(nd,num[i]);
if(dt.t>t)
{
fl=1;
cout<<get_pos(num[i],L[i],R[i],nd,t)<<'\n';
break;
}
nd=dt;
}
if(fl==0) cout<<get_pos(1,0,n-1,nd,(t-nd.t)%(tr[1].sum*tr[1].mx)+nd.t)<<'\n';
}
}
return 0;
}

View File

@ -0,0 +1,202 @@
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=200001;
int n,q;
int a[N];
struct node{
int mx;
ll sm,smx,tt;
int tg1,tg2;
}t[N*4];
#define ls (p<<1)
#define rs (p<<1|1)
#define mid (l+r>>1)
void build(int p,int l,int r){
for(int i=l;i<=r;i++){
t[p].mx=max(t[p].mx,a[i]);
t[p].sm+=a[i];
t[p].smx+=t[p].mx;
t[p].tt+=1ll*t[p].mx*a[i];
}
if(l==r)return;
build(ls,l,mid);
build(rs,mid+1,r);
}
void pushdown(int p,int l,int r){
int tg;
if(t[p].tg1){
tg=t[p].tg1;
t[p].tg1=0;
t[ls].mx=t[rs].mx=tg;
t[ls].sm=1ll*(mid-l+1)*tg,t[rs].sm=1ll*(r-mid)*tg;
t[ls].smx=t[ls].sm,t[rs].smx=t[rs].sm;
t[ls].tt=t[ls].sm*tg,t[rs].tt=t[rs].sm*tg;
t[ls].tg1=t[rs].tg1=tg;
t[ls].tg2=t[rs].tg2=0;
}
if(t[p].tg2){
tg=t[p].tg2;
t[p].tg2=0;
t[ls].mx+=tg,t[rs].mx+=tg;
t[ls].tt+=tg*(1ll*tg*(mid-l+1)+t[ls].sm+t[ls].smx),t[rs].tt+=tg*(1ll*tg*(r-mid)+t[rs].sm+t[rs].smx);
t[ls].sm+=1ll*(mid-l+1)*tg,t[rs].sm+=1ll*(r-mid)*tg;
t[ls].smx+=1ll*(mid-l+1)*tg,t[rs].smx+=1ll*(r-mid)*tg;
t[ls].tg2+=tg,t[rs].tg2+=tg;
}
}
int qmx;
ll qsmx,qtt;
void query(int p,int l,int r){
if(l==r){
if(qmx>t[p].mx)qsmx+=qmx,qtt+=qmx*t[p].sm;
else qsmx+=t[p].mx,qtt+=t[p].tt,qmx=t[p].mx;
return;
}
pushdown(p,l,r);
if(qmx>t[ls].mx){
qsmx+=1ll*qmx*(mid-l+1);
qtt+=qmx*t[ls].sm;
query(rs,mid+1,r);
}
else{
qsmx+=t[p].smx-t[ls].smx;
qtt+=t[p].tt-t[ls].tt;
query(ls,l,mid);
qmx=t[p].mx;
}
}
void update(int p,int l,int r){
t[p].mx=max(t[ls].mx,t[rs].mx);
t[p].sm=t[ls].sm+t[rs].sm;
qmx=t[ls].mx;
qsmx=t[ls].smx;
qtt=t[ls].tt;
query(rs,mid+1,r);
t[p].smx=qsmx;
t[p].tt=qtt;
}
void modify1(int p,int l,int r,int x,int y,int d){
if(x<=l&&r<=y){
t[p].mx=d;
t[p].sm=1ll*(r-l+1)*d;
t[p].smx=t[p].sm;
t[p].tt=t[p].sm*d;
t[p].tg1=d;
t[p].tg2=0;
return;
}
pushdown(p,l,r);
if(x<=mid)modify1(ls,l,mid,x,y,d);
if(mid<y)modify1(rs,mid+1,r,x,y,d);
update(p,l,r);
}
void modify2(int p,int l,int r,int x,int y,int d){
if(x<=l&&r<=y){
t[p].mx+=d;
t[p].tt+=d*(1ll*d*(r-l+1)+t[p].sm+t[p].smx);
t[p].sm+=1ll*(r-l+1)*d;
t[p].smx+=1ll*(r-l+1)*d;
t[p].tg2+=d;
return;
}
pushdown(p,l,r);
if(x<=mid)modify2(ls,l,mid,x,y,d);
if(mid<y)modify2(rs,mid+1,r,x,y,d);
update(p,l,r);
}
struct seg{
int p,l,r;
}b[100];
int m;
void getseg(int p,int l,int r,int x,int y){
if(x<=l&&r<=y){
b[++m]=(seg){p,l,r};
return;
}
pushdown(p,l,r);
if(x<=mid)getseg(ls,l,mid,x,y);
if(mid<y)getseg(rs,mid+1,r,x,y);
}
int gettt(int p,int l,int r,ll tm){
if(l==r){
query(p,l,r);
if(qtt>tm)return l-1;
return l;
}
pushdown(p,l,r);
int mx=qmx;
ll tt=qtt;
query(ls,l,mid);
if(qtt>tm){
qmx=mx,qtt=tt;
return gettt(ls,l,mid,tm);
}
return gettt(rs,mid+1,r,tm);
}
int getsm(int p,int l,int r,ll tm){
if(l==r){
if(t[p].sm>tm)return l-1;
return l;
}
pushdown(p,l,r);
if(t[ls].sm>tm)return getsm(ls,l,mid,tm);
return getsm(rs,mid+1,r,tm-t[ls].sm);
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
cin>>n>>q;
for(int i=0;i<n;i++)cin>>a[i];
build(1,0,n-1);
while(q--){
char op[2];
cin>>op;
if(op[0]=='R'){
int l,r,d;
cin>>l>>r>>d;
if(l<=r)modify1(1,0,n-1,l,r,d);
else modify1(1,0,n-1,l,n-1,d),modify1(1,0,n-1,0,r,d);
}
if(op[0]=='P'){
int l,r,d;
cin>>l>>r>>d;
if(l<=r)modify2(1,0,n-1,l,r,d);
else modify2(1,0,n-1,l,n-1,d),modify2(1,0,n-1,0,r,d);
}
if(op[0]=='Q'){
int s;
ll tm;
cin>>s>>tm;
m=0;
getseg(1,0,n-1,s,n-1);
if(s)getseg(1,0,n-1,0,s-1);
bool flag=0;
int mx=0;
ll tt=0;
for(int i=1;i<=m;i++){
qmx=mx,qtt=tt;
query(b[i].p,b[i].l,b[i].r);
if(qtt>tm){
qmx=mx,qtt=tt;
cout<<(gettt(b[i].p,b[i].l,b[i].r,tm)+1)%n<<'\n';
flag=1;
break;
}
mx=qmx,tt=qtt;
}
if(flag)continue;
tm-=qtt;
tm/=t[1].mx;
tm%=t[1].sm;
for(int i=1;i<=m;i++){
if(t[b[i].p].sm>tm){
cout<<(getsm(b[i].p,b[i].l,b[i].r,tm)+1)%n<<'\n';
break;
}
tm-=t[b[i].p].sm;
}
}
}
return 0;
}

View File

@ -0,0 +1,166 @@
#include <bits/stdc++.h>
using namespace std;
#define M 200005
typedef long long LL;
int A[M],n,q;
struct YD{
struct TNode{
int l,r,mark,mx,ad;
LL ans,sum,mx_s;
int len(){
return r-l+1;
}
void Reset(int ty,int v){
if(ty==1){
if(mark!=-1)mark+=v;
else ad+=v;
mx+=v;
ans+=1ll*v*v*len()+sum*v;
ans+=mx_s*v;
sum+=1ll*len()*v;
mx_s+=1ll*len()*v;
}else{
ad=0;
mark=mx=v;
mx_s=sum=1ll*len()*v;
ans=1ll*v*v*len();
}
}
}tree[M<<2];
#define fa tree[p]
#define lson tree[p<<1]
#define rson tree[p<<1|1]
void Down(int p){
if(fa.ad!=0)lson.Reset(1,fa.ad),rson.Reset(1,fa.ad),fa.ad=0;
if(fa.mark!=-1)lson.Reset(2,fa.mark),rson.Reset(2,fa.mark),fa.mark=-1;
}
LL get_mx_s(int p,int mx){
LL ret=0;
while(fa.l^fa.r){
Down(p);
if(lson.mx<=mx)ret+=1LL*mx*lson.len(),p=p<<1|1;
else ret+=fa.mx_s-lson.mx_s,p<<=1;
}
return ret+max(mx,fa.mx);
}
LL get_ans(int p,int mx){
LL ret=0;
while(fa.l^fa.r){
Down(p);
if(lson.mx<=mx)ret+=lson.sum*mx,p=p<<1|1;
else ret+=fa.ans-lson.ans,p<<=1;
}
return ret+1LL*fa.mx*max(fa.mx,mx);
}
void Up(int p){
fa.mx=max(lson.mx,rson.mx);
fa.sum=lson.sum+rson.sum;
fa.ans=lson.ans+get_ans(p<<1|1,lson.mx);
fa.mx_s=lson.mx_s+get_mx_s(p<<1|1,lson.mx);
}
int pos[M];
void build(int l,int r,int p=1){
fa.l=l,fa.r=r;
fa.mark=-1;
fa.ad=0;
if(l==r){
pos[l]=p;
fa.mx=A[l];
fa.ans=1ll*A[l]*A[l];
fa.sum=A[l];
fa.mx_s=A[l];
return;
}
int mid=l+r>>1;
build(l,mid,p<<1);
build(mid+1,r,p<<1|1);
Up(p);
}
void Update(int l,int r,int ty,int v,int p=1){
if(l==fa.l&&r==fa.r)return void(fa.Reset(ty,v));
Down(p);
int mid=fa.l+fa.r>>1;
if(r<=mid)Update(l,r,ty,v,p<<1);
else if(l>mid)Update(l,r,ty,v,p<<1|1);
else Update(l,mid,ty,v,p<<1),Update(mid+1,r,ty,v,p<<1|1);
Up(p);
}
int stk[M],top;
void Pb(int x){
for(int i=pos[x]>>1;i;i>>=1)stk[++top]=i;
while(top)Down(stk[top--]);
}
LL sum,tmp;
int mx;
int Ask_r(int x,LL t){
Pb(x);
int p=pos[x];
if(1LL*fa.mx*max(mx,fa.mx)>=t)return x-1;
int ret=x;
sum=1LL*max(mx,fa.mx)*fa.mx;
mx=max(mx,fa.mx);
while(p!=1){
if(p&1)p>>=1;
else{
p>>=1;
if(sum+(tmp=get_ans(p<<1|1,mx)) < t){
ret=rson.r,mx=max(mx,rson.mx),sum+=tmp;
}
else {
Down(p);
p=p<<1|1;
while(fa.l^fa.r){
Down(p);
if(sum+(tmp=get_ans(p<<1,mx)) < t)ret=lson.r,mx=max(mx,lson.mx),sum+=tmp,p=p<<1|1;
else p<<=1;
}
if(sum+(tmp=1LL*fa.mx*max(fa.mx,mx)) < t)ret=fa.l,mx=max(mx,lson.mx),sum+=tmp;
return ret;
}
}
}
return ret;
}
int Query(int x,LL t){
mx=sum=0;
int r=Ask_r(x,t);
if(r<n-1)return r+1;
else{
t-=sum;
sum=0;
r = Ask_r(0,t);
if(r<n-1)return r+1;
t-=sum;
sum=0;
LL tt = tree[1].sum * mx;
LL k = t/tt-(t%tt==0);
t-=tt*k;
return Ask_r(0,t)+1;
}
}
#undef fa
#undef lson
#undef rson
}Seg;
int main(){
scanf("%d%d",&n,&q);
for(int i=0;i<n;i++)scanf("%d",&A[i]);
Seg.build(0,n-1);
for(int i=1;i<=q;i++){
LL l,r,v;
char op[3];
scanf("%s%lld%lld",op,&l,&r);
if(op[0]=='Q'){
printf("%d\n",Seg.Query(l,r+1));
}else {
scanf("%lld",&v);
if(l<=r)Seg.Update(l,r,op[0]=='P'?1:2,v);
else {
Seg.Update(l,n-1,op[0]=='P'?1:2,v);
Seg.Update(0, r ,op[0]=='P'?1:2,v);
}
}
}
return 0;
}

117
src/include/bits/stdc++.h Normal file
View File

@ -0,0 +1,117 @@
// C++ includes used for precompiling -*- C++ -*-
// Copyright (C) 2003-2014 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// Under Section 7 of GPL version 3, you are granted additional
// permissions described in the GCC Runtime Library Exception, version
// 3.1, as published by the Free Software Foundation.
// You should have received a copy of the GNU General Public License and
// a copy of the GCC Runtime Library Exception along with this program;
// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
// <http://www.gnu.org/licenses/>.
/** @file stdc++.h
* This is an implementation file for a precompiled header.
*/
// 17.4.1.2 Headers
// C
#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
// #include <cstdalign>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif
// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif