1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
| #include<bits/stdc++.h> using namespace std; #ifdef LOCAL #define debug(...) fprintf(stdout, ##__VA_ARGS__) #else #define debug(...) void(0) #endif #define int long long inline int read(){int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+ (x<<3)+(ch^48);ch=getchar();}return x*f;} #define Z(x) (x)*(x) #define pb push_back #define fi first #define se second
#define E9 1000000000 int n, m, i, j, k, T; int ans, x, l1, l2, lx, h1, h2, hx, nh1, nh2, nl1, nl2; int S, L; stack<pair<int, int> >z;
int count(int a, int b, int c) { debug("count[%lld %lld] * %lld\n", a, b, c); if(a < 0 || b < 0 || c < 0) return 0; return (a * (a - 1) / 2 + a * c) * b; }
signed main() { #ifdef LOCAL freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout); #endif
T = read(); while(T--) { n = read(); ans = 0; while(!z.empty()) z.pop(); for(i = 1; i <= n; ++i) { x = read() - i + E9;
if(z.empty() || x > z.top().fi) z.push({x, 1}); else if(x == z.top().fi) { auto t = z.top(); z.pop(); z.push({t.fi, t.se + 1}); } else { debug("For i = %lld : \n", i); h1 = x; l1 = 1; h2 = E9; l2 = 0; while(!z.empty() && h1 <= z.top().fi) { auto t = z.top(); z.pop(); hx = t.fi; lx = t.se; debug("[%lld %lld] [%lld %lld][%lld %lld] ==> ", hx - E9, lx, h1 - E9, l1, h2 - E9, l2); S = hx * lx + h1 * l1 + h2 * l2; L = lx + l1 + l2; nh1 = S / L; nh2 = nh1 + 1; nl2 = S % L; nl1 = L - nl2; debug("[%lld %lld][%lld %lld]\n", nh1 - E9, nl1, nh2 - E9, nl2); if(nl1 <= lx) { ans += count(nl1, hx - nh1, lx - nl1 + 1); ans += count(lx - nl1, hx - nh2, 1); ans += count(l1, nh2 - h1, 0); if(l2) ans += count(l2, nh2 - h2, l1); } else if(nl1 <= lx + l1) { ans += count(lx, hx - nh1, 1); ans += count(nl1 - lx, nh1 - h1, 0); ans += count(lx + l1 - nl1, nh2 - h1, nl1 - lx); if(l2) ans += count(l2, nh2 - h2, l1); } else { ans += count(lx, hx - nh1, 1); ans += count(l1, nh1 - h1, 0); ans += count(nl1 - l1 - lx, nh1 - h2, l1); if(l2) ans += count(nl2, nh2 - h2, nl1 - lx); } debug(" now ans is %lld\n", ans); l1 = nl1; h1 = nh1; l2 = nl2; h2 = nh2; } z.push({h1, l1}); if(l2) z.push({h2, l2}); } debug("now ans is %lld\n", ans); }
printf("%lld\n", ans); }
return 0; }
|