Submission #1022040

# Submission time Handle Problem Language Result Execution time Memory
1022040 2024-07-13T09:19:00 Z huutuan Flooding Wall (BOI24_wall) C++14
100 / 100
3851 ms 237212 KB
#include<bits/stdc++.h>

using namespace std;

#define int long long

const int mod=1e9+7, N=1e6+10;
int power2[N], invpower2[N];

struct Node{
   int len, l, r, lr, lazyl, lazyr;
   Node (){
      len=lazyl=lazyr=0;
      l=r=lr=0;
   }
   Node operator+(const Node &b){
      Node ans;
      ans.len=len+b.len;
      ans.l=(l+b.l)%mod;
      ans.r=(r+b.r)%mod;
      ans.lr=(lr+b.lr)%mod;
      return ans;
   }
   int get_l(){
      return (mod-(l-len+mod)%mod)%mod;
   }
   int get_r(){
      return (mod-(r-len+mod)%mod)%mod;
   }
   int get_lr(){
      return (lr-l-r+len+mod+mod)%mod;
   }
};

struct SegmentTree{
   int n;
   vector<Node> t;
   void init(int _n){
      n=_n;
      t.assign(4*n+1, Node());
   }
   void build(int k, int l, int r, const vector<int> &vals){
      if (l==r){
         t[k].len=vals[r]-vals[l-1];
         t[k].l=t[k].len;
         t[k].r=t[k].len;
         t[k].lr=t[k].len;
         return;
      }
      int mid=(l+r)>>1;
      build(k<<1, l, mid, vals);
      build(k<<1|1, mid+1, r, vals);
      t[k]=t[k<<1]+t[k<<1|1];
   }
   void apply(int k, int lazyl, int lazyr){
      if (lazyl){
         int x=lazyl>0?power2[lazyl]:invpower2[-lazyl];
         t[k].l=1ll*t[k].l*x%mod;
         t[k].lr=1ll*t[k].lr*x%mod;
         t[k].lazyl+=lazyl;
      }
      if (lazyr){
         int x=lazyr>0?power2[lazyr]:invpower2[-lazyr];
         t[k].r=1ll*t[k].r*x%mod;
         t[k].lr=1ll*t[k].lr*x%mod;
         t[k].lazyr+=lazyr;
      }
   }
   void push(int k){
      apply(k<<1, t[k].lazyl, t[k].lazyr);
      apply(k<<1|1, t[k].lazyl, t[k].lazyr);
      t[k].lazyl=t[k].lazyr=0;
   }
   void update(int k, int l, int r, int L, int R, int lazyl, int lazyr){
      if (r<L || R<l) return;
      if (L<=l && r<=R){
         apply(k, lazyl, lazyr);
         return;
      }
      push(k);
      int mid=(l+r)>>1;
      update(k<<1, l, mid, L, R, lazyl, lazyr);
      update(k<<1|1, mid+1, r, L, R, lazyl, lazyr);
      t[k]=t[k<<1]+t[k<<1|1];
   }
   Node get(int k, int l, int r, int L, int R){
      if (r<L || R<l) return t[0];
      if (L<=l && r<=R) return t[k];
      push(k);
      int mid=(l+r)>>1;
      return get(k<<1, l, mid, L, R)+get(k<<1|1, mid+1, r, L, R);
   }
} st;

int n, a[N], b[N];
int pf[N], sf[N];

int32_t main(){
   ios_base::sync_with_stdio(false);
   cin.tie(nullptr);
   power2[0]=1;
   for (int i=1; i<N; ++i) power2[i]=power2[i-1]*2%mod;
   invpower2[0]=1;
   invpower2[1]=(mod+1)/2;
   for (int i=2; i<N; ++i) invpower2[i]=invpower2[i-1]*invpower2[1]%mod;
   cin >> n;
   vector<int> vals{0};
   for (int i=1; i<=n; ++i) cin >> a[i], vals.push_back(a[i]);
   for (int i=1; i<=n; ++i) cin >> b[i], vals.push_back(b[i]);
   sort(vals.begin(), vals.end()); vals.resize(unique(vals.begin(), vals.end())-vals.begin());
   st.init((int)vals.size()-1);
   st.build(1, 1, st.n, vals);
   for (int i=1; i<=n; ++i){
      a[i]=lower_bound(vals.begin(), vals.end(), a[i])-vals.begin();
      b[i]=lower_bound(vals.begin(), vals.end(), b[i])-vals.begin();
      st.update(1, 1, st.n, min(a[i], b[i])+1, max(a[i], b[i]), 0, -1);
      pf[i]=max(pf[i-1], min(a[i], b[i]));
   }
   for (int i=n; i>=1; --i) sf[i]=max(sf[i+1], min(a[i], b[i]));
   int ans=0;
   for (int i=1; i<=n; ++i){
      st.update(1, 1, st.n, min(a[i], b[i])+1, max(a[i], b[i]), 0, 1);
      if (i!=1 && i!=n){
         int t1=pf[i-1], t2=sf[i+1];
         {
            int l=a[i], r=min(t1, t2);
            if (l<r){
               ans=(ans+(vals[r]-vals[l])*power2[n-1])%mod;
            }
         }
         {
            int l=max(a[i], t1), r=t2;
            if (l<r){
               ans=(ans+st.get(1, 1, st.n, l+1, r).get_l()*power2[n-1])%mod;
            }
         }
         {
            int l=max(a[i], t2), r=t1;
            if (l<r){
               ans=(ans+st.get(1, 1, st.n, l+1, r).get_r()*power2[n-1])%mod;
            }
         }
         {
            int l=max({a[i], t1, t2}), r=st.n;
            if (l<r){
               ans=(ans+st.get(1, 1, st.n, l+1, r).get_lr()*power2[n-1])%mod;
            }
         }
         {
            int l=b[i], r=min(t1, t2);
            if (l<r){
               ans=(ans+(vals[r]-vals[l])*power2[n-1])%mod;
            }
         }
         {
            int l=max(b[i], t1), r=t2;
            if (l<r){
               ans=(ans+st.get(1, 1, st.n, l+1, r).get_l()*power2[n-1])%mod;
            }
         }
         {
            int l=max(b[i], t2), r=t1;
            if (l<r){
               ans=(ans+st.get(1, 1, st.n, l+1, r).get_r()*power2[n-1])%mod;
            }
         }
         {
            int l=max({b[i], t1, t2}), r=st.n;
            if (l<r){
               ans=(ans+st.get(1, 1, st.n, l+1, r).get_lr()*power2[n-1])%mod;
            }
         }
      }
      st.update(1, 1, st.n, min(a[i], b[i])+1, max(a[i], b[i]), -1, 0);
   }
   cout << ans << '\n';
   return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 15 ms 15964 KB Output is correct
2 Correct 14 ms 16120 KB Output is correct
3 Correct 14 ms 15964 KB Output is correct
4 Correct 17 ms 16032 KB Output is correct
5 Correct 14 ms 15964 KB Output is correct
6 Correct 14 ms 16092 KB Output is correct
7 Correct 14 ms 16116 KB Output is correct
8 Correct 15 ms 15964 KB Output is correct
9 Correct 15 ms 15960 KB Output is correct
10 Correct 14 ms 15960 KB Output is correct
11 Correct 15 ms 15964 KB Output is correct
12 Correct 15 ms 15964 KB Output is correct
13 Correct 15 ms 15964 KB Output is correct
14 Correct 15 ms 15960 KB Output is correct
15 Correct 16 ms 15964 KB Output is correct
16 Correct 17 ms 15964 KB Output is correct
17 Correct 15 ms 15940 KB Output is correct
18 Correct 15 ms 16104 KB Output is correct
19 Correct 16 ms 15964 KB Output is correct
20 Correct 15 ms 15964 KB Output is correct
21 Correct 13 ms 16104 KB Output is correct
22 Correct 16 ms 15960 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 15960 KB Output is correct
2 Correct 16 ms 15964 KB Output is correct
3 Correct 15 ms 15964 KB Output is correct
4 Correct 15 ms 15964 KB Output is correct
5 Correct 14 ms 15948 KB Output is correct
6 Correct 15 ms 15944 KB Output is correct
7 Correct 16 ms 15960 KB Output is correct
8 Correct 15 ms 15964 KB Output is correct
9 Correct 14 ms 15964 KB Output is correct
10 Correct 14 ms 16084 KB Output is correct
11 Correct 14 ms 15916 KB Output is correct
12 Correct 14 ms 15960 KB Output is correct
13 Correct 14 ms 15964 KB Output is correct
14 Correct 14 ms 15964 KB Output is correct
15 Correct 15 ms 16024 KB Output is correct
16 Correct 14 ms 15964 KB Output is correct
17 Correct 15 ms 15960 KB Output is correct
18 Correct 15 ms 15960 KB Output is correct
19 Correct 15 ms 16328 KB Output is correct
20 Correct 14 ms 15960 KB Output is correct
21 Correct 14 ms 16136 KB Output is correct
22 Correct 16 ms 15960 KB Output is correct
23 Correct 15 ms 15904 KB Output is correct
24 Correct 15 ms 15964 KB Output is correct
25 Correct 16 ms 15964 KB Output is correct
26 Correct 16 ms 15964 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 15960 KB Output is correct
2 Correct 16 ms 15964 KB Output is correct
3 Correct 15 ms 15964 KB Output is correct
4 Correct 15 ms 15964 KB Output is correct
5 Correct 14 ms 15948 KB Output is correct
6 Correct 15 ms 15944 KB Output is correct
7 Correct 16 ms 15960 KB Output is correct
8 Correct 15 ms 15964 KB Output is correct
9 Correct 14 ms 15964 KB Output is correct
10 Correct 14 ms 16084 KB Output is correct
11 Correct 14 ms 15916 KB Output is correct
12 Correct 14 ms 15960 KB Output is correct
13 Correct 14 ms 15964 KB Output is correct
14 Correct 14 ms 15964 KB Output is correct
15 Correct 15 ms 16024 KB Output is correct
16 Correct 14 ms 15964 KB Output is correct
17 Correct 15 ms 15960 KB Output is correct
18 Correct 15 ms 15960 KB Output is correct
19 Correct 15 ms 16328 KB Output is correct
20 Correct 14 ms 15960 KB Output is correct
21 Correct 14 ms 16136 KB Output is correct
22 Correct 16 ms 15960 KB Output is correct
23 Correct 15 ms 15904 KB Output is correct
24 Correct 15 ms 15964 KB Output is correct
25 Correct 16 ms 15964 KB Output is correct
26 Correct 16 ms 15964 KB Output is correct
27 Correct 32 ms 16864 KB Output is correct
28 Correct 33 ms 16860 KB Output is correct
29 Correct 18 ms 16864 KB Output is correct
30 Correct 16 ms 16604 KB Output is correct
31 Correct 20 ms 16676 KB Output is correct
32 Correct 20 ms 16728 KB Output is correct
33 Correct 25 ms 16608 KB Output is correct
34 Correct 20 ms 16736 KB Output is correct
35 Correct 19 ms 16696 KB Output is correct
36 Correct 21 ms 16608 KB Output is correct
37 Correct 17 ms 16604 KB Output is correct
38 Correct 16 ms 16640 KB Output is correct
39 Correct 18 ms 16628 KB Output is correct
40 Correct 19 ms 16608 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 15964 KB Output is correct
2 Correct 14 ms 16120 KB Output is correct
3 Correct 14 ms 15964 KB Output is correct
4 Correct 17 ms 16032 KB Output is correct
5 Correct 14 ms 15964 KB Output is correct
6 Correct 14 ms 16092 KB Output is correct
7 Correct 14 ms 16116 KB Output is correct
8 Correct 15 ms 15964 KB Output is correct
9 Correct 15 ms 15960 KB Output is correct
10 Correct 14 ms 15960 KB Output is correct
11 Correct 15 ms 15964 KB Output is correct
12 Correct 15 ms 15964 KB Output is correct
13 Correct 15 ms 15964 KB Output is correct
14 Correct 15 ms 15960 KB Output is correct
15 Correct 16 ms 15964 KB Output is correct
16 Correct 17 ms 15964 KB Output is correct
17 Correct 15 ms 15940 KB Output is correct
18 Correct 15 ms 16104 KB Output is correct
19 Correct 16 ms 15964 KB Output is correct
20 Correct 15 ms 15964 KB Output is correct
21 Correct 13 ms 16104 KB Output is correct
22 Correct 16 ms 15960 KB Output is correct
23 Correct 15 ms 15960 KB Output is correct
24 Correct 16 ms 15964 KB Output is correct
25 Correct 15 ms 15964 KB Output is correct
26 Correct 15 ms 15964 KB Output is correct
27 Correct 14 ms 15948 KB Output is correct
28 Correct 15 ms 15944 KB Output is correct
29 Correct 16 ms 15960 KB Output is correct
30 Correct 15 ms 15964 KB Output is correct
31 Correct 14 ms 15964 KB Output is correct
32 Correct 14 ms 16084 KB Output is correct
33 Correct 14 ms 15916 KB Output is correct
34 Correct 14 ms 15960 KB Output is correct
35 Correct 14 ms 15964 KB Output is correct
36 Correct 14 ms 15964 KB Output is correct
37 Correct 15 ms 16024 KB Output is correct
38 Correct 14 ms 15964 KB Output is correct
39 Correct 15 ms 15960 KB Output is correct
40 Correct 15 ms 15960 KB Output is correct
41 Correct 15 ms 16328 KB Output is correct
42 Correct 14 ms 15960 KB Output is correct
43 Correct 14 ms 16136 KB Output is correct
44 Correct 16 ms 15960 KB Output is correct
45 Correct 15 ms 15904 KB Output is correct
46 Correct 15 ms 15964 KB Output is correct
47 Correct 16 ms 15964 KB Output is correct
48 Correct 16 ms 15964 KB Output is correct
49 Correct 32 ms 16864 KB Output is correct
50 Correct 33 ms 16860 KB Output is correct
51 Correct 18 ms 16864 KB Output is correct
52 Correct 16 ms 16604 KB Output is correct
53 Correct 20 ms 16676 KB Output is correct
54 Correct 20 ms 16728 KB Output is correct
55 Correct 25 ms 16608 KB Output is correct
56 Correct 20 ms 16736 KB Output is correct
57 Correct 19 ms 16696 KB Output is correct
58 Correct 21 ms 16608 KB Output is correct
59 Correct 17 ms 16604 KB Output is correct
60 Correct 16 ms 16640 KB Output is correct
61 Correct 18 ms 16628 KB Output is correct
62 Correct 19 ms 16608 KB Output is correct
63 Correct 49 ms 20696 KB Output is correct
64 Correct 47 ms 20692 KB Output is correct
65 Correct 20 ms 16864 KB Output is correct
66 Correct 21 ms 16608 KB Output is correct
67 Correct 20 ms 16812 KB Output is correct
68 Correct 24 ms 16620 KB Output is correct
69 Correct 31 ms 17800 KB Output is correct
70 Correct 31 ms 17632 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 15964 KB Output is correct
2 Correct 15 ms 15960 KB Output is correct
3 Correct 15 ms 15960 KB Output is correct
4 Correct 15 ms 15996 KB Output is correct
5 Correct 14 ms 15964 KB Output is correct
6 Correct 15 ms 15912 KB Output is correct
7 Correct 15 ms 15964 KB Output is correct
8 Correct 16 ms 16104 KB Output is correct
9 Correct 16 ms 16336 KB Output is correct
10 Correct 17 ms 16600 KB Output is correct
11 Correct 16 ms 16608 KB Output is correct
12 Correct 17 ms 16680 KB Output is correct
13 Correct 17 ms 16608 KB Output is correct
14 Correct 127 ms 41628 KB Output is correct
15 Correct 126 ms 41640 KB Output is correct
16 Correct 131 ms 41652 KB Output is correct
17 Correct 121 ms 41648 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 15964 KB Output is correct
2 Correct 14 ms 16120 KB Output is correct
3 Correct 14 ms 15964 KB Output is correct
4 Correct 17 ms 16032 KB Output is correct
5 Correct 14 ms 15964 KB Output is correct
6 Correct 14 ms 16092 KB Output is correct
7 Correct 14 ms 16116 KB Output is correct
8 Correct 15 ms 15964 KB Output is correct
9 Correct 15 ms 15960 KB Output is correct
10 Correct 14 ms 15960 KB Output is correct
11 Correct 15 ms 15964 KB Output is correct
12 Correct 15 ms 15964 KB Output is correct
13 Correct 15 ms 15964 KB Output is correct
14 Correct 15 ms 15960 KB Output is correct
15 Correct 16 ms 15964 KB Output is correct
16 Correct 17 ms 15964 KB Output is correct
17 Correct 15 ms 15940 KB Output is correct
18 Correct 15 ms 16104 KB Output is correct
19 Correct 16 ms 15964 KB Output is correct
20 Correct 15 ms 15964 KB Output is correct
21 Correct 13 ms 16104 KB Output is correct
22 Correct 16 ms 15960 KB Output is correct
23 Correct 15 ms 15960 KB Output is correct
24 Correct 16 ms 15964 KB Output is correct
25 Correct 15 ms 15964 KB Output is correct
26 Correct 15 ms 15964 KB Output is correct
27 Correct 14 ms 15948 KB Output is correct
28 Correct 15 ms 15944 KB Output is correct
29 Correct 16 ms 15960 KB Output is correct
30 Correct 15 ms 15964 KB Output is correct
31 Correct 14 ms 15964 KB Output is correct
32 Correct 14 ms 16084 KB Output is correct
33 Correct 14 ms 15916 KB Output is correct
34 Correct 14 ms 15960 KB Output is correct
35 Correct 14 ms 15964 KB Output is correct
36 Correct 14 ms 15964 KB Output is correct
37 Correct 15 ms 16024 KB Output is correct
38 Correct 14 ms 15964 KB Output is correct
39 Correct 15 ms 15960 KB Output is correct
40 Correct 15 ms 15960 KB Output is correct
41 Correct 15 ms 16328 KB Output is correct
42 Correct 14 ms 15960 KB Output is correct
43 Correct 14 ms 16136 KB Output is correct
44 Correct 16 ms 15960 KB Output is correct
45 Correct 15 ms 15904 KB Output is correct
46 Correct 15 ms 15964 KB Output is correct
47 Correct 16 ms 15964 KB Output is correct
48 Correct 16 ms 15964 KB Output is correct
49 Correct 32 ms 16864 KB Output is correct
50 Correct 33 ms 16860 KB Output is correct
51 Correct 18 ms 16864 KB Output is correct
52 Correct 16 ms 16604 KB Output is correct
53 Correct 20 ms 16676 KB Output is correct
54 Correct 20 ms 16728 KB Output is correct
55 Correct 25 ms 16608 KB Output is correct
56 Correct 20 ms 16736 KB Output is correct
57 Correct 19 ms 16696 KB Output is correct
58 Correct 21 ms 16608 KB Output is correct
59 Correct 17 ms 16604 KB Output is correct
60 Correct 16 ms 16640 KB Output is correct
61 Correct 18 ms 16628 KB Output is correct
62 Correct 19 ms 16608 KB Output is correct
63 Correct 49 ms 20696 KB Output is correct
64 Correct 47 ms 20692 KB Output is correct
65 Correct 20 ms 16864 KB Output is correct
66 Correct 21 ms 16608 KB Output is correct
67 Correct 20 ms 16812 KB Output is correct
68 Correct 24 ms 16620 KB Output is correct
69 Correct 31 ms 17800 KB Output is correct
70 Correct 31 ms 17632 KB Output is correct
71 Correct 16 ms 15964 KB Output is correct
72 Correct 15 ms 15960 KB Output is correct
73 Correct 15 ms 15960 KB Output is correct
74 Correct 15 ms 15996 KB Output is correct
75 Correct 14 ms 15964 KB Output is correct
76 Correct 15 ms 15912 KB Output is correct
77 Correct 15 ms 15964 KB Output is correct
78 Correct 16 ms 16104 KB Output is correct
79 Correct 16 ms 16336 KB Output is correct
80 Correct 17 ms 16600 KB Output is correct
81 Correct 16 ms 16608 KB Output is correct
82 Correct 17 ms 16680 KB Output is correct
83 Correct 17 ms 16608 KB Output is correct
84 Correct 127 ms 41628 KB Output is correct
85 Correct 126 ms 41640 KB Output is correct
86 Correct 131 ms 41652 KB Output is correct
87 Correct 121 ms 41648 KB Output is correct
88 Correct 3548 ms 237192 KB Output is correct
89 Correct 3851 ms 237212 KB Output is correct
90 Correct 278 ms 41656 KB Output is correct
91 Correct 293 ms 49584 KB Output is correct
92 Correct 325 ms 41648 KB Output is correct
93 Correct 254 ms 41676 KB Output is correct
94 Correct 330 ms 49820 KB Output is correct
95 Correct 386 ms 41652 KB Output is correct
96 Correct 1281 ms 92424 KB Output is correct
97 Correct 1267 ms 92596 KB Output is correct