QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#600344#5000. Balanced Seesaw Array333zhanWA 246ms28908kbC++207.2kb2024-09-29 16:01:552024-09-29 16:01:56

Judging History

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

  • [2024-09-29 16:01:56]
  • 评测
  • 测评结果:WA
  • 用时:246ms
  • 内存:28908kb
  • [2024-09-29 16:01:55]
  • 提交

answer

#include <bits/stdc++.h>
#define int long long
#define lson (rt << 1)
#define rson (rt << 1 | 1)

using namespace std;
template <class Info, class Tag>
struct SegmentTree {
    vector <Info> tr;
    vector <Tag> tag;
    int n;
    
    SegmentTree () : n (0) {}
    
    SegmentTree (int N, vector <Info> a) : tr (N << 2 | 1), tag (N << 2 | 1) {
        n = N;
        auto build = [&] (auto self, int rt, int l, int r) -> void {
            if (l == r) {
                tr[rt] = a[l];
                return;
            }
            int mid = (l + r) >> 1;
            self (self, lson, l, mid); self (self, rson, mid + 1, r);
            pushup (rt);
		};
		build (build, 1, 1, n);
    }
    
    SegmentTree (int N) : tr (N << 2 | 1), tag (N << 2 | 1) {
        n = N;
    }
    
    void pushup (int rt) {
        tr[rt] = tr[lson] + tr[rson];
    }

    void apply (int rt, const Tag &x) {
        tr[rt].apply (x);
        tag[rt].apply (x);
    }

    void pushdown (int rt) {
        apply (lson, tag[rt]);
        apply (rson, tag[rt]);
        tag[rt] = Tag ();
    }
    
    void update (int rt, int l, int r, int x, const Info &y) {
        if (l == r) {
            tr[rt] = y;
            return;
        }
        int mid = (l + r) / 2;
        if (x <= mid) {
            update (lson, l, mid, x, y);
        } else {
            update (rson, mid + 1, r, x, y);
        }
        pushup (rt);
    }
    
    void modify (int rt, int l, int r, int x, int y, const Tag &t) {
        if (l > y || r < x) return;
        if (l >= x && r <= y) {
            apply (rt, t);
            return;
        }
        pushdown (rt);
        int mid = (l + r) >> 1;
        modify (lson, l, mid, x, y, t);
        modify (rson, mid + 1, r, x, y, t);
        pushup (rt);
    }
    
    Info query (int rt, int l, int r, int x, int y) {
        if (l >= x && r <= y) {
            return tr[rt];
        }
        pushdown (rt);
        int mid = (l + r) >> 1;
        if (x <= mid && y > mid) {
            return query (lson, l, mid, x, y) + query (rson, mid + 1, r, x, y);
        }
        if (x <= mid) return query (lson, l, mid, x, y);
        return query (rson, mid + 1, r, x, y);
    }
    
    template <class F>
    int findFirst (int rt, int l, int r, int x, int y, F &&pred) {
        if (l > y || r < x) {
            return -1;
        }
        if (l >= x && r <= y && ! pred (tr[rt])) {
            return -1;
        } 
        if (l == r) {
            return l;
        }
        pushdown (rt);
        int mid = (l + r) / 2;
        int ans = findFirst (lson, l, mid, x, y, pred);
        if (ans == -1) {
            ans = findFirst (rson, mid + 1, r, x, y, pred);
        }
        return ans;
    }

    template <class F>
    int findLast (int rt, int l, int r, int x, int y, F &&pred) {
        if (l > y || r < x) {
            return -1;
        }
        if (l >= x && r <= y && ! pred (tr[rt])) {
            return -1;
        } 
        if (l == r) {
            return l;
        }
        pushdown (rt);
        int mid = (l + r) / 2;
        int ans = findLast (rson, mid + 1, r, x, y, pred);
        if (ans == -1) {
            ans = findLast (lson, l, mid, x, y, pred);
        }
        return ans;
    }
};

constexpr int Z = 1E4 + 1;

struct Tag {
    int lazy1 = 0;
    int lazy2 = Z;

    void apply (const Tag &x) & {
        if (x.lazy2 != Z) {
            lazy1 = 0;
            lazy2 = x.lazy2;
        } else {
            lazy1 = x.lazy1;
        }
    }
};

struct Info {
    int l, r;
    int sum1 = 0;
    int sum2 = 0;
    
