提出 #4032958
ソースコード 拡げる
#include<iostream>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<unordered_map>
#include<unordered_set>
#include<set>
#include<algorithm>
#include<functional>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cassert>
#include<ctime>
using namespace std;
#define mind(a,b) (a>b?b:a)
#define maxd(a,b) (a>b?a:b)
#define absd(x) (x<0?-(x):x)
#define pow2(x) ((x)*(x))
#define rep(i,n) for(int i=0; i<n; ++i)
#define repr(i,n) for(int i=n-1; i>=0; --i)
#define repl(i,s,n) for(int i=s; i<=n; ++i)
#define replr(i,s,n) for(int i=n; i>=s; --i)
#define repf(i,s,n,j) for(int i=s; i<=n; i+=j)
#define repe(e,obj) for(auto e : obj)
#define SP << " " <<
#define COL << " : " <<
#define COM << ", " <<
#define ARR << " -> " <<
#define PNT(STR) cout << STR << endl
#define POS(X,Y) "(" << X << ", " << Y << ")"
#define DEB(A) " (" << #A << ") " << A
#define DEBREP(i,n,val) for(int i=0; i<n; ++i) cout << val << " "; cout << endl
#define ALL(V) (V).begin(), (V).end()
#define INF 1000000007
#define INFLL 1000000000000000007LL
#define EPS 1e-9
typedef unsigned int uint;
typedef unsigned long ulong;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
#define P_TYPE int
typedef pair<P_TYPE, P_TYPE> P;
typedef pair<P, P_TYPE> PI;
typedef pair<P_TYPE, P> IP;
typedef pair<P, P> PP;
typedef priority_queue<P, vector<P>, greater<P> > pvqueue;
#define MOD 1000000007
struct Node {
Node *left, *right;
ll v;
Node() : left(nullptr), right(nullptr), v(0) {}
};
// Dynamic Segment Tree
class SegmentTree {
Node *root;
int n, n0;
ll query(int a, int b, Node *n, int l, int r) {
if(a <= l && r <= b) {
return n->v;
}
if(r <= a || b <= l) {
return 0;
}
ll lv = n->left ? query(a, b, n->left, l, (l + r) >> 1) : 0;
ll rv = n->right ? query(a, b, n->right, (l + r) >> 1, r) : 0;
return (lv + rv) % MOD;
}
public:
SegmentTree(int n) : n(n) {
n0 = 1;
while(n0 < n) n0 <<= 1;
root = new Node();
}
~SegmentTree() {
delete root; root = nullptr;
}
void update(int k, ll x) {
Node *n = root;
int l = 0, r = n0;
n->v = (n->v + x) % MOD;
while(r-l > 1) {
int m = (l + r) >> 1;
if(k < m) {
if(!n->left) n->left = new Node();
n = n->left;
r = m;
} else {
if(!n->right) n->right = new Node();
n = n->right;
l = m;
}
n->v = (n->v + x) % MOD;
}
}
ll query(int a, int b) {
return query(a, b, root, 0, n0);
}
ll lquery(int b) {
return query(0, b, root, 0, n0);
}
ll rquery(int a) {
return query(a, n0, root, 0, n0);
}
};
ll fast_pow(ll x, int n) {
ll r = 1;
while(n) {
if(n & 1) {
r = r * x % MOD;
}
x = x * x % MOD;
n >>= 1;
}
return r;
}
#define N 100007
int n;
vector<int> a;
SegmentTree *seg[N];
unordered_set<int> ss[N];
ll x[N], y[N], p[N];
map<int, int> mp;
stack<int> st;
int cur = 0;
int main() {
cin >> n;
rep(i, n) {
int e; cin >> e;
if(e > 0) {
mp[e] = 1;
}
a.push_back(e);
}
int c = 0;
repe(&p, mp) {
mp[p.first] = c++;
}
rep(i, n) {
if(a[i] > 0) {
seg[cur] = new SegmentTree(n);
seg[cur]->update(mp[a[i]], 1);
ss[cur].insert(mp[a[i]]);
p[cur] = 1; x[cur] = y[cur] = 0;
st.push(cur++);
} else if(a[i] < 0) {
int j = st.top();
ll k = -a[i];
x[j] = ((k * x[j] % MOD) + ((k*(k-1)/2 % MOD) * y[j] % MOD)) % MOD;
y[j] = (k * k % MOD) * y[j] % MOD;
p[j] = k * p[j] % MOD;
} else {
int t = st.top(); st.pop();
int s = st.top(); st.pop();
ll xs = 0, ys = 0;
if(ss[s].size() < ss[t].size()) {
repe(k, ss[s]) {
ll v = seg[s]->query(k, k+1);
ll p = seg[t]->lquery(k);
ll q = seg[t]->rquery(k+1);
xs = (xs + (p*v % MOD)) % MOD;
ys = (ys + ((p + q)*v % MOD)) % MOD;
}
xs = (xs * p[s] % MOD) * p[t] % MOD;
ys = (ys * p[s] % MOD) * p[t] % MOD;
x[t] = (xs + x[s] + x[t]) % MOD;
y[t] = (ys + y[s] + y[t]) % MOD;
ll rev = fast_pow(p[t], MOD-2);
repe(k, ss[s]) {
ll v = seg[s]->query(k, k+1) * p[s] % MOD;
seg[t]->update(k, rev * v % MOD);
ss[t].insert(k);
}
st.push(t);
delete seg[s]; seg[s] = nullptr;
} else {
repe(k, ss[t]) {
ll v = seg[t]->query(k, k+1);
ll p = seg[s]->lquery(k);
ll q = seg[s]->rquery(k+1);
xs = (xs + (q*v % MOD)) % MOD;
ys = (ys + ((p + q)*v % MOD)) % MOD;
}
xs = (xs * p[s] % MOD) * p[t] % MOD;
ys = (ys * p[s] % MOD) * p[t] % MOD;
x[s] = (xs + x[s] + x[t]) % MOD;
y[s] = (ys + y[s] + y[t]) % MOD;
ll rev = fast_pow(p[s], MOD-2);
repe(k, ss[t]) {
ll v = seg[t]->query(k, k+1) * p[t] % MOD;
seg[s]->update(k, rev * v % MOD);
ss[s].insert(k);
}
st.push(s);
delete seg[t]; seg[t] = nullptr;
}
}
}
int k = st.top();
cout << x[k] << endl;
return 0;
}
提出情報
| 提出日時 |
|
| 問題 |
D - バブルソート |
| ユーザ |
yaketake08 |
| 言語 |
C++14 (GCC 5.4.1) |
| 得点 |
100 |
| コード長 |
5401 Byte |
| 結果 |
AC |
| 実行時間 |
650 ms |
| メモリ |
143732 KiB |
ジャッジ結果
| セット名 |
Sample |
Subtask1 |
All |
| 得点 / 配点 |
0 / 0 |
50 / 50 |
50 / 50 |
| 結果 |
|
|
|
| セット名 |
テストケース |
| Sample |
sample_01.txt, sample_02.txt |
| Subtask1 |
sample_01.txt, sample_02.txt, 1_01.txt, 1_02.txt, 1_03.txt, 1_04.txt, 1_05.txt, 1_06.txt, 1_07.txt, 1_08.txt, 1_09.txt, 1_10.txt, 1_11.txt, 1_12.txt, 1_13.txt, 1_14.txt, 1_15.txt, 1_16.txt, 1_17.txt, 1_18.txt, 1_19.txt, 1_20.txt, 1_21.txt, 1_22.txt, 1_23.txt, 1_24.txt, 1_25.txt, 1_26.txt, 1_27.txt, 1_28.txt, 1_29.txt, 1_30.txt |
| All |
sample_01.txt, sample_02.txt, 1_01.txt, 1_02.txt, 1_03.txt, 1_04.txt, 1_05.txt, 1_06.txt, 1_07.txt, 1_08.txt, 1_09.txt, 1_10.txt, 1_11.txt, 1_12.txt, 1_13.txt, 1_14.txt, 1_15.txt, 1_16.txt, 1_17.txt, 1_18.txt, 1_19.txt, 1_20.txt, 1_21.txt, 1_22.txt, 1_23.txt, 1_24.txt, 1_25.txt, 1_26.txt, 1_27.txt, 1_28.txt, 1_29.txt, 1_30.txt, 2_01.txt, 2_02.txt, 2_03.txt, 2_04.txt, 2_05.txt, 2_06.txt, 2_07.txt, 2_08.txt, 2_09.txt, 2_10.txt, 2_11.txt, 2_12.txt, 2_13.txt, 2_14.txt, 2_15.txt, 2_16.txt, 2_17.txt, 2_18.txt, 2_19.txt, 2_20.txt, 2_21.txt, 2_22.txt, 2_23.txt, 2_24.txt, 2_25.txt |
| ケース名 |
結果 |
実行時間 |
メモリ |
| 1_01.txt |
AC |
5 ms |
6400 KiB |
| 1_02.txt |
AC |
10 ms |
7808 KiB |
| 1_03.txt |
AC |
6 ms |
6784 KiB |
| 1_04.txt |
AC |
10 ms |
7808 KiB |
| 1_05.txt |
AC |
9 ms |
7552 KiB |
| 1_06.txt |
AC |
5 ms |
6400 KiB |
| 1_07.txt |
AC |
7 ms |
7424 KiB |
| 1_08.txt |
AC |
6 ms |
6656 KiB |
| 1_09.txt |
AC |
7 ms |
7040 KiB |
| 1_10.txt |
AC |
7 ms |
6912 KiB |
| 1_11.txt |
AC |
5 ms |
6400 KiB |
| 1_12.txt |
AC |
7 ms |
7424 KiB |
| 1_13.txt |
AC |
5 ms |
6656 KiB |
| 1_14.txt |
AC |
6 ms |
6912 KiB |
| 1_15.txt |
AC |
6 ms |
6912 KiB |
| 1_16.txt |
AC |
5 ms |
6400 KiB |
| 1_17.txt |
AC |
10 ms |
7808 KiB |
| 1_18.txt |
AC |
6 ms |
6784 KiB |
| 1_19.txt |
AC |
10 ms |
7808 KiB |
| 1_20.txt |
AC |
9 ms |
7552 KiB |
| 1_21.txt |
AC |
4 ms |
6400 KiB |
| 1_22.txt |
AC |
10 ms |
7808 KiB |
| 1_23.txt |
AC |
6 ms |
6784 KiB |
| 1_24.txt |
AC |
10 ms |
7808 KiB |
| 1_25.txt |
AC |
9 ms |
7552 KiB |
| 1_26.txt |
AC |
4 ms |
6400 KiB |
| 1_27.txt |
AC |
4 ms |
6400 KiB |
| 1_28.txt |
AC |
4 ms |
6400 KiB |
| 1_29.txt |
AC |
4 ms |
6400 KiB |
| 1_30.txt |
AC |
4 ms |
6400 KiB |
| 2_01.txt |
AC |
37 ms |
7036 KiB |
| 2_02.txt |
AC |
524 ms |
89716 KiB |
| 2_03.txt |
AC |
163 ms |
32888 KiB |
| 2_04.txt |
AC |
642 ms |
143476 KiB |
| 2_05.txt |
AC |
536 ms |
113908 KiB |
| 2_06.txt |
AC |
37 ms |
7036 KiB |
| 2_07.txt |
AC |
241 ms |
74360 KiB |
| 2_08.txt |
AC |
97 ms |
25596 KiB |
| 2_09.txt |
AC |
189 ms |
46328 KiB |
| 2_10.txt |
AC |
175 ms |
44408 KiB |
| 2_11.txt |
AC |
37 ms |
7036 KiB |
| 2_12.txt |
AC |
210 ms |
74356 KiB |
| 2_13.txt |
AC |
84 ms |
25208 KiB |
| 2_14.txt |
AC |
119 ms |
39412 KiB |
| 2_15.txt |
AC |
119 ms |
39412 KiB |
| 2_16.txt |
AC |
37 ms |
7036 KiB |
| 2_17.txt |
AC |
529 ms |
89716 KiB |
| 2_18.txt |
AC |
158 ms |
32120 KiB |
| 2_19.txt |
AC |
648 ms |
143476 KiB |
| 2_20.txt |
AC |
537 ms |
111988 KiB |
| 2_21.txt |
AC |
25 ms |
7036 KiB |
| 2_22.txt |
AC |
530 ms |
89716 KiB |
| 2_23.txt |
AC |
141 ms |
32248 KiB |
| 2_24.txt |
AC |
650 ms |
143732 KiB |
| 2_25.txt |
AC |
530 ms |
113524 KiB |
| sample_01.txt |
AC |
4 ms |
6400 KiB |
| sample_02.txt |
AC |
4 ms |
6400 KiB |