답안 #1085949

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1085949 2024-09-09T07:03:31 Z jerzyk Flooding Wall (BOI24_wall) C++17
100 / 100
3127 ms 81036 KB
#include <bits/stdc++.h>

using namespace std;
#define pb push_back
#define st first
#define nd second
typedef long long ll;
typedef long double ld;
const ll I = 1000LL * 1000LL * 1000LL * 1000LL * 1000LL * 1000LL;
const ll M = 1000LL * 1000LL * 1000LL + 7LL;
const int N = 1<<20;
int tab[N][2], rev[N];
ll il[N][2];
bool zer[2 * N];
ll drz[2 * N], drzs[2 * N], laz[2 * N];
ll p2[N];

void Sk(int n)
{
    vector<pair<ll, int>> v;
    for(int i = 1; i <= n; ++i)
    {
        v.pb(make_pair(tab[i][0], i));
        v.pb(make_pair(tab[i][1], i));
    }
    sort(v.begin(), v.end());
    int l = 0;
    for(int i = 0; i < 2 * n; ++i)
    {
        if(i == 0 || v[i].st != v[i - 1].st)
            ++l;
        rev[l] = v[i].st;
        if(tab[v[i].nd][0] == v[i].st) tab[v[i].nd][0] = l;
        if(tab[v[i].nd][1] == v[i].st) tab[v[i].nd][1] = l;
    }
}

void P2(int n)
{
    p2[0] = 1LL;
    for(int i = 1; i <= n; ++i)
        p2[i] = (p2[i - 1] * 2LL) % M;
}

inline void Clean()
{
    for(int i = 1; i < 2 * N; ++i)
    {
        drz[i] = 0LL; drzs[i] = 0LL;
        laz[i] = 1LL;
        zer[i] = false;
    }
}

inline void Push(int v)
{
    if(zer[v])
    {
        zer[v * 2] = true;
        zer[v * 2 + 1] = true;
        return;
    }
    for(int s = v * 2; s <= v * 2 + 1; ++s)
    {
        drz[s] = (drz[s] * laz[v]) % M;
        drzs[s] = (drzs[s] * laz[v]) % M;
        laz[s] = (laz[s] * laz[v]) % M;
    }
    laz[v] = 1LL;
}

inline void Upd(int v)
{
    if(zer[v])
        return;
    drz[v] = (((drz[v * 2] + drz[v * 2 + 1]) % M) * laz[v]) % M;
    drzs[v] = (((drzs[v * 2] + drzs[v * 2 + 1]) % M) * laz[v]) % M;
}

void Zeruj(int v, int p, int k, int pz, int kz)
{
    if(pz > kz) return;
    if(p > kz || k < pz || zer[v]) return;
    if(p >= pz && k <= kz)
    {
        drz[v] = 0LL; drzs[v] = 0LL;
        laz[v] = 0LL;
        zer[v] = true;
        return;
    }
    Zeruj(v * 2, p, (p + k) / 2, pz, kz);
    Zeruj(v * 2 + 1, (p + k) / 2 + 1, k, pz, kz);
    Upd(v);
}

void Dod(int v, ll x)
{
    v += N;
    if(zer[v]) return;
    int sv = v;
    v /= 2;
    vector<int> xd;
    while(v > 0)
        {xd.pb(v); v /= 2;}
    for(int i = (int)xd.size() - 1; i >= 0; --i)
        Push(xd[i]);
    v = sv;
    if(zer[v]) return;
    drz[v] += x;
    drzs[v] += (x * (ll)rev[(v - N)]) % M;
    drz[v] %= M; drzs[v] %= M;
    v /= 2;
    while(v > 0)
        {Upd(v); v /= 2;}
}

void Mn2(int v, int p, int k, int pz, int kz)
{
    if(p > kz || k < pz || zer[v]) return;
    if(p >= pz && k <= kz)
    {
        drz[v] = (drz[v] * 2LL) % M;
        drzs[v] = (drzs[v] * 2LL) % M;
        laz[v] = (laz[v] * 2LL) % M;
        return;
    }
    Mn2(v * 2, p, (p + k) / 2, pz, kz);
    Mn2(v * 2 + 1, (p + k) / 2 + 1, k, pz, kz);
    Upd(v);
}

pair<ll, ll> Sum(int v, int p, int k, int pz, int kz)
{
    if(p > kz || k < pz || zer[v]) return make_pair(0LL, 0LL);
    if(p >= pz && k <= kz)
        return make_pair(drz[v], drzs[v]);
    pair<ll, ll> a, b;
    a = Sum(v * 2, p, (p + k) / 2, pz, kz);
    b = Sum(v * 2 + 1, (p + k) / 2 + 1, k, pz, kz);
    a = make_pair((a.st + b.st) * laz[v], (a.nd + b.nd) * laz[v]);
    //if(kz == 1)
        //cout << "Sum: " << v << " " << a.nd << "\n";
    a.st %= M; a.nd %= M;
    return a;
}

ll LR(int n)
{
    Clean();
    ll ans = 0LL;
    Dod(tab[1][0], 1LL);
    Dod(tab[1][1], 1LL);
    il[1][0] = 1LL;
    il[1][1] = 1LL;
    for(int i = 2; i <= n; ++i)
    {
        pair<ll, ll> s1, s2;
        ll a = rev[tab[i][0]], b = rev[tab[i][1]];

        s1 = Sum(1, 0, N - 1, 0, tab[i][0] - 1);
        s2 = Sum(1, 0, N - 1, 0, tab[i][1] - 1);

        //cout << "LR: " << s1.st << " " << s2.st << "\n";

        Zeruj(1, 0, N - 1, 0, tab[i][0] - 1);
        Mn2(1, 0, N - 1, tab[i][1], N - 1);

        ans += (((((a * s1.st - s1.nd) % M) * (ll)(i - 1)) % M) * p2[n - i]) % M;
        ans += (((((b * s2.st - s2.nd) % M) * (ll)(i - 1)) % M) * p2[n - i]) % M;
        ans %= M;

        il[i][0] = s1.st;
        il[i][1] = s2.st;

        Dod(tab[i][0], s1.st);
        Dod(tab[i][1], s2.st);
    }
    //cout << ans << "\n";
    ans %= M;
    ans += M;
    ans %= M;
    return ans;
}

ll RL(int n)
{
    ll ans = 0LL;
    Clean();
    Dod(tab[n][0], 1LL);
    Dod(tab[n][1], 1LL);
    for(int i = n - 1; i >= 1; --i)
    {
        pair<ll, ll> s1, s2;
        ll a = rev[tab[i][0]], b = rev[tab[i][1]];

        s1 = Sum(1, 0, N - 1, 0, tab[i][0] - 1);
        s2 = Sum(1, 0, N - 1, 0, tab[i][1] - 1);

        //cout << drz[N / 2] << " " << "LR: " << s1.nd << " " << s2.nd << "\n";

        il[i][0] *= Sum(1, 0, N - 1, 0, tab[i][0]).st;
        il[i][1] *= Sum(1, 0, N - 1, 0, tab[i][1]).st;
        il[i][0] %= M; il[i][1] %= M;


        Zeruj(1, 0, N - 1, 0, tab[i][0] - 1);
        Mn2(1, 0, N - 1, tab[i][1], N - 1);

        ans += (((((a * s1.st - s1.nd) % M) * (ll)(n - i)) % M) * p2[i - 1]) % M;
        ans += (((((b * s2.st - s2.nd) % M) * (ll)(n - i)) % M) * p2[i - 1]) % M;
        ans %= M;



        Dod(tab[i][0], s1.st);
        Dod(tab[i][1], s2.st);
    }

    //for(int i = 1; i <= n; ++i)
        //cout << "IL: " << il[i][0] << " " << il[i][1] << "\n";

    //cout << ans << "\n";

    ans %= M;
    ans += M;
    ans %= M;
    return ans;
}

void Solve()
{
    int n;
    ll ans = 0LL, s = 0LL;
    cin >> n;
    P2(n);
    for(int r = 0; r < 2; ++r)
        for(int i = 1; i <= n; ++i)
        {
            cin >> tab[i][r];
            s += (tab[i][r] * p2[n - 1]) % M;
        }
    s %= M;
    for(int i = 1; i <= n; ++i)
        if(tab[i][0] > tab[i][1]) swap(tab[i][0], tab[i][1]);
    Sk(n);
    //cout << "S: " << s << "\n";
    s += LR(n);
    s += RL(n);
    //cout << "S: " << s << "\n";
    s %= M;
    for(int i = 1; i <= n; ++i)
    {
        ans += il[i][0] * rev[tab[i][0]];
        ans += il[i][1] * rev[tab[i][1]];
        ans %= M;
    }
    //cout << "sp: " << ans << "\n";
    ans *= (ll)n;
    ans %= M;
    ans = (ans - s + M) % M;
    cout << ans << "\n";
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    //int t; cin >> t;
    //while(t--)
        Solve();

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 51544 KB Output is correct
2 Correct 24 ms 51700 KB Output is correct
3 Correct 25 ms 51656 KB Output is correct
4 Correct 24 ms 51544 KB Output is correct
5 Correct 24 ms 51636 KB Output is correct
6 Correct 25 ms 51548 KB Output is correct
7 Correct 24 ms 51548 KB Output is correct
8 Correct 24 ms 51544 KB Output is correct
9 Correct 28 ms 51836 KB Output is correct
10 Correct 24 ms 51540 KB Output is correct
11 Correct 24 ms 51548 KB Output is correct
12 Correct 25 ms 51540 KB Output is correct
13 Correct 24 ms 51660 KB Output is correct
14 Correct 26 ms 51548 KB Output is correct
15 Correct 24 ms 51548 KB Output is correct
16 Correct 24 ms 51752 KB Output is correct
17 Correct 24 ms 51548 KB Output is correct
18 Correct 24 ms 51536 KB Output is correct
19 Correct 29 ms 51540 KB Output is correct
20 Correct 24 ms 51588 KB Output is correct
21 Correct 24 ms 51756 KB Output is correct
22 Correct 24 ms 51544 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 51548 KB Output is correct
2 Correct 26 ms 51764 KB Output is correct
3 Correct 24 ms 51544 KB Output is correct
4 Correct 25 ms 51548 KB Output is correct
5 Correct 24 ms 51548 KB Output is correct
6 Correct 24 ms 51548 KB Output is correct
7 Correct 23 ms 51548 KB Output is correct
8 Correct 23 ms 51536 KB Output is correct
9 Correct 24 ms 51548 KB Output is correct
10 Correct 25 ms 51548 KB Output is correct
11 Correct 24 ms 51548 KB Output is correct
12 Correct 23 ms 51548 KB Output is correct
13 Correct 24 ms 51544 KB Output is correct
14 Correct 24 ms 51544 KB Output is correct
15 Correct 24 ms 51804 KB Output is correct
16 Correct 25 ms 51544 KB Output is correct
17 Correct 26 ms 51544 KB Output is correct
18 Correct 26 ms 51540 KB Output is correct
19 Correct 24 ms 51548 KB Output is correct
20 Correct 24 ms 51660 KB Output is correct
21 Correct 24 ms 51536 KB Output is correct
22 Correct 24 ms 51548 KB Output is correct
23 Correct 24 ms 51796 KB Output is correct
24 Correct 34 ms 51632 KB Output is correct
25 Correct 25 ms 51548 KB Output is correct
26 Correct 25 ms 51548 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 51548 KB Output is correct
2 Correct 26 ms 51764 KB Output is correct
3 Correct 24 ms 51544 KB Output is correct
4 Correct 25 ms 51548 KB Output is correct
5 Correct 24 ms 51548 KB Output is correct
6 Correct 24 ms 51548 KB Output is correct
7 Correct 23 ms 51548 KB Output is correct
8 Correct 23 ms 51536 KB Output is correct
9 Correct 24 ms 51548 KB Output is correct
10 Correct 25 ms 51548 KB Output is correct
11 Correct 24 ms 51548 KB Output is correct
12 Correct 23 ms 51548 KB Output is correct
13 Correct 24 ms 51544 KB Output is correct
14 Correct 24 ms 51544 KB Output is correct
15 Correct 24 ms 51804 KB Output is correct
16 Correct 25 ms 51544 KB Output is correct
17 Correct 26 ms 51544 KB Output is correct
18 Correct 26 ms 51540 KB Output is correct
19 Correct 24 ms 51548 KB Output is correct
20 Correct 24 ms 51660 KB Output is correct
21 Correct 24 ms 51536 KB Output is correct
22 Correct 24 ms 51548 KB Output is correct
23 Correct 24 ms 51796 KB Output is correct
24 Correct 34 ms 51632 KB Output is correct
25 Correct 25 ms 51548 KB Output is correct
26 Correct 25 ms 51548 KB Output is correct
27 Correct 41 ms 52128 KB Output is correct
28 Correct 41 ms 52128 KB Output is correct
29 Correct 84 ms 52240 KB Output is correct
30 Correct 85 ms 52108 KB Output is correct
31 Correct 57 ms 52128 KB Output is correct
32 Correct 57 ms 52164 KB Output is correct
33 Correct 55 ms 52244 KB Output is correct
34 Correct 57 ms 52120 KB Output is correct
35 Correct 57 ms 52244 KB Output is correct
36 Correct 55 ms 52164 KB Output is correct
37 Correct 89 ms 52380 KB Output is correct
38 Correct 83 ms 52148 KB Output is correct
39 Correct 53 ms 52256 KB Output is correct
40 Correct 56 ms 52204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 51544 KB Output is correct
2 Correct 24 ms 51700 KB Output is correct
3 Correct 25 ms 51656 KB Output is correct
4 Correct 24 ms 51544 KB Output is correct
5 Correct 24 ms 51636 KB Output is correct
6 Correct 25 ms 51548 KB Output is correct
7 Correct 24 ms 51548 KB Output is correct
8 Correct 24 ms 51544 KB Output is correct
9 Correct 28 ms 51836 KB Output is correct
10 Correct 24 ms 51540 KB Output is correct
11 Correct 24 ms 51548 KB Output is correct
12 Correct 25 ms 51540 KB Output is correct
13 Correct 24 ms 51660 KB Output is correct
14 Correct 26 ms 51548 KB Output is correct
15 Correct 24 ms 51548 KB Output is correct
16 Correct 24 ms 51752 KB Output is correct
17 Correct 24 ms 51548 KB Output is correct
18 Correct 24 ms 51536 KB Output is correct
19 Correct 29 ms 51540 KB Output is correct
20 Correct 24 ms 51588 KB Output is correct
21 Correct 24 ms 51756 KB Output is correct
22 Correct 24 ms 51544 KB Output is correct
23 Correct 25 ms 51548 KB Output is correct
24 Correct 26 ms 51764 KB Output is correct
25 Correct 24 ms 51544 KB Output is correct
26 Correct 25 ms 51548 KB Output is correct
27 Correct 24 ms 51548 KB Output is correct
28 Correct 24 ms 51548 KB Output is correct
29 Correct 23 ms 51548 KB Output is correct
30 Correct 23 ms 51536 KB Output is correct
31 Correct 24 ms 51548 KB Output is correct
32 Correct 25 ms 51548 KB Output is correct
33 Correct 24 ms 51548 KB Output is correct
34 Correct 23 ms 51548 KB Output is correct
35 Correct 24 ms 51544 KB Output is correct
36 Correct 24 ms 51544 KB Output is correct
37 Correct 24 ms 51804 KB Output is correct
38 Correct 25 ms 51544 KB Output is correct
39 Correct 26 ms 51544 KB Output is correct
40 Correct 26 ms 51540 KB Output is correct
41 Correct 24 ms 51548 KB Output is correct
42 Correct 24 ms 51660 KB Output is correct
43 Correct 24 ms 51536 KB Output is correct
44 Correct 24 ms 51548 KB Output is correct
45 Correct 24 ms 51796 KB Output is correct
46 Correct 34 ms 51632 KB Output is correct
47 Correct 25 ms 51548 KB Output is correct
48 Correct 25 ms 51548 KB Output is correct
49 Correct 41 ms 52128 KB Output is correct
50 Correct 41 ms 52128 KB Output is correct
51 Correct 84 ms 52240 KB Output is correct
52 Correct 85 ms 52108 KB Output is correct
53 Correct 57 ms 52128 KB Output is correct
54 Correct 57 ms 52164 KB Output is correct
55 Correct 55 ms 52244 KB Output is correct
56 Correct 57 ms 52120 KB Output is correct
57 Correct 57 ms 52244 KB Output is correct
58 Correct 55 ms 52164 KB Output is correct
59 Correct 89 ms 52380 KB Output is correct
60 Correct 83 ms 52148 KB Output is correct
61 Correct 53 ms 52256 KB Output is correct
62 Correct 56 ms 52204 KB Output is correct
63 Correct 43 ms 52380 KB Output is correct
64 Correct 40 ms 52376 KB Output is correct
65 Correct 56 ms 52380 KB Output is correct
66 Correct 55 ms 52176 KB Output is correct
67 Correct 56 ms 52364 KB Output is correct
68 Correct 60 ms 52248 KB Output is correct
69 Correct 70 ms 52116 KB Output is correct
70 Correct 65 ms 52300 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 51548 KB Output is correct
2 Correct 24 ms 51748 KB Output is correct
3 Correct 24 ms 51548 KB Output is correct
4 Correct 24 ms 51548 KB Output is correct
5 Correct 28 ms 51652 KB Output is correct
6 Correct 22 ms 51544 KB Output is correct
7 Correct 23 ms 51548 KB Output is correct
8 Correct 27 ms 51544 KB Output is correct
9 Correct 23 ms 51544 KB Output is correct
10 Correct 83 ms 52120 KB Output is correct
11 Correct 84 ms 52116 KB Output is correct
12 Correct 95 ms 52128 KB Output is correct
13 Correct 82 ms 52108 KB Output is correct
14 Correct 3127 ms 69504 KB Output is correct
15 Correct 2915 ms 69480 KB Output is correct
16 Correct 2889 ms 69472 KB Output is correct
17 Correct 2880 ms 69480 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 51544 KB Output is correct
2 Correct 24 ms 51700 KB Output is correct
3 Correct 25 ms 51656 KB Output is correct
4 Correct 24 ms 51544 KB Output is correct
5 Correct 24 ms 51636 KB Output is correct
6 Correct 25 ms 51548 KB Output is correct
7 Correct 24 ms 51548 KB Output is correct
8 Correct 24 ms 51544 KB Output is correct
9 Correct 28 ms 51836 KB Output is correct
10 Correct 24 ms 51540 KB Output is correct
11 Correct 24 ms 51548 KB Output is correct
12 Correct 25 ms 51540 KB Output is correct
13 Correct 24 ms 51660 KB Output is correct
14 Correct 26 ms 51548 KB Output is correct
15 Correct 24 ms 51548 KB Output is correct
16 Correct 24 ms 51752 KB Output is correct
17 Correct 24 ms 51548 KB Output is correct
18 Correct 24 ms 51536 KB Output is correct
19 Correct 29 ms 51540 KB Output is correct
20 Correct 24 ms 51588 KB Output is correct
21 Correct 24 ms 51756 KB Output is correct
22 Correct 24 ms 51544 KB Output is correct
23 Correct 25 ms 51548 KB Output is correct
24 Correct 26 ms 51764 KB Output is correct
25 Correct 24 ms 51544 KB Output is correct
26 Correct 25 ms 51548 KB Output is correct
27 Correct 24 ms 51548 KB Output is correct
28 Correct 24 ms 51548 KB Output is correct
29 Correct 23 ms 51548 KB Output is correct
30 Correct 23 ms 51536 KB Output is correct
31 Correct 24 ms 51548 KB Output is correct
32 Correct 25 ms 51548 KB Output is correct
33 Correct 24 ms 51548 KB Output is correct
34 Correct 23 ms 51548 KB Output is correct
35 Correct 24 ms 51544 KB Output is correct
36 Correct 24 ms 51544 KB Output is correct
37 Correct 24 ms 51804 KB Output is correct
38 Correct 25 ms 51544 KB Output is correct
39 Correct 26 ms 51544 KB Output is correct
40 Correct 26 ms 51540 KB Output is correct
41 Correct 24 ms 51548 KB Output is correct
42 Correct 24 ms 51660 KB Output is correct
43 Correct 24 ms 51536 KB Output is correct
44 Correct 24 ms 51548 KB Output is correct
45 Correct 24 ms 51796 KB Output is correct
46 Correct 34 ms 51632 KB Output is correct
47 Correct 25 ms 51548 KB Output is correct
48 Correct 25 ms 51548 KB Output is correct
49 Correct 41 ms 52128 KB Output is correct
50 Correct 41 ms 52128 KB Output is correct
51 Correct 84 ms 52240 KB Output is correct
52 Correct 85 ms 52108 KB Output is correct
53 Correct 57 ms 52128 KB Output is correct
54 Correct 57 ms 52164 KB Output is correct
55 Correct 55 ms 52244 KB Output is correct
56 Correct 57 ms 52120 KB Output is correct
57 Correct 57 ms 52244 KB Output is correct
58 Correct 55 ms 52164 KB Output is correct
59 Correct 89 ms 52380 KB Output is correct
60 Correct 83 ms 52148 KB Output is correct
61 Correct 53 ms 52256 KB Output is correct
62 Correct 56 ms 52204 KB Output is correct
63 Correct 43 ms 52380 KB Output is correct
64 Correct 40 ms 52376 KB Output is correct
65 Correct 56 ms 52380 KB Output is correct
66 Correct 55 ms 52176 KB Output is correct
67 Correct 56 ms 52364 KB Output is correct
68 Correct 60 ms 52248 KB Output is correct
69 Correct 70 ms 52116 KB Output is correct
70 Correct 65 ms 52300 KB Output is correct
71 Correct 25 ms 51548 KB Output is correct
72 Correct 24 ms 51748 KB Output is correct
73 Correct 24 ms 51548 KB Output is correct
74 Correct 24 ms 51548 KB Output is correct
75 Correct 28 ms 51652 KB Output is correct
76 Correct 22 ms 51544 KB Output is correct
77 Correct 23 ms 51548 KB Output is correct
78 Correct 27 ms 51544 KB Output is correct
79 Correct 23 ms 51544 KB Output is correct
80 Correct 83 ms 52120 KB Output is correct
81 Correct 84 ms 52116 KB Output is correct
82 Correct 95 ms 52128 KB Output is correct
83 Correct 82 ms 52108 KB Output is correct
84 Correct 3127 ms 69504 KB Output is correct
85 Correct 2915 ms 69480 KB Output is correct
86 Correct 2889 ms 69472 KB Output is correct
87 Correct 2880 ms 69480 KB Output is correct
88 Correct 511 ms 81036 KB Output is correct
89 Correct 562 ms 80892 KB Output is correct
90 Correct 1634 ms 69632 KB Output is correct
91 Correct 1618 ms 77296 KB Output is correct
92 Correct 1595 ms 69480 KB Output is correct
93 Correct 1523 ms 69480 KB Output is correct
94 Correct 1541 ms 77412 KB Output is correct
95 Correct 1485 ms 69344 KB Output is correct
96 Correct 1899 ms 74340 KB Output is correct
97 Correct 1874 ms 74340 KB Output is correct