QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#548114#8237. Sugar Sweet IIpengpeng_fudan#WA 361ms46564kbC++235.6kb2024-09-05 15:33:472024-09-05 15:33:47

Judging History

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

  • [2024-11-04 16:59:03]
  • hack成功,自动添加数据
  • (/hack/1109)
  • [2024-09-05 15:33:47]
  • 评测
  • 测评结果:WA
  • 用时:361ms
  • 内存:46564kb
  • [2024-09-05 15:33:47]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

#define maxn 1000005
#define mod 1000000007

int64_t Mod(int64_t x) { return x >= mod ? x - mod : x; }

class Matrix {
   private:
    vector<vector<int64_t>> a;
    int n, m;

   public:
    void resize(int _n, int _m) { return a.resize(n = _n, vector<int64_t>(m = _m, 0));  }

    vector<int64_t>& operator[](size_t x) { return a[x]; }
    const int64_t get(size_t x, size_t y) const { return a[x][y]; }

    Matrix operator+(const Matrix& o) const {
        Matrix ans;
        ans.resize(n, m);
        for (int i = 0; i < n; i++)
            for (int j = 0; j < m; j++) ans[i][j] = Mod(this->get(i, j) + o.get(i, j));
        return ans;
    }
    Matrix operator*(const Matrix& o) const {
        Matrix ans;
        ans.resize(n, o.m);
        for (int i = 0; i < n; i++)
            for (int j = 0; j < o.n; j++)
                for (int k = 0; k < o.m; k++)
                    ans[i][k] = (ans[i][k] + this->get(i, j) * o.get(j, k)) % mod;
        return ans;
    }
    Matrix& operator+=(const Matrix& o) { return *this = *this + o; }
    Matrix& operator*=(const Matrix& o) { return *this = *this * o; }
};

class SegmentTree {
   private:
    vector<Matrix> tree, laz;
    int n;

    void build(int p, int l, int r, Matrix a[]) {
        laz[p].resize(1, 2);
        if (l == r) return tree[p] = a[l], void();
        int mid = (l + r) >> 1;
        build(p << 1, l, mid, a), build(p << 1 | 1, mid + 1, r, a);
        return tree[p] = tree[p << 1] * tree[p << 1 | 1], void();
    }
    void insert(int p, int l, int r, int ql, int qr, Matrix& v) {
        if (ql <= l && r <= qr) return laz[p] += v, v *= tree[p], void();
        int mid = (l + r) >> 1;
        if (ql <= mid) insert(p << 1, l, mid, ql, qr, v);
        if (qr > mid) insert(p << 1 | 1, mid + 1, r, ql, qr, v);
        return;
    }
    void solve(int p, int l, int r) {
        if (l == r) return val[l] = (laz[p] * tree[p])[0][0], void();
        int mid = (l + r) >> 1;
        laz[p << 1] += laz[p], laz[p << 1 | 1] += laz[p] * tree[p << 1];
        solve(p << 1, l, mid), solve(p << 1 | 1, mid + 1, r);
        return;
    }

   public:
    vector<int> val;

    void resize(int _n) { return n = _n, tree.resize((n + 5) << 2), laz.resize((n + 5) << 2), val.resize(n + 1); }
    void build(Matrix a[]) { return build(1, 1, n, a); }
    void insert(int l, int r, Matrix v) { return insert(1, 1, n, l, r, v); }
    void solve(void) { return solve(1, 1, n); }
};

int64_t qpow(int64_t a, int64_t x) {
    int64_t ans = 1;
    while (x) {
        if (x & 1) ans = ans * a % mod;
        a = a * a % mod, x >>= 1;
    }
    return ans;
}
int64_t inv(int64_t a) { return qpow(a, mod - 2); }

const int inv2 = inv(2);

int a[maxn], w[maxn], fa[maxn], indeg[maxn];
int64_t f[maxn];
Matrix mar[maxn];
vector<int> graph[maxn];

