Educational Codeforces Round 132 (Rated for Div. 2)
B
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const ll N=1e5+5;
ll a[N],s1[N],s2[N],l,r,n,m;
int main(){
scanf("%lld%lld",&n,&m);
for(ll i=1;i<=n;i++) scanf("%lld",&a[i]);
for(ll i=1;i<n;i++){
if(a[i]<a[i+1]) s2[i]=a[i+1]-a[i];
else s1[i]=a[i]-a[i+1];
}
for(ll i=1;i<n;i++){
s1[i]=s1[i-1]+s1[i];
}
for(ll i=n-1;i;i--){
s2[i]=s2[i+1]+s2[i];
}
for(ll i=1;i<=m;i++){
scanf("%lld%lld",&l,&r);
if(r>l) printf("%lld\n",s1[r-1]-s1[l-1]);
else printf("%lld\n",s2[r]-s2[l]);
}
return 0;
}
CodeTON Round 2 (Div. 1 + Div. 2, Rated, Prizes!)
B
#include <bits/stdc++.h>
using namespace std;
int t,n,m,a;
int main(){
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
int f=0,l=-1e9,r=1e9,ans=0;
for(int i=1;i<=n;i++){
scanf("%d",&a);
l=max(l, a-m);
r=min(r, a+m);
if(l>r){
l=a-m;
r=a+m;
ans++;
}
}
printf("%d\n",ans);
}
return 0;
}
Codeforces Round #813 (Div. 2)
B
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N=1e5+5;
int t,n;
int main(){
scanf("%d",&t);
while(t--){
scanf("%d",&n);
if(n==1){
printf("1");
}
else if(n==2){
printf("2 1");
}
else if(n%2==0){
for(int i=1;i<=n;i++){
if(i%2==0) printf("%d ",i-1);
else printf("%d ",i+1);
}
}
else{
printf("1 3 2 ");
for(int i=4;i<=n;i++){
if(i%2==0) printf("%d ",i+1);
else printf("%d ",i-1);
}
}
printf("\n");
}
return 0;
}
Codeforces Round #814 (Div. 2)
C
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N=1e5+5;
const int INF=0x3f3f3f3f;
int t,n,q,a[N],s1[N],s2[N];
int main(){
scanf("%d",&t);
while(t--){
memset(s1,0,sizeof(s1));
memset(s2,0,sizeof(s2));
scanf("%d%d",&n,&q);
int m=0,id=0;
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
if(a[i]>m){
s1[a[i]]=i;
s2[m]=i;
m=a[i];
}
}
for(int i=1;i<=q;i++){
int x,y;
scanf("%d%d",&x,&y);
if(a[x]==m){
if(s1[a[x]]>2)printf("%d\n",max(0,y-s1[a[x]]+2));
else if(s1[a[x]]==1) printf("%d\n",max(0,y-s1[a[x]]+2)-1);
else printf("%d\n",max(0,y-s1[a[x]]+2));
}
else{
if(s1[a[x]]==0) printf("0\n");
else {
if(s1[a[x]]==1) printf("%d\n",max(0,min(y+2,s2[a[x]])-s1[a[x]]-1));
else printf("%d\n",max(0,min(y+2,s2[a[x]])-s1[a[x]]));
}
}
}
}
return 0;
}