初赛第六场C
题意:B的数据加强版
思路:RMQ
1 #include<bits/stdc++.h> 2 using namespace std; 3 4 int a[200005]; 5 int d[200005][20]; 6 struct node{ 7 int x,y; 8 }b[2005]; 9 bool cmp(node p,node q){ 10 return p.x<q.x; 11 } 12 int RMQ(int l,int r){ 13 int k=0; 14 while((1<<(k+1))<=r-l+1) 15 k++; 16 return min(d[l][k],d[r-(1<<k)+1][k]); 17 } 18 int main(){ 19 int n,kk; 20 scanf("%d%d",&n,&kk); 21 memset(d,127,sizeof(d)); 22 for(int i=0;i<n;i++){ 23 scanf("%d",&a[i]); 24 d[i][0]=a[i]; 25 } 26 for(int j=1;(1<<j)<=n;j++) 27 for(int i=0;i+(1<<j)-1<n;i++){ 28 d[i][j]=min(d[i][j-1],d[i+(1<<(j-1))][j-1]); 29 } 30 int m; 31 scanf("%d",&m); 32 for(int i=1;i<=m;i++){ 33 scanf("%d%d",&b[i].x,&b[i].y); 34 } 35 int xx,yy; 36 int sum; 37 int ssum=0; 38 sort(b+1,b+1+m,cmp); 39 for(int i=1;i<=m;i++){ 40 for(int j=i+1;j<=m;j++){ 41 sum=0; 42 yy=b[i].y; 43 yy=RMQ(b[i].x,b[j].x-1); 44 sum+=b[j].x-b[i].x; 45 sum+=b[i].y-yy; 46 sum+=abs(yy-b[j].y); 47 if(sum<=kk) ssum++; 48 } 49 } 50 51 cout<<ssum<<endl; 52 }