题意:中文
思路:直接一个n^2 的预处理就行了,然后有个logn,n比较小可以通过
#include <vector>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3fll;
const int maxn=1010;
ll A[maxn],ans[maxn][maxn];
inline int getint(){
int res=0;
char c=getchar();
bool mi=false;
while(c<'0' || c>'9') mi=(c=='-'),c=getchar();
while('0'<=c && c<='9') res=res*10+c-'0',c=getchar();
return mi ? -res : res;
}
inline ll getll(){
ll res=0;
char c=getchar();
bool mi=false;
while(c<'0' || c>'9') mi=(c=='-'),c=getchar();
while('0'<=c && c<='9') res=res*10+c-'0',c=getchar();
return mi ? -res : res;
}
struct Node{
Node *ch[2];
int r,s;
ll v,sum;
Node(ll v):v(v){ch[0]=ch[1]=NULL;r=rand();s=1;sum=0;}
int cmp(ll x){
if(x==v) return -1;
return x<v? 0:1;
}
void maintain(){
s=1,sum=v;
if(ch[0]!=NULL) s+=ch[0]->s,sum+=ch[0]->sum;
if(ch[1]!=NULL) s+=ch[1]->s,sum+=ch[1]->sum;
}
};
Node *root;
void Rotate(Node* &o,int d){
Node* k=o->ch[d^1];o->ch[d^1]=k->ch[d];k->ch[d]=o;
o->maintain();k->maintain();o=k;
}
void Insert(Node* &o,ll x){
if(o==NULL) o=new Node(x);
else{
int d=x<(o->v)? 0:1;
Insert(o->ch[d],x);
if(o->ch[d]->r>o->r) Rotate(o,d^1);
}
o->maintain();
}
ll Kth(Node* o,int k,int op,ll pp){
int s=(o->ch[0]==NULL?0:o->ch[0]->s);
ll lll=0,qqq=0;
if(o->ch[1]!=NULL) lll=o->ch[1]->sum;
if(o->ch[0]!=NULL) qqq=o->ch[0]->sum;
if(k<s+1) return Kth(o->ch[0],k,op,pp+lll+o->v);
else if(k>s+1) return Kth(o->ch[1],k-(s+1),op,pp-qqq-o->v);
else{
ll tmp1=pp,tmp2=0;
if(o->ch[1]!=NULL) tmp1+=o->ch[1]->sum;
if(o->ch[0]!=NULL) tmp2=o->ch[0]->sum;
if(op==1) return tmp1-(o->v)-tmp2;
else return tmp1-tmp2;
}
}
int main(){
int n,m,x,y,op;
while(scanf("%d%d",&n,&m)!=-1){
for(int i=1;i<=n;i++) A[i]=getll();
memset(ans,0,sizeof(ans));
ll fans=0;
for(int i=1;i<=n;i++){
root=NULL;
for(int j=i;j<=n;j++){
Insert(root,A[j]);
if((j-i+1)%2==0) op=1;
else op=0;
ans[i][j]=Kth(root,(j-i)/2+1,op,0);
}
}
for(int i=0;i<m;i++){
x=getint();y=getint();
fans+=ans[x][y];
}
cout<<fans<<endl;
}
return 0;
}