joylintp

Untitled

Apr 27th, 2021
830
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.84 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. //#pragma gcc optimize("Ofast,unroll-loops")
  5. //#pragma gcc target("sse,sse2,sse3,ssse3,sse4,avx,avx2,fma,abm,mmx,popcnt,tune=native")
  6. //#define int long long
  7. //#define double long double
  8. #define MP make_pair
  9. #define F first
  10. #define S second
  11. #define MOD 43200000000000
  12.  
  13. signed main()
  14. {
  15.     ios_base::sync_with_stdio(false);
  16.     cin.tie(0);
  17.  
  18.     int N, K;
  19.     cin >> N >> K;
  20.  
  21.     vector<int> c(K), d(K);
  22.     for (int i = 0; i < K; i++)
  23.         cin >> c[i] >> d[i];
  24.  
  25.     vector<long long> dp1(K);
  26.     for (int i = K - 1; i >= 0; i--)
  27.         if (i == K - 1 || c[i] > c[i + 1] || d[i] > d[i + 1])
  28.             dp1[i] = d[i];
  29.         else
  30.             dp1[i] = dp1[i + 1] + 1;
  31.  
  32.     vector<long long> dp2(K);
  33.     for (int i = K - 1; i >= 0; i--)
  34.         if (i == K - 1 || c[i] > c[i + 1])
  35.             dp2[i] = dp1[i] * (dp1[i] + 1) / 2;
  36.         else if (d[i] <= d[i + 1])
  37.             dp2[i] = (dp1[i + 1] + 1) * d[i];
  38.         else
  39.             dp2[i] = (dp1[i + 1] + 1) * d[i + 1] + (long long)(d[i] + d[i + 1] + 1) * (d[i] - d[i + 1]) / 2;
  40.  
  41.     long long ans = 0;
  42.     for (long long i : dp2)
  43.         ans += i;
  44.     cout << ans << '\n';
  45.  
  46.     return 0;
  47. }
  48.  
  49. //      *   *  *****  *   *  *   *   ****  ****    ****     **    **
  50. //     *   *  *      **  *  *   *  *      *   *  *        * *   * *
  51. //    *****  *****  * * *  *   *  *      ****   *       *  *     *
  52. //   *   *      *  *  **  *   *  *      *  *   *       *****    *
  53. //  *   *  *****  *   *   ***    ****  *   *    ****     *   *****
  54.  
  55. //      *****  *****  *   *  *      *****  *   *  *****  *****
  56. //        *   *   *  *   *  *        *    **  *    *    *   *
  57. //       *   *   *  *****  *        *    * * *    *    *****
  58. //   *  *   *   *    *    *        *    *  **    *    *
  59. //  ****   *****    *    *****  *****  *   *    *    *
  60.  
Advertisement
Add Comment
Please, Sign In to add comment