Submission #2245263
Source Code Expand
//2018年 3月 23日 金曜日 03:33:36 JST
#include <bits/stdc++.h>
#define ADD(a, b) a = (a + ll(b)) % mod
#define MUL(a, b) a = (a * ll(b)) % mod
#define MAX(a, b) a = max(a, b)
#define MIN(a, b) a = min(a, b)
#define rep(i, a, b) for(int i = int(a); i < int(b); i++)
#define rer(i, a, b) for(int i = int(a) - 1; i >= int(b); i--)
#define all(a) (a).begin(), (a).end()
#define sz(v) (int)(v).size()
#define pb push_back
#define sec second
#define fst first
#define debug(fmt, ...) Debug(__LINE__, ":", fmt, ##__VA_ARGS__)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<int, pi> ppi;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vl> mat;
typedef complex<double> comp;
void Debug() {cout << '\n'; }
template<class FIRST, class... REST>void Debug(FIRST arg, REST... rest){
cout<<arg<<" ";Debug(rest...);}
template<class T>ostream& operator<<(ostream& out,const vector<T>& v) {
out<<"[";if(!v.empty()){rep(i,0,sz(v)-1)out<<v[i]<<", ";out<<v.back();}out<<"]";return out;}
template<class S, class T>ostream& operator<<(ostream& out,const pair<S, T>& v){
out<<"("<<v.first<<", "<<v.second<<")";return out;}
const int MAX_N = 200010;
const int MAX_V = 100010;
const double eps = 1e-6;
const ll mod = 1000000007;
const int inf = 1 << 29;
const ll linf = 1LL << 60;
const double PI = 3.14159265358979323846;
///////////////////////////////////////////////////////////////////////////////////////////////////
int N;
set<pi> I; // (r, l)
int at[MAX_N];
int A[MAX_N];
void solve() {
cin >> N;
rep(i, 0, N) {
cin >> A[i];
at[A[i]] = i;
}
I.insert(pi(N - 1, 0));
ll res = 0;
rep(i, 1, N + 1) {
int a = at[i];
auto it = I.lower_bound(pi(a, -1));
pi p = (*it);
int l = p.sec, r = p.fst;
res += 1LL * i * (a - l + 1) * (r - a + 1);
I.erase(it);
if(l <= a - 1) I.insert(pi(a - 1, l));
if(a + 1 <= r) I.insert(pi(r, a + 1));
}
cout << res << "\n";
}
int main() {
#ifndef LOCAL
ios::sync_with_stdio(false);
cin.tie(0);
#endif
cout << fixed;
cout.precision(20);
srand((unsigned int)time(NULL));
#ifdef LOCAL
//freopen("in.txt", "wt", stdout); //for tester
freopen("in.txt", "rt", stdin);
#endif
solve();
#ifdef LOCAL
cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
return 0;
}
Submission Info
Submission Time |
|
Task |
B - Minimum Sum |
User |
omochana2 |
Language |
C++14 (GCC 5.4.1) |
Score |
400 |
Code Size |
2442 Byte |
Status |
AC |
Exec Time |
96 ms |
Memory |
4224 KB |
Judge Result
Set Name |
Sample |
All |
Score / Max Score |
0 / 0 |
400 / 400 |
Status |
|
|
Set Name |
Test Cases |
Sample |
example0, example1, example2 |
All |
corner0, corner1, corner2, corner3, example0, example1, example2, maxrand0, maxrand1, maxrand2, rand0, rand1, rand2 |
Case Name |
Status |
Exec Time |
Memory |
corner0 |
AC |
28 ms |
1792 KB |
corner1 |
AC |
28 ms |
1792 KB |
corner2 |
AC |
1 ms |
256 KB |
corner3 |
AC |
28 ms |
1792 KB |
example0 |
AC |
1 ms |
256 KB |
example1 |
AC |
1 ms |
256 KB |
example2 |
AC |
1 ms |
256 KB |
maxrand0 |
AC |
96 ms |
4224 KB |
maxrand1 |
AC |
96 ms |
4224 KB |
maxrand2 |
AC |
96 ms |
4224 KB |
rand0 |
AC |
1 ms |
256 KB |
rand1 |
AC |
1 ms |
256 KB |
rand2 |
AC |
1 ms |
256 KB |