Advertisement
Guest User

Untitled

a guest
Oct 15th, 2022
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.72 KB | Software | 0 0
  1. #pragma GCC optimize("O2")
  2. #include <bits/stdc++.h>
  3. #include <chrono>
  4.  
  5. using namespace std;
  6.  
  7. using ll = long long;
  8. using ull = unsigned long long;
  9. using ld = long double;
  10.  
  11. #define Endl "\n"
  12.  
  13. // Macros
  14. #define int long long
  15. // #define ll long long
  16. #define ld long double
  17. #define vt vector
  18. #define vi vector<int>
  19. #define vvi vector<vi>
  20. #define vii vector<pair<int,int>>
  21.  
  22. #define pii pair<int,int>
  23.  
  24. constexpr ll inf = 9e18;
  25.  
  26.  
  27. #define pb push_back
  28. #define em emplace_back
  29. #define all(X) (X).begin(), (X).end()
  30. #define allr(X) (X).rbegin(), (X).rend()
  31. #define sz(X) (int)X.size()
  32.  
  33. #define each(x, a) for (auto &x: a)
  34. #define forn(i, n) for (int i = 0; i < n; ++i)
  35. #define forr(i, n) for (int i = n; i >=0; --i)
  36. #define rep(i,a,b) for(int i = a; i <= b; i++)
  37.  
  38. // Debug macro
  39. string to_string(string s){return '"'+s+'"';}
  40. string to_string(const char* s){return to_string((string)s);}
  41. string to_string(const bool& b){return(b?"true":"false");}
  42. template<class T>string to_string(T x){ostringstream sout;sout<<x;return sout.str();}
  43. template<class A,class B>string to_string(pair<A,B> p){return "("+to_string(p.first)+", "+to_string(p.second)+")";}
  44. template<class A>string to_string(const vector<A> v){
  45.     int f=1;string res="{";for(const auto x:v){if(!f)res+= ", ";f=0;res+=to_string(x);}res+="}";
  46.     return res;
  47. }
  48. template<class A>string to_string(const set<A> v){
  49.     int f=1;string res="{";for(const auto x:v){if(!f)res+= ", ";f=0;res+=to_string(x);}res+="}";
  50.     return res;
  51. }
  52. template<class A, class B>string to_string(const map<A, B> v){
  53.     int f=1;string res="{";for(const auto x:v){if(!f)res+= ", ";f=0;res+=to_string(x);}res+="}";
  54.     return res;
  55. }
  56. template<class A>string to_string(const multiset<A> v){
  57.     int f=1;string res="{";for(const auto x:v){if(!f)res+= ", ";f=0;res+=to_string(x);}res+="}";
  58.     return res;
  59. }
  60. void debug_out(){puts("");}
  61. template<class T,class... U>void debug_out(const T& h,const U&... t){cerr<<" "<<to_string(h);debug_out(t...);}
  62. #ifndef ONLINE_JUDGE
  63. #define dbg(...) cerr<<"["<<#__VA_ARGS__<<"]:",debug_out(__VA_ARGS__),cerr<<"\n";
  64. #else
  65. #define dbg(...) 233;
  66. #endif
  67.  
  68. // Read
  69. template<typename T1, typename T2> // cin >> pair<T1, T2>
  70. istream& operator>>(istream &istream, pair<T1,T2> &p){ return (istream >> p.first >> p.second); }
  71. template<typename T> // cin >> vector<T>
  72. istream& operator>>(istream &istream, vector<T> &v){
  73.   for(auto &it: v) cin >> it;
  74.   return istream;
  75. }
  76. // Print
  77. template<typename T1, typename T2> // cout << pair<T1, T2>
  78. ostream& operator<<(ostream &ostream, const pair<T1, T2> &p){ return (ostream << p.first << " " << p.second); }
  79. template<typename T> //cout << vector<T>
  80. ostream& operator<<(ostream &ostream, const vector<T> &c){ for (auto &it: c) cout << it << " "; return ostream;}
  81.  
  82. // Utility functions
  83. template<typename T>
  84. void print(T &&t) {cout << t << Endl;}
  85. template <typename T, typename... Args>
  86. void print(T &&t, Args &&... args){
  87.   cout << t << " ";
  88.   print(forward<Args>(args)...);
  89. }
  90.  
  91. int n = -inf, m = -inf;
  92. void solve(){
  93.   int k,i, j, q, x, y;
  94.   k = i = j = q = x = y = n = m = -1;
  95.   cin >> n;
  96.   vi a(n+1);
  97.   rep(i,1,n) cin >> a[i];
  98.   vi pre(n+1);
  99.   rep(i,1,n) pre[i] = pre[i-1]+a[i];
  100.   vi pre2(n+1);
  101.   rep(i,1,n) pre2[i] = pre2[i-1]+pre[i];
  102.   dbg(pre);
  103.   dbg(pre2);
  104.   int ans = 0;
  105.   auto calc = [&] (int i, int j){
  106.     return pre2[j]-(pre2[i-1]+pre[i-1]*(j-i+1));
  107.   };
  108.   vi nsl(n+2);
  109.   stack<int> st;
  110.   // a.pb(-inf); // NOTICE
  111.   pre.pb(-inf);
  112.   st.push(n+1);
  113.   for(i = n; i >= 0; i--){
  114.     while(pre[st.top()] >= pre[i]){
  115.       st.pop();
  116.     }
  117.     nsl[i] = st.top();
  118.     st.push(i);
  119.   }
  120.   // int temp = calc(1,3);
  121.   // dbg(temp);
  122.   // dbg()
  123.   rep(i,1,n){
  124.     if(a[i] < 0) continue;
  125.     int till = nsl[i-1]-1;
  126.     ans += calc(i,till);
  127.     dbg(i,till);
  128.     // dbg(i,ans);
  129.   }
  130.   print(ans);
  131. }
  132.  
  133.  
  134. signed main(){
  135.   ios_base::sync_with_stdio(false);
  136.   cin.tie(NULL);
  137.  
  138. #ifdef LOCAL
  139.   freopen("input.txt", "r", stdin);
  140.   freopen("output.txt", "w", stdout);
  141.   auto start = std::chrono::system_clock::now();
  142. #endif
  143.  
  144.   int t = 1;
  145.   cin >> t;
  146.   rep(tt, 1,t){
  147.     // dbg(t);
  148.     cout << "Case #" << tt << ": ";
  149.     solve();
  150. #ifdef LOCAL
  151.     auto end = std::chrono::system_clock::now();
  152.     std::cerr << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() << "ms" << Endl;
  153. #endif
  154.   }
  155. #ifdef LOCAL
  156.   auto end = std::chrono::system_clock::now();
  157.   std::cerr << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() << "ms" << Endl;
  158.   cerr << "Completed" << Endl;
  159. #endif
  160. }
  161.  
  162. /* stuff you should look for
  163.   * constraints
  164.     * int overflow, array bounds
  165.     * special cases (n=1, n = 0?)
  166.     * do smth instead of nothing and stay organized
  167.     * WRITE STUFF DOWN
  168.     * DON'T GET STUCK ON ONE APPROACH
  169. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement