GTMD数组越界返回WA
AC代码:
#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <bitset>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <fstream>
#include <cstdlib>
#include <sstream>
#include <cstring>
#include <iostream>
#include <algorithm>
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
//#define maxd 1010
#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1
#define mc(x, y) memcpy(x, y, sizeof(x))
#define ms(x,y) memset(x,y,sizeof(x))
#define rep(i,n) for(int i=0;i<(n);i++)
#define repf(i,a,b) for(int i=(a);i<=(b);i++)
#define PI pair<int,int>
//#define mapp make_pair
#define FI first
#define SE second
#define IT iterator
#define PB push_back
#define Times 10
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int,int > pce;
//#define N 100
const double eps = 1e-10;
const double pi = acos(-1.0);
const ll mod = 1e9+7;
const int inf = 0x3f3f3f3f;
//const ll INF = (ll)1e18+300;
const int maxd = 202000 + 10;
int n, k;
int ac[maxd];
int max_[maxd][20];
int min_[maxd][20];
void RMQ() {
for (int i = 1; i <= n; i++) {
min_[i][0] = max_[i][0] = ac[i];
}
for (int j = 1; (1<<j) <= n; j++){
for (int i = 1; i <= n; i++) {
if(i + (1<<j) - 1<=n) {
max_[i][j] = max(max_[i][j-1], max_[i + (1<<(j-1))][j-1]);
min_[i][j] = min(min_[i][j-1], min_[i + (1<<(j-1))][j-1]);
}
}
}
}
int query(int l, int r) {
int k = (int)(log(r - l + 1) / log(2.0));
int max_ans = max(max_[l][k], max_[r - (1<<k) + 1][k]);
int min_ans = min(min_[l][k], min_[r - (1<<k) + 1][k]);
return max_ans - min_ans;
}
int main() {
int t;
cin >> t;
while(t --) {
cin >> n >> k;
for (int i = 1; i <= n; i++) {
scanf("%d", &ac[i]);
}
RMQ();
ll ans = 0;
for (int i = 1; i <= n; i++) {
int l = i;
int r = n;
int mid ;
while(r - 1 > l) {
mid = (r+l)>>1;
if(query(i, mid) < k) {
l = mid ;
}
else {
r = mid;
}
}
if (query(i, r) < k)
ans += (r * 1LL - i + 1);
else ans += (l * 1LL - i + 1);
}
cout << ans << endl;
}
}