    void apply (const Tag &x) & {
        if (x.lazy2 != Z) {
            sum1 = (r - l + 1) * x.lazy2;
            int n = r - l + 1;
            int d = x.lazy2;
            int a1 = l * d;
            sum2 = n * a1 + n * (n - 1) / 2 * d;
        } else {
            sum1 += (r - l + 1) * x.lazy1;
            int n = r - l + 1;
            int d = x.lazy1;
            int a1 = l * d;
            sum2 += n * a1 + n * (n - 1) / 2 * d;
        }
    }
};

Info operator+(const Info &x, const Info &y) {
    Info c;
    c.l = x.l;
    c.r = y.r;
    c.sum1 = x.sum1 + y.sum1;
    c.sum2 = x.sum2 + y.sum2;
    return c;
}

void solve () {
    int n, q;
    cin >> n >> q;

    vector <int> a (n + 1);
    for (int i = 1; i <= n; i ++) {
        cin >> a[i];
    }

    vector <Info> info (n + 1);
    for (int i = 1; i <= n; i ++) {
        info[i] = {.l = i, .r = i, .sum1 = a[i], .sum2 = i * a[i]}; 
    }

    SegmentTree <Info, Tag> seg (n, info);

    for (int i = 1; i <= q; i ++) {
        int op, l, r;
        cin >> op >> l >> r;

        if (op == 1) {
            int x;
            cin >> x;

            seg.modify (1, 1, n, l, r, {x, Z});
        } else if (op == 2) {
            int x;
            cin >> x;

            seg.modify (1, 1, n, l, r, {0, x});
        } else {
            auto info = seg.query (1, 1, n, l, r);
            int sum1 = info.sum1;
            int sum2 = info.sum2;

            int t = sum2 - (l - 1) * sum1;
            int len = r - l + 1;

            if (sum1 == 0) {
                cout << (sum1 == 0 ? "Yes\n" : "No\n");
            } else if (sum1 > 0) {
                if (t < 0) {
                    cout << "No\n";
                } else {
                    auto check = [&] (int x) {
                        return x * sum1 >= t;
                    };
                    l = 1, r = len;
                    while (l < r) {
                        int mid = (l + r) / 2;
                        if (check (mid)) {
                            r = mid;
                        } else {
                            l = mid + 1;
                        }
                    }
                    // if (i == 4) {
                    //     cerr << l << " " << sum1 << " " << t << '\n';
                    // }
                    if (l * sum1 != t) {
                        cout << "No\n";
                    } else {
                        cout << "Yes\n";
                    }
                }
            } else {
                if (t > 0) {
                    cout << "No\n";
                } else {
                    auto check = [&] (int x) {
                        return x * sum1 <= t;
                    };
                    l = 1, r = len;
                    while (l < r) {
                        int mid = (l + r) / 2;
                        if (check (mid)) {
                            r = mid;
                        } else {
                            l = mid + 1;
                        }
                    }
                    if (l * sum1 != t) {
                        cout << "No\n";
                    } else {
                        cout << "Yes\n";
                    }
                }
            }
        }

        // if (i == 4) {
        //     cout << seg.query (1, 1, n, 1, 3).sum1 << " " << seg.query (1, 1, n, 1, 3).sum2 << '\n';
        // }
    }
}

signed main () {
    ios::sync_with_stdio (false);
    cin.tie (nullptr);

    int T = 1;
    // cin >> T;

    while (T --) {
        solve ();
    }

    return 0;
}

詳細信息

Test #1:

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

input:

3 6
1 2 3
3 1 1
3 1 3
1 1 1 2
3 1 3
2 2 2 0
3 2 3

output:

Yes
No
Yes
Yes

result:

ok 4 lines

Test #2:

score: -100
Wrong Answer
time: 246ms
memory: 28908kb

input:

100000 451163
-18 609 -793 393 375 313 -55 -892 -446 928 -207 -390 729 -383 27 318 -400 31 -661 202 -978 212 238 -368 351 -613 -23 400 809 1000 -431 -174 -103 886 73 -150 25 820 -689 972 777 794 -36 -231 -966 632 -418 -288 -476 725 -713 -379 896 -19 -883 338 -797 937 -557 -809 -241 -539 704 44 576 -...

output:

No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
Yes
No
No
No
No
No
No
No
No
No
No
Yes
No
No
No
No
No
No
No
Yes
No
No
No
No
No
Yes
No
No
No
No
No
No
No
No
No
No
Yes
No
No
No
No
No
No
No
No
No
No
Yes
No
Yes
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
Yes
No
N...

result:

wrong answer 2nd lines differ - expected: 'Yes', found: 'No'