QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#584355#9252. Penguins in Refrigeratorucup-team3519WA 88ms41392kbC++145.9kb2024-09-23 13:03:472024-09-23 13:03:52

Judging History

你现在查看的是最新测评结果

  • [2024-09-23 13:03:52]
  • 评测
  • 测评结果:WA
  • 用时:88ms
  • 内存:41392kb
  • [2024-09-23 13:03:47]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define V vector
#define all0(x) (x).begin(),(x).end()
#define all1(x) (x).begin()+1,(x).end()
#define pb push_back
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define cin std::cin
#define cout std::cout
typedef long long LL;
typedef pair<int, int> pi;
typedef pair<LL, LL> pl;

const int MN = 1e6 + 20;//?
const int INF = 2e9 + 1e8;//INF
const LL INFLL = 8e18 + 1000;//INF long long 
mt19937 mrand(chrono::steady_clock().now().time_since_epoch().count());
const LL mod = 1e9 + 7;
LL MOD(LL x) {
    if(x < 0) return x + mod;
    if(x >= mod) return x - mod;
    return x;
}
//模板区域~~~~~~~
struct tag{
	LL add;
    bool empty(){return add == 0;}
}t0{0};
struct info{
	LL mn, mx, mnpos, mxpos, mnord, mxord;
    int siz;
}i0{INFLL, -INFLL};
tag operator + (const tag& l, const tag& r){
	return {l.add + r.add};
}
info operator + (const info& i, const tag& t){
	return {i.mn + t.add, i.mx + t.add, i.mnpos, i.mxpos, i.mnord, i.mxord, i.siz};
}
info operator + (const info& l, const info& r){
	return {min(l.mn, r.mn), max(l.mx, r.mx), l.mn <= r.mn ? l.mnpos : r.mnpos, l.mx >= r.mx ? l.mxpos : r.mxpos,
    l.mn <= r.mn ? l.mnord : r.mnord, l.mx >= r.mx ? l.mxord : r.mxord, l.siz + r.siz};
}
struct node{
	tag t;
	info i;	
}seg[MN * 4];
void pullup(int x){
	seg[x].i = seg[x * 2].i + seg[x * 2 + 1].i;
}
void pushdown(int x){
	if(seg[x].t.empty()) return;
	seg[x * 2].i  = seg[x * 2].i  + seg[x].t;
	seg[x * 2 + 1].i = seg[x * 2 + 1].i + seg[x].t;
	seg[x * 2].t = seg[x * 2].t + seg[x].t;
	seg[x * 2 + 1].t = seg[x * 2 + 1].t + seg[x].t;
	seg[x].t = t0;
}
void build(const V<info>& a){
    int n = a.size() - 1;
    auto dfs = [&](int x, int l, int r, auto dfs){
        seg[x].t = t0;
        if(l == r){
            seg[x].i = a[l];
            return;
        }
        int mid = (l + r) / 2;
        dfs(x * 2, l, mid, dfs), dfs(x * 2 + 1, mid + 1, r, dfs);
        pullup(x);
    };
    dfs(1, 1, n, dfs);
}
void modify(int x, int l, int r, int ql, int qr, info d){
    if(ql > qr) return;
	if(ql <= l && r <= qr) {
		seg[x].i = d;
		return;
	}
	int mid = (l + r) / 2;
	pushdown(x);
	if(ql <= mid) modify(x * 2, l, mid, ql, qr, d);
	if(qr >= mid+1) modify(x * 2 + 1, mid + 1, r, ql, qr, d);
	pullup(x); 
}
info query(int x, int l, int r, int ql, int qr){
    if(ql > qr) return i0;
	if(ql <= l && r <= qr) {
		return seg[x].i;
	}
	int mid = (l + r) / 2;
	pushdown(x);
	info ans = i0;
	if(ql <= mid) ans = ans + query(x * 2, l, mid, ql, qr);
	if(qr >= mid + 1) ans = ans + query(x * 2 + 1, mid + 1, r, ql, qr);
	return ans;
}

constexpr LL qpow(LL a, LL k) {
	LL ans = 1;
	while (k) {
		if (k & 1) ans = ans * a % mod;
		k >>= 1;
		a = a * a % mod;
	}
	return ans;
}

V<LL> _fac, _ifac;
void ini_comb(int n) {
    _fac.resize(n + 1), _ifac.resize(n + 1);
    _fac[0] = 1;
    for (int i = 1; i <= n; ++i) {
        _fac[i] = _fac[i - 1] * i % mod;
    }
    _ifac[n] = qpow(_fac[n], mod - 2);
    for (int i = n - 1; i >= 0; --i) {
        _ifac[i] = _ifac[i + 1] * (i + 1) % mod;
    }
}
LL fac(int x) {
    return _fac[x];
}
LL ifac(int x) {
    return _ifac[x];
}
LL C(int n, int m) {
    if(m == 0) return 1;
    if(m < 0 || m > n) return 0;
    return fac(n) * ifac(n - m) % mod * ifac(m) % mod;
}
//模板结束~~~~~~~

