题解:
性质:对于连续的R次同样的操作,可以变成一次操作。
解法一:对原序列分块,然后暴力操作即可。时间复杂度
O(SC−−√)
O
(
S
C
)
解法二:使用平衡树维护序列。 时间复杂度
O(Slogn+n)
O
(
S
l
o
g
n
+
n
)
解法三:暴力维护当前序列的区间状态,每次操作沿左右端点将区间切开。时间复杂度
O(S2)
O
(
S
2
)
综上所述,此题是一道优秀的数据结构模板题兼暴力题。
#include<bits/stdc++.h>
#define LL long long
#define ull unsigned long long
#define ULL ull
#define mp make_pair
#define pii pair<int,int>
#define piii pair<int, pii >
#define pll pair <ll,ll>
#define pb push_back
#define big 20160116
#define INF 2147483647
#define pq priority_queue
using namespace std;
inline int read(){
int x=0,f=1;
char ch=getchar();
while (ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=getchar();}
while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
namespace Mymath{
LL qp(LL x,LL p,LL mod){
LL ans=1;
while (p){
if (p&1) ans=ans*x%mod;
x=x*x%mod;
p>>=1;
}
return ans;
}
LL inv(LL x,LL mod){
return qp(x,mod-2,mod);
}
LL C(LL N,LL K,LL fact[],LL mod){
return fact[N]*inv(fact[K],mod)%mod*inv(fact[N-K],mod)%mod;
}
template <typename Tp> Tp gcd(Tp A,Tp B){
if (B==0) return A;
return gcd(B,A%B);
}
template <typename Tp> Tp lcm(Tp A,Tp B){
return A*B/gcd(A,B);
}
};
namespace fwt{
using namespace Mymath;
void FWT(int a[],int n,LL mod)
{
for(int d=1;d<n;d<<=1)
for(int m=d<<1,i=0;i<n;i+=m)
for(int j=0;j<d;j++)
{
int x=a[i+j],y=a[i+j+d];
a[i+j]=(x+y)%mod,a[i+j+d]=(x-y+mod)%mod;
//xor:a[i+j]=x+y,a[i+j+d]=x-y;
//and:a[i+j]=x+y;
//or:a[i+j+d]=x+y;
}
}
void UFWT(int a[],int n,LL mod)
{
LL rev=inv(2,mod);
for(int d=1;d<n;d<<=1)
for(int m=d<<1,i=0;i<n;i+=m)
for(int j=0;j<d;j++)
{
int x=a[i+j],y=a[i+j+d];
a[i+j]=1LL*(x+y)*rev%mod,a[i+j+d]=(1LL*(x-y)*rev%mod+mod)%mod;
//xor:a[i+j]=(x+y)/2,a[i+j+d]=(x-y)/2;
//and:a[i+j]=x-y;
//or:a[i+j+d]=y-x;
}
}
void solve(int a[],int b[],int n,LL mod)
{
FWT(a,n,mod);
FWT(b,n,mod);
for(int i=0;i<n;i++) a[i]=1LL*a[i]*b[i]%mod;
UFWT(a,n,mod);
}
};
const int Maxn=2e6+5;
struct node{
node* l,*r;
int siz,fix;
int val;
node(int V){
l=NULL;r=NULL;
val=V;
siz=1;
fix=rand();
}
int lsiz(){
return (l)?l->siz:0;
}
int rsiz(){
return (r)?r->siz:0;
}
void Update(){
siz=lsiz()+rsiz()+1;
}
};
void Update(node*&x){
if (!x) return;
x->Update();
}
node* root;
void Merge(node* &P,node* lf,node* rg){
Update(P);Update(lf);Update(rg);
if (!lf || !rg){
P=(lf)?lf:rg;
Update(P);
return;
}
if (lf->fix>rg->fix){
Merge(lf->r,lf->r,rg);
P=lf;
}
else{
Merge(rg->l,lf,rg->l);
P=rg;
}
Update(P);
return ;
}
void Split(node* p,node*& l,node*& r,int k){
if (!p){
l=r=p;
return;
}
if (!k){
r=p;
Update(r);
l=0;
return;
}
if (p->lsiz()>=k){
Split(p->l,l,p->l,k);
r=p;
Update(r);
Update(l);
return;
}
else{
Split(p->r,p->r,r,k-p->lsiz()-1);
l=p;
Update(r);
Update(l);
return;
}
}
LL dfs(node* t,int f){
int tot=f+t->lsiz();
LL ret=0;
if (tot%2==0) ret+=t->val;
if (t->l){
ret+=dfs(t->l,f);
}
if (t->r){
ret+=dfs(t->r,tot+1);
}
return ret;
}
void mian(int ti,int tmd){
int c,s;
c=read();s=read();
root=0;
for (int i=0;i<c;i++){
node* tmp=new node(i);
Merge(root,root,tmp);
}
//dfs(root,0);
//cout<<endl;
for (int i=0;i<s;i++){
int p,l,tmm;
p=read();l=read();tmm=read();
node* lf;
Split(root,lf,root,p+l);
//dfs(lf,0);
int tt=l+p;
l=l*tmm%(l+p);
node* L0,*R0;
Split(lf,L0,R0,tt-l);
Merge(R0,R0,L0);
Merge(root,R0,root);
//dfs(root,0);
}
LL ans=dfs(root,0);
printf("Case %d:\n%lld\n",ti,ans);
if (tmd) printf("\n");
}
int main(){
int tc;
tc=read();
int tt=0;
while (tc--){
tt++;
mian(tt,tc);
}
return 0;
}