void solve(void) {
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> a[i], graph[i].clear(), f[i] = indeg[i] = 0;
    for (int i = 1; i <= n; i++) cin >> fa[i], graph[fa[i]].push_back(i), indeg[fa[i]]++;
    for (int i = 1; i <= n; i++) cin >> w[i];

    vector<int> lis;
    queue<int> que;
    for (int i = 1; i <= n; i++)
        if (!indeg[i]) que.push(i);
    while (!que.empty()) {
        int p = que.front();
        que.pop();
        lis.push_back(p);
        if (!--indeg[fa[p]]) que.push(fa[p]);
    }
    reverse(lis.begin(), lis.end());

    for (int i = 1; i <= n; i++) {
        if (!indeg[i]) continue;
        vector<int> ring;
        int p = i;
        ring.push_back(p), p = fa[p], indeg[p]--;
        while (p != i) ring.push_back(p), p = fa[p], indeg[p]--;
        reverse(ring.begin(), ring.end());

        if (ring.size() == 1) continue;

        int m = ring.size();
        for (int i = 0; i < m; i++) ring.push_back(ring[i]);
        m = ring.size();

        SegmentTree SGT;
        SGT.resize(m);
        for (int i = 0; i < m; i++) {
            int pre = (i + m - 1) % m;
            mar[i + 1].resize(2, 2);
            mar[i + 1][0][0] = mar[i + 1][0][1] = mar[i + 1][1][0] = mar[i + 1][1][1] = 0;
            if (a[ring[pre]] > a[ring[i]])
                mar[i + 1][1][0] = mar[i + 1][1][1] = 1;
            else if (a[ring[pre]] + w[ring[pre]] > a[ring[i]])
                mar[i + 1][0][0] = inv2, mar[i + 1][1][1] = 1;
            else
                mar[i + 1][1][1] = 1;
        }
        SGT.build(mar);
        int64_t val = inv(m >> 1);
        for (int i = 0; i < (m >> 1); i++) {
            int pre = (i + m - 1) % m, nxt = i + 1, val = a[ring[i]];
            Matrix base;
            base.resize(1, 2), base[0][1] = 1;
            if (a[ring[pre]] > a[ring[i]]) f[ring[i]] = Mod(f[ring[i]] + 1), val += w[ring[i]];
            if (val > a[ring[nxt]]) f[ring[nxt]] = Mod(f[ring[nxt]] + 1), base[0][0] = 1;
            if ((m >> 1) > 2) SGT.insert(i + 3, i + (m >> 1), base);
        }
        SGT.solve();
        for (int i = 0; i < m; i++) f[ring[i]] = (f[ring[i]] + SGT.val[i + 1]) % mod;
        for (int i = 0; i < (m >> 1); i++) f[ring[i]] = f[ring[i]] * val % mod;
    }

    int64_t inv2 = inv(2);
    for (auto p : lis)
        if (a[fa[p]] > a[p])
            f[p] = 1;
        else if (a[fa[p]] + w[fa[p]] > a[p])
            f[p] = inv2 * f[fa[p]] % mod;
        else
            f[p] = 0;

    for (int i = 1; i <= n; i++) cout << (a[i] + f[i] * w[i]) % mod << ' ';
    cout << endl;
    return;
}

int main() {
    ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);

    int _ = 1;
    cin >> _;
    while (_--) solve();

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 4ms
memory: 46564kb

input:

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

output:

500000007 5 5 6 
5 10 9 
166666673 5 6 
500000006 4 3 4 5 

result:

ok 15 numbers

Test #2:

score: -100
Wrong Answer
time: 361ms
memory: 44860kb

input:

50000
5
508432375 168140163 892620793 578579275 251380640
3 4 4 1 3
346232959 736203130 186940774 655629320 607743104
1
863886789
1
364158084
18
864679185 463975750 558804051 604216585 694033700 499417132 375390750 337590759 467353355 111206671 983760005 984444619 322277587 138763925 205122047 97736...

output:

854665334 904343293 590444253 906393935 859123744 
863886789 
871186919 814243920 968784984 206455474 17527050 449261413 196759729 901433117 519383814 907574792 983760005 984444619 489899014 435736558 113628626 977360756 482247153 963066959 
665922935 577926775 132646723 421298438 601054667 99438820...

result:

wrong answer 40th numbers differ - expected: '307105586', found: '534432987'