This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"
const int DECALAGE=(1<<17),PERMUTMAXI=10;
int nbpers,nbpermut,nbquest,val,l,r,m,rep;
int arbre[DECALAGE*2][3];
int permut[PERMUTMAXI];
int indice[PERMUTMAXI];
int somme(int a,int b,int tab){
if (b<a){
return 0;
}
if (a==b){
return arbre[a][tab];
}
if (a%2==1){
return arbre[a][tab]+somme(a+1,b,tab);
}
if (b%2==0){
return arbre[b][tab]+somme(a,b-1,tab);
}
return somme(a/2,b/2,tab);
}
void modif(int a,int tab){
a/=2;
while (a>0){
arbre[a][tab]=arbre[a*2][tab]+arbre[a*2+1][tab];
a/=2;
}
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>nbpers>>nbpermut;
for (int i=0;i<nbpers;i++){
cin>>arbre[i+DECALAGE][1];
arbre[i+DECALAGE][0]=arbre[i+DECALAGE][1]*(i+1);
arbre[i+DECALAGE][2]=arbre[i+DECALAGE][1]*(nbpers-i);
}
for (int i=DECALAGE-1;i>=0;i--){
for (int j=0;j<3;j++){
arbre[i][j]=arbre[i*2][j]+arbre[i*2+1][j];
}
}
cin>>nbquest;
for (int iquest=0;iquest<nbquest;iquest++){
cin>>val;
if (val==2){
cin>>l>>r>>m;
l--;
r--;
rep=somme(l+DECALAGE,min(l+m-2,r-m)+DECALAGE,0)-somme(l+DECALAGE,min(l+m-2,r-m)+DECALAGE,1)*l;
rep+=somme(min(l+m-2,r-m)+1+DECALAGE,max(l+m,r-m+2)-1+DECALAGE,1)*min(m,r-l-m+2);
rep+=somme(max(l+m,r-m+2)+DECALAGE,r+DECALAGE,2)-somme(max(l+m,r-m+2)+DECALAGE,r+DECALAGE,1)*(nbpers-r-1);
cout<<rep<<endl;
}
else {
for (int i=0;i<nbpermut;i++){
cin>>indice[i];
indice[i]--;
permut[i]=arbre[indice[i]+DECALAGE][1];
}
if (nbpermut>1){
for (int i=0;i<nbpermut-1;i++){
arbre[indice[i]+DECALAGE][0]=permut[i+1]*(indice[i]+1);
modif(indice[i]+DECALAGE,0);
arbre[indice[i]+DECALAGE][1]=permut[i+1];
modif(indice[i]+DECALAGE,1);
arbre[indice[i]+DECALAGE][2]=permut[i+1]*(nbpers-indice[i]);
modif(indice[i]+DECALAGE,2);
}
arbre[indice[nbpermut-1]+DECALAGE][0]=permut[0]*(indice[nbpermut-1]+1);
modif(indice[nbpermut-1]+DECALAGE,0);
arbre[indice[nbpermut-1]+DECALAGE][1]=permut[0];
modif(indice[nbpermut-1]+DECALAGE,1);
arbre[indice[nbpermut-1]+DECALAGE][2]=permut[0]*(nbpers-indice[nbpermut-1]);
modif(indice[nbpermut-1]+DECALAGE,2);
}
/*for (int i=0;i<nbpers;i++){
cout<<arbre[i+DECALAGE][1]<<" ";
}
cout<<endl;*/
}
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |