Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define endl "\n"
- #define bug(x) cerr << #x << " = " << x << endl
- #define sz(x) (int)x.size()
- #define mp make_pair
- #define LL long long
- int n;
- void solve(){
- cin >> n;
- vector<int> a(n);
- for(int i = 0; i < n; i ++) cin >> a[i];
- vector<int> vec; vec.clear();
- int L = 0, R = n;
- for(int i = 0; i < n - 1; i ++){
- if(a[i] > a[i + 1]){
- L = max(L, a[i] - a[i + 1]);
- vec.push_back(i);
- }
- }
- if(vec.size() == 0) {
- cout << (1ll + n) * n / 2 << endl;
- return;
- }
- for(int i = 0; i < vec.size() - 1; i ++) if(vec[i] + 1 == vec[i + 1]){
- cout << 0 << endl;
- return;
- }
- for(int i = 0; i < vec.size() - 1; i ++){
- int st = vec[i] + 1, en = vec[i + 1];
- assert(st < en);
- int tmx = 0;
- while(st != en){
- tmx = max(a[st + 1] - a[st], tmx);
- st ++;
- }
- R = min(tmx, R);
- }
- if(R < L) {
- cout << 0 << endl;
- }
- else {
- cout << ((long long)L + R) * (R - L + 1) / 2 << endl;
- }
- }
- int main(){
- ios::sync_with_stdio(0);
- cin.tie(0), cout.tie(0);
- int Tc = 1;
- cin >> Tc;
- while(Tc --){
- solve();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment