답안 #1066169

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1066169 2024-08-19T15:58:59 Z Boomyday Flooding Wall (BOI24_wall) C++17
70 / 100
5000 ms 184404 KB
//
// Created by adavy on 8/19/2024.
//

#include <bits/stdc++.h>


using namespace std;

using ll = long long;
const ll MOD = 1e9 + 7;



struct Modular {
    int value;
    static const int MOD_value = MOD;

    Modular(long long v = 0) { value = v % MOD; if (value < 0) value += MOD;}
    Modular(long long a, long long b) : value(0){ *this += a; *this /= b;}

    Modular& operator+=(Modular const& b) {value += b.value; if (value >= MOD) value -= MOD; return *this;}
    Modular& operator-=(Modular const& b) {value -= b.value; if (value < 0) value += MOD;return *this;}
    Modular& operator*=(Modular const& b) {value = (long long)value * b.value % MOD;return *this;}

    friend Modular mexp(Modular a, long long e);
    friend Modular inverse(Modular a) { return mexp(a, MOD - 2); }

    Modular& operator/=(Modular const& b) { return *this *= inverse(b); }
    friend Modular operator+(Modular a, Modular const b) { return a += b; }
    friend Modular operator-(Modular a, Modular const b) { return a -= b; }
    friend Modular operator-(Modular const a) { return 0 - a; }
    friend Modular operator*(Modular a, Modular const b) { return a *= b; }
    friend Modular operator/(Modular a, Modular const b) { return a /= b; }
    friend std::ostream& operator<<(std::ostream& os, Modular const& a) {return os << a.value;}
    friend bool operator==(Modular const& a, Modular const& b) {return a.value == b.value;}
    friend bool operator!=(Modular const& a, Modular const& b) {return a.value != b.value;}
};


Modular mexp(Modular a, long long e) {
    Modular res = 1; while (e) { if (e&1) res *= a; a *= a; e >>= 1; }
    return res;
}


struct segtree{
    vector<Modular> lzadd, lzmul, seg;
    int n, sz;
    segtree(int n){
        this->n = n;
        sz = 1;
        while (sz < n) sz *= 2;
        lzadd.assign(2*sz,0);
        lzmul.assign(2*sz,1);
        seg.assign(2*sz,0);
    }
    // first multiply, then add

    void pushdown(int rt, int tl, int tr){
        seg[rt]*= lzmul[rt];
        seg[rt]+= lzadd[rt];
        if (tl != tr){
            lzadd[2*rt] *= lzmul[rt];
            lzmul[2*rt] *= lzmul[rt];
            lzadd[2*rt] += lzadd[rt];
            lzadd[2*rt+1] *= lzmul[rt];
            lzmul[2*rt+1] *= lzmul[rt];
            lzadd[2*rt+1] += lzadd[rt];
        }
        lzadd[rt] = 0;
        lzmul[rt] = 1;
    }

    Modular sm(int l, int r, int rt, int tl, int tr){
        pushdown(rt,tl,tr);
        if (l > tr || r < tl) return 0;
        if (l <= tl && tr <= r) return seg[rt];
        int mid = (tl+tr)/2;
        return sm(l,r,2*rt,tl,mid)+sm(l,r,2*rt+1,mid+1,tr);
    }
    void mul(Modular x, int l, int r, int rt, int tl, int tr){
        pushdown(rt,tl,tr);
        if (l > tr || r < tl) return;
        if (l <= tl && tr <= r){
            lzadd[rt] *= x;
            lzmul[rt] *= x;
            pushdown(rt,tl,tr);
            return;
        }
        int mid = (tl+tr)/2;
        mul(x,l,r,2*rt,tl,mid);
        mul(x,l,r,2*rt+1,mid+1,tr);
        seg[rt] = seg[2*rt]+seg[2*rt+1];
    }
    void add(Modular x, int l, int r, int rt, int tl, int tr){
        pushdown(rt,tl,tr);
        if (l > tr || r < tl) return;
        if (l <= tl && tr <= r){
            lzadd[rt] += x;
            pushdown(rt,tl,tr);
            return;
        }
        int mid = (tl+tr)/2;
        add(x,l,r,2*rt,tl,mid);
        add(x,l,r,2*rt+1,mid+1,tr);
        seg[rt] = seg[2*rt]+seg[2*rt+1];
    }
};

int n,n2;
ll top = 2e9 + 3;
set<ll> coords = {0,top};
vector<ll> A,B;
vector<ll> coords_v; // small to big
map<ll,ll> coords_ind; // big to small


pair<Modular,Modular> calculate(){
    Modular leftsum = 0;

    segtree nums(n2), vals(n2);
    nums.add(1,0,0,1,0,nums.sz-1);
    for(int i=0;i<n;++i){
        Modular powtwo = mexp(2, n-i-1);
        ll mn = min(coords_ind[A[i]],coords_ind[B[i]]), mx = max(coords_ind[A[i]],coords_ind[B[i]]);

        Modular max_mn = nums.sm(0,mn,1,0,nums.sz-1);
        Modular max_mx = nums.sm(0,mx,1,0,nums.sz-1);
        leftsum += max_mn*coords_v[mn]*powtwo;
        leftsum += max_mx*coords_v[mx]*powtwo;
        leftsum += vals.sm(mx+1,n2-1,1,0,vals.sz-1)*2*powtwo;
        leftsum += vals.sm(mn+1,mx,1,0,vals.sz-1)*powtwo;

        //updates
        nums.mul(2,mx+1,n2-1,1,0,nums.sz-1);
        vals.mul(2,mx+1,n2-1,1,0,vals.sz-1);
        nums.mul(0,0,mn,1,0,nums.sz-1);
        vals.mul(0,0,mn,1,0,vals.sz-1);

        //cout << "maxmn: " << max_mn << " maxmx: " << max_mx << endl;

        // add the ones with max "mn"
        nums.add(max_mn,mn,mn,1,0,nums.sz-1);
        vals.add(max_mn*coords_v[mn],mn,mn,1,0,vals.sz-1);
        // add the ones with max "mx"
        nums.add(max_mx,mx,mx,1,0,nums.sz-1);
        vals.add(max_mx*coords_v[mx],mx,mx,1,0,vals.sz-1);
        //cout << leftsum << endl;
    }

    Modular max_blox = 0;
    for(int i=0;i<n2;++i){
        max_blox += nums.sm(i,i,1,0,nums.sz-1)*coords_v[i]*n;
    }

    return {leftsum,max_blox};

}
int main(){
    cin >> n;
    A.resize(n); B.resize(n);

    for(int i=0;i<n;++i){
        cin >> A[i]; coords.insert(A[i]);
    }
    for(int i=0;i<n;++i){
        cin >> B[i]; coords.insert(B[i]);
    }
    coords_v = vector<ll>(coords.begin(),coords.end());

    for(int i=0;i<coords_v.size();++i){
        coords_ind[coords_v[i]] = i;
    }
   n2 = coords.size();


    Modular buildsum = 0;
    for(int i=0;i<n;++i){
        Modular powtwo = mexp(2, n-1);
        buildsum += A[i]*powtwo + B[i]*powtwo;
    }
    auto [leftsum,max_blox] = calculate();

    max_blox -= buildsum;

    reverse(A.begin(),A.end());
    reverse(B.begin(),B.end());
    auto [rightsum, max_blox2] = calculate();








    cout << leftsum+rightsum-2*buildsum-max_blox << endl;



}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:172:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  172 |     for(int i=0;i<coords_v.size();++i){
      |                 ~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 428 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 102 ms 604 KB Output is correct
28 Correct 103 ms 604 KB Output is correct
29 Correct 18 ms 600 KB Output is correct
30 Correct 18 ms 604 KB Output is correct
31 Correct 32 ms 636 KB Output is correct
32 Correct 33 ms 604 KB Output is correct
33 Correct 32 ms 604 KB Output is correct
34 Correct 32 ms 604 KB Output is correct
35 Correct 33 ms 604 KB Output is correct
36 Correct 32 ms 600 KB Output is correct
37 Correct 17 ms 600 KB Output is correct
38 Correct 17 ms 604 KB Output is correct
39 Correct 32 ms 604 KB Output is correct
40 Correct 32 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 428 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 1 ms 348 KB Output is correct
38 Correct 1 ms 348 KB Output is correct
39 Correct 0 ms 348 KB Output is correct
40 Correct 0 ms 348 KB Output is correct
41 Correct 1 ms 348 KB Output is correct
42 Correct 1 ms 348 KB Output is correct
43 Correct 1 ms 348 KB Output is correct
44 Correct 1 ms 348 KB Output is correct
45 Correct 1 ms 348 KB Output is correct
46 Correct 1 ms 348 KB Output is correct
47 Correct 1 ms 348 KB Output is correct
48 Correct 1 ms 348 KB Output is correct
49 Correct 102 ms 604 KB Output is correct
50 Correct 103 ms 604 KB Output is correct
51 Correct 18 ms 600 KB Output is correct
52 Correct 18 ms 604 KB Output is correct
53 Correct 32 ms 636 KB Output is correct
54 Correct 33 ms 604 KB Output is correct
55 Correct 32 ms 604 KB Output is correct
56 Correct 32 ms 604 KB Output is correct
57 Correct 33 ms 604 KB Output is correct
58 Correct 32 ms 600 KB Output is correct
59 Correct 17 ms 600 KB Output is correct
60 Correct 17 ms 604 KB Output is correct
61 Correct 32 ms 604 KB Output is correct
62 Correct 32 ms 604 KB Output is correct
63 Correct 202 ms 4700 KB Output is correct
64 Correct 204 ms 4700 KB Output is correct
65 Correct 36 ms 604 KB Output is correct
66 Correct 35 ms 604 KB Output is correct
67 Correct 35 ms 604 KB Output is correct
68 Correct 32 ms 612 KB Output is correct
69 Correct 139 ms 1624 KB Output is correct
70 Correct 143 ms 1628 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 17 ms 624 KB Output is correct
11 Correct 17 ms 628 KB Output is correct
12 Correct 17 ms 604 KB Output is correct
13 Correct 17 ms 604 KB Output is correct
14 Correct 892 ms 10064 KB Output is correct
15 Correct 885 ms 10064 KB Output is correct
16 Correct 881 ms 10208 KB Output is correct
17 Correct 905 ms 10024 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 428 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 1 ms 348 KB Output is correct
38 Correct 1 ms 348 KB Output is correct
39 Correct 0 ms 348 KB Output is correct
40 Correct 0 ms 348 KB Output is correct
41 Correct 1 ms 348 KB Output is correct
42 Correct 1 ms 348 KB Output is correct
43 Correct 1 ms 348 KB Output is correct
44 Correct 1 ms 348 KB Output is correct
45 Correct 1 ms 348 KB Output is correct
46 Correct 1 ms 348 KB Output is correct
47 Correct 1 ms 348 KB Output is correct
48 Correct 1 ms 348 KB Output is correct
49 Correct 102 ms 604 KB Output is correct
50 Correct 103 ms 604 KB Output is correct
51 Correct 18 ms 600 KB Output is correct
52 Correct 18 ms 604 KB Output is correct
53 Correct 32 ms 636 KB Output is correct
54 Correct 33 ms 604 KB Output is correct
55 Correct 32 ms 604 KB Output is correct
56 Correct 32 ms 604 KB Output is correct
57 Correct 33 ms 604 KB Output is correct
58 Correct 32 ms 600 KB Output is correct
59 Correct 17 ms 600 KB Output is correct
60 Correct 17 ms 604 KB Output is correct
61 Correct 32 ms 604 KB Output is correct
62 Correct 32 ms 604 KB Output is correct
63 Correct 202 ms 4700 KB Output is correct
64 Correct 204 ms 4700 KB Output is correct
65 Correct 36 ms 604 KB Output is correct
66 Correct 35 ms 604 KB Output is correct
67 Correct 35 ms 604 KB Output is correct
68 Correct 32 ms 612 KB Output is correct
69 Correct 139 ms 1624 KB Output is correct
70 Correct 143 ms 1628 KB Output is correct
71 Correct 1 ms 348 KB Output is correct
72 Correct 0 ms 348 KB Output is correct
73 Correct 0 ms 348 KB Output is correct
74 Correct 0 ms 348 KB Output is correct
75 Correct 0 ms 348 KB Output is correct
76 Correct 0 ms 348 KB Output is correct
77 Correct 0 ms 348 KB Output is correct
78 Correct 0 ms 348 KB Output is correct
79 Correct 0 ms 348 KB Output is correct
80 Correct 17 ms 624 KB Output is correct
81 Correct 17 ms 628 KB Output is correct
82 Correct 17 ms 604 KB Output is correct
83 Correct 17 ms 604 KB Output is correct
84 Correct 892 ms 10064 KB Output is correct
85 Correct 885 ms 10064 KB Output is correct
86 Correct 881 ms 10208 KB Output is correct
87 Correct 905 ms 10024 KB Output is correct
88 Execution timed out 5053 ms 184404 KB Time limit exceeded
89 Halted 0 ms 0 KB -