Alex_tz307

SecvCost infopro X

Nov 14th, 2020
363
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.90 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define int long long
  3. #define INF 0x3f3f3f3f
  4.  
  5. using namespace std;
  6.  
  7. ifstream fin("secvcost.in");
  8. ofstream fout("secvcost.out");
  9.  
  10. int32_t main() {
  11.     fin.sync_with_stdio(false);
  12.     fout.sync_with_stdio(false);
  13.     fin.tie(nullptr);
  14.     fout.tie(nullptr);
  15.     int N, Q;
  16.     fin >> N >> Q;
  17.     vector < int > a(N + 2), lg(N + 2, 1);
  18.     a[0] = a[N + 1] = INF;
  19.     for(int i = 1; i <= N; ++i)
  20.         fin >> a[i];
  21.     for(int i = 1; i <= N; ++i) {
  22.         int st = i - 1, dr = i + 1;
  23.         while(a[st] < a[i])
  24.             --st;
  25.         while(a[dr] < a[i])
  26.             ++dr;
  27.         lg[i] += i - st - 1;
  28.         lg[i] += dr - i - 1;
  29.     }
  30.     a[0] = 0;
  31.     for(int i = 1; i <= N; ++i) {
  32.         a[i] *= lg[i];
  33.         a[i] += a[i - 1];
  34.     }
  35.     while(Q--) {
  36.         int st, dr;
  37.         fin >> st >> dr;
  38.         fout << a[dr] - a[st - 1] << '\n';
  39.     }
  40. }
  41.  
Advertisement
Add Comment
Please, Sign In to add comment