# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
792598 |
2023-07-25T07:16:24 Z |
이동현(#10054) |
Binaria (CCO23_day1problem1) |
C++17 |
|
13 ms |
23816 KB |
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#define int long long
using namespace std;
const int NS = (int)1e6 + 4, mod = (int)1e6 + 3;
int n, k;
int a[NS];
int r[NS], l[NS], chk[NS];
int pw(int x, int y){
if(!y) return 1;
if(y == 1) return x;
int v = pw(x, y / 2);
return v * v % mod * (y % 2 ? x : 1) % mod;
}
int fdr(int x){
return (x == r[x] ? x : r[x] = fdr(r[x]));
}
int fdl(int x){
return (x == l[x] || x == -1 ? x : l[x] = fdl(l[x]));
}
struct Fenwick{
int n;
vector<int> tr;
Fenwick(int m){
n = m + 4;
tr.resize(n);
}
void push(int pos, int val){
pos += 2;
for(int i = pos; i < n; i += (i & -i)){
tr[i] += val;
}
}
int get(int pos){
int rv = 0;
pos += 2;
for(int i = pos; i > 0; i -= (i & -i)){
rv += tr[i];
}
return rv;
}
int get(int l, int r){
if(l > r) return 0;
return get(r) - get(l - 1);
}
};
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
iota(r, r + NS, 0);
iota(l, l + NS, 0);
memset(chk, -1, sizeof(chk));
int n, k;
cin >> n >> k;
vector<int> a(n - k + 1);
for(int i = 0; i < n - k + 1; ++i){
cin >> a[i];
}
Fenwick tr(n);
auto cant = [&]{
cout << "0\n";
exit(0);
};
auto nor = [&](vector<int> x)->vector<int>{
if(x[0] > x[1]) return x;
int nl = fdr(x[0]);
int nr = fdl(x[1]);
if(nl > nr){
int nv = x[2] - tr.get(x[0], x[1]);
if(nv) cant();
return {nl, nr, 0};
}
int nv = x[2] - tr.get(x[0], nl - 1) - tr.get(nr + 1, x[1]);
return {nl, nr, nv};
};
auto upd = [&](int pos, int val){
if(val < 0 || val > 1) cant();
chk[pos] = val;
tr.push(pos, val);
l[pos] = pos - 1;
r[pos] = pos + 1;
};
deque<vector<int>> stk;
stk.push_back({0, k - 1, a[0]});
int stval = 0;
for(int i = 1; i < n - k + 1; ++i){
vector<int> now = nor({i, i + k - 1, a[i]});
if(!(int)stk.size()){
upd(now[1], now[2]);
continue;
}
while((int)stk.size()){
stk.back() = nor(stk.back());
// cout << stk.back()[0] << ' ' << stk.back()[1] << ' ' << now[0] << ' ' << now[1] << endl;
if(stk.back()[0] == stk.back()[1]) break;
int sval = stk.back()[2] - tr.get(stk.back()[0], stk.back()[1]);
int nval = now[2] - tr.get(now[0], now[1]);
if(sval == nval) break;
int p = -1;
int cut = stk.back()[0];
while(p + 1 < (int)stk.size() && (stk[p + 1] = nor(stk[p + 1]))[1] < cut){
++p;
}
// cout << p << endl;
if(stk.back()[0] == now[0]){
upd(now[1], nval - sval);
p = -1;
}
else if(sval > nval){
upd(stk.back()[0], sval - nval);
upd(now[1], 0);
}
else{
upd(now[1], nval - sval);
upd(stk.back()[0], 0);
}
for(int j = p; j >= 0; --j){
stk[j] = nor(stk[j]);
stk[j + 1] = nor(stk[j + 1]);
upd(stk[j][0], stk[j][2] - tr.get(stk[j][0], stk[j][1]) - (stk[j + 1][2] - tr.get(stk[j + 1][0], stk[j + 1][1])));
++stval;
}
for(int j = 0; j <= p; ++j) stk.pop_front();
now = nor(now);
stk.pop_back();
}
while((int)stk.size()){
stk.back() = nor(stk.back());
if(stk.back()[0] < stk.back()[1]) break;
upd(stk.back()[1], stk.back()[2]);
stk.pop_back();
}
if(now[0] < now[1]){
stk.push_back(now);
}
else if(now[0] == now[1]){
upd(now[1], now[2]);
}
}
if(!(int)stk.size()){
cout << "1\n";
return 0;
}
int ac = 0, oc = a[stval];
for(int i = stval; i < stval + k; ++i){
ac += (chk[i] == -1);
oc -= (chk[i] == 1);
}
int ans = 1;
for(int i = oc + 1; i <= ac; ++i){
(ans *= i) %= mod;
}
for(int i = 2; i <= ac - oc; ++i){
(ans *= pw(i, mod - 2)) %= mod;
}
auto naive = [&]{
vector<vector<int>> ran(n - k + 1);
for(int i = 0; i < n - k + 1; ++i){
ran[i] = {i, i + k - 1, a[i]};
}
while(true){
int f = 0;
for(int i = 0; i + 1 < (int)ran.size(); ++i){
ran[i] = nor(ran[i]);
// cout << i << ' ' << ran[i + 1][0] << ' ' << ran[i + 1][1] << ' ' << ran[i + 1][2] << endl;
ran[i + 1] = nor(ran[i + 1]);
int sval = ran[i][2] - tr.get(ran[i][0], ran[i][1]);
int nval = ran[i + 1][2] - tr.get(ran[i + 1][0], ran[i + 1][1]);
if(ran[i][0] > ran[i][1] || ran[i + 1][0] > ran[i + 1][1]) continue;
if(ran[i][0] == ran[i + 1][0] && ran[i][1] == ran[i + 1][1]){
if(sval != nval) cant();
continue;
}
if(ran[i][0] < ran[i + 1][0] && ran[i][1] < ran[i + 1][1] && sval == nval){
continue;
}
f = 1;
// cout << i << ' ' << sval << ' ' << nval << endl;
if(ran[i][0] == ran[i + 1][0]){
upd(ran[i + 1][1], nval - sval);
}
else if(ran[i][1] == ran[i + 1][1]){
upd(ran[i][0], sval - nval);
}
else{
if(sval > nval){
upd(ran[i][0], sval - nval);
upd(ran[i + 1][1], 0);
}
else{
upd(ran[i + 1][1], nval - sval);
upd(ran[i][0], 0);
}
}
}
if(!f) break;
}
// cout << "HI" << endl;
int stval = 0;
while(stval < n - k + 1){
ran[stval] = nor(ran[stval]);
if(ran[stval][0] <= ran[stval][1]) break;
++stval;
}
if(stval == n - k + 1){
return 1ll;
}
int ac = 0, oc = a[stval];
for(int i = stval; i < stval + k; ++i){
ac += (chk[i] == -1);
oc -= (chk[i] == 1);
}
int ans = 1;
for(int i = oc + 1; i <= ac; ++i){
(ans *= i) %= mod;
}
for(int i = 2; i <= ac - oc; ++i){
(ans *= pw(i, mod - 2)) %= mod;
}
return ans;
};
tr = Fenwick(n);
iota(r, r + NS, 0);
iota(l, l + NS, 0);
memset(chk, -1, sizeof(chk));
assert(ans == naive());
cout << ans << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
23764 KB |
Output is correct |
2 |
Correct |
12 ms |
23732 KB |
Output is correct |
3 |
Correct |
11 ms |
23764 KB |
Output is correct |
4 |
Correct |
9 ms |
23720 KB |
Output is correct |
5 |
Correct |
11 ms |
23808 KB |
Output is correct |
6 |
Correct |
12 ms |
23808 KB |
Output is correct |
7 |
Correct |
12 ms |
23800 KB |
Output is correct |
8 |
Correct |
12 ms |
23764 KB |
Output is correct |
9 |
Correct |
12 ms |
23816 KB |
Output is correct |
10 |
Incorrect |
11 ms |
23760 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
23764 KB |
Output is correct |
2 |
Correct |
12 ms |
23732 KB |
Output is correct |
3 |
Correct |
11 ms |
23764 KB |
Output is correct |
4 |
Correct |
9 ms |
23720 KB |
Output is correct |
5 |
Correct |
11 ms |
23808 KB |
Output is correct |
6 |
Correct |
12 ms |
23808 KB |
Output is correct |
7 |
Correct |
12 ms |
23800 KB |
Output is correct |
8 |
Correct |
12 ms |
23764 KB |
Output is correct |
9 |
Correct |
12 ms |
23816 KB |
Output is correct |
10 |
Incorrect |
11 ms |
23760 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
23764 KB |
Output is correct |
2 |
Correct |
12 ms |
23732 KB |
Output is correct |
3 |
Correct |
11 ms |
23764 KB |
Output is correct |
4 |
Correct |
9 ms |
23720 KB |
Output is correct |
5 |
Correct |
11 ms |
23808 KB |
Output is correct |
6 |
Correct |
12 ms |
23808 KB |
Output is correct |
7 |
Correct |
12 ms |
23800 KB |
Output is correct |
8 |
Correct |
12 ms |
23764 KB |
Output is correct |
9 |
Correct |
12 ms |
23816 KB |
Output is correct |
10 |
Incorrect |
11 ms |
23760 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
23764 KB |
Output is correct |
2 |
Correct |
12 ms |
23732 KB |
Output is correct |
3 |
Correct |
11 ms |
23764 KB |
Output is correct |
4 |
Correct |
9 ms |
23720 KB |
Output is correct |
5 |
Correct |
11 ms |
23808 KB |
Output is correct |
6 |
Correct |
12 ms |
23808 KB |
Output is correct |
7 |
Correct |
12 ms |
23800 KB |
Output is correct |
8 |
Correct |
12 ms |
23764 KB |
Output is correct |
9 |
Correct |
12 ms |
23816 KB |
Output is correct |
10 |
Incorrect |
11 ms |
23760 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
23764 KB |
Output is correct |
2 |
Correct |
12 ms |
23732 KB |
Output is correct |
3 |
Correct |
11 ms |
23764 KB |
Output is correct |
4 |
Correct |
9 ms |
23720 KB |
Output is correct |
5 |
Correct |
11 ms |
23808 KB |
Output is correct |
6 |
Correct |
12 ms |
23808 KB |
Output is correct |
7 |
Correct |
12 ms |
23800 KB |
Output is correct |
8 |
Correct |
12 ms |
23764 KB |
Output is correct |
9 |
Correct |
12 ms |
23816 KB |
Output is correct |
10 |
Incorrect |
11 ms |
23760 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
23764 KB |
Output is correct |
2 |
Correct |
12 ms |
23732 KB |
Output is correct |
3 |
Correct |
11 ms |
23764 KB |
Output is correct |
4 |
Correct |
9 ms |
23720 KB |
Output is correct |
5 |
Correct |
11 ms |
23808 KB |
Output is correct |
6 |
Correct |
12 ms |
23808 KB |
Output is correct |
7 |
Correct |
12 ms |
23800 KB |
Output is correct |
8 |
Correct |
12 ms |
23764 KB |
Output is correct |
9 |
Correct |
12 ms |
23816 KB |
Output is correct |
10 |
Incorrect |
11 ms |
23760 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |