QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#484869 | #8712. Flooding Wall | fractal | 12 | 2348ms | 337688kb | C++20 | 6.9kb | 2024-07-20 04:17:57 | 2024-07-20 04:17:57 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define make_unique(x) sort(all(x)), x.erase(unique(all(x)), x.end())
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
mt19937_64 Rng(chrono::steady_clock::now().time_since_epoch().count());
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const int N = 1e6 + 200;
const int M = 1e9;
const int inf = 2e9 + 3;
const ll INF = 1e18;
template<int mod>
class Modular {
public:
int val;
Modular() : val(0) {}
Modular(int new_val) : val(new_val) {}
// Modular(long long new_val) : val(new_val % mod) {} // AFFECTS OPERATOR* (because it has one more %)
friend Modular operator+(const Modular& a, const Modular& b) {
if (a.val + b.val >= mod) return a.val + b.val - mod;
else return a.val + b.val;
}
friend Modular operator-(const Modular& a, const Modular& b) {
if (a.val - b.val < 0) return a.val - b.val + mod;
else return a.val - b.val;
}
friend Modular operator*(const Modular& a, const Modular& b) {
return 1ll * a.val * b.val % mod;
}
friend Modular binpow(Modular a, long long n) {
Modular res = 1;
for (; n; n >>= 1) {
if (n & 1) res *= a;
a *= a;
}
return res;
}
/* ALTERNATIVE INVERSE FUNCTION USING EXTENDED EUCLIDEAN ALGORITHM
friend void gcd(int a, int b, Modular& x, Modular& y) {
if (a == 0) {
x = Modular(0);
y = Modular(1);
return;
}
Modular x1, y1;
gcd(b % a, a, x1, y1);
x = y1 - (b / a) * x1;
y = x1;
}
friend Modular inv(const Modular& a) {
Modular x, y;
gcd(a.val, mod, x, y);
return x;
}
*/
friend Modular inv(const Modular& a) {
return binpow(a, mod - 2);
}
Modular operator/(const Modular& ot) const {
return *this * inv(ot);
}
Modular& operator++() {
if (val + 1 == mod) val = 0;
else ++val;
return *this;
}
Modular operator++(int) {
Modular tmp = *this;
++(*this);
return tmp;
}
Modular operator+() const {
return *this;
}
Modular operator-() const {
return 0 - *this;
}
Modular& operator+=(const Modular& ot) {
return *this = *this + ot;
}
Modular& operator-=(const Modular& ot) {
return *this = *this - ot;
}
Modular& operator*=(const Modular& ot) {
return *this = *this * ot;
}
Modular& operator/=(const Modular& ot) {
return *this = *this / ot;
}
bool operator==(const Modular& ot) const {
return val == ot.val;
}
bool operator!=(const Modular& ot) const {
return val != ot.val;
}
bool operator<(const Modular& ot) const {
return val < ot.val;
}
bool operator>(const Modular& ot) const {
return val > ot.val;
}
explicit operator int() const {
return val;
}
};
template<int mod>
istream& operator>>(istream& istr, Modular<mod>& x) {
return istr >> x.val;
}
template<int mod>
ostream& operator<<(ostream& ostr, const Modular<mod>& x) {
return ostr << x.val;
}
const int mod = 1e9 + 7;
using Mint = Modular<mod>;
struct node {
int l, r;
Mint sum, sumH;
Mint p;
node() {
l = r = 0;
p = 1;
sum = sumH = 0;
}
} t[N << 4];
int sz = 0;
void push(int v, int tl = 0, int tr = M) {
if (t[v].p == 1) return;
if (tl != tr) {
t[v].l = (t[v].l ? t[v].l : ++sz);
t[v].r = (t[v].r ? t[v].r : ++sz);
t[t[v].l].p *= t[v].p;
t[t[v].r].p *= t[v].p;
}
t[v].sum *= t[v].p;
t[v].sumH *= t[v].p;
t[v].p = 1;
}
void upd_mul(int l, int r, Mint val, int v = 1, int tl = 0, int tr = M) {
push(v, tl, tr);
if (l <= tl && tr <= r) {
t[v].p *= val;
push(v, tl, tr);
return;
}
if (r < tl || tr < l)
return;
int tm = tl + tr >> 1;
t[v].l = (t[v].l ? t[v].l : ++sz);
t[v].r = (t[v].r ? t[v].r : ++sz);
upd_mul(l, r, val, t[v].l, tl, tm);
upd_mul(l, r, val, t[v].r, tm + 1, tr);
t[v].sum = t[t[v].l].sum + t[t[v].r].sum;
t[v].sumH = t[t[v].l].sumH + t[t[v].r].sumH;
}
void upd(int pos, Mint val, int v = 1, int tl = 0, int tr = M) {
push(v, tl, tr);
if (tl == tr) {
t[v].sum += val;
t[v].sumH += val * Mint(tl);
return;
}
int tm = tl + tr >> 1;
if (pos <= tm) {
t[v].l = (t[v].l ? t[v].l : ++sz);
upd(pos, val, t[v].l, tl, tm);
}
else {
t[v].r = (t[v].r ? t[v].r : ++sz);
upd(pos, val, t[v].r, tm + 1, tr);
}
t[v].sum = t[t[v].l].sum + t[t[v].r].sum;
t[v].sumH = t[t[v].l].sumH + t[t[v].r].sumH;
}
Mint get(int l, int r, int v = 1, int tl = 0, int tr = M) {
push(v, tl, tr);
if (r < tl || tr < l || v == 0)
return 0;
if (l <= tl && tr <= r)
return t[v].sum;
int tm = tl + tr >> 1;
return get(l, r, t[v].l, tl, tm) + get(l, r, t[v].r, tm + 1, tr);
}
Mint getH(int l, int r, int v = 1, int tl = 0, int tr = M) {
push(v, tl, tr);
if (r < tl || tr < l || v == 0)
return 0;
if (l <= tl && tr <= r)
return t[v].sumH;
int tm = tl + tr >> 1;
return getH(l, r, t[v].l, tl, tm) + getH(l, r, t[v].r, tm + 1, tr);
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0);
int n;
cin >> n;
int a[n + 1], b[n + 1];
Mint A[4][n + 1], B[4][n + 1], pw[n + 1];
pw[0] = 1;
Mint ans = 0;
for (int i = 1; i <= n; ++i) {
pw[i] = pw[i - 1] * Mint(2);
}
for (int i = 1; i <= n; ++i) {
cin >> a[i];
ans = ans - Mint(a[i]) * pw[n - 1];
}
for (int i = 1; i <= n; ++i) {
cin >> b[i];
ans = ans - Mint(b[i]) * pw[n - 1];
}
++sz;
upd(0, 1);
for (int i = 1; i <= n; ++i) {
if (a[i] > b[i]) swap(a[i], b[i]);
A[1][i] = get(0, a[i] - 1);
B[0][i] = get(0, b[i]);
upd(a[i], A[1][i]);
upd(b[i], B[0][i]);
upd_mul(0, a[i] - 1, 0);
upd_mul(b[i] + 1, M, 2);
ans = ans + getH(0, M) * pw[n - i];
if (i == n) {
ans = ans - getH(0, M) * Mint(n);
}
}
++sz;
++sz;
int r = sz;
upd(0, 1, r);
for (int i = n; i >= 1; --i) {
A[3][i] = get(0, a[i] - 1, r);
B[2][i] = get(0, b[i], r);
upd(a[i], A[3][i], r);
upd(b[i], B[2][i], r);
upd_mul(0, a[i] - 1, 0, r);
upd_mul(b[i] + 1, M, 2, r);
ans = ans + getH(0, M, r) * pw[i - 1];
}
cout << ans << '\n';
}
详细
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 8
Accepted
time: 23ms
memory: 316268kb
input:
4 1 1 1 1 2 2 2 2
output:
6
result:
ok single line: '6'
Test #2:
score: 0
Accepted
time: 33ms
memory: 316204kb
input:
10 1 2 3 4 5 6 7 8 9 10 10 9 8 7 6 5 4 3 2 1
output:
21116
result:
ok single line: '21116'
Test #3:
score: 0
Accepted
time: 36ms
memory: 316200kb
input:
1 1 2
output:
0
result:
ok single line: '0'
Test #4:
score: 0
Accepted
time: 20ms
memory: 316184kb
input:
2 1 1 2 2
output:
0
result:
ok single line: '0'
Test #5:
score: 0
Accepted
time: 20ms
memory: 316112kb
input:
3 1 1 1 2 2 2
output:
1
result:
ok single line: '1'
Test #6:
score: 0
Accepted
time: 23ms
memory: 316124kb
input:
3 1 1 1 3 2 3
output:
3
result:
ok single line: '3'
Test #7:
score: 0
Accepted
time: 16ms
memory: 316132kb
input:
3 2 1 1 3 2 3
output:
4
result:
ok single line: '4'
Test #8:
score: 0
Accepted
time: 24ms
memory: 316100kb
input:
3 1 1 2 3 2 3
output:
4
result:
ok single line: '4'
Test #9:
score: 0
Accepted
time: 35ms
memory: 316276kb
input:
4 1 1 2 2 2 2 1 1
output:
6
result:
ok single line: '6'
Test #10:
score: 0
Accepted
time: 19ms
memory: 316064kb
input:
3 1 4 4 3 1 1
output:
2
result:
ok single line: '2'
Test #11:
score: 0
Accepted
time: 30ms
memory: 316128kb
input:
20 801072306 297281669 94099424 745640358 822582129 579751930 776707816 425952653 529794053 256263083 615445445 401366545 990263003 967996508 788867983 916880116 837997768 346996508 623409388 122077161 141734988 448434751 822901346 825591177 388082644 468025968 260356829 1164654 537396602 730502935 ...
output:
840988190
result:
ok single line: '840988190'
Test #12:
score: 0
Accepted
time: 37ms
memory: 316172kb
input:
15 792312195 195812430 401437979 703756992 502275277 612055402 556065888 470806179 556125857 640437896 240743729 861383776 500299988 911972088 161499505 167335533 694282920 379241393 144223073 973249939 83979787 962250505 359871412 130233016 655843497 928153117 542969567 974346871 369758881 962874102
output:
617169726
result:
ok single line: '617169726'
Test #13:
score: 0
Accepted
time: 19ms
memory: 316260kb
input:
20 1 1 2 1 1 1 2 2 2 2 2 1 1 1 1 2 2 1 2 1 2 2 1 2 2 2 1 1 1 1 1 2 2 2 2 1 1 2 1 2
output:
8388630
result:
ok single line: '8388630'
Test #14:
score: 0
Accepted
time: 25ms
memory: 316200kb
input:
15 2 1 1 2 1 2 2 1 2 1 2 1 2 2 1 1 2 2 1 2 1 1 2 1 2 1 2 1 1 2
output:
180241
result:
ok single line: '180241'
Test #15:
score: -8
Wrong Answer
time: 28ms
memory: 316260kb
input:
20 10 3 8 2 5 7 8 3 3 10 5 6 1 2 1 9 10 2 7 10 6 6 2 3 6 10 4 6 7 2 3 3 5 7 2 8 2 1 5 1
output:
78061568
result:
wrong answer 1st lines differ - expected: '78020608', found: '78061568'
Subtask #2:
score: 0
Wrong Answer
Test #23:
score: 0
Wrong Answer
time: 19ms
memory: 316132kb
input:
100 948 425 211 688 416 81 356 602 712 954 117 522 797 75 281 491 662 669 78 156 939 526 929 937 916 619 166 777 48 898 449 278 298 714 668 755 679 38 389 602 195 135 672 833 655 541 473 27 596 274 351 353 598 993 837 246 950 99 179 751 481 843 550 195 964 279 806 82 330 599 124 756 649 838 513 625 ...
output:
690465390
result:
wrong answer 1st lines differ - expected: '164439470', found: '690465390'
Subtask #3:
score: 0
Skipped
Dependency #2:
0%
Subtask #4:
score: 0
Skipped
Dependency #1:
0%
Subtask #5:
score: 12
Accepted
Test #57:
score: 12
Accepted
time: 2327ms
memory: 337660kb
input:
500000 1 1 1 1 2 2 2 1 1 1 1 1 2 2 1 1 2 2 2 2 1 2 1 1 2 1 1 1 1 2 1 2 2 1 1 2 2 2 1 1 2 2 2 2 1 2 2 2 2 1 2 2 2 1 2 2 2 1 2 2 2 2 2 2 2 2 1 1 2 1 2 1 2 2 1 2 2 2 2 2 2 2 1 1 1 2 2 2 1 1 2 1 2 2 1 2 2 1 2 2 2 1 2 2 2 2 2 2 2 2 1 1 1 2 1 2 1 1 2 1 2 1 2 2 1 1 2 1 1 1 1 1 2 2 2 2 2 2 1 2 2 1 1 2 1 2 1...
output:
869044223
result:
ok single line: '869044223'
Test #58:
score: 0
Accepted
time: 2323ms
memory: 337608kb
input:
499993 1 1 2 1 1 2 1 1 2 1 1 1 2 2 2 2 1 1 1 2 1 1 1 2 2 2 1 1 2 2 1 2 1 2 1 2 1 2 2 2 2 1 1 2 1 2 1 2 2 1 1 2 2 2 2 2 1 1 2 1 1 2 1 1 2 2 2 1 2 2 1 2 1 1 2 1 1 2 1 1 1 1 2 1 1 1 2 2 1 1 2 1 1 1 2 2 2 2 1 1 2 1 2 1 2 2 1 2 1 1 1 1 2 1 1 2 1 2 2 1 2 2 2 2 2 1 2 2 1 1 1 2 1 1 1 1 2 1 1 1 2 1 1 2 1 1 1...
output:
480826834
result:
ok single line: '480826834'
Test #59:
score: 0
Accepted
time: 2325ms
memory: 337688kb
input:
500000 2 2 1 1 2 2 2 1 1 2 2 2 2 2 1 1 1 1 1 2 1 2 1 2 1 2 2 1 1 1 2 1 1 1 1 1 1 2 2 2 2 2 2 2 2 1 2 1 2 1 2 1 1 2 1 1 2 2 2 1 2 2 1 2 2 1 2 2 1 1 1 2 1 1 1 2 2 2 1 1 1 1 1 1 1 2 1 1 1 1 1 2 2 1 2 1 2 2 2 1 2 1 1 2 1 2 1 1 2 2 2 1 2 1 2 2 2 2 2 2 1 1 2 2 2 2 1 1 1 1 1 1 2 2 1 1 1 2 1 2 2 2 1 1 1 2 2...
output:
869044223
result:
ok single line: '869044223'
Test #60:
score: 0
Accepted
time: 2348ms
memory: 337688kb
input:
499999 2 1 2 2 1 2 1 1 2 1 1 1 2 2 2 1 1 2 2 2 1 1 1 2 2 2 2 2 1 2 2 1 1 2 2 2 2 1 1 2 1 2 2 1 2 2 2 2 2 2 1 2 2 2 2 2 2 1 2 2 2 2 2 1 2 1 2 1 2 1 2 1 1 1 1 1 1 2 1 1 1 1 2 1 2 2 1 1 2 2 1 2 1 1 2 1 2 1 1 2 1 1 1 2 2 1 1 2 2 2 2 2 1 2 1 2 2 1 1 1 2 2 1 1 2 1 1 2 1 1 1 2 1 2 1 1 2 1 2 1 2 1 2 2 2 1 1...
output:
192864306
result:
ok single line: '192864306'
Subtask #6:
score: 0
Skipped
Dependency #1:
0%