void solve() {
    int n, W; cin >> n >> W;
    ini_comb(n);
    V<int> a(n + 1);
    V<int> p(n + 1);
    for(int i = 1; i <= n; i++) cin >> p[i];
    for(int i = 1; i <= n; i++) cin >> a[i];

    V<int> b(n + 1);
    for(int i = 1; i <= n; i++) b[i] = a[p[i]];

    swap(a, b);
    reverse(all1(p));
    reverse(all1(a));

    V<info> ini_seg(n + 1);
    for(int i = 1; i <= n; i++) {
        ini_seg[i] = {a[i], a[i], i, i, p[i], p[i], 1};
    }
    build(ini_seg);


    V<V<pi>> chuang(n + 2); 
    V<V<int>> mie(n + 2);
    V<int> cnt(n + 1);
    V<int> val(n + 1);
    V<int> ord(1);
    V<int> ans(1);

    auto dfs = [&](int l, int r, int chuang_pos, int mie_pos, int &sz, auto dfs) -> LL {
        auto q = query(1, 1, n, l, r);
        if(q.siz == 0) {
            return 1;
        }
        int mid = q.mxpos;
        int mx = q.mx;
        while(query(1, 1, n, l, r).siz > 1 && query(1, 1, n, l, r).mn <= W - mx) {
            auto qq = query(1, 1, n, l, r);
            chuang[chuang_pos].pb({qq.mnord, mid});
            cnt[mid]++;
            modify(1, 1, n, qq.mnpos, qq.mnpos, i0);
        }
        mie[mie_pos].pb(mid);
        int cur_sz = 0;
        LL x = dfs(l, mid - 1, chuang_pos, mid, cur_sz, dfs);
        ord.pb(mid);
        LL y = dfs(mid + 1, r, mid, mie_pos, cur_sz, dfs);
        cur_sz += cnt[mid] + 1;
        sz += cur_sz;
        // cout << l << " " << r << " " << cur_sz << " " << mid << " " << cnt[mid] << endl;
        // cout << "ans : " << x * y % mod * C(cur_sz, cnt[mid]) % mod << endl;
        // cout << "chuang, mie : " << chuang_pos << " " << mie_pos << endl;
        return x * y % mod * C(cur_sz, cnt[mid]) % mod * fac(cnt[mid]) % mod;
    };
    int tmp = 0;
    cout << dfs(1, n, 0, n + 1, tmp, dfs) << endl;
    ord.pb(n + 1);

    multiset<pi> st;
    auto add = [&](pi x) -> void {
        st.insert(x);
    };
    auto del = [&]() -> void {
        ans.pb(st.begin()->fi);
        cnt[st.begin()->se]--;
        st.erase(st.begin());
    };
    for(auto x : ord) {
        for(auto y : mie[x]) {
            while(cnt[y]) del();
        }
        if(x >= 1 && x <= n) {
            while(st.size() && st.begin()->fi < p[x]) del();
            ans.pb(p[x]);
        }
        for(auto y : chuang[x]) {
            add(y);
        }
    }
    for(int i = 1; i <= n; i++) cout << ans[i] << " ";
    cout << endl;
}

int32_t main() {
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    int t=1;
    //cin >> t;
    while (t--) 
    solve();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3700kb

input:

5 10
1 2 3 4 5
6 5 3 9 2

output:

3
5 4 2 1 3 

result:

ok 2 lines

Test #2:

score: 0
Accepted
time: 0ms
memory: 3672kb

input:

5 10
1 2 3 4 5
2 4 3 3 8

output:

30
1 5 2 3 4 

result:

ok 2 lines

Test #3:

score: 0
Accepted
time: 0ms
memory: 3628kb

input:

5 10
1 2 3 4 5
2 3 4 5 1

output:

120
1 2 3 4 5 

result:

ok 2 lines

Test #4:

score: 0
Accepted
time: 0ms
memory: 3628kb

input:

5 10
1 2 3 4 5
2 3 4 5 6

output:

60
1 2 3 5 4 

result:

ok 2 lines

Test #5:

score: -100
Wrong Answer
time: 88ms
memory: 41392kb

input:

100000 96
1996 78922 45321 68844 32404 82013 66552 81163 17216 48170 35495 56660 13480 43118 23173 47257 50168 87069 26167 67231 31758 25694 61063 56642 8923 7727 54528 96554 38964 7604 6822 16256 45300 58869 31359 48638 87645 14779 81505 59585 89293 9291 7002 31810 84701 77648 78295 42595 11394 479...

output:

0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 10...

result:

wrong answer 1st lines differ - expected: '457992974', found: '0'