Submission #404757

# Submission time Handle Problem Language Result Execution time Memory
404757 2021-05-14T23:22:27 Z gevacrt Strange Device (APIO19_strange_device) C++17
25 / 100
5000 ms 282224 KB
#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

#define int ll

typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<vi> vvi;
typedef vector<vii> vvii;

#define INF INT_MAX
#define MOD 1000000007
#define all(x) x.begin(), x.end()

namespace DST {
    struct node {
        int sum{}, lazy{}, tot{};
        unique_ptr<node> l, r;
    };
    auto root = make_unique<node>();

    void upd(unique_ptr<node> &n, int l, int r){
        if(!n) return;
        if(n->sum) n->tot = r-l+1;
        else{
            n->tot = 0;
            if(n->l) n->tot += n->l->tot;
            if(n->r) n->tot += n->r->tot;
        }
        return;
    }

    void update(unique_ptr<node> &n, int l, int r, int L, int R, int val){
        if(r < L or R < l) return;

        if(L <= l and r <= R){
            n->sum += val;
            upd(n, l, r);
            return;
        }

        if(!n->l) n->l = make_unique<node>();
        if(!n->r) n->r = make_unique<node>();

        int m = (l+r)/2;
        update(n->l, l, m, L, R, val);
        update(n->r, m+1, r, L, R, val);
        upd(n, l, r);
    }

    void update(int L, int R, int val){
        update(root, 0, 1e18+10, L, R, val);
    }

    int query(){
        return root->tot;
    }
};

struct rect {
    int x1, y1, x2, y2;
};

int rect_solve(vector<rect> &B){
    ll ans = 0;

    vector<array<int, 4>> q; q.reserve(2*B.size());
    for(auto i : B){
        q.push_back({i.x2+1, -1, i.y1, i.y2+1});
        q.push_back({i.x1, 1, i.y1, i.y2+1});
    }

    sort(all(q)); int prev_x = 0;
    for(auto [x, offset, y1, y2] : q){
        ans += ll(x-prev_x)*DST::query();
        prev_x = x;
        DST::update(y1, y2-1, offset);
    }
    return ans;
}

int up(int a, int b){return (a+b-1)/b;}
int down(int a, int b){return a/b;}

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

    int Q, A, B; cin >> Q >> A >> B;
    vector<rect> RC; int A1 = A/__gcd(A, B+1);

    RC.reserve(6e6);
    while(Q--){
        int l, r; cin >> l >> r;
        int k1 = up(l, B), k2 = down(r, B);

        auto ins = [&](int x1, int x2, int y1, int y2){
            if(x2-x1+1 >= A1) RC.push_back({0, y1, A1-1, y2});
            else{
                int t1 = x1%A1, t2 = x2%A1;
                if(t1 <= t2) RC.push_back({t1, y1, t2, y2});
                else RC.push_back({t1, y1, A1-1, y2}), RC.push_back({0, y1, t2, y2});
            }
        };

        if(k1 <= k2){
            if(k1 < k2) ins(k1, k2-1, 0, B-1);
            if(l%B != 0) ins(k1-1, k1-1, l%B, B-1);
            ins(k2, k2, 0, r%B);
        }else{
            ins(k2, k2, l%B, r%B);
        }
    }

    cout << rect_solve(RC) << endl;

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 22 ms 1400 KB Output is correct
3 Correct 30 ms 1728 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 22 ms 1344 KB Output is correct
17 Correct 217 ms 10392 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 6 ms 588 KB Output is correct
3 Correct 4 ms 760 KB Output is correct
4 Correct 4 ms 460 KB Output is correct
5 Correct 3440 ms 188308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1967 ms 94208 KB Output is correct
3 Correct 3723 ms 188308 KB Output is correct
4 Correct 3505 ms 188228 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1967 ms 94208 KB Output is correct
3 Correct 3723 ms 188308 KB Output is correct
4 Correct 3505 ms 188228 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Execution timed out 5093 ms 282168 KB Time limit exceeded
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1967 ms 94208 KB Output is correct
3 Correct 3723 ms 188308 KB Output is correct
4 Correct 3505 ms 188228 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 364 ms 19040 KB Output is correct
7 Correct 560 ms 28612 KB Output is correct
8 Correct 531 ms 28432 KB Output is correct
9 Correct 535 ms 28484 KB Output is correct
10 Correct 337 ms 16576 KB Output is correct
11 Correct 541 ms 28508 KB Output is correct
12 Correct 573 ms 28484 KB Output is correct
13 Correct 567 ms 28580 KB Output is correct
14 Correct 341 ms 38084 KB Output is correct
15 Correct 540 ms 20124 KB Output is correct
16 Correct 405 ms 37292 KB Output is correct
17 Correct 790 ms 56628 KB Output is correct
18 Execution timed out 5115 ms 282224 KB Time limit exceeded
19 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 299 ms 20408 KB Output is correct
3 Correct 464 ms 55028 KB Output is correct
4 Execution timed out 5093 ms 237808 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 22 ms 1400 KB Output is correct
3 Correct 30 ms 1728 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 22 ms 1344 KB Output is correct
17 Correct 217 ms 10392 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 204 KB Output is correct
24 Correct 1 ms 204 KB Output is correct
25 Correct 6 ms 588 KB Output is correct
26 Correct 4 ms 760 KB Output is correct
27 Correct 4 ms 460 KB Output is correct
28 Correct 3440 ms 188308 KB Output is correct
29 Correct 1 ms 204 KB Output is correct
30 Correct 1967 ms 94208 KB Output is correct
31 Correct 3723 ms 188308 KB Output is correct
32 Correct 3505 ms 188228 KB Output is correct
33 Correct 1 ms 332 KB Output is correct
34 Execution timed out 5093 ms 282168 KB Time limit exceeded
35 Halted 0 ms 0 KB -