Search is not available for this dataset
name
stringlengths
2
88
description
stringlengths
31
8.62k
public_tests
dict
private_tests
dict
solution_type
stringclasses
2 values
programming_language
stringclasses
5 values
solution
stringlengths
1
983k
1129_D. Isolation
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once. Since the answer can be large, find it modulo 998 244 353. Input The first line contains two space-separated integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the number of elements in the array a and the restriction from the statement. The following line contains n space-separated integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n) β€” elements of the array a. Output The first and only line contains the number of ways to divide an array a modulo 998 244 353. Examples Input 3 1 1 1 2 Output 3 Input 5 2 1 1 2 1 3 Output 14 Input 5 5 1 2 3 4 5 Output 16 Note In the first sample, the three possible divisions are as follows. * [[1], [1], [2]] * [[1, 1], [2]] * [[1, 1, 2]] Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
{ "input": [ "5 5\n1 2 3 4 5\n", "3 1\n1 1 2\n", "5 2\n1 1 2 1 3\n" ], "output": [ "16", "3", "14" ] }
{ "input": [ "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n", "100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n", "10 1\n4 2 9 2 1 4 4 1 4 10\n", "100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n", "1 1\n1\n", "100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n", "2 2\n2 1\n", "2 1\n2 2\n" ], "output": [ "16", "726975503", "24", "244208148", "1", "1", "2", "2" ] }
IN-CORRECT
cpp
#include<bits/stdc++.h> #define rg register #define fp( i , x , y ) for( rg int i=(x); i<=(y); ++i ) #define fq( i , x , y ) for( rg int i=(y); i>=(x); --i ) #define i60 long long using namespace std ; const int N = 1e5+10 , B = 325 , skc = 998244353 ; int lef[B] , rig[B] , pos[N] , n , k , a[N] , f[N] , las[N] , pre[N] ; void upd( int &x , int y ) { x = (x+y) % skc ; } struct node { int b[B] , tg , pt , vl[B] , s[N] ; void change( int l , int r , int z ) { l = l-pt+1 ; r = r-pt+1 ; fp( i , l , r ) { b[i] += z ; if( z == 1 ) upd( s[b[i]] , vl[i] ) ; else upd( s[b[i]+1] , skc-vl[i] ) ; } } int ask( ) { return ( s[0] + skc - (k-tg+1>=0?s[k-tg+1]:0) ) % skc ; } int xun( int l , int r ) { i60 cnt = 0 ; l=l-pt+1 ; r=r-pt+1 ; fp( i , l , r ) if( b[i]+tg <= k ) cnt += vl[i] ; return cnt%skc ; } void ins( int p , int z ) { p = p-pt+1 ; vl[p] = z ; s[0] = (s[0]+z) % skc ; } } bl[B] ; void modify( int l , int r , int z ) { int lp = pos[l] , rp = pos[r] ; if( lp == rp ) { bl[lp].change( l , r , z ) ; return ; } bl[lp].change( l , rig[lp] , z ) ; fp( i , lp+1 , rp-1 ) bl[i].tg += z ; bl[rp].change( lef[rp] , r , z ) ; } int query( int r ) { int rp = pos[r] ; i60 cnt = 0 ; if( rp == 1 ) return bl[1].xun( 0 , r ) ; fp( i , 1 , rp-1 ) cnt += bl[i].ask( ) ; cnt += bl[rp].xun( lef[rp] , r ) ; return cnt%skc ; } signed main( ) { ios::sync_with_stdio(false) ; cin.tie(0) ; cin >> n >> k ; memset( las , -1 , sizeof(las) ) ; int blo = sqrt(n) ; fp( i , 1 , n ) { cin >> a[i] ; pre[i] = las[a[i]] ; las[a[i]] = i ; pos[i] = (i-1)/blo+1 ; rig[pos[i]] = i ; } pos[0] = 1 ; fq( i , 0 , n ) lef[pos[i]] = i ; fp( i , 1 , pos[n] ) bl[i].pt = lef[i] ; bl[1].ins( 0 , 1 ) ; int p1 , p2 ; fp( i , 1 , n ) { p1 = pre[i] ; if( p1 == -1 ) modify( 0 , i-1 , 1 ) ; else { p2 = pre[p1] ; if( p2 == -1 ) modify( 0 , p1-1 , -1 ) , modify( p1 , i-1 , 1 ) ; else modify( p2 , p1-1 , -1 ) , modify( p1 , i-1 , 1 ) ; } f[i] = query( i-1 ) ; bl[pos[i]].ins( i , f[i] ) ; } cout << f[n] << '\n' ; return 0 ; }
1129_D. Isolation
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once. Since the answer can be large, find it modulo 998 244 353. Input The first line contains two space-separated integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the number of elements in the array a and the restriction from the statement. The following line contains n space-separated integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n) β€” elements of the array a. Output The first and only line contains the number of ways to divide an array a modulo 998 244 353. Examples Input 3 1 1 1 2 Output 3 Input 5 2 1 1 2 1 3 Output 14 Input 5 5 1 2 3 4 5 Output 16 Note In the first sample, the three possible divisions are as follows. * [[1], [1], [2]] * [[1, 1], [2]] * [[1, 1, 2]] Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
{ "input": [ "5 5\n1 2 3 4 5\n", "3 1\n1 1 2\n", "5 2\n1 1 2 1 3\n" ], "output": [ "16", "3", "14" ] }
{ "input": [ "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n", "100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n", "10 1\n4 2 9 2 1 4 4 1 4 10\n", "100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n", "1 1\n1\n", "100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n", "2 2\n2 1\n", "2 1\n2 2\n" ], "output": [ "16", "726975503", "24", "244208148", "1", "1", "2", "2" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int BASE = 998244353; const int SZ = 320; struct BlockData { int sumC; vector<long long> sumF; BlockData() {} }; int n, k, c[100100]; long long f[100100]; BlockData blocks[SZ]; long long calc(int i) { long long res = 0; int sumC = 0, block = i / SZ; for (int j = i; j >= block * SZ; j--) { sumC += c[j]; if (sumC <= k) res += j ? f[j - 1] : 1; } for (int j = block - 1; j >= 0; j--) { res += blocks[j].sumF[max(min(k - sumC, SZ), 0) + SZ]; sumC += blocks[j].sumC; } return res % BASE; } void constructBlock(int block) { BlockData &data = blocks[block]; data.sumC = 0; data.sumF = vector<long long>(SZ * 2 + 1, 0); for (int i = block * SZ + SZ - 1; i >= block * SZ; i--) { data.sumC += c[i]; data.sumF[data.sumC + SZ] += i ? f[i - 1] : 1; } for (int i = 0; i <= SZ * 2; i++) { if (i) data.sumF[i] += data.sumF[i - 1]; data.sumF[i] %= BASE; } } int main() { ios::sync_with_stdio(0); cin.tie(0); int x; cin >> n >> k; vector<int> last(n + 1, -1); for (int i = 0; i < n; i++) { cin >> x; c[i]++; if (last[x] >= 0) { c[last[x]] = -1; if (last[x] / SZ != i / SZ) constructBlock(last[x] / SZ); } last[x] = i; f[i] = calc(i); if (i % SZ == SZ - 1) constructBlock(i / SZ); } cout << f[n - 1] << endl; }
1129_D. Isolation
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once. Since the answer can be large, find it modulo 998 244 353. Input The first line contains two space-separated integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the number of elements in the array a and the restriction from the statement. The following line contains n space-separated integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n) β€” elements of the array a. Output The first and only line contains the number of ways to divide an array a modulo 998 244 353. Examples Input 3 1 1 1 2 Output 3 Input 5 2 1 1 2 1 3 Output 14 Input 5 5 1 2 3 4 5 Output 16 Note In the first sample, the three possible divisions are as follows. * [[1], [1], [2]] * [[1, 1], [2]] * [[1, 1, 2]] Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
{ "input": [ "5 5\n1 2 3 4 5\n", "3 1\n1 1 2\n", "5 2\n1 1 2 1 3\n" ], "output": [ "16", "3", "14" ] }
{ "input": [ "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n", "100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n", "10 1\n4 2 9 2 1 4 4 1 4 10\n", "100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n", "1 1\n1\n", "100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n", "2 2\n2 1\n", "2 1\n2 2\n" ], "output": [ "16", "726975503", "24", "244208148", "1", "1", "2", "2" ] }
IN-CORRECT
cpp
#include<stdint.h> #include<ext/pb_ds/assoc_container.hpp> #include<ext/pb_ds/tree_policy.hpp> #include<ext/rope> using namespace __gnu_pbds; using namespace __gnu_cxx; #define VIS(it,con) for(auto it=con.begin();it!=con.end();++it) #define pob pop_back #define pf push_front #define pof pop_front #define MIN(x,y) (x=min(x,(y))) #define MAX(x,y) (x=max(x,(y))) #define mid (l+r>>1) #define lch (idx*2+1) #define rch (idx*2+2) /*****************************************************************************/ #include<bits/stdc++.h> #define int int_fast64_t using namespace std; typedef pair<int,int> pii; typedef vector<int> VI; #define REP(i,j,k) for(register int i=(j);i<(k);++i) #define RREP(i,j,k) for(register int i=(j)-1;i>=(k);--i) #define ALL(a) a.begin(),a.end() #define MST(a,v) memset(a,(v),sizeof a) #define pb push_back #define F first #define S second #define endl '\n' #define __debug #ifdef __debug #define IOS (void)0 #define de(...) cerr<<__VA_ARGS__ #define ar(a,s,t) {REP(__i,s,t)de(a[__i]<<' ');de(endl);} #else #define IOS cin.tie(0),cout.tie(0),ios_base::sync_with_stdio(false) #define de(...) (void)0 #define ar(...) (void)0 #endif /***********************************default***********************************/ const int maxn=1e5+9,mo=998244353,block=320,rg=650,base=325; int n,k,a[maxn],dp[maxn]; int pre[maxn],last[maxn]; int pfx[block][2*block+9],tag[maxn],sumt[block]; void add(int&x,int y){x=(x+y)%mo;} void upd(int b){ int cnt=0; MST(pfx[b],0); RREP(i,(b+1)*block,b*block){ cnt+=tag[i]; add(pfx[b][base+cnt],dp[i]); } sumt[b]=cnt; REP(i,1,rg)add(pfx[b][i],pfx[b][i-1]); } main(){ IOS; cin>>n>>k; REP(i,0,n)cin>>a[i]; MST(last,-1); REP(i,0,n)pre[i]=last[a[i]],last[a[i]]=i; MST(pfx,0); MST(tag,0); MST(sumt,0); MST(dp,0); dp[0]=1; REP(i,0,n){ tag[i]=1; upd(i/block); if(pre[i]>=0){ tag[pre[i]]=-1; upd(pre[i]/block); if(pre[pre[i]]>=0){ tag[pre[pre[i]]]=0; upd(pre[pre[i]]/block); } } int sum=0; RREP(j,i/block+1,0){ add(dp[i+1],pfx[j][max((int)0,min(rg-1,base+k-sum))]); sum+=sumt[j]; } } cout<<dp[n]<<endl; }
1129_D. Isolation
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once. Since the answer can be large, find it modulo 998 244 353. Input The first line contains two space-separated integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the number of elements in the array a and the restriction from the statement. The following line contains n space-separated integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n) β€” elements of the array a. Output The first and only line contains the number of ways to divide an array a modulo 998 244 353. Examples Input 3 1 1 1 2 Output 3 Input 5 2 1 1 2 1 3 Output 14 Input 5 5 1 2 3 4 5 Output 16 Note In the first sample, the three possible divisions are as follows. * [[1], [1], [2]] * [[1, 1], [2]] * [[1, 1, 2]] Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
{ "input": [ "5 5\n1 2 3 4 5\n", "3 1\n1 1 2\n", "5 2\n1 1 2 1 3\n" ], "output": [ "16", "3", "14" ] }
{ "input": [ "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n", "100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n", "10 1\n4 2 9 2 1 4 4 1 4 10\n", "100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n", "1 1\n1\n", "100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n", "2 2\n2 1\n", "2 1\n2 2\n" ], "output": [ "16", "726975503", "24", "244208148", "1", "1", "2", "2" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> //#pragma GCC optimize("unroll-loops") //#pragma GCC optimize("-O3") //#pragma GCC optimize("Ofast") #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #include <ext/rope> #define sz(x) int(x.size()) #define all(x) x.begin(),x.end() #define pii pair<int,int> #define PB push_back #define pll pair<ll,ll> #define pii pair<int,int> #define pil pair<int,ll> #define pli pair<ll, int> #define pi4 pair<pii, pii> #define pib pair<int, bool> #define ft first #define sd second using namespace std; using namespace __gnu_cxx; using namespace __gnu_pbds; typedef long long ll; typedef long double ld; template<class T> using ordered_set = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>; const int N = 100100; const int M = (1 << 10); const int oo = 2e9; const ll OO = 1e18; const int PW = 20; const int md = 998244353; const int BLOCK = 500; const int K = N / BLOCK + 10; int f[N], SQ[K][2 * N], mid[K], sum[K], a[N], n, k, ppr[N], pr[N], vl[N]; int SUM(int x, int y) { return (x + y) % md; } int SUB(int x, int y) { return (x + md - y) % md; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); // freopen("in.txt","r",stdin); cin >> n >> k; for (int i = 0; i < n; i++) { cin >> a[i]; a[i]--; } fill(mid, mid + K, N); for (int i = 0; i < n; i++){ ppr[i] = pr[i] = -1; } for (int i = 0; i < n; i++){ int l = ppr[a[i]], r = pr[a[i]]; swap(ppr[a[i]], pr[a[i]]); pr[a[i]] = i; int nm = i / BLOCK; sum[nm] = SUM(sum[nm], (i ? f[i - 1] : 1)); SQ[nm][mid[nm]] += (i ? f[i - 1] : 1); for (int j = max(r + 1, BLOCK * nm); j <= i; j++){ SQ[nm][mid[nm] + vl[j]] = SUB(SQ[nm][mid[nm] + vl[j]], (j ? f[j - 1] : 1)); vl[j]++; SQ[nm][mid[nm] + vl[j]] = SUM(SQ[nm][mid[nm] + vl[j]], (j ? f[j - 1] : 1)); if (vl[j] == k + 1) sum[nm] = SUB(sum[nm], (j ? f[j - 1] : 1)); } if ((r + 1) / BLOCK != nm){ nm--; int nd = (r + 1) / BLOCK; for (; nm > nd; nm--){ sum[nm] = SUB(sum[nm], SQ[nm][mid[nm] + k]); mid[nm]--; } for (int j = r + 1; j < (nm + 1) * BLOCK; j++){ SQ[nm][N + vl[j]] = SUB(SQ[nm][N + vl[j]], (j ? f[j - 1] : 1)); vl[j]++; SQ[nm][N + vl[j]] = SUM(SQ[nm][N + vl[j]], (j ? f[j - 1] : 1)); if (vl[j] + N - mid[nm] == k + 1) sum[nm] = SUB(sum[nm], (j ? f[j - 1] : 1)); } } if (r >= 0){ int nd = (l + 1) / BLOCK; if (nd < nm){ for (int j = BLOCK * nm; j < r + 1; j++){ SQ[nm][N + vl[j]] = SUB(SQ[nm][N + vl[j]], (j ? f[j - 1] : 1)); vl[j]--; SQ[nm][N + vl[j]] = SUM(SQ[nm][N + vl[j]], (j ? f[j - 1] : 1)); if (vl[j] + N - mid[nm] == k) sum[nm] = SUM(sum[nm], (j ? f[j - 1] : 1)); } nm--; } for (; nm > nd; nm--){ sum[nm] = SUM(sum[nm], SQ[nm][mid[nm] + k + 1]); mid[nm]++; } for (int j = l + 1; j < min(BLOCK * (nm + 1), r + 1); j++){ SQ[nm][N + vl[j]] = SUB(SQ[nm][N + vl[j]], (j ? f[j - 1] : 1)); vl[j]--; SQ[nm][N + vl[j]] = SUM(SQ[nm][N + vl[j]], (j ? f[j - 1] : 1)); if (vl[j] + N - mid[nm] == k) sum[nm] = SUM(sum[nm], (j ? f[j - 1] : 1)); } } for (int j = 0; j * BLOCK <= i; j++) f[i] = SUM(f[i], sum[j]); nm = i / BLOCK; } cout << f[n - 1]; return 0; }
1129_D. Isolation
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once. Since the answer can be large, find it modulo 998 244 353. Input The first line contains two space-separated integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the number of elements in the array a and the restriction from the statement. The following line contains n space-separated integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n) β€” elements of the array a. Output The first and only line contains the number of ways to divide an array a modulo 998 244 353. Examples Input 3 1 1 1 2 Output 3 Input 5 2 1 1 2 1 3 Output 14 Input 5 5 1 2 3 4 5 Output 16 Note In the first sample, the three possible divisions are as follows. * [[1], [1], [2]] * [[1, 1], [2]] * [[1, 1, 2]] Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
{ "input": [ "5 5\n1 2 3 4 5\n", "3 1\n1 1 2\n", "5 2\n1 1 2 1 3\n" ], "output": [ "16", "3", "14" ] }
{ "input": [ "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n", "100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n", "10 1\n4 2 9 2 1 4 4 1 4 10\n", "100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n", "1 1\n1\n", "100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n", "2 2\n2 1\n", "2 1\n2 2\n" ], "output": [ "16", "726975503", "24", "244208148", "1", "1", "2", "2" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int MOD = 998244353; const long long BIG = 1446803456761533460; const int Big = 336860180; stringstream sss; const int maxn = 100010; const int SQ = 400; const int maxnsq = maxn / SQ + 10; int n, k; int A[maxn], P[maxn]; map<int, int> lst; int block[maxnsq][maxn * 2]; int lazy[maxnsq], val[maxn]; int dp[maxn]; int sum = 0; int& blk(int x, int y) { return block[x / SQ][y + maxn]; } void inc(int l, int r) { while (l < r) { if (l % SQ == 0 && l + SQ <= r) { if (k - lazy[l / SQ] <= 0) sum = ((sum) + (MOD - blk(l, k - lazy[l / SQ]))) % MOD; ++lazy[l / SQ]; l += SQ; } else { if (val[l] + lazy[l / SQ] == k) sum = ((sum) + (MOD - dp[l])) % MOD; blk(l, val[l]) = ((blk(l, val[l])) + (MOD - dp[l])) % MOD; ++val[l]; blk(l, val[l]) = ((blk(l, val[l])) + (dp[l])) % MOD; ++l; } } } void dec(int l, int r) { while (l < r) { if (l % SQ == 0 && l + SQ <= r) { --lazy[l / SQ]; if (k - lazy[l / SQ] <= 0) sum = ((sum) + (blk(l, k - lazy[l / SQ]))) % MOD; l += SQ; } else { blk(l, val[l]) = ((blk(l, val[l])) + (MOD - dp[l])) % MOD; --val[l]; blk(l, val[l]) = ((blk(l, val[l])) + (dp[l])) % MOD; if (val[l] + lazy[l / SQ] == k) sum = ((sum) + (dp[l])) % MOD; ++l; } } } void MAIN() { cin >> n >> k; for (int i = (0); i < (n); ++i) { cin >> A[i]; P[i + 1] = lst.count(A[i]) ? lst[A[i]] : 0; lst[A[i]] = i + 1; } dp[0] = 1; sum = 1; blk(0, 0) = 1; for (int i = (1); i < (n + 1); ++i) { inc(P[i], i); dec(P[P[i]], P[i]); dp[i] = sum; sum = ((sum) + (dp[i])) % MOD; val[i] = -lazy[i / SQ]; blk(i, val[i]) = ((blk(i, val[i])) + (dp[i])) % MOD; } cout << dp[n] << '\n'; } int main() { ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); cout << fixed << setprecision(10); sss << R"( 5 2 1 1 2 1 3 )"; MAIN(); return 0; }
1129_D. Isolation
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once. Since the answer can be large, find it modulo 998 244 353. Input The first line contains two space-separated integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the number of elements in the array a and the restriction from the statement. The following line contains n space-separated integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n) β€” elements of the array a. Output The first and only line contains the number of ways to divide an array a modulo 998 244 353. Examples Input 3 1 1 1 2 Output 3 Input 5 2 1 1 2 1 3 Output 14 Input 5 5 1 2 3 4 5 Output 16 Note In the first sample, the three possible divisions are as follows. * [[1], [1], [2]] * [[1, 1], [2]] * [[1, 1, 2]] Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
{ "input": [ "5 5\n1 2 3 4 5\n", "3 1\n1 1 2\n", "5 2\n1 1 2 1 3\n" ], "output": [ "16", "3", "14" ] }
{ "input": [ "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n", "100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n", "10 1\n4 2 9 2 1 4 4 1 4 10\n", "100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n", "1 1\n1\n", "100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n", "2 2\n2 1\n", "2 1\n2 2\n" ], "output": [ "16", "726975503", "24", "244208148", "1", "1", "2", "2" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 5; const int MOD = 998244353; int n, a[MAXN], m, dp[MAXN], cnt[MAXN]; void add(int &a, int b) { a = (a + b) % MOD; } int main() { scanf("%d %d", &n, &m); for (register int i = 1; i <= n; ++i) scanf("%d", &a[i]); dp[0] = 1; for (register int i = 1; i <= n; ++i) { memset(cnt, 0, sizeof cnt); int now = 0; for (register int j = i; j > 0; --j) { cnt[a[j]]++; if (cnt[a[j]] == 1) now++; else now--; if (now <= m) add(dp[i], dp[j - 1]); } } printf("%d", dp[n]); return 0; }
1129_D. Isolation
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once. Since the answer can be large, find it modulo 998 244 353. Input The first line contains two space-separated integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the number of elements in the array a and the restriction from the statement. The following line contains n space-separated integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n) β€” elements of the array a. Output The first and only line contains the number of ways to divide an array a modulo 998 244 353. Examples Input 3 1 1 1 2 Output 3 Input 5 2 1 1 2 1 3 Output 14 Input 5 5 1 2 3 4 5 Output 16 Note In the first sample, the three possible divisions are as follows. * [[1], [1], [2]] * [[1, 1], [2]] * [[1, 1, 2]] Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
{ "input": [ "5 5\n1 2 3 4 5\n", "3 1\n1 1 2\n", "5 2\n1 1 2 1 3\n" ], "output": [ "16", "3", "14" ] }
{ "input": [ "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n", "100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n", "10 1\n4 2 9 2 1 4 4 1 4 10\n", "100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n", "1 1\n1\n", "100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n", "2 2\n2 1\n", "2 1\n2 2\n" ], "output": [ "16", "726975503", "24", "244208148", "1", "1", "2", "2" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int MOD = 998244353; int n, k; int a[100055]; int dp[100055]; int l[100055]; int r[100055]; int last[100055]; int dat[400055]; int high[400055]; int low[400055]; int lazy[400055]; vector<int> out[100055]; int d[100055]; void push(int from, int to) { high[to] += lazy[from]; low[to] += lazy[from]; lazy[to] += lazy[from]; } void add(int id, int x, int y, int w, int val) { if (x == y) { dat[id] = val; return; } int mid = (x + y) >> 1; if (lazy[id]) { push(id, id + id); push(id, id + id + 1); lazy[id] = 0; } if (w <= mid) { add(id + id, x, mid, w, val); } else { add(id + id + 1, mid + 1, y, w, val); } dat[id] = dat[id + id] + dat[id + id + 1]; high[id] = max(high[id + id], high[id + id + 1]); low[id] = min(low[id + id], low[id + id + 1]); } void update(int id, int x, int y, int l, int r, int delta) { if (l > y || r < x) return; if (l <= x && y <= r && high[id] == low[id]) { lazy[id] += delta; high[id] += delta; low[id] += delta; if (delta == 1 && high[id] == k + 1) { dat[id] = 0; } else if (delta == -1 && high[id] == k) { dat[id] = (d[y] - d[x - 1] + MOD) % MOD; } return; } int mid = (x + y) >> 1; if (lazy[id]) { push(id, id + id); push(id, id + id + 1); lazy[id] = 0; } update(id + id, x, mid, l, r, delta); update(id + id + 1, mid + 1, y, l, r, delta); dat[id] = dat[id + id] + dat[id + id + 1]; high[id] = max(high[id + id], high[id + id + 1]); low[id] = min(low[id + id], low[id + id + 1]); } int main() { cin >> n >> k; for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); } dp[0] = 1; { memset(last, 0, sizeof(last)); for (int i = 1; i <= n; i++) { l[i] = last[a[i]] + 1; last[a[i]] = i; } } { for (int i = 1; i <= n; i++) { last[i] = n + 1; } for (int i = n; i >= 1; i--) { r[i] = last[a[i]] - 1; last[a[i]] = i; } } dp[0] = 1; d[0] = 1; add(1, 0, n, 0, 1); for (int i = 1; i <= n; i++) { for (auto j : out[i]) { update(1, 0, n, l[j] - 1, j - 1, -1); } update(1, 0, n, l[i] - 1, i - 1, +1); out[r[i] + 1].push_back(i); dp[i] = dat[1]; d[i] = (d[i - 1] + dp[i]) % MOD; add(1, 0, n, i, dp[i]); } printf("%d\n", dp[n]); }
1129_D. Isolation
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once. Since the answer can be large, find it modulo 998 244 353. Input The first line contains two space-separated integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the number of elements in the array a and the restriction from the statement. The following line contains n space-separated integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n) β€” elements of the array a. Output The first and only line contains the number of ways to divide an array a modulo 998 244 353. Examples Input 3 1 1 1 2 Output 3 Input 5 2 1 1 2 1 3 Output 14 Input 5 5 1 2 3 4 5 Output 16 Note In the first sample, the three possible divisions are as follows. * [[1], [1], [2]] * [[1, 1], [2]] * [[1, 1, 2]] Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
{ "input": [ "5 5\n1 2 3 4 5\n", "3 1\n1 1 2\n", "5 2\n1 1 2 1 3\n" ], "output": [ "16", "3", "14" ] }
{ "input": [ "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n", "100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n", "10 1\n4 2 9 2 1 4 4 1 4 10\n", "100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n", "1 1\n1\n", "100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n", "2 2\n2 1\n", "2 1\n2 2\n" ], "output": [ "16", "726975503", "24", "244208148", "1", "1", "2", "2" ] }
IN-CORRECT
cpp
#include<bits/stdc++.h> #define R register #define ll long long using namespace std; const int MOD = 998244353; const int MAXN = 1e5+5; int n,m,pre[MAXN],head[MAXN],Le[355],Ri[355],bel[MAXN]; int tag[355],tot[355],p[MAXN],sum[355][MAXN],a[MAXN],dp[MAXN]; void Make_Group() { int len=sqrt(n),tot=0; for(R int i=1;i*len<=n;++i) { Le[i]=(i-1)*len+1; Ri[i]=i*len; tot++; } if(Ri[tot]<n) tot++,Le[tot]=Ri[tot-1]+1,Ri[tot]=n; for(R int i=1;i<=tot;++i) for(R int j=Le[i];j<=Ri[i];++j) bel[j]=i; } void change(int l,int r,int x) { if(bel[l]==bel[r]) { for(R int i=l;i<=r;++i) { sum[bel[l]][p[i]]=(sum[bel[l]][p[i]]-dp[i-1]+MOD)%MOD; if(p[i]+tag[bel[l]]<=m) tot[bel[l]]=(tot[bel[l]]-dp[i-1]+MOD)%MOD; p[i]+=x; sum[bel[l]][p[i]]=(sum[bel[l]][p[i]]+dp[i-1])%MOD; if(p[i]+tag[bel[l]]<=m) tot[bel[l]]=(tot[bel[l]]+dp[i-1])%MOD; } } else { for(R int i=l;i<=Ri[bel[l]];++i) { sum[bel[l]][p[i]]=(sum[bel[l]][p[i]]-dp[i-1]+MOD)%MOD; if(p[i]+tag[bel[l]]<=m) tot[bel[l]]=(tot[bel[l]]-dp[i-1]+MOD)%MOD; p[i]+=x; sum[bel[l]][p[i]]=(sum[bel[l]][p[i]]+dp[i-1])%MOD; if(p[i]+tag[bel[l]]<=m) tot[bel[l]]=(tot[bel[l]]+dp[i-1])%MOD; } for(R int i=Le[bel[r]];i<=r;++i) { sum[bel[r]][p[i]]=(sum[bel[r]][p[i]]-dp[i-1]+MOD)%MOD; if(p[i]+tag[bel[r]]<=m) tot[bel[r]]=(tot[bel[r]]-dp[i-1]+MOD)%MOD; p[i]+=x; sum[bel[r]][p[i]]=(sum[bel[r]][p[i]]+dp[i-1])%MOD; if(p[i]+tag[bel[r]]<=m) tot[bel[r]]=(tot[bel[r]]+dp[i-1])%MOD; } for(R int i=bel[l]+1;i<bel[r];++i) { if(x==-1&&tag[i]<=m) tot[i]=(tot[i]+sum[i][m-tag[i]+1])%MOD; if(x==1&&tag[i]<m) tot[i]=(tot[i]-sum[i][m-tag[i]]+MOD)%MOD; tag[i]+=x; } } } ll query(int l,int r) { ll ans=0; if(bel[l]==bel[r]) { for(R int i=l;i<=r;++i) if(p[i]+tag[bel[l]]<=m) ans+=dp[i-1],ans%=MOD; } else { for(R int i=l;i<=Ri[bel[l]];++i) if(p[i]+tag[bel[l]]<=m) ans+=dp[i-1],ans%=MOD; for(R int i=Le[bel[r]];i<=r;++i) if(p[i]+tag[bel[r]]<=m) ans+=dp[i-1],ans%=MOD; for(R int i=bel[l]+1;i<bel[r];++i) ans+=tot[i],ans%MOD; } return ans; } int main() { scanf("%d %d",&n,&m); for(R int i=1;i<=n;++i) scanf("%d",&a[i]); dp[0]=1; Make_Group(); for(R int i=1;i<=n;++i) { pre[i]=head[a[i]]; head[a[i]]=i; p[i]=1; sum[bel[i]][p[i]]=(sum[bel[i]][p[i]]+dp[i-1])%MOD; if(p[i]<=m) tot[bel[i]]=(tot[bel[i]]+dp[i-1])%MOD; if(pre[i]+1<=i-1)change(pre[i]+1,i-1,1); if(pre[i]) change(pre[pre[i]]+1,pre[i],-1); dp[i]=query(1,i)%MOD; } printf("%d",dp[n]); return 0; }
1129_D. Isolation
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once. Since the answer can be large, find it modulo 998 244 353. Input The first line contains two space-separated integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the number of elements in the array a and the restriction from the statement. The following line contains n space-separated integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n) β€” elements of the array a. Output The first and only line contains the number of ways to divide an array a modulo 998 244 353. Examples Input 3 1 1 1 2 Output 3 Input 5 2 1 1 2 1 3 Output 14 Input 5 5 1 2 3 4 5 Output 16 Note In the first sample, the three possible divisions are as follows. * [[1], [1], [2]] * [[1, 1], [2]] * [[1, 1, 2]] Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
{ "input": [ "5 5\n1 2 3 4 5\n", "3 1\n1 1 2\n", "5 2\n1 1 2 1 3\n" ], "output": [ "16", "3", "14" ] }
{ "input": [ "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n", "100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n", "10 1\n4 2 9 2 1 4 4 1 4 10\n", "100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n", "1 1\n1\n", "100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n", "2 2\n2 1\n", "2 1\n2 2\n" ], "output": [ "16", "726975503", "24", "244208148", "1", "1", "2", "2" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> #pragma GCC optimize(2) #pragma GCC optimize(3) using namespace std; int a1, a2, x1, x2, j, k, g[2000000], mi[2000000], ma[2000000]; int n, m, p[100005], num[100005], wen[100005], bz[2000000], la[2000000]; int s[2000000], f[100005]; int max1(int a, int b) { if (a > b) return a; return b; } int min1(int a, int b) { if (a < b) return a; return b; } void built(int x, int l, int r, int xx, int ss) { bz[x] = 1; int mid = (l + r) / 2; if (l == r) { s[x] = ss; mi[x] = ma[x] = 1; return; } if (xx <= mid) built(x * 2, l, mid, xx, ss); if (xx > mid) built(x * 2 + 1, mid + 1, r, xx, ss); s[x] = (s[x * 2] + s[x * 2 + 1]) % 998244353; ma[x] = max1(ma[x * 2], ma[x * 2 + 1]) + g[x]; mi[x] = min1(mi[x * 2], mi[x * 2 + 1]) + g[x]; } void make(int x, int l, int r, int l1, int r1, int sp) { if (l1 > r1) return; int mid = (l + r) / 2; bz[x] = 1; if ((l == l1) && (r == r1)) { if (l == r) { mi[x] = ma[x] = ma[x] + sp; return; } g[x] = g[x] + sp; bz[x] = 2; ma[x] = max1(ma[x * 2], ma[x * 2 + 1]) + g[x]; mi[x] = min1(mi[x * 2], mi[x * 2 + 1]) + g[x]; return; } if (r1 <= mid) { make(x * 2, l, mid, l1, r1, sp); ma[x] = max1(ma[x * 2], ma[x * 2 + 1]) + g[x]; mi[x] = min1(mi[x * 2], mi[x * 2 + 1]) + g[x]; return; } if (l1 > mid) { make(x * 2 + 1, mid + 1, r, l1, r1, sp); ma[x] = max1(ma[x * 2], ma[x * 2 + 1]) + g[x]; mi[x] = min1(mi[x * 2], mi[x * 2 + 1]) + g[x]; return; } make(x * 2, l, mid, l1, mid, sp); make(x * 2 + 1, mid + 1, r, mid + 1, r1, sp); ma[x] = max1(ma[x * 2], ma[x * 2 + 1]) + g[x]; mi[x] = min1(mi[x * 2], mi[x * 2 + 1]) + g[x]; } int find(int x, int l, int r, int ss) { int mid = (l + r) / 2; if (mi[x] > ss) return 0; if (bz[x] == 0) return la[x]; if (ma[x] <= ss) { la[x] = s[x]; return la[x]; } if (bz[x] == 2) { if (l != r) { bz[x * 2] = 2; bz[x * 2 + 1] = 2; } } bz[x] = 0; if (l == r) { la[x] = 0; return la[x]; } ss = ss - g[x]; la[x] = (find(x * 2, l, mid, ss) + find(x * 2 + 1, mid + 1, r, ss)) % 998244353; return la[x]; } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) { scanf("%d", &p[i]); } f[0] = 1; for (int i = 1; i <= n; i++) { built(1, 1, n, i, f[i - 1]); a1 = num[p[i]]; a2 = wen[a1]; wen[i] = num[p[i]]; num[p[i]] = i; a1++; a2++; make(1, 1, n, a1, i - 1, 1); make(1, 1, n, a2, a1 - 1, -1); f[i] = find(1, 1, n, m) % 998244353; } printf("%d\n", f[n]); return 0; }
1129_D. Isolation
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once. Since the answer can be large, find it modulo 998 244 353. Input The first line contains two space-separated integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the number of elements in the array a and the restriction from the statement. The following line contains n space-separated integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n) β€” elements of the array a. Output The first and only line contains the number of ways to divide an array a modulo 998 244 353. Examples Input 3 1 1 1 2 Output 3 Input 5 2 1 1 2 1 3 Output 14 Input 5 5 1 2 3 4 5 Output 16 Note In the first sample, the three possible divisions are as follows. * [[1], [1], [2]] * [[1, 1], [2]] * [[1, 1, 2]] Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
{ "input": [ "5 5\n1 2 3 4 5\n", "3 1\n1 1 2\n", "5 2\n1 1 2 1 3\n" ], "output": [ "16", "3", "14" ] }
{ "input": [ "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n", "100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n", "10 1\n4 2 9 2 1 4 4 1 4 10\n", "100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n", "1 1\n1\n", "100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n", "2 2\n2 1\n", "2 1\n2 2\n" ], "output": [ "16", "726975503", "24", "244208148", "1", "1", "2", "2" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10, K = 320; const long long P = 998244353; int m; int a[N], pre[N]; map<int, int> mp; long long dp[N]; namespace blocks { int b[N], lft[K], rgt[K]; inline void build(int n) { int k = (int)sqrt(n); for (int i = 1; i <= n; ++i) b[i] = (i - 1) / k + 1; b[0] = 0; int num = (int)ceil(n * 1.0 / k); for (int i = 1; i < num; ++i) lft[i] = (i - 1) * k + 1, rgt[i] = i * k; lft[num] = rgt[num - 1] + 1, rgt[num] = n; lft[0] = rgt[0] = 0; } int val[N], tag[N]; long long ans[K][N], Ans[K]; inline void modify(int l, int r, int k, int p) { for (int i = l; i <= r; ++i) { (ans[k][val[i] + tag[k]] += P - dp[i]) %= P; if (val[i] + tag[k] <= m) (Ans[k] += P - dp[i]) %= P; val[i] += p; (ans[k][val[i] + tag[k]] += dp[i]) %= P; if (val[i] + tag[k] <= m) (Ans[k] += dp[i]) %= P; } } inline void Add(int l, int r, int p) { if (l > r) return; if (b[l] == b[r]) { modify(l, r, b[l], p); return; } modify(l, rgt[b[l]], b[l], p); for (int k = b[l] + 1; k < b[r]; ++k) { if (p == 1) (Ans[k] += P - ans[k][m]) %= P; else (Ans[k] += ans[k][m + 1]) %= P; tag[k] += p; } modify(lft[b[r]], r, b[r], p); } } // namespace blocks using namespace blocks; int main() { int n; scanf("%d%d", &n, &m); for (int i = 1; i <= n; ++i) { scanf("%d", &a[i]); pre[i] = mp[a[i]]; mp[a[i]] = i; } build(n); dp[0] = ans[0][0] = Ans[0] = 1; for (int i = 1; i <= n; ++i) { Add(pre[i], i - 1, 1); Add(pre[pre[i]], pre[i] - 1, -1); (dp[i] += Ans[b[i]]) %= P; for (int k = 0; k < b[i]; ++k) (dp[i] += Ans[k]) %= P; (ans[b[i]][val[i] + tag[b[i]]] += dp[i]) %= P; if (val[i] + tag[b[i]] <= m) (Ans[b[i]] += dp[i]) %= P; } printf("%lld\n", dp[n]); return 0; }
1129_D. Isolation
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once. Since the answer can be large, find it modulo 998 244 353. Input The first line contains two space-separated integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the number of elements in the array a and the restriction from the statement. The following line contains n space-separated integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n) β€” elements of the array a. Output The first and only line contains the number of ways to divide an array a modulo 998 244 353. Examples Input 3 1 1 1 2 Output 3 Input 5 2 1 1 2 1 3 Output 14 Input 5 5 1 2 3 4 5 Output 16 Note In the first sample, the three possible divisions are as follows. * [[1], [1], [2]] * [[1, 1], [2]] * [[1, 1, 2]] Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
{ "input": [ "5 5\n1 2 3 4 5\n", "3 1\n1 1 2\n", "5 2\n1 1 2 1 3\n" ], "output": [ "16", "3", "14" ] }
{ "input": [ "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n", "100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n", "10 1\n4 2 9 2 1 4 4 1 4 10\n", "100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n", "1 1\n1\n", "100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n", "2 2\n2 1\n", "2 1\n2 2\n" ], "output": [ "16", "726975503", "24", "244208148", "1", "1", "2", "2" ] }
IN-CORRECT
cpp
#pragma GCC optimize ("O3") #pragma GCC target ("sse4") #include <bits/stdc++.h> #include <ext/pb_ds/tree_policy.hpp> #include <ext/pb_ds/assoc_container.hpp> #include <ext/rope> using namespace std; using namespace __gnu_pbds; using namespace __gnu_cxx; typedef long long ll; typedef long double ld; typedef complex<ld> cd; typedef pair<int, int> pi; typedef pair<ll,ll> pl; typedef pair<ld,ld> pd; typedef vector<int> vi; typedef vector<ld> vd; typedef vector<ll> vl; typedef vector<pi> vpi; typedef vector<pl> vpl; typedef vector<cd> vcd; template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>; #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define F0R(i, a) for (int i = 0; i < (a); i++) #define FORd(i,a,b) for (int i = (b)-1; i >= (a); i--) #define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--) #define trav(a, x) for (auto& a : x) #define mp make_pair #define pb push_back #define f first #define s second #define lb lower_bound #define ub upper_bound #define sz(x) (int)x.size() #define beg(x) x.begin() #define en(x) x.end() #define all(x) beg(x), en(x) #define resz resize const int MOD = 998244353; const ll INF = 1e18; const int MX = 320*320; const ld PI = 4*atan((ld)1); template<class T> void ckmin(T &a, T b) { a = min(a, b); } template<class T> void ckmax(T &a, T b) { a = max(a, b); } namespace input { template<class T> void re(complex<T>& x); template<class T1, class T2> void re(pair<T1,T2>& p); template<class T> void re(vector<T>& a); template<class T, size_t SZ> void re(array<T,SZ>& a); template<class T> void re(T& x) { cin >> x; } void re(double& x) { string t; re(t); x = stod(t); } void re(ld& x) { string t; re(t); x = stold(t); } template<class Arg, class... Args> void re(Arg& first, Args&... rest) { re(first); re(rest...); } template<class T> void re(complex<T>& x) { T a,b; re(a,b); x = cd(a,b); } template<class T1, class T2> void re(pair<T1,T2>& p) { re(p.f,p.s); } template<class T> void re(vector<T>& a) { F0R(i,sz(a)) re(a[i]); } template<class T, size_t SZ> void re(array<T,SZ>& a) { F0R(i,SZ) re(a[i]); } } using namespace input; namespace output { template<class T1, class T2> void pr(const pair<T1,T2>& x); template<class T, size_t SZ> void pr(const array<T,SZ>& x); template<class T> void pr(const vector<T>& x); template<class T> void pr(const set<T>& x); template<class T1, class T2> void pr(const map<T1,T2>& x); template<class T> void pr(const T& x) { cout << x; } template<class Arg, class... Args> void pr(const Arg& first, const Args&... rest) { pr(first); pr(rest...); } template<class T1, class T2> void pr(const pair<T1,T2>& x) { pr("{",x.f,", ",x.s,"}"); } template<class T> void prContain(const T& x) { pr("{"); bool fst = 1; trav(a,x) pr(!fst?", ":"",a), fst = 0; pr("}"); } template<class T, size_t SZ> void pr(const array<T,SZ>& x) { prContain(x); } template<class T> void pr(const vector<T>& x) { prContain(x); } template<class T> void pr(const set<T>& x) { prContain(x); } template<class T1, class T2> void pr(const map<T1,T2>& x) { prContain(x); } template<class Arg> void ps(const Arg& first) { pr(first,"\n"); } // print w/ spaces template<class Arg, class... Args> void ps(const Arg& first, const Args&... rest) { pr(first," "); ps(rest...); } } using namespace output; namespace io { void setIn(string s) { freopen(s.c_str(),"r",stdin); } void setOut(string s) { freopen(s.c_str(),"w",stdout); } void setIO(string s = "") { ios_base::sync_with_stdio(0); cin.tie(0); // fast I/O if (sz(s)) { setIn(s+".in"), setOut(s+".out"); } // for USACO } } using namespace io; namespace modOp { int ad(int a, int b, int mod = MOD) { return (a+b)%mod; } int sub(int a, int b, int mod = MOD) { return (a-b+mod)%mod; } int mul(int a, int b, int mod = MOD) { return (ll)a*b%mod; } int AD(int& a, int b, int mod = MOD) { return a = ad(a,b,mod); } int SUB(int& a, int b, int mod = MOD) { return a = sub(a,b,mod); } int MUL(int& a, int b, int mod = MOD) { return a = mul(a,b,mod); } int po (int b, int p, int mod = MOD) { return !p?1:mul(po(mul(b,b,mod),p/2,mod),p&1?b:1,mod); } int inv (int b, int mod = MOD) { return po(b,mod-2,mod); } int invGeneral(int a, int b) { // 0 < a < b, gcd(a,b) = 1 if (a == 0) return b == 1 ? 0 : -1; int x = invGeneral(b%a,a); return x == -1 ? -1 : ((1-(ll)b*x)/a+b)%b; } } using namespace modOp; const int BUC = 320; int n,k,cur; int sqr[BUC],val[MX],u[BUC][MX]; int ans[MX]; vi a, oc[MX]; int get(int a, int b) { if (b-sqr[a] < 0 || b-sqr[a] >= MX) return 0; return u[a][b-sqr[a]]; // b-sqr[a] } void upd(int len, int num) { int b = len/BUC; FOR(i,b*BUC,(b+1)*BUC) { u[b][val[i]] = 0; val[i] += sqr[b]; if (i < len) { val[i] += num; if (num == -1) { AD(cur,ans[i]*(val[i] == k)); } else { SUB(cur,ans[i]*(val[i] == k+1)); } } } sqr[b] = 0; FOR(i,b*BUC,(b+1)*BUC) AD(u[b][val[i]],ans[i]); F0R(i,len/BUC) { sqr[i] += num; if (num == -1) AD(cur,get(i,k));// add the ones now equal to k else SUB(cur,get(i,k+1)); } } void recalc(int ind) { int b = ind/BUC; assert(!sqr[b]); AD(cur,ans[ind]); AD(u[b][val[ind]],ans[ind]); } int main() { // you should actually read the stuff at the bottom setIO(); re(n,k); FOR(i,1,n+1) oc[i].pb(0); ans[0] = 1; recalc(0); FOR(i,1,n+1) { int x; re(x); oc[x].pb(i); if (sz(oc[x]) > 2) upd(oc[x][sz(oc[x])-3],1); upd(oc[x][sz(oc[x])-2],-1); upd(oc[x][sz(oc[x])-2],-1); upd(oc[x][sz(oc[x])-1],1); ans[i] = cur; recalc(i); // -1 to oc[x][sz(oc[x])-2]-1 // +1 to oc[x].back()-1 } ps(ans[n]); // you should actually read the stuff at the bottom } /* stuff you should look for * int overflow, array bounds * special cases (n=1?), set tle * do smth instead of nothing and stay organized */
1129_D. Isolation
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once. Since the answer can be large, find it modulo 998 244 353. Input The first line contains two space-separated integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the number of elements in the array a and the restriction from the statement. The following line contains n space-separated integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n) β€” elements of the array a. Output The first and only line contains the number of ways to divide an array a modulo 998 244 353. Examples Input 3 1 1 1 2 Output 3 Input 5 2 1 1 2 1 3 Output 14 Input 5 5 1 2 3 4 5 Output 16 Note In the first sample, the three possible divisions are as follows. * [[1], [1], [2]] * [[1, 1], [2]] * [[1, 1, 2]] Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
{ "input": [ "5 5\n1 2 3 4 5\n", "3 1\n1 1 2\n", "5 2\n1 1 2 1 3\n" ], "output": [ "16", "3", "14" ] }
{ "input": [ "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n", "100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n", "10 1\n4 2 9 2 1 4 4 1 4 10\n", "100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n", "1 1\n1\n", "100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n", "2 2\n2 1\n", "2 1\n2 2\n" ], "output": [ "16", "726975503", "24", "244208148", "1", "1", "2", "2" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2") using namespace std; using ll = long long; int a[101001]; int b[101010]; int dp[101010]; int p1[101010]; int p2[101010]; int ans = 0; int n, k; void add(int* __restrict a, int* __restrict dp, int n) { for (int i = 0; i < n; ++i) { a[i]++; ans -= a[i] == 0 ? dp[i] : 0; ans += ans < 0 ? 998244353 : 0; } } void sub(int* __restrict a, int* __restrict dp, int n) { for (int i = 0; i < n; ++i) { ans += a[i] == 0 ? dp[i] : 0; a[i]--; ans -= ans >= 998244353 ? 998244353 : 0; } } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> k; for (int i = 0; i < n; ++i) { b[i] = -k - 1; cin >> a[i]; a[i] = 1 + rand() % n; --a[i]; p1[i] = p2[i] = -1; } dp[0] = 1; ans = 1; for (int i = 0; i < n; ++i) { if (i) ans = (ans + ans) % 998244353; int x = a[i]; sub(b + p2[x] + 1, dp + p2[x] + 1, p1[x] - p2[x]); add(b + p1[x] + 1, dp + p1[x] + 1, i - p1[x]); dp[i + 1] = ans; p2[x] = p1[x]; p1[x] = i; } cout << ans << endl; return 0; }
1129_D. Isolation
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once. Since the answer can be large, find it modulo 998 244 353. Input The first line contains two space-separated integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the number of elements in the array a and the restriction from the statement. The following line contains n space-separated integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n) β€” elements of the array a. Output The first and only line contains the number of ways to divide an array a modulo 998 244 353. Examples Input 3 1 1 1 2 Output 3 Input 5 2 1 1 2 1 3 Output 14 Input 5 5 1 2 3 4 5 Output 16 Note In the first sample, the three possible divisions are as follows. * [[1], [1], [2]] * [[1, 1], [2]] * [[1, 1, 2]] Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
{ "input": [ "5 5\n1 2 3 4 5\n", "3 1\n1 1 2\n", "5 2\n1 1 2 1 3\n" ], "output": [ "16", "3", "14" ] }
{ "input": [ "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n", "100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n", "10 1\n4 2 9 2 1 4 4 1 4 10\n", "100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n", "1 1\n1\n", "100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n", "2 2\n2 1\n", "2 1\n2 2\n" ], "output": [ "16", "726975503", "24", "244208148", "1", "1", "2", "2" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10; const int block = 350; int add(int x, int y) { x += y; if (x >= 998244353) x -= 998244353; return x; } int n, lim, a[N], b[N], last[N], lastlast[N], bel[N], f[N]; struct BlockType { int sumc, sz, l, r; int presum[(block << 1) + 10]; } tn[N / block]; void mdy(int pos, int v) { int L = tn[bel[pos]].l, R = tn[bel[pos]].r, sum = 0; memset(tn[bel[pos]].presum, 0, sizeof tn[bel[pos]].presum); b[pos] = v; for (int i = R; i >= L; i--) { sum += b[i]; tn[bel[pos]].presum[sum + block] += f[i - 1]; } tn[bel[pos]].sumc = sum; for (int i = -tn[bel[pos]].sz + 1; i <= tn[bel[pos]].sz; i++) { tn[bel[pos]].presum[i + block] += tn[bel[pos]].presum[i - 1 + block]; } } int main() { scanf("%d%d", &n, &lim); for (int i = 1; i <= n; i++) scanf("%d", a + i), bel[i] = ((i - 1) / block) + 1; for (int i = 1; i <= n; i++) { tn[bel[i]].sz++; if (!tn[bel[i]].l) tn[bel[i]].l = i; tn[bel[i]].r = i; } f[0] = 1; for (int i = 1; i <= n; i++) { if (lastlast[a[i]]) mdy(lastlast[a[i]], 0); if (last[a[i]]) mdy(last[a[i]], -1); mdy(i, 1); lastlast[a[i]] = last[a[i]]; last[a[i]] = i; int t = 0, s = 0; for (int j = i; j >= tn[bel[i]].l; j--) { t += b[j]; if (t <= lim) s = add(s, f[j - 1]); } for (int j = bel[i] - 1; j >= 1; j--) { int delta = lim - t; if (delta >= -tn[j].sz) { s = add(s, tn[j].presum[min(delta, tn[j].sz) + block]); } t += tn[j].sumc; } f[i] = s; mdy(i, 1); } cout << f[n]; return 0; }
1129_D. Isolation
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once. Since the answer can be large, find it modulo 998 244 353. Input The first line contains two space-separated integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the number of elements in the array a and the restriction from the statement. The following line contains n space-separated integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n) β€” elements of the array a. Output The first and only line contains the number of ways to divide an array a modulo 998 244 353. Examples Input 3 1 1 1 2 Output 3 Input 5 2 1 1 2 1 3 Output 14 Input 5 5 1 2 3 4 5 Output 16 Note In the first sample, the three possible divisions are as follows. * [[1], [1], [2]] * [[1, 1], [2]] * [[1, 1, 2]] Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
{ "input": [ "5 5\n1 2 3 4 5\n", "3 1\n1 1 2\n", "5 2\n1 1 2 1 3\n" ], "output": [ "16", "3", "14" ] }
{ "input": [ "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n", "100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n", "10 1\n4 2 9 2 1 4 4 1 4 10\n", "100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n", "1 1\n1\n", "100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n", "2 2\n2 1\n", "2 1\n2 2\n" ], "output": [ "16", "726975503", "24", "244208148", "1", "1", "2", "2" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ull = uint64_t; using ll = int64_t; using ld = long double; const int N = 200228; #ifndef BZ const int SQ = 160; #else const int SQ = 2; #endif const int RSQ = N / SQ; vector<int> a[N]; ll f[N]; int g[N]; const int INF = N * 5; const int MOD = 998244353; int n, k; ll sum; pair<int, ll> vec[SQ]; struct Block { int c; int idx; ll csum; pair<int, ll> a[SQ + 3]; int p; void mdf(int x) { if (x == -1) { if (a[p - 1].first == k - --c) { csum += a[--p].second; csum %= MOD; sum += a[p].second; sum %= MOD; } } else { if (a[p].first == k - c++) { csum -= a[p].second; csum %= MOD; sum -= a[p++].second; sum %= MOD; } } } void build() { int m = idx * SQ; int n = 0; a[n] = { -INF, 0 }; sum -= csum; csum = 0; for (int i = 0; i < SQ; ++i) { vec[i] = { g[m + i], f[m + i] }; } sort(vec, vec + SQ, greater<>{}); for (auto val: vec) { csum += val.second; csum %= MOD; if (val.first != a[n].first) { a[++n] = val; } else { a[n].second += val.second; a[n].second %= MOD; } } a[++n] = { INF, 0 }; p = 1; while (a[p].first + c > k) { csum -= a[p++].second; csum %= MOD; } sum = (sum + csum) % MOD; } }; Block b[RSQ]; void mdf(int l, int r, int x) { if (l == r) { return; } int lg = l / SQ + 1; int rg = (r - 1) / SQ; int lx = lg * SQ; int rx = rg * SQ; for (int i = lg; i < rg; ++i) { b[i].mdf(x); } if (lg < rg) { for (int i = l; i < lx; ++i) { g[i] += x; } for (int i = rx; i < r; ++i) { g[i] += x; } } else { for (int i = l; i < r; ++i) { g[i] += x; } } b[lg - 1].build(); b[rg].build(); } int main() { #ifdef BZ freopen("input.txt", "r", stdin); #endif ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cout.setf(ios::fixed); cout.precision(20); for (int i = 0; i < RSQ; ++i) { b[i].idx = i; b[i].build(); } cin >> n >> k; for (int i = 1; i <= n; ++i) { a[i] = { 0, 0 }; } f[0] = 1; for (int i = 0; i < n; ++i) { b[i / SQ].build(); int x; cin >> x; int j = i + 1; mdf(a[x].back(), j, 1); mdf(a[x][a[x].size() - 2], a[x].back(), -1); a[x].push_back(j); f[j] = sum; } cout << (f[n] + MOD) % MOD << "\n"; }
1129_D. Isolation
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once. Since the answer can be large, find it modulo 998 244 353. Input The first line contains two space-separated integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the number of elements in the array a and the restriction from the statement. The following line contains n space-separated integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n) β€” elements of the array a. Output The first and only line contains the number of ways to divide an array a modulo 998 244 353. Examples Input 3 1 1 1 2 Output 3 Input 5 2 1 1 2 1 3 Output 14 Input 5 5 1 2 3 4 5 Output 16 Note In the first sample, the three possible divisions are as follows. * [[1], [1], [2]] * [[1, 1], [2]] * [[1, 1, 2]] Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
{ "input": [ "5 5\n1 2 3 4 5\n", "3 1\n1 1 2\n", "5 2\n1 1 2 1 3\n" ], "output": [ "16", "3", "14" ] }
{ "input": [ "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n", "100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n", "10 1\n4 2 9 2 1 4 4 1 4 10\n", "100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n", "1 1\n1\n", "100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n", "2 2\n2 1\n", "2 1\n2 2\n" ], "output": [ "16", "726975503", "24", "244208148", "1", "1", "2", "2" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int Pow(int a, int b, int md, int ans = 1) { for (; b; b >>= 1, a = a * a % md) if (b & 1) ans = ans * a % md; return ans; } const int MAXN = 1e5 + 10; const int SQ = 512; const int tof = SQ - 1; const int INF = 1e9; const int MOD = 998244353; int M[MAXN], ps[SQ][MAXN], lst[2][MAXN], dp[MAXN], lazy[2][MAXN], A[MAXN], n, k; void add(int &x, int y) { x = ((int)x + (int)y % MOD + MOD) % MOD; } void update(int l, int r, int x) { for (int i = l; i < r; i++) { if ((i & tof) == 0 && i + SQ <= r) lazy[1][i / SQ] += x, i += SQ - 1; else { if (M[i / SQ] == 0) { lazy[0][i] += x; continue; } if (~x) add(ps[i / SQ][lazy[0][i]], -dp[i]); else add(ps[i / SQ][lazy[0][i] - 1], dp[i]); lazy[0][i] += x; } } } int get(int r, int res = 0) { for (int i = 0; i < r; i++) { if ((i & tof) == 0 && i + SQ <= r && lazy[1][i / SQ] <= k) add(res, ps[i / SQ][k - lazy[1][i / SQ]]), i += SQ - 1; else if (lazy[0][i] + lazy[1][i / SQ] <= k) add(res, dp[i]); } return res; } void relax(int p) { M[p / SQ] = 1; for (int i = p / SQ * SQ; i < p / SQ * SQ + SQ; i++) add(ps[p / SQ][lazy[0][i]], dp[i]); for (int i = 1; i <= n; i++) { add(ps[p / SQ][i], ps[p / SQ][i - 1]); } } int main() { scanf("%d%d", &n, &k); dp[0] = 1; for (int i = 1; i <= n; i++) { scanf("%d", &A[i]); } for (int i = 1; i <= n; i++) { update(lst[0][A[i]], lst[1][A[i]], -1); update(lst[1][A[i]], i, 1); dp[i] = get(i); if ((i & tof) == tof) relax(i); lst[0][A[i]] = lst[1][A[i]]; lst[1][A[i]] = i; } printf("%d\n", dp[n]); return 0; }
1129_D. Isolation
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once. Since the answer can be large, find it modulo 998 244 353. Input The first line contains two space-separated integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the number of elements in the array a and the restriction from the statement. The following line contains n space-separated integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n) β€” elements of the array a. Output The first and only line contains the number of ways to divide an array a modulo 998 244 353. Examples Input 3 1 1 1 2 Output 3 Input 5 2 1 1 2 1 3 Output 14 Input 5 5 1 2 3 4 5 Output 16 Note In the first sample, the three possible divisions are as follows. * [[1], [1], [2]] * [[1, 1], [2]] * [[1, 1, 2]] Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
{ "input": [ "5 5\n1 2 3 4 5\n", "3 1\n1 1 2\n", "5 2\n1 1 2 1 3\n" ], "output": [ "16", "3", "14" ] }
{ "input": [ "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n", "100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n", "10 1\n4 2 9 2 1 4 4 1 4 10\n", "100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n", "1 1\n1\n", "100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n", "2 2\n2 1\n", "2 1\n2 2\n" ], "output": [ "16", "726975503", "24", "244208148", "1", "1", "2", "2" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long INF = 0x3f3f3f3f; const int N = 2e5 + 10; const int M = 11; const double PI = acos(-1.0); const int blo = 320; inline void add(int &a, int b) { a += b; if (a >= 998244353) a -= 998244353; } int v[N]; int cnt[blo][(blo << 1) + 10], sum[blo]; int vc[N], w[N], pre[N], l[N], dd[3] = {1, -1, 0}; void up(int x, int y) { vc[x] = y; int id = x / blo; memset(cnt[id], 0, sizeof(cnt[id])); sum[id] = 0; for (int i = blo - 1, be = id * blo; i >= 0; --i) { add(cnt[id][blo + sum[id]], w[be + i]); sum[id] += vc[be + i]; } for (int i = 1; i <= blo * 2; ++i) add(cnt[id][i], cnt[id][i - 1]); } void qu(int x, int k) { int id = x / blo; for (int i = id; i >= 0; --i) { if (k + blo >= 0) add(w[x], cnt[i][blo + k]); k -= sum[i]; } } int main() { int n, k; scanf("%d%d", &n, &k); for (int i = 1; i <= n; ++i) scanf("%d", &v[i]); w[0] = 1; for (int i = 1; i <= n; ++i) { pre[i] = l[v[i]], l[v[i]] = i; for (int j = 0, now = i; j < 3 && now; ++j, now = pre[now]) up(now, dd[j]); qu(i, k); } printf("%d\n", w[n]); return 0; }
1129_D. Isolation
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once. Since the answer can be large, find it modulo 998 244 353. Input The first line contains two space-separated integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the number of elements in the array a and the restriction from the statement. The following line contains n space-separated integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n) β€” elements of the array a. Output The first and only line contains the number of ways to divide an array a modulo 998 244 353. Examples Input 3 1 1 1 2 Output 3 Input 5 2 1 1 2 1 3 Output 14 Input 5 5 1 2 3 4 5 Output 16 Note In the first sample, the three possible divisions are as follows. * [[1], [1], [2]] * [[1, 1], [2]] * [[1, 1, 2]] Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
{ "input": [ "5 5\n1 2 3 4 5\n", "3 1\n1 1 2\n", "5 2\n1 1 2 1 3\n" ], "output": [ "16", "3", "14" ] }
{ "input": [ "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n", "100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n", "10 1\n4 2 9 2 1 4 4 1 4 10\n", "100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n", "1 1\n1\n", "100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n", "2 2\n2 1\n", "2 1\n2 2\n" ], "output": [ "16", "726975503", "24", "244208148", "1", "1", "2", "2" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <class X, class Y> void amax(X &x, const Y &y) { if (x < y) x = y; } template <class X, class Y> void amin(X &x, const Y &y) { if (x > y) x = y; } const int INF = 1e9 + 10; const long long INFL = (long long)1e18 + 10; const int MAX = 1e5 + 10; const int MAXS = 320; const int MOD = 998244353; int n, k, sn; int a[MAX], prv[MAX], last[MAX], dp[MAX], q[MAXS][2 * MAXS]; int c[MAX], sc[MAXS]; int gb(int i) { return (i - 1) / sn + 1; } int bl(int i) { if (i <= n / sn) return sn; return n - (n / sn) * sn; } pair<int, int> itv(int i) { return make_pair((i - 1) * sn + 1, min(i * sn, n)); } int add(int a, int b) { return (a + b) % MOD; } int gq(int i, int j) { return q[i][j + MAXS]; } void uq(int i, int j, int v) { q[i][j + MAXS] = add(q[i][j + MAXS], v); } void calcq(int i) { memset(q[i], 0, 2 * MAXS * sizeof(q[i][0])); int l = itv(i).first, r = itv(i).second; int t = 0; for (int j = r; j >= l; j--) { t += c[j]; if (t < -bl(i)) uq(i, -bl(i), dp[j - 1]); else if (t <= bl(i)) uq(i, t, dp[j - 1]); } for (int j = -bl(i) + 1; j <= bl(i); j++) uq(i, j, gq(i, j - 1)); sc[i] = t; } void process() { cin >> n >> k; for (int i = 1; i <= n; i++) cin >> a[i]; sn = max((int)sqrt(n), 1); for (int i = 1; i <= n; i++) { prv[i] = last[a[i]]; last[a[i]] = i; } dp[0] = 1; for (int i = 1; i <= n; i++) { if (i > 0 && gb(i) > gb(i - 1)) calcq(gb(i - 1)); if (prv[prv[i]] != 0) { c[prv[prv[i]]] = 0; if (gb(prv[prv[i]]) < gb(i)) calcq(gb(prv[prv[i]])); } if (prv[i] != 0) { c[prv[i]] = -1; if (gb(prv[i]) < gb(i)) calcq(gb(prv[i])); } c[i] = 1; int l = itv(gb(i)).first; int t = 0; for (int j = i; j >= l; j--) { t += c[j]; if (t <= k) dp[i] = add(dp[i], dp[j - 1]); } int x = k - t; for (int j = gb(i) - 1; j >= 1; j--) { if (x >= -bl(j)) { if (x <= bl(j)) dp[i] = add(dp[i], gq(j, x)); else dp[i] = add(dp[i], gq(j, bl(j))); } x += sc[j]; } } cout << dp[n]; } int main() { ios_base::sync_with_stdio(false); process(); }
1129_D. Isolation
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once. Since the answer can be large, find it modulo 998 244 353. Input The first line contains two space-separated integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the number of elements in the array a and the restriction from the statement. The following line contains n space-separated integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n) β€” elements of the array a. Output The first and only line contains the number of ways to divide an array a modulo 998 244 353. Examples Input 3 1 1 1 2 Output 3 Input 5 2 1 1 2 1 3 Output 14 Input 5 5 1 2 3 4 5 Output 16 Note In the first sample, the three possible divisions are as follows. * [[1], [1], [2]] * [[1, 1], [2]] * [[1, 1, 2]] Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
{ "input": [ "5 5\n1 2 3 4 5\n", "3 1\n1 1 2\n", "5 2\n1 1 2 1 3\n" ], "output": [ "16", "3", "14" ] }
{ "input": [ "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n", "100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n", "10 1\n4 2 9 2 1 4 4 1 4 10\n", "100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n", "1 1\n1\n", "100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n", "2 2\n2 1\n", "2 1\n2 2\n" ], "output": [ "16", "726975503", "24", "244208148", "1", "1", "2", "2" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int K; void add(int &x, int y) { x = x + y; if (x >= 998244353) x -= 998244353; } int val[100008]; int dp[100008]; int bel[100008]; int n; struct oneblockforall { vector<int> inv; int cnt[100008]; int tag; int all; int l, r; void add(int &x, int y) { x = x + y; if (x >= 998244353) x -= 998244353; } void release() { for (int i = 0; i < inv.size(); i++) cnt[i] = 0; inv.clear(); for (int i = l; i <= r; i++) val[i] += tag; tag = 0; all = 0; } void chg(int a, int b, int y) { release(); for (int i = l; i <= r; i++) { if (i < a or i > b) ; else val[i] += y; inv.push_back(val[i]), add(cnt[val[i]], dp[i]); if (val[i] < K) add(all, dp[i]); } } void add() { tag++; if (K >= tag) all -= cnt[K - tag], all %= 998244353; } void sub() { if (K >= tag) all += cnt[K - tag], all %= 998244353; tag--; } void chg(int y) { if (y == 1) add(); else if (y == -1) sub(); else assert(0); } } sb[350]; int last[100008], pre[100008]; int dig[100008]; int all() { int ans = 0; for (int i = 0; i < 350; i++) add(ans, sb[i].all); return ans; } void chg(int l, int r, int y) { if (l > r) return; if (bel[l] == bel[r]) { int x = bel[l]; sb[x].chg(l, r, y); return; } for (int i = bel[l] + 1; i < bel[r]; i++) sb[i].chg(y); sb[bel[l]].chg(l, r, y); sb[bel[r]].chg(l, r, y); } int main() { scanf("%d%d", &n, &K); K++; for (int i = 1; i <= n; i++) scanf("%d", &dig[i]); for (int i = 1; i <= n; i++) { pre[i] = last[dig[i]]; last[dig[i]] = i; int x = bel[i] = i / 350; if (!sb[x].l) sb[x].l = sb[x].r = i; else sb[x].r = i; } sb[0].l = 0; dp[0] = 1; sb[0].chg(0, 0, 0); for (int i = 1; i <= n; i++) { int x = pre[i]; chg(pre[x], x - 1, -1); chg(x, i - 1, 1); dp[i] = all(); sb[bel[i]].chg(i, i, 0); } printf("%d\n", dp[n]); return 0; }
1129_D. Isolation
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once. Since the answer can be large, find it modulo 998 244 353. Input The first line contains two space-separated integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the number of elements in the array a and the restriction from the statement. The following line contains n space-separated integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n) β€” elements of the array a. Output The first and only line contains the number of ways to divide an array a modulo 998 244 353. Examples Input 3 1 1 1 2 Output 3 Input 5 2 1 1 2 1 3 Output 14 Input 5 5 1 2 3 4 5 Output 16 Note In the first sample, the three possible divisions are as follows. * [[1], [1], [2]] * [[1, 1], [2]] * [[1, 1, 2]] Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
{ "input": [ "5 5\n1 2 3 4 5\n", "3 1\n1 1 2\n", "5 2\n1 1 2 1 3\n" ], "output": [ "16", "3", "14" ] }
{ "input": [ "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n", "100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n", "10 1\n4 2 9 2 1 4 4 1 4 10\n", "100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n", "1 1\n1\n", "100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n", "2 2\n2 1\n", "2 1\n2 2\n" ], "output": [ "16", "726975503", "24", "244208148", "1", "1", "2", "2" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int MAX_N = 100002; const int MOD = 1000000007; const int MAGIC = 500; const int INF = 1e9; int n, k, prv[MAX_N], pos[MAX_N], a[MAX_N]; int nBlock, L[MAGIC + 2], R[MAGIC + 2], blockID[MAX_N]; int v[MAX_N], offset[MAGIC + 2], head[MAGIC + 2], ps[MAGIC + 2][MAX_N]; int f[MAX_N]; vector<pair<int, int> > all, comp[MAGIC + 2]; void readInput() { cin >> n >> k; for (int i = 1; i <= n; ++i) cin >> a[i]; } void init() { for (int i = 1; i <= n; ++i) { prv[i] = pos[a[i]]; pos[a[i]] = i; } } void sqrtDecompostion() { for (int i = 1; i <= n; ++i) { if (i % MAGIC == 1) { R[nBlock] = i - 1; L[++nBlock] = i; } } R[nBlock] = n; for (int i = 1; i <= nBlock; ++i) { for (int j = L[i]; j <= R[i]; ++j) blockID[j] = i; } } void upd(int idx, int l, int r, int delta) { for (int i = L[idx]; i <= R[idx]; ++i) { v[i] += offset[idx]; if (l <= i && i <= r) v[i] += delta; } offset[idx] = 0; all.clear(); comp[idx].clear(); for (int i = L[idx]; i <= R[idx]; ++i) all.push_back({v[i], f[i - 1]}); sort(all.begin(), all.end()); comp[idx].push_back({-INF, 0}); for (auto x : all) { if (x.first != comp[idx].back().first) comp[idx].push_back(x); else comp[idx].back().second = (comp[idx].back().second + x.second) % MOD; } for (int i = 1; i < comp[idx].size(); ++i) ps[idx][i] = (comp[idx][i].second + ps[idx][i - 1]) % MOD; for (int i = 0; i < comp[idx].size(); ++i) { if (comp[idx][i].first <= k) head[idx] = i; } } void upd(int l, int r, int delta) { if (l > r) return; if (blockID[l] == blockID[r]) return upd(blockID[l], l, r, delta); for (int i = blockID[l] + 1; i < blockID[r]; ++i) { offset[i] += delta; if (delta == -1) { while (head[i] + 1 < comp[i].size() && comp[i][head[i] + 1].first + offset[i] <= k) ++head[i]; } else { while (head[i] > 0 && comp[i][head[i]].first + offset[i] > k) --head[i]; } } upd(l, R[blockID[l]], delta); upd(L[blockID[r]], r, delta); } int get() { int res = 0; for (int i = 1; i <= nBlock; ++i) res = (res + ps[i][head[i]]) % MOD; return res; } void solve() { for (int i = 1; i <= nBlock; ++i) upd(L[i], R[i], 0); f[0] = 1; for (int i = 1; i <= n; ++i) { int x1 = prv[i]; int x2 = prv[x1]; upd(x1 + 1, i, 1); upd(x2 + 1, x1, -1); f[i] = get(); upd(L[blockID[i]], R[blockID[i]], 0); } cout << f[n]; } int main() { ios::sync_with_stdio(0); cin.tie(0); readInput(); init(); sqrtDecompostion(); solve(); }
1129_D. Isolation
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once. Since the answer can be large, find it modulo 998 244 353. Input The first line contains two space-separated integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the number of elements in the array a and the restriction from the statement. The following line contains n space-separated integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n) β€” elements of the array a. Output The first and only line contains the number of ways to divide an array a modulo 998 244 353. Examples Input 3 1 1 1 2 Output 3 Input 5 2 1 1 2 1 3 Output 14 Input 5 5 1 2 3 4 5 Output 16 Note In the first sample, the three possible divisions are as follows. * [[1], [1], [2]] * [[1, 1], [2]] * [[1, 1, 2]] Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
{ "input": [ "5 5\n1 2 3 4 5\n", "3 1\n1 1 2\n", "5 2\n1 1 2 1 3\n" ], "output": [ "16", "3", "14" ] }
{ "input": [ "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n", "100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n", "10 1\n4 2 9 2 1 4 4 1 4 10\n", "100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n", "1 1\n1\n", "100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n", "2 2\n2 1\n", "2 1\n2 2\n" ], "output": [ "16", "726975503", "24", "244208148", "1", "1", "2", "2" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> int n, k, a[100001], P = 998244353, lcol[100001], last[100001], f[100001], val[100001], pos[100001], block; inline int sub(int a, const int &b) { a -= b; return a < 0 ? a + P : a; } inline int add(int a, const int &b) { a += b; return a >= P ? a - P : a; } inline int mul(const int &a, const int &b) { return (long long)a * b - (long long)a * b / P * P; } struct sum { int num[200001], tot, delta; sum() { memset(num, 0, sizeof num); tot = 0; } void Add(const int &x) { tot = add(tot, x); } void upd(const int &x, int &pos, const int &del) { if (del == 1) num[pos + n] = sub(num[pos + n], x); else num[pos - 1 + n] = add(num[pos - 1 + n], x); pos += del; } int query() { return add(k >= delta ? tot : 0, num[k - delta + n]); } } S[201]; void update(int L, int R, int l) { if (pos[L] == pos[R]) { for (int i = L; i <= R; i++) S[pos[i]].upd(f[i], val[i], l); } else { for (int i = L; pos[i] == pos[L]; ++i) S[pos[i]].upd(f[i], val[i], l); for (int i = pos[L] + 1; i < pos[R]; ++i) S[i].delta += l; for (int i = R; pos[i] == pos[R]; --i) S[pos[i]].upd(f[i], val[i], l); } } int main() { scanf("%d%d", &n, &k); block = 500; for (int i = 0; i <= n; i++) pos[i] = i / block + 1; f[0] = 1; S[1].Add(1); for (int i = 1; i <= n; i++) { scanf("%d", a + i); last[i] = lcol[a[i]]; lcol[a[i]] = i; update(last[i], i - 1, 1); if (last[i]) update(last[last[i]], last[i] - 1, -1); for (int j = pos[i]; j; --j) f[i] = add(f[i], S[j].query()); S[pos[i]].Add(f[i]); } printf("%d\n", f[n]); }
1129_D. Isolation
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once. Since the answer can be large, find it modulo 998 244 353. Input The first line contains two space-separated integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the number of elements in the array a and the restriction from the statement. The following line contains n space-separated integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n) β€” elements of the array a. Output The first and only line contains the number of ways to divide an array a modulo 998 244 353. Examples Input 3 1 1 1 2 Output 3 Input 5 2 1 1 2 1 3 Output 14 Input 5 5 1 2 3 4 5 Output 16 Note In the first sample, the three possible divisions are as follows. * [[1], [1], [2]] * [[1, 1], [2]] * [[1, 1, 2]] Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
{ "input": [ "5 5\n1 2 3 4 5\n", "3 1\n1 1 2\n", "5 2\n1 1 2 1 3\n" ], "output": [ "16", "3", "14" ] }
{ "input": [ "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n", "100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n", "10 1\n4 2 9 2 1 4 4 1 4 10\n", "100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n", "1 1\n1\n", "100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n", "2 2\n2 1\n", "2 1\n2 2\n" ], "output": [ "16", "726975503", "24", "244208148", "1", "1", "2", "2" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2") using namespace std; using ll = signed long long; const int MOD = 998244353; int ultim[100005], penultim[100005], dp[100005], cate[100005], n, k, x; inline void add(ll &a, int b) { a += b; if (a >= MOD) a -= MOD; if (a < 0) a += MOD; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> k; dp[0] = 1; ll ras = 1; for (int i = 1; i <= n; ++i) { cin >> x; for (int j = penultim[x]; j < ultim[x]; ++j) { if (--cate[j] == k) ras += dp[j]; } for (int j = ultim[x]; j < i; ++j) { if (++cate[j] == k + 1) ras -= dp[j]; } ras %= MOD; dp[i] = ras; penultim[x] = ultim[x]; ultim[x] = i; ras = (ras + dp[i]) % MOD; } cout << dp[n] % MOD; }
1129_D. Isolation
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once. Since the answer can be large, find it modulo 998 244 353. Input The first line contains two space-separated integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the number of elements in the array a and the restriction from the statement. The following line contains n space-separated integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n) β€” elements of the array a. Output The first and only line contains the number of ways to divide an array a modulo 998 244 353. Examples Input 3 1 1 1 2 Output 3 Input 5 2 1 1 2 1 3 Output 14 Input 5 5 1 2 3 4 5 Output 16 Note In the first sample, the three possible divisions are as follows. * [[1], [1], [2]] * [[1, 1], [2]] * [[1, 1, 2]] Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
{ "input": [ "5 5\n1 2 3 4 5\n", "3 1\n1 1 2\n", "5 2\n1 1 2 1 3\n" ], "output": [ "16", "3", "14" ] }
{ "input": [ "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n", "100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n", "10 1\n4 2 9 2 1 4 4 1 4 10\n", "100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n", "1 1\n1\n", "100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n", "2 2\n2 1\n", "2 1\n2 2\n" ], "output": [ "16", "726975503", "24", "244208148", "1", "1", "2", "2" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> //#pragma GCC optimize("unroll-loops") //#pragma GCC optimize("-O3") //#pragma GCC optimize("Ofast") #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #include <ext/rope> #define sz(x) int(x.size()) #define all(x) x.begin(),x.end() #define pii pair<int,int> #define PB push_back #define pll pair<ll,ll> #define pii pair<int,int> #define pil pair<int,ll> #define pli pair<ll, int> #define pi4 pair<pii, pii> #define pib pair<int, bool> #define ft first #define sd second using namespace std; using namespace __gnu_cxx; using namespace __gnu_pbds; typedef long long ll; typedef long double ld; template<class T> using ordered_set = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>; const int N = 100100; //const int N = 2010; const int M = (1 << 10); const int oo = 2e9; const ll OO = 1e18; const int PW = 20; const int md = int(1e9) + 7; const int BLOCK = 1000; //const int BLOCK = 2; const int K = N / BLOCK; int f[N], SQ[K][2 * N], mid[K], sum[K], a[N], n, k, ppr[N], pr[N], vl[N]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); // freopen("in.txt","r",stdin); cin >> n >> k; for (int i = 0; i < n; i++) { cin >> a[i]; a[i]--; } fill(mid, mid + K, N); for (int i = 0; i < n; i++){ ppr[i] = pr[i] = -1; } for (int i = 0; i < n; i++){ int l = ppr[a[i]], r = pr[a[i]]; swap(ppr[a[i]], pr[a[i]]); pr[a[i]] = i; int nm = i / BLOCK; for (int j = max(r + 1, BLOCK * nm); j <= i; j++){ SQ[nm][mid[nm] + vl[j]] -= (j ? f[j - 1] : 1); vl[j]++; SQ[nm][mid[nm] + vl[j]] += (j ? f[j - 1] : 1); if (vl[j] == k + 1) sum[nm] -= (j ? f[j - 1] : 1); } if ((r + 1) / BLOCK != nm){ nm--; int nd = (r + 1) / BLOCK; for (; nm > nd; nm--){ sum[nm] -= SQ[nm][mid[nm] + k]; mid[nm]--; } for (int j = r + 1; j < (nm + 1) * BLOCK; j++){ SQ[nm][mid[nm] + vl[j]] -= (j ? f[j - 1] : 1); vl[j]++; SQ[nm][mid[nm] + vl[j]] += (j ? f[j - 1] : 1); if (vl[j] == k + 1) sum[nm] -= (j ? f[j - 1] : 1); } } if (r >= 0){ int nd = (l + 1) / BLOCK; if (nd < nm){ for (int j = BLOCK * nm; j < r + 1; j++){ SQ[nm][mid[nm] + vl[j]] -= (j ? f[j - 1] : 1); vl[j]--; SQ[nm][mid[nm] + vl[j]] += (j ? f[j - 1] : 1); if (vl[j] == k) sum[nm] += (j ? f[j - 1] : 1); } nm--; } for (; nm > nd; nm--){ sum[nm] += SQ[nm][mid[nm] + k + 1]; mid[nm]++; } for (int j = l + 1; j <= r; j++){ SQ[nm][mid[nm] + vl[j]] -= (j ? f[j - 1] : 1); vl[j]--; SQ[nm][mid[nm] + vl[j]] += (j ? f[j - 1] : 1); if (vl[j] == k) sum[nm] += (j ? f[j - 1] : 1); } } for (int j = 0; j * BLOCK <= i; j++) f[i] += sum[j]; f[i] += (i ? f[i - 1] : 1); nm = i / BLOCK; SQ[nm][mid[nm] + vl[i]] += (i ? f[i - 1] : 1); if (vl[i] <= k) sum[nm] += (i ? f[i - 1] : 1); } cout << f[n - 1]; return 0; }
1129_D. Isolation
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once. Since the answer can be large, find it modulo 998 244 353. Input The first line contains two space-separated integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the number of elements in the array a and the restriction from the statement. The following line contains n space-separated integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n) β€” elements of the array a. Output The first and only line contains the number of ways to divide an array a modulo 998 244 353. Examples Input 3 1 1 1 2 Output 3 Input 5 2 1 1 2 1 3 Output 14 Input 5 5 1 2 3 4 5 Output 16 Note In the first sample, the three possible divisions are as follows. * [[1], [1], [2]] * [[1, 1], [2]] * [[1, 1, 2]] Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
{ "input": [ "5 5\n1 2 3 4 5\n", "3 1\n1 1 2\n", "5 2\n1 1 2 1 3\n" ], "output": [ "16", "3", "14" ] }
{ "input": [ "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n", "100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n", "10 1\n4 2 9 2 1 4 4 1 4 10\n", "100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n", "1 1\n1\n", "100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n", "2 2\n2 1\n", "2 1\n2 2\n" ], "output": [ "16", "726975503", "24", "244208148", "1", "1", "2", "2" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; inline void read(long long &x) { char ch; bool flag = false; for (ch = getchar(); !isdigit(ch); ch = getchar()) if (ch == '-') flag = true; for (x = 0; isdigit(ch); x = x * 10 + ch - '0', ch = getchar()) ; x = flag ? -x : x; } inline void write(long long x) { static const long long maxlen = 100; static char s[maxlen]; if (x < 0) { putchar('-'); x = -x; } if (!x) { putchar('0'); return; } long long len = 0; for (; x; x /= 10) s[len++] = x % 10 + '0'; for (long long i = len - 1; i >= 0; --i) putchar(s[i]); } const long long MAXN = 110000; const long long MAX_block = 336; const long long MAX_val = 1000; const long long AVG_val = 456; long long belong[MAXN]; long long l[MAX_block], r[MAX_block], cnt; long long add[MAX_block]; long long num[MAX_block][MAX_val]; long long val[MAXN]; long long n, lim; void prepare() { long long N = sqrt(n); for (long long i = 1; i <= n; i += N) { ++cnt; l[cnt] = i; r[cnt] = i + N - 1; } r[cnt] = min(r[cnt], n); for (long long i = 1; i <= cnt; i++) for (long long j = l[i]; j <= r[i]; j++) belong[j] = i; } const long long P = 998244353; long long a[MAXN]; long long f[MAXN]; void modify_block(int a, int b, int tag) { long long now = belong[a]; int flag = 0; for (long long i = a; i <= b; i++) { if (tag == 1) { long long &x = num[now][val[i] + AVG_val]; x = (x - f[i - 1] + P) % P; } else { long long &x = num[now][val[i] + AVG_val - 1]; x = (x + f[i - 1]) % P; } val[i] += tag; if (val[i] >= AVG_val) flag = 1; if (val[i] <= -AVG_val) flag = -1; } if (flag == 1) { add[now]++; add[now + 1]--; for (int i = 0; i < MAX_val - 1; i++) num[now][i] = num[now][i + 1]; for (int i = l[now]; i <= r[now]; i++) val[i]--; } if (flag == -1) { add[now]--; add[now + 1]++; for (int i = MAX_val - 1; i >= 0; i++) num[now][i + 1] = num[now][i]; for (int i = l[now]; i <= r[now]; i++) val[i]++; } } void modify(long long a, long long b, long long tag) { if (belong[a] == belong[b]) { modify_block(a, b, tag); return; } add[belong[a] + 1] += tag; add[belong[b]] -= tag; modify(a, r[belong[a]], tag); modify(l[belong[b]], b, tag); } long long query() { long long sum = 0; long long kt = 0; for (long long i = 1; i <= cnt; i++) { kt += add[i]; int tmp = min(lim - kt + AVG_val, MAX_val - 1); if (tmp < 0) continue; sum = (sum + num[i][tmp]) % P; } return sum; } long long last[MAXN]; long long pre[MAXN]; int main() { read(n); read(lim); for (long long i = 1; i <= n; i++) read(a[i]); prepare(); f[0] = 1; for (long long i = 1; i <= n; i++) { pre[i] = last[a[i]]; long long now = belong[i]; for (long long j = AVG_val; j < MAX_val; j++) num[now][j] = (f[i - 1] + num[now][j]) % P; if (pre[i] == 0) { add[1]++; add[now]--; modify(l[now], i, 1); } else modify(pre[i] + 1, i, 1); if (pre[i] != 0) { modify(pre[pre[i]] + 1, pre[i], -1); } f[i] = query(); last[a[i]] = i; } cout << f[n] << endl; return 0; }
1129_D. Isolation
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once. Since the answer can be large, find it modulo 998 244 353. Input The first line contains two space-separated integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the number of elements in the array a and the restriction from the statement. The following line contains n space-separated integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n) β€” elements of the array a. Output The first and only line contains the number of ways to divide an array a modulo 998 244 353. Examples Input 3 1 1 1 2 Output 3 Input 5 2 1 1 2 1 3 Output 14 Input 5 5 1 2 3 4 5 Output 16 Note In the first sample, the three possible divisions are as follows. * [[1], [1], [2]] * [[1, 1], [2]] * [[1, 1, 2]] Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
{ "input": [ "5 5\n1 2 3 4 5\n", "3 1\n1 1 2\n", "5 2\n1 1 2 1 3\n" ], "output": [ "16", "3", "14" ] }
{ "input": [ "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n", "100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n", "10 1\n4 2 9 2 1 4 4 1 4 10\n", "100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n", "1 1\n1\n", "100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n", "2 2\n2 1\n", "2 1\n2 2\n" ], "output": [ "16", "726975503", "24", "244208148", "1", "1", "2", "2" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int MX_N = 1e5 + 5; const int MX_K = 1e5 + 5; const int MX_A = MX_N; const int MOD = 998244353; const int B = 300; int N, K, A[MX_N]; int pos[MX_A], prv[MX_N], dp[MX_N]; struct Bucket { int pre[B], cnt[B], offset; Bucket() { for (int i = (0); i <= (B - 1); ++i) { pre[i] = 0; cnt[i] = 0; } offset = 0; } void rebuild(int b) { int mn = MX_N; int j = b * B; for (int i = (0); i <= (B - 1); ++i) { pre[i] = 0; mn = min(mn, cnt[i]); } offset += mn; for (int i = (0); i <= (B - 1); ++i) { cnt[i] -= mn; pre[cnt[i]] += dp[i + j]; if (pre[cnt[i]] >= MOD) pre[cnt[i]] -= MOD; } for (int i = (1); i <= (B - 1); ++i) { pre[i] += pre[i - 1]; if (pre[i] >= MOD) pre[i] -= MOD; } } int sum() { if (offset > K) return 0; return pre[min(B - 1, K - offset)]; } int get(int b, int i) { if (offset + cnt[i] > K) return 0; return dp[b * B + i]; } } bucket[MX_N / B + 1]; void update(int x, int y, int v) { if (x > y) return; int bx, jx = remquo(x, B, &bx), by, jy = remquo(y, B, &by); if (bx == by) { for (int i = (jx); i <= (jy); ++i) { bucket[bx].cnt[i] += v; } bucket[bx].rebuild(bx); } else { for (int i = (jx); i <= (B - 1); ++i) { bucket[bx].cnt[i] += v; } bucket[bx].rebuild(bx); for (int i = (bx + 1); i <= (by - 1); ++i) { bucket[i].offset += v; } for (int i = (0); i <= (jy); ++i) { bucket[by].cnt[i] += v; } bucket[by].rebuild(by); } } int query(int i) { int b, j = remquo(i, B, &b); int sum = 0; for (int k = (0); k <= (b - 1); ++k) { sum += bucket[k].sum(); if (sum >= MOD) sum -= MOD; } for (int k = (0); k <= (j); ++k) { sum += bucket[b].get(b, k); if (sum >= MOD) sum -= MOD; } return sum; } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> N >> K; for (int i = (1); i <= (N); ++i) { cin >> A[i]; prv[i] = pos[A[i]]; pos[A[i]] = i; } dp[0] = 1; bucket[0].rebuild(0); for (int i = (1); i <= (N); ++i) { update(prv[prv[i]], prv[i] - 1, -1); update(prv[i], i - 1, 1); dp[i] = query(i - 1); int b = i / B; bucket[b].rebuild(b); } cout << dp[N]; }
1129_D. Isolation
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once. Since the answer can be large, find it modulo 998 244 353. Input The first line contains two space-separated integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the number of elements in the array a and the restriction from the statement. The following line contains n space-separated integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n) β€” elements of the array a. Output The first and only line contains the number of ways to divide an array a modulo 998 244 353. Examples Input 3 1 1 1 2 Output 3 Input 5 2 1 1 2 1 3 Output 14 Input 5 5 1 2 3 4 5 Output 16 Note In the first sample, the three possible divisions are as follows. * [[1], [1], [2]] * [[1, 1], [2]] * [[1, 1, 2]] Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
{ "input": [ "5 5\n1 2 3 4 5\n", "3 1\n1 1 2\n", "5 2\n1 1 2 1 3\n" ], "output": [ "16", "3", "14" ] }
{ "input": [ "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n", "100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n", "10 1\n4 2 9 2 1 4 4 1 4 10\n", "100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n", "1 1\n1\n", "100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n", "2 2\n2 1\n", "2 1\n2 2\n" ], "output": [ "16", "726975503", "24", "244208148", "1", "1", "2", "2" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long mod = 998244353LL; int k; class Block { public: int aux; vector<long long> pre; int st, en; int lo, hi; vector<long long> vals; vector<int> dist; Block(int a, int b) { aux = 0; st = a; en = b; lo = 0; hi = 0; pre.push_back(0LL); pre.push_back(0LL); for (int i = st; i <= en; i++) { vals.push_back(0LL); dist.push_back(0); } } void change(int ind, long long val) { if (ind < st || ind > en) { return; } vals[ind - st] = val; for (int i = dist[ind - st]; i <= hi; i++) { pre[i - lo + 1] += val; pre[i - lo + 1] %= mod; } } void modify(int l, int r, int delta) { if (l <= st && r >= en) { aux += delta; return; } if (st > r || en < l) { return; } for (int i = max(l, st); i <= min(r, en); i++) { dist[i - st] += delta; } for (int i = st; i <= en; i++) { dist[i - st] += aux; if (i == st) { lo = dist[i - st]; hi = dist[i - st]; } else { lo = min(lo, dist[i - st]); hi = max(hi, dist[i - st]); } } aux = 0; pre.clear(); pre.resize(hi - lo + 2); for (int i = st; i <= en; i++) { pre[dist[i - st] - lo + 1] += vals[i - st]; pre[dist[i - st] - lo + 1] %= mod; } for (int i = 1; i < pre.size(); i++) { pre[i] += pre[i - 1]; pre[i] %= mod; } } long long sum(int l, int r) { if (st > r || en < l) { return 0LL; } if (l <= st && r >= en) { k -= aux; if (k < lo) { return 0LL; } if (k >= hi) { return pre.back(); } return pre[k - lo + 1]; } long long ret = 0LL; for (int i = max(l, st); i <= min(r, en); i++) { k -= aux; if (dist[i - st] <= k) { ret += vals[i - st]; ret %= mod; } } return (ret % mod); } }; int sz = 300; int n; vector<Block> blocks; void build() { int point = 0; while (point < n + 1) { int en = min(point + sz - 1, n); blocks.push_back(Block(point, en)); point = en + 1; } } void add(int l, int r, int delta) { for (int i = 0; i < blocks.size(); i++) { blocks[i].modify(l, r, delta); } } long long gsum(int l, int r) { long long ret = 0LL; for (int i = 0; i < blocks.size(); i++) { ret += blocks[i].sum(l, r); } return ret % mod; } void doChange(int ind, long long vv) { for (int i = 0; i < blocks.size(); i++) { blocks[i].change(ind, vv); } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n >> k; vector<int> list[n]; int a[n + 1]; int bef[n + 1]; int bef2[n + 1]; for (int i = 1; i <= n; i++) { cin >> a[i]; a[i]--; list[a[i]].push_back(i); int siz = list[a[i]].size(); if (list[a[i]].size() == 1) { bef[i] = -1; bef2[i] = -1; } else if (list[a[i]].size() == 2) { bef[i] = list[a[i]][siz - 2]; bef2[i] = -1; } else { bef[i] = list[a[i]][siz - 2]; bef2[i] = list[a[i]][siz - 3]; } } build(); doChange(0, 1LL); for (int i = 1; i <= n; i++) { if (bef[i] == -1) { add(0, i - 1, 1); } else if (bef2[i] == -1) { add(0, bef[i] - 1, -1); add(bef[i], i - 1, 1); } else { add(bef2[i], bef[i] - 1, -1); add(bef[i], i - 1, 1); } long long got = gsum(0, i - 1) % mod; doChange(i, got); } cout << blocks.back().vals.back() << endl; }
1129_D. Isolation
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once. Since the answer can be large, find it modulo 998 244 353. Input The first line contains two space-separated integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the number of elements in the array a and the restriction from the statement. The following line contains n space-separated integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n) β€” elements of the array a. Output The first and only line contains the number of ways to divide an array a modulo 998 244 353. Examples Input 3 1 1 1 2 Output 3 Input 5 2 1 1 2 1 3 Output 14 Input 5 5 1 2 3 4 5 Output 16 Note In the first sample, the three possible divisions are as follows. * [[1], [1], [2]] * [[1, 1], [2]] * [[1, 1, 2]] Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
{ "input": [ "5 5\n1 2 3 4 5\n", "3 1\n1 1 2\n", "5 2\n1 1 2 1 3\n" ], "output": [ "16", "3", "14" ] }
{ "input": [ "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n", "100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n", "10 1\n4 2 9 2 1 4 4 1 4 10\n", "100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n", "1 1\n1\n", "100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n", "2 2\n2 1\n", "2 1\n2 2\n" ], "output": [ "16", "726975503", "24", "244208148", "1", "1", "2", "2" ] }
IN-CORRECT
cpp
#include<stdint.h> #include<ext/pb_ds/assoc_container.hpp> #include<ext/pb_ds/tree_policy.hpp> #include<ext/rope> using namespace __gnu_pbds; using namespace __gnu_cxx; #define VIS(it,con) for(auto it=con.begin();it!=con.end();++it) #define pob pop_back #define pf push_front #define pof pop_front #define MIN(x,y) (x=min(x,(y))) #define MAX(x,y) (x=max(x,(y))) #define mid (l+r>>1) #define lch (idx*2+1) #define rch (idx*2+2) /*****************************************************************************/ #include<bits/stdc++.h> #define int int_fast64_t using namespace std; typedef pair<int,int> pii; typedef vector<int> VI; #define REP(i,j,k) for(register int i=(j);i<(k);++i) #define RREP(i,j,k) for(register int i=(j)-1;i>=(k);--i) #define ALL(a) a.begin(),a.end() #define MST(a,v) memset(a,(v),sizeof a) #define pb push_back #define F first #define S second #define endl '\n' // #define __debug #ifdef __debug #define IOS (void)0 #define de(...) cerr<<__VA_ARGS__ #define ar(a,s,t) {REP(__i,s,t)de(a[__i]<<' ');de(endl);} #else #define IOS cin.tie(0),cout.tie(0),ios_base::sync_with_stdio(false) #define de(...) (void)0 #define ar(...) (void)0 #endif /***********************************default***********************************/ const int maxn=1e5+9,mo=998244353,block=320,rg=650,base=325; int n,k,a[maxn],dp[maxn]; int pre[maxn],last[maxn]; int pfx[block+100][rg+100],tag[maxn],sumt[block+100]; void add(int&x,int y){x=(x+y)%mo;} void upd(int b){ int cnt=0; MST(pfx[b],0); RREP(i,(b+1)*block,b*block){ cnt+=tag[i]; assert(base+cnt<rg&&base+cnt>=0); add(pfx[b][base+cnt],dp[i]); } sumt[b]=cnt; REP(i,1,rg)add(pfx[b][i],pfx[b][i-1]); } main(){ IOS; cin>>n>>k; REP(i,0,n)cin>>a[i]; MST(last,-1); REP(i,0,n)pre[i]=last[a[i]],last[a[i]]=i; MST(pfx,0); MST(tag,0); MST(sumt,0); MST(dp,0); dp[0]=1; REP(i,0,n){ tag[i]=1; upd(i/block); if(pre[i]>=0){ tag[pre[i]]=-1; upd(pre[i]/block); if(pre[pre[i]]>=0){ tag[pre[pre[i]]]=0; upd(pre[pre[i]]/block); } } int sum=0; RREP(j,i/block+1,0){ add(dp[i+1],pfx[j][max((int)0,min(rg-1,base+k-sum))]); sum+=sumt[j]; } } cout<<dp[n]<<endl; }
1129_D. Isolation
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once. Since the answer can be large, find it modulo 998 244 353. Input The first line contains two space-separated integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the number of elements in the array a and the restriction from the statement. The following line contains n space-separated integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n) β€” elements of the array a. Output The first and only line contains the number of ways to divide an array a modulo 998 244 353. Examples Input 3 1 1 1 2 Output 3 Input 5 2 1 1 2 1 3 Output 14 Input 5 5 1 2 3 4 5 Output 16 Note In the first sample, the three possible divisions are as follows. * [[1], [1], [2]] * [[1, 1], [2]] * [[1, 1, 2]] Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
{ "input": [ "5 5\n1 2 3 4 5\n", "3 1\n1 1 2\n", "5 2\n1 1 2 1 3\n" ], "output": [ "16", "3", "14" ] }
{ "input": [ "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n", "100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n", "10 1\n4 2 9 2 1 4 4 1 4 10\n", "100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n", "1 1\n1\n", "100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n", "2 2\n2 1\n", "2 1\n2 2\n" ], "output": [ "16", "726975503", "24", "244208148", "1", "1", "2", "2" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ii = pair<int, int>; using ll = long long; using db = long double; const int N = 1e5 + 5, K = 20, MOD = 1e9 + 7, MOD2 = 998244353; const ll MX = 1e15; const int blkSz = 320; int n, k, sq, a[N]; int dp[N], lz[blkSz]; int sweeped[N]; deque<int> dq[blkSz]; deque<int> vals[N]; int calc[blkSz]; bool isBuilt[blkSz]; void buildBlock(int idx) { int l = idx * sq; int r = l + sq; int mx = *max_element(sweeped + l, sweeped + r); for (int j = 0; j <= mx; j++) { dq[idx].push_back(0); } for (int i = l; i < r; i++) { dq[idx][sweeped[i]] += dp[i]; if (dq[idx][sweeped[i]] >= MOD) dq[idx][sweeped[i]] -= MOD; } for (int i = 0; i < min(k + 1, (int)dq[idx].size()); i++) { calc[idx] += dq[idx][i]; if (calc[idx] >= MOD) calc[idx] -= MOD; } } void reBuildBlock(int idx) { int l = idx * sq; int r = l + sq; for (int i = l; i < r; i++) { sweeped[i] += lz[idx]; } lz[idx] = 0; } void modifyBlock(int idx, bool push) { lz[idx] += (push ? 1 : -1); if (push) { if (dq[idx].size() > k) { calc[idx] -= dq[idx][k]; if (calc[idx] < 0) calc[idx] += MOD; } dq[idx].push_front(0); } else { assert(dq[idx].front() == 0); dq[idx].pop_front(); if (dq[idx].size() > k) { calc[idx] += dq[idx][k]; if (calc[idx] >= MOD) calc[idx] -= MOD; } } } void incPrefix(int idx) { --idx; int org = idx / sq; if (isBuilt[org]) { reBuildBlock(org); } for (; idx >= org * sq; --idx) { if (isBuilt[org]) { dq[idx / sq][sweeped[idx]] -= dp[idx]; if (dq[idx / sq][sweeped[idx]] < 0) dq[idx / sq][sweeped[idx]] += MOD; if (sweeped[idx] <= k) { calc[idx / sq] -= dp[idx]; if (calc[idx / sq] < 0) calc[idx / sq] += MOD; } } sweeped[idx]++; if (isBuilt[org]) { if (dq[idx / sq].size() <= sweeped[idx]) dq[idx / sq].push_back(0); dq[idx / sq][sweeped[idx]] += dp[idx]; if (dq[idx / sq][sweeped[idx]] >= MOD) dq[idx / sq][sweeped[idx]] -= MOD; if (sweeped[idx] <= k) { calc[idx / sq] += dp[idx]; if (calc[idx / sq] >= MOD) calc[idx / sq] -= MOD; } } } for (; idx >= 0; idx -= sq) { modifyBlock(idx / sq, 1); } } void decPrefix(int idx) { --idx; int org = idx / sq; if (isBuilt[org]) { reBuildBlock(org); } for (; idx >= org * idx; --idx) { if (isBuilt[org]) { dq[idx / sq][sweeped[idx]] -= dp[idx]; if (dq[idx / sq][sweeped[idx]] < 0) dq[idx / sq][sweeped[idx]] += MOD; if (sweeped[idx] <= k) { calc[idx / sq] -= dp[idx]; if (calc[idx / sq] < 0) calc[idx / sq] += MOD; } } sweeped[idx]--; if (isBuilt[org]) { if (dq[idx / sq].size() <= sweeped[idx]) dq[idx / sq].push_back(0); dq[idx / sq][sweeped[idx]] += dp[idx]; if (dq[idx / sq][sweeped[idx]] >= MOD) dq[idx / sq][sweeped[idx]] -= MOD; if (sweeped[idx] <= k) { calc[idx / sq] += dp[idx]; if (calc[idx / sq] >= MOD) calc[idx / sq] -= MOD; } } } for (; idx >= 0; idx -= sq) { modifyBlock(idx / sq, 0); } } void calcDP(int idx) { int o = idx; --idx; int org = idx / sq; for (; idx >= org * sq; --idx) { if (sweeped[idx] <= k) { dp[o] += dp[idx]; if (dp[o] >= MOD) dp[o] -= MOD; } } for (; idx >= 0; idx -= sq) { dp[o] += calc[idx / sq]; if (dp[o] >= MOD) dp[o] -= MOD; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> k; dp[0] = 1; for (int i = 1; i <= n; i++) cin >> a[i]; sq = sqrt(n + 1); if (sq * sq != n + 1) ++sq; for (int i = 1; i <= n; i++) { vals[a[i]].push_back(i); incPrefix(i); if (vals[a[i]].size() >= 2) { decPrefix(vals[a[i]][vals[a[i]].size() - 2]); decPrefix(vals[a[i]][vals[a[i]].size() - 2]); } if (vals[a[i]].size() == 3) { incPrefix(vals[a[i]].front()); vals[a[i]].pop_front(); } calcDP(i); if (i % sq == 0) { buildBlock(i / sq - 1); } } cout << dp[n] << '\n'; }
1129_D. Isolation
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once. Since the answer can be large, find it modulo 998 244 353. Input The first line contains two space-separated integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the number of elements in the array a and the restriction from the statement. The following line contains n space-separated integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n) β€” elements of the array a. Output The first and only line contains the number of ways to divide an array a modulo 998 244 353. Examples Input 3 1 1 1 2 Output 3 Input 5 2 1 1 2 1 3 Output 14 Input 5 5 1 2 3 4 5 Output 16 Note In the first sample, the three possible divisions are as follows. * [[1], [1], [2]] * [[1, 1], [2]] * [[1, 1, 2]] Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
{ "input": [ "5 5\n1 2 3 4 5\n", "3 1\n1 1 2\n", "5 2\n1 1 2 1 3\n" ], "output": [ "16", "3", "14" ] }
{ "input": [ "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n", "100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n", "10 1\n4 2 9 2 1 4 4 1 4 10\n", "100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n", "1 1\n1\n", "100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n", "2 2\n2 1\n", "2 1\n2 2\n" ], "output": [ "16", "726975503", "24", "244208148", "1", "1", "2", "2" ] }
IN-CORRECT
cpp
#pragma comment(linker, "/stack:200000000") #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,avx,avx2") #define _CRT_SECURE_NO_WARNINGS # include <iostream> # include <cmath> # include <algorithm> # include <stdio.h> # include <cstdint> # include <cstring> # include <string> # include <cstdlib> # include <vector> # include <bitset> # include <map> # include <queue> # include <ctime> # include <stack> # include <set> # include <list> # include <random> # include <deque> # include <functional> # include <iomanip> # include <sstream> # include <fstream> # include <complex> # include <numeric> # include <immintrin.h> # include <cassert> # include <array> # include <tuple> # include <unordered_map> # include <unordered_set> //#ifdef LOCAL //# include <opencv2/core/core.hpp> //# include <opencv2/highgui/highgui.hpp> //# include <opencv2/imgproc/imgproc.hpp> //#endif using namespace std; #define VA_NUM_ARGS(...) VA_NUM_ARGS_IMPL_((0,__VA_ARGS__, 6,5,4,3,2,1)) #define VA_NUM_ARGS_IMPL_(tuple) VA_NUM_ARGS_IMPL tuple #define VA_NUM_ARGS_IMPL(_0,_1,_2,_3,_4,_5,_6,N,...) N #define macro_dispatcher(macro, ...) macro_dispatcher_(macro, VA_NUM_ARGS(__VA_ARGS__)) #define macro_dispatcher_(macro, nargs) macro_dispatcher__(macro, nargs) #define macro_dispatcher__(macro, nargs) macro_dispatcher___(macro, nargs) #define macro_dispatcher___(macro, nargs) macro ## nargs #define DBN1(a) cerr<<#a<<"="<<(a)<<"\n" #define DBN2(a,b) cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<"\n" #define DBN3(a,b,c) cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<"\n" #define DBN4(a,b,c,d) cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<"\n" #define DBN5(a,b,c,d,e) cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<", "<<#e<<"="<<(e)<<"\n" #define DBN6(a,b,c,d,e,f) cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<", "<<#e<<"="<<(e)<<", "<<#f<<"="<<(f)<<"\n" #define DBN(...) macro_dispatcher(DBN, __VA_ARGS__)(__VA_ARGS__) #define DA(a,n) cerr<<#a<<"=["; printarray(a,n); cerr<<"]\n" #define DAR(a,n,s) cerr<<#a<<"["<<s<<"-"<<n-1<<"]=["; printarray(a,n,s); cerr<<"]\n" #ifdef _MSC_VER #define PREFETCH(ptr, rw, level) ((void)0) #else #define PREFETCH(ptr, rw, level) __builtin_prefetch(ptr, rw, level) #endif #if defined _MSC_VER #define ASSUME(condition) ((void)0) #define __restrict #elif defined __clang__ #define ASSUME(condition) __builtin_assume(condition) #elif defined __GNUC__ [[noreturn]] __attribute__((always_inline)) inline void undefinedBehaviour() {} #define ASSUME(condition) ((condition) ? true : (undefinedBehaviour(), false)) #endif #ifdef LOCAL #define CURTIME() cerr << clock() * 1.0 / CLOCKS_PER_SEC << endl #else #define CURTIME() #endif typedef long long ll; typedef long double ld; typedef unsigned int uint; typedef unsigned long long ull; typedef pair<int, int> pii; typedef pair<long long, long long> pll; typedef vector<int> vi; typedef vector<long long> vll; typedef int itn; template<class T1, class T2, class T3> struct triple{ T1 a; T2 b; T3 c; triple() : a(T1()), b(T2()), c(T3()) {}; triple(T1 _a, T2 _b, T3 _c) :a(_a), b(_b), c(_c){} }; template<class T1, class T2, class T3> bool operator<(const triple<T1,T2,T3>&t1,const triple<T1,T2,T3>&t2){if(t1.a!=t2.a)return t1.a<t2.a;else if(t1.b!=t2.b)return t1.b<t2.b;else return t1.c<t2.c;} template<class T1, class T2, class T3> bool operator>(const triple<T1,T2,T3>&t1,const triple<T1,T2,T3>&t2){if(t1.a!=t2.a)return t1.a>t2.a;else if(t1.b!=t2.b)return t1.b>t2.b;else return t1.c>t2.c;} #define tri triple<int,int,int> #define trll triple<ll,ll,ll> #define FI(n) for(int i=0;i<(n);++i) #define FJ(n) for(int j=0;j<(n);++j) #define FK(n) for(int k=0;k<(n);++k) #define FL(n) for(int l=0;l<(n);++l) #define FQ(n) for(int q=0;q<(n);++q) #define FOR(i,a,b) for(int i = (a), __e = (int) (b); i < __e; ++i) #define all(a) std::begin(a), std::end(a) #define reunique(v) v.resize(std::unique(v.begin(), v.end()) - v.begin()) #define sqr(x) ((x) * (x)) #define sqrt(x) sqrt(1.0 * (x)) #define pow(x, n) pow(1.0 * (x), n) #define COMPARE(obj) [&](const std::decay_t<decltype(obj)>& a, const std::decay_t<decltype(obj)>& b) #define COMPARE_BY(obj, field) [&](const std::decay_t<decltype(obj)>& a, const std::decay_t<decltype(obj)>& b) { return a.field < b.field; } #define checkbit(n, b) (((n) >> (b)) & 1) #define setbit(n, b) ((n) | (static_cast<std::decay<decltype(n)>::type>(1) << (b))) #define removebit(n, b) ((n) & ~(static_cast<std::decay<decltype(n)>::type>(1) << (b))) #define flipbit(n, b) ((n) ^ (static_cast<std::decay<decltype(n)>::type>(1) << (b))) inline int countBits(uint v){v=v-((v>>1)&0x55555555);v=(v&0x33333333)+((v>>2)&0x33333333);return((v+(v>>4)&0xF0F0F0F)*0x1010101)>>24;} inline int countBits(ull v){uint t=v>>32;uint p=(v & ((1ULL << 32) - 1)); return countBits(t) + countBits(p); } inline int countBits(ll v){return countBits((ull)v); } inline int countBits(int v){return countBits((uint)v); } unsigned int reverseBits(uint x){ x = (((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1)); x = (((x & 0xcccccccc) >> 2) | ((x & 0x33333333) << 2)); x = (((x & 0xf0f0f0f0) >> 4) | ((x & 0x0f0f0f0f) << 4)); x = (((x & 0xff00ff00) >> 8) | ((x & 0x00ff00ff) << 8)); return((x >> 16) | (x << 16)); } template<class T> inline int sign(T x){ return x > 0 ? 1 : x < 0 ? -1 : 0; } inline bool isPowerOfTwo(int x){ return (x != 0 && (x&(x - 1)) == 0); } constexpr ll power(ll x, int p) { return p == 0 ? 1 : (x * power(x, p - 1)); } template<class T> inline bool inRange(const T& val, const T& min, const T& max) { return min <= val && val <= max; } //template<class T1, class T2, class T3> T1 inline clamp(T1 x, const T2& a, const T3& b) { if (x < a) return a; else if (x > b) return b; else return x; } unsigned long long rdtsc() { unsigned long long ret = 0; #ifdef __clang__ return __builtin_readcyclecounter(); #endif #ifndef _MSC_VER asm volatile("rdtsc" : "=A" (ret) : :); #endif return ret; } // Fast IO ******************************************************************************************************** const int __BS = 4096; static char __bur[__BS + 16], *__er = __bur + __BS, *__ir = __er; template<class T = int> T readInt() { auto c = [&]() { if (__ir == __er) std::fill(__bur, __bur + __BS, 0), cin.read(__bur, __BS), __ir = __bur; }; c(); while (*__ir && (*__ir < '0' || *__ir > '9') && *__ir != '-') ++__ir; c(); bool m = false; if (*__ir == '-') ++__ir, c(), m = true; T r = 0; while (*__ir >= '0' && *__ir <= '9') r = r * 10 + *__ir - '0', ++__ir, c(); ++__ir; return m ? -r : r; } string readString() { auto c = [&]() { if (__ir == __er) std::fill(__bur, __bur + __BS, 0), cin.read(__bur, __BS), __ir = __bur; }; string r; c(); while (*__ir && isspace(*__ir)) ++__ir, c(); while (!isspace(*__ir)) r.push_back(*__ir), ++__ir, c(); ++__ir; return r; } char readChar() { auto c = [&]() { if (__ir == __er) std::fill(__bur, __bur + __BS, 0), cin.read(__bur, __BS), __ir = __bur; }; c(); while (*__ir && isspace(*__ir)) ++__ir, c(); return *__ir++; } static char __buw[__BS + 20], *__iw = __buw, *__ew = __buw + __BS; template<class T> void writeInt(T x, char endc = '\n') { if (x < 0) *__iw++ = '-', x = -x; if (x == 0) *__iw++ = '0'; char* s = __iw; while (x) { T t = x / 10; char c = x - 10 * t + '0'; *__iw++ = c; x = t; } char* f = __iw - 1; while (s < f) swap(*s, *f), ++s, --f; if (__iw > __ew) cout.write(__buw, __iw - __buw), __iw = __buw; if (endc) { *__iw++ = endc; } } template<class T> void writeStr(const T& str) { int i = 0; while (str[i]) { *__iw++ = str[i++]; if (__iw > __ew) cout.write(__buw, __iw - __buw), __iw = __buw; } } struct __FL__ { ~__FL__() { if (__iw != __buw) cout.write(__buw, __iw - __buw); } }; static __FL__ __flushVar__; //STL output ***************************************************************************************************** #define TT1 template<class T> #define TT1T2 template<class T1, class T2> #define TT1T2T3 template<class T1, class T2, class T3> TT1T2 ostream& operator << (ostream& os, const pair<T1, T2>& p); TT1 ostream& operator << (ostream& os, const vector<T>& v); TT1T2 ostream& operator << (ostream& os, const set<T1, T2>&v); TT1T2 ostream& operator << (ostream& os, const multiset<T1, T2>&v); TT1T2 ostream& operator << (ostream& os, priority_queue<T1, T2> v); TT1T2T3 ostream& operator << (ostream& os, const map<T1, T2, T3>& v); TT1T2T3 ostream& operator << (ostream& os, const multimap<T1, T2, T3>& v); TT1T2T3 ostream& operator << (ostream& os, const triple<T1, T2, T3>& t); template<class T, size_t N> ostream& operator << (ostream& os, const array<T, N>& v); TT1T2 ostream& operator << (ostream& os, const pair<T1, T2>& p){ return os <<"("<<p.first<<", "<< p.second<<")"; } TT1 ostream& operator << (ostream& os, const vector<T>& v){ bool f=1;os<<"[";for(auto& i : v) { if (!f)os << ", ";os<<i;f=0;}return os << "]"; } template<class T, size_t N> ostream& operator << (ostream& os, const array<T, N>& v) { bool f=1;os<<"[";for(auto& i : v) { if (!f)os << ", ";os<<i;f=0;}return os << "]"; } TT1T2 ostream& operator << (ostream& os, const set<T1, T2>&v){ bool f=1;os<<"[";for(auto& i : v) { if (!f)os << ", ";os<<i;f=0;}return os << "]"; } TT1T2 ostream& operator << (ostream& os, const multiset<T1,T2>&v){bool f=1;os<<"[";for(auto& i : v) { if (!f)os << ", ";os<<i;f=0;}return os << "]"; } TT1T2T3 ostream& operator << (ostream& os, const map<T1,T2,T3>& v){ bool f = 1; os << "["; for (auto& ii : v) { if (!f)os << ", "; os << "(" << ii.first << " -> " << ii.second << ") "; f = 0; }return os << "]"; } TT1T2 ostream& operator << (ostream& os, const multimap<T1, T2>& v){ bool f = 1; os << "["; for (auto& ii : v) { if (!f)os << ", "; os << "(" << ii.first << " -> " << ii.second << ") "; f = 0; }return os << "]"; } TT1T2 ostream& operator << (ostream& os, priority_queue<T1, T2> v) { bool f = 1; os << "["; while (!v.empty()) { auto x = v.top(); v.pop(); if (!f) os << ", "; f = 0; os << x; } return os << "]"; } TT1T2T3 ostream& operator << (ostream& os, const triple<T1, T2, T3>& t){ return os << "(" << t.a << ", " << t.b << ", " << t.c << ")"; } TT1T2 void printarray(const T1& a, T2 sz, T2 beg = 0){ for (T2 i = beg; i<sz; i++) cout << a[i] << " "; cout << endl; } //STL input ***************************************************************************************************** TT1T2T3 inline istream& operator >> (istream& os, triple<T1, T2, T3>& t); TT1T2 inline istream& operator >> (istream& os, pair<T1, T2>& p) { return os >> p.first >> p.second; } TT1 inline istream& operator >> (istream& os, vector<T>& v) { if (v.size()) for (T& t : v) os >> t; else { string s; T obj; while (s.empty()) {getline(os, s); if (!os) return os;} stringstream ss(s); while (ss >> obj) v.push_back(obj); } return os; } TT1T2T3 inline istream& operator >> (istream& os, triple<T1, T2, T3>& t) { return os >> t.a >> t.b >> t.c; } //Pair magic ***************************************************************************************************** #define PT1T2 pair<T1, T2> TT1T2 inline PT1T2 operator+(const PT1T2 &p1 , const PT1T2 &p2) { return PT1T2(p1.first + p2.first, p1.second + p2.second); } TT1T2 inline PT1T2& operator+=(PT1T2 &p1 , const PT1T2 &p2) { p1.first += p2.first, p1.second += p2.second; return p1; } TT1T2 inline PT1T2 operator-(const PT1T2 &p1 , const PT1T2 &p2) { return PT1T2(p1.first - p2.first, p1.second - p2.second); } TT1T2 inline PT1T2& operator-=(PT1T2 &p1 , const PT1T2 &p2) { p1.first -= p2.first, p1.second -= p2.second; return p1; } #undef TT1 #undef TT1T2 #undef TT1T2T3 #define FREIN(FILE) freopen(FILE, "rt", stdin) #define FREOUT(FILE) freopen(FILE, "wt", stdout) #ifdef LOCAL #define BEGIN_PROFILE(idx, name) int profileIdx = idx; profileName[profileIdx] = name; totalTime[profileIdx] -= rdtsc() / 1e3; #define END_PROFILE totalTime[profileIdx] += rdtsc() / 1e3; totalCount[profileIdx]++; #else #define BEGIN_PROFILE(idx, name) #define END_PROFILE #endif const int USUAL_MOD = 1000000007; template<class T> inline void normmod(T &x, T m = USUAL_MOD) { x %= m; if (x < 0) x += m; } template<class T1, class T2> inline T2 summodfast(T1 x, T1 y, T2 m = USUAL_MOD) { T2 res = x + y; if (res >= m) res -= m; return res; } template<class T1, class T2, class T3 = int> inline void addmodfast(T1 &x, T2 y, T3 m = USUAL_MOD) { x += y; if (x >= m) x -= m; } template<class T1, class T2, class T3 = int> inline void submodfast(T1 &x, T2 y, T3 m = USUAL_MOD) { x -= y; if (x < 0) x += m; } inline ll mulmod(ll x, ll n, ll m){ x %= m; n %= m; ll r = x * n - ll(ld(x)*ld(n) / ld(m)) * m; while (r < 0) r += m; while (r >= m) r -= m; return r; } inline ll powmod(ll x, ll n, ll m){ ll r = 1; normmod(x, m); while (n){ if (n & 1) r *= x; x *= x; r %= m; x %= m; n /= 2; }return r; } inline ll powmulmod(ll x, ll n, ll m) { ll res = 1; normmod(x, m); while (n){ if (n & 1)res = mulmod(res, x, m); x = mulmod(x, x, m); n /= 2; } return res; } template<class T> inline T gcd(T a, T b) { while (b) { T t = a % b; a = b; b = t; } return a; } template<class T> T fast_gcd(T u, T v) { int shl = 0; while ( u && v && u != v) { T eu = u & 1; u >>= eu ^ 1; T ev = v & 1; v >>= ev ^ 1; shl += (~(eu | ev) & 1); T d = u & v & 1 ? (u + v) >> 1 : 0; T dif = (u - v) >> (sizeof(T) * 8 - 1); u -= d & ~dif; v -= d & dif; } return std::max(u, v) << shl; } inline ll lcm(ll a, ll b){ return a / gcd(a, b) * b; } template<class T> inline T gcd(T a, T b, T c){ return gcd(gcd(a, b), c); } ll gcdex(ll a, ll b, ll& x, ll& y) { if (!a) { x = 0; y = 1; return b; } ll y1; ll d = gcdex(b % a, a, y, y1); x = y1 - (b / a) * y; return d; } bool isPrime(long long n) { if (n <= (1 << 14)) { int x = (int) n; if (x <= 4 || x % 2 == 0 || x % 3 == 0) return x == 2 || x == 3; for (int i = 5; i * i <= x; i += 6) if (x % i == 0 || x % (i + 2) == 0) return 0; return 1; } long long s = n - 1; int t = 0; while (s % 2 == 0) { s /= 2; ++t; } for (int a : {2, 325, 9375, 28178, 450775, 9780504, 1795265022}) { if (!(a %= n)) return true; long long f = powmulmod(a, s, n); if (f == 1 || f == n - 1) continue; for (int i = 1; i < t; ++i) if ((f = mulmod(f, f, n)) == n - 1) goto nextp; return false; nextp:; } return true; } // Useful constants //int some_primes[7] = {24443, 100271, 1000003, 1000333, 5000321, 98765431, #define T9 1000000000 #define T18 1000000000000000000LL #define INF 1011111111 #define LLINF 1000111000111000111LL #define mod 1000000007 #define fftmod 998244353 #define EPS 1e-10 #define PI 3.14159265358979323846264 #define link himomisubmitted #define rank thatsnoteasytask //************************************************************************************* int32_t solve(); int32_t main(int argc, char** argv) { ios_base::sync_with_stdio(0);cin.tie(0); #ifdef LOCAL FREIN("input.txt"); // FREOUT("out.txt"); #endif return solve(); } int a[101001]; int b[101010]; int dp[104040]; vector<int> positions[101010]; __m256i _mm256_load_pi32x4(const int *x) { return _mm256_cvtepi32_epi64(_mm_load_si128((const __m128i*)x)); } __m256i _mm256_set_pi32x4(int x) { return _mm256_cvtepi32_epi64(_mm_set1_epi32(x)); } ll getSumSlow(int* __restrict b, int* __restrict dp, int n) { ll res = 0; for (int i = 0; i < n; ++i) { res += b[i] > 0 ? dp[i] : 0; } return res; } ll getSum(int* __restrict b, int* __restrict dp, int n) { ll res = 0; __m256i y0 = _mm256_set_pi32x4(0); __m256i y7 = _mm256_set_pi32x4(0); while (n % 16) { --n; res += b[0] > 0 ? dp[0] : 0; ++b; ++dp; } n /= 16; while (n--) { __m256i y1 = _mm256_cmpgt_epi64( _mm256_load_pi32x4(b), y0); __m256i y2 = _mm256_cmpgt_epi64( _mm256_load_pi32x4(b + 4), y0); __m256i y3 = _mm256_cmpgt_epi64( _mm256_load_pi32x4(b + 8), y0); __m256i y4 = _mm256_cmpgt_epi64( _mm256_load_pi32x4(b + 12), y0); y7 = _mm256_add_epi64(y7, _mm256_and_si256(y1, _mm256_load_pi32x4(dp))); y7 = _mm256_add_epi64(y7, _mm256_and_si256(y1, _mm256_load_pi32x4(dp + 4))); y7 = _mm256_add_epi64(y7, _mm256_and_si256(y1, _mm256_load_pi32x4(dp + 8))); y7 = _mm256_add_epi64(y7, _mm256_and_si256(y1, _mm256_load_pi32x4(dp + 12))); b += 16; dp += 16; } ll arr[4]; _mm256_store_si256((__m256i*)arr, y7); return res + arr[0] + arr[1] + arr[2] + arr[3]; } int solve() { int n = readInt(); int k = readInt(); FI(n) { b[i] += k + 1; } FI(n) { a[i] = readInt(); --a[i]; positions[i].push_back(-1); positions[i].push_back(-1); } dp[0] = 1; for (int i = 0; i < n; ++i) { int x = a[i]; { int to = positions[x].back(); int from = *++positions[x].rbegin() + 1; for (int j = from; j <= to; ++j) { b[j]++; } } int from = positions[x].back() + 1; for (int j = from; j <= i; ++j) { b[j]--; } ll res = getSum(b, dp, i + 1); dp[i + 1] = res % fftmod; positions[x].push_back(i); } cout << dp[n] << endl; return 0; }
1129_D. Isolation
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once. Since the answer can be large, find it modulo 998 244 353. Input The first line contains two space-separated integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the number of elements in the array a and the restriction from the statement. The following line contains n space-separated integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n) β€” elements of the array a. Output The first and only line contains the number of ways to divide an array a modulo 998 244 353. Examples Input 3 1 1 1 2 Output 3 Input 5 2 1 1 2 1 3 Output 14 Input 5 5 1 2 3 4 5 Output 16 Note In the first sample, the three possible divisions are as follows. * [[1], [1], [2]] * [[1, 1], [2]] * [[1, 1, 2]] Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
{ "input": [ "5 5\n1 2 3 4 5\n", "3 1\n1 1 2\n", "5 2\n1 1 2 1 3\n" ], "output": [ "16", "3", "14" ] }
{ "input": [ "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n", "100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n", "10 1\n4 2 9 2 1 4 4 1 4 10\n", "100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n", "1 1\n1\n", "100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n", "2 2\n2 1\n", "2 1\n2 2\n" ], "output": [ "16", "726975503", "24", "244208148", "1", "1", "2", "2" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> //#pragma GCC optimize("unroll-loops") //#pragma GCC optimize("-O3") //#pragma GCC optimize("Ofast") #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #include <ext/rope> #define sz(x) int(x.size()) #define all(x) x.begin(),x.end() #define pii pair<int,int> #define PB push_back #define pll pair<ll,ll> #define pii pair<int,int> #define pil pair<int,ll> #define pli pair<ll, int> #define pi4 pair<pii, pii> #define pib pair<int, bool> #define ft first #define sd second using namespace std; using namespace __gnu_cxx; using namespace __gnu_pbds; typedef long long ll; typedef long double ld; template<class T> using ordered_set = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>; const int N = 100100; //const int N = 2010; const int M = (1 << 10); const int oo = 2e9; const ll OO = 1e18; const int PW = 20; const int md = 998244353; const int BLOCK = 750; //const int BLOCK = 1; const int K = N / BLOCK + 10; int f[N], SQ[K][2 * N], mid[K], sum[K], a[N], n, k, ppr[N], pr[N], vl[N]; int SUM(int x, int y) { return (x + y) % md; } int SUB(int x, int y) { return (x + md - y) % md; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); // freopen("in.txt","r",stdin); cin >> n >> k; for (int i = 0; i < n; i++) { cin >> a[i]; a[i]--; } fill(mid, mid + K, N); for (int i = 0; i < n; i++){ ppr[i] = pr[i] = -1; } for (int i = 0; i < n; i++){ int l = ppr[a[i]], r = pr[a[i]]; swap(ppr[a[i]], pr[a[i]]); pr[a[i]] = i; int nm = i / BLOCK; sum[nm] = SUM(sum[nm], (i ? f[i - 1] : 1)); SQ[nm][mid[nm]] += (i ? f[i - 1] : 1); for (int j = max(r + 1, BLOCK * nm); j <= i; j++){ SQ[nm][mid[nm] + vl[j]] = SUB(SQ[nm][mid[nm] + vl[j]], (j ? f[j - 1] : 1)); vl[j]++; SQ[nm][mid[nm] + vl[j]] = SUM(SQ[nm][mid[nm] + vl[j]], (j ? f[j - 1] : 1)); if (vl[j] == k + 1) sum[nm] = SUB(sum[nm], (j ? f[j - 1] : 1)); } if ((r + 1) / BLOCK != nm){ nm--; int nd = (r + 1) / BLOCK; for (; nm > nd; nm--){ sum[nm] = SUB(sum[nm], SQ[nm][mid[nm] + k]); mid[nm]--; } for (int j = r + 1; j < (nm + 1) * BLOCK; j++){ SQ[nm][N + vl[j]] = SUB(SQ[nm][N + vl[j]], (j ? f[j - 1] : 1)); vl[j]++; SQ[nm][N + vl[j]] = SUM(SQ[nm][N + vl[j]], (j ? f[j - 1] : 1)); if (vl[j] + N - mid[nm] == k + 1) sum[nm] = SUB(sum[nm], (j ? f[j - 1] : 1)); } } if (r >= 0){ int nd = (l + 1) / BLOCK; if (nd < nm){ for (int j = BLOCK * nm; j < r + 1; j++){ SQ[nm][N + vl[j]] = SUB(SQ[nm][N + vl[j]], (j ? f[j - 1] : 1)); vl[j]--; SQ[nm][N + vl[j]] = SUM(SQ[nm][N + vl[j]], (j ? f[j - 1] : 1)); if (vl[j] + N - mid[nm] == k) sum[nm] = SUM(sum[nm], (j ? f[j - 1] : 1)); } nm--; } for (; nm > nd; nm--){ sum[nm] = SUM(sum[nm], SQ[nm][mid[nm] + k + 1]); mid[nm]++; } for (int j = l + 1; j < min(BLOCK * (nm + 1), r + 1); j++){ SQ[nm][N + vl[j]] = SUB(SQ[nm][N + vl[j]], (j ? f[j - 1] : 1)); vl[j]--; SQ[nm][N + vl[j]] = SUM(SQ[nm][N + vl[j]], (j ? f[j - 1] : 1)); if (vl[j] + N - mid[nm] == k) sum[nm] = SUM(sum[nm], (j ? f[j - 1] : 1)); } } for (int j = 0; j * BLOCK <= i; j++) f[i] = SUM(f[i], sum[j]); nm = i / BLOCK; } cout << f[n - 1]; return 0; }
1129_D. Isolation
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once. Since the answer can be large, find it modulo 998 244 353. Input The first line contains two space-separated integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the number of elements in the array a and the restriction from the statement. The following line contains n space-separated integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n) β€” elements of the array a. Output The first and only line contains the number of ways to divide an array a modulo 998 244 353. Examples Input 3 1 1 1 2 Output 3 Input 5 2 1 1 2 1 3 Output 14 Input 5 5 1 2 3 4 5 Output 16 Note In the first sample, the three possible divisions are as follows. * [[1], [1], [2]] * [[1, 1], [2]] * [[1, 1, 2]] Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
{ "input": [ "5 5\n1 2 3 4 5\n", "3 1\n1 1 2\n", "5 2\n1 1 2 1 3\n" ], "output": [ "16", "3", "14" ] }
{ "input": [ "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n", "100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n", "10 1\n4 2 9 2 1 4 4 1 4 10\n", "100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n", "1 1\n1\n", "100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n", "2 2\n2 1\n", "2 1\n2 2\n" ], "output": [ "16", "726975503", "24", "244208148", "1", "1", "2", "2" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <typename T> bool GetMax(T &a, T b) { return (a < b) ? (a = b, true) : false; } template <typename T> bool GetMin(T &a, T b) { return (a > b) ? (a = b, true) : false; } const int N = 1e5 + 10; const int B = 20; const int MOD = 998244353; int D(int x, int y) { return ((x -= y) < 0) ? (x + MOD) : x; } int U(int x, int y) { return ((x += y) >= MOD) ? (x - MOD) : x; } int n, ki, a[N]; int mn[N << 2], mx[N << 2], sm[N << 2], tr[N << 2], lz[N << 2]; void Update(int p, int x, int o = 1, int l = 1, int r = n) { sm[o] += x; tr[o] += x; if (l == r) return; int mid = (l + r) >> 1; if (p <= mid) Update(p, x, o << 1, l, mid); else Update(p, x, (o << 1) ^ 1, mid + 1, r); } void Apply(int o, int x, int l, int r) { mn[o] += x; mx[o] += x; if (l == r) (mx[o] <= ki) ? (tr[o] = sm[o]) : (tr[o] = 0); else if (mx[o] <= ki) { lz[o] += x; tr[o] = sm[o]; } else if (mn[o] > ki) { lz[o] += x; tr[o] = 0; } else { int mid = (l + r) >> 1; Apply(o << 1, x, l, mid); Apply((o << 1) ^ 1, x, mid + 1, r); tr[o] = U(tr[o << 1], tr[(o << 1) ^ 1]); } } void Pushdown(int o, int l, int r) { if (!lz[o] || l == r) return; int mid = (r + l) >> 1; Apply(o << 1, lz[o], l, mid); Apply((o << 1) ^ 1, lz[o], mid + 1, r); lz[o] = 0; } void Add(int ql, int qr, int x, int o = 1, int l = 1, int r = n) { Pushdown(o, l, r); if (ql <= l && r <= qr) { Apply(o, x, l, r); return; } int mid = (l + r) >> 1, lc = o << 1, rc = lc ^ 1; if (ql <= mid) Add(ql, qr, x, lc, l, mid); if (qr > mid) Add(ql, qr, x, rc, mid + 1, r); mn[o] = min(mn[lc], mn[rc]); mx[o] = max(mx[lc], mx[rc]); tr[o] = U(tr[lc], tr[rc]); } int lst[N][2], ans; void Work() { Update(1, 1); for (int i = 1; i <= n; ++i) { if (lst[a[i]][1]) Add(lst[a[i]][0] + 1, lst[a[i]][1], -1); Add(lst[a[i]][1] + 1, i, 1); ans = tr[1]; if (i == n) break; Update(i + 1, ans); lst[a[i]][0] = lst[a[i]][1]; lst[a[i]][1] = i; } } int main() { scanf("%d%d", &n, &ki); for (int i = 1; i <= n; ++i) scanf("%d", a + i); Work(); printf("%d", ans); return 0; }
1129_D. Isolation
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once. Since the answer can be large, find it modulo 998 244 353. Input The first line contains two space-separated integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the number of elements in the array a and the restriction from the statement. The following line contains n space-separated integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n) β€” elements of the array a. Output The first and only line contains the number of ways to divide an array a modulo 998 244 353. Examples Input 3 1 1 1 2 Output 3 Input 5 2 1 1 2 1 3 Output 14 Input 5 5 1 2 3 4 5 Output 16 Note In the first sample, the three possible divisions are as follows. * [[1], [1], [2]] * [[1, 1], [2]] * [[1, 1, 2]] Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
{ "input": [ "5 5\n1 2 3 4 5\n", "3 1\n1 1 2\n", "5 2\n1 1 2 1 3\n" ], "output": [ "16", "3", "14" ] }
{ "input": [ "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n", "100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n", "10 1\n4 2 9 2 1 4 4 1 4 10\n", "100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n", "1 1\n1\n", "100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n", "2 2\n2 1\n", "2 1\n2 2\n" ], "output": [ "16", "726975503", "24", "244208148", "1", "1", "2", "2" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> #pragma comment(linker, "/stack:200000000") #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,avx,avx2") using namespace std; template <class T1, class T2, class T3> struct triple { T1 a; T2 b; T3 c; triple() : a(T1()), b(T2()), c(T3()){}; triple(T1 _a, T2 _b, T3 _c) : a(_a), b(_b), c(_c) {} }; template <class T1, class T2, class T3> bool operator<(const triple<T1, T2, T3>& t1, const triple<T1, T2, T3>& t2) { if (t1.a != t2.a) return t1.a < t2.a; else if (t1.b != t2.b) return t1.b < t2.b; else return t1.c < t2.c; } template <class T1, class T2, class T3> bool operator>(const triple<T1, T2, T3>& t1, const triple<T1, T2, T3>& t2) { if (t1.a != t2.a) return t1.a > t2.a; else if (t1.b != t2.b) return t1.b > t2.b; else return t1.c > t2.c; } inline int countBits(unsigned int v) { v = v - ((v >> 1) & 0x55555555); v = (v & 0x33333333) + ((v >> 2) & 0x33333333); return ((v + (v >> 4) & 0xF0F0F0F) * 0x1010101) >> 24; } inline int countBits(unsigned long long v) { unsigned int t = v >> 32; unsigned int p = (v & ((1ULL << 32) - 1)); return countBits(t) + countBits(p); } inline int countBits(long long v) { return countBits((unsigned long long)v); } inline int countBits(int v) { return countBits((unsigned int)v); } unsigned int reverseBits(unsigned int x) { x = (((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1)); x = (((x & 0xcccccccc) >> 2) | ((x & 0x33333333) << 2)); x = (((x & 0xf0f0f0f0) >> 4) | ((x & 0x0f0f0f0f) << 4)); x = (((x & 0xff00ff00) >> 8) | ((x & 0x00ff00ff) << 8)); return ((x >> 16) | (x << 16)); } template <class T> inline int sign(T x) { return x > 0 ? 1 : x < 0 ? -1 : 0; } inline bool isPowerOfTwo(int x) { return (x != 0 && (x & (x - 1)) == 0); } constexpr long long power(long long x, int p) { return p == 0 ? 1 : (x * power(x, p - 1)); } template <class T> inline bool inRange(const T& val, const T& min, const T& max) { return min <= val && val <= max; } unsigned long long rdtsc() { unsigned long long ret = 0; return __builtin_readcyclecounter(); asm volatile("rdtsc" : "=A"(ret) : :); return ret; } const int __BS = 4096; static char __bur[__BS + 16], *__er = __bur + __BS, *__ir = __er; template <class T = int> T readInt() { auto c = [&]() { if (__ir == __er) std::fill(__bur, __bur + __BS, 0), cin.read(__bur, __BS), __ir = __bur; }; c(); while (*__ir && (*__ir < '0' || *__ir > '9') && *__ir != '-') ++__ir; c(); bool m = false; if (*__ir == '-') ++__ir, c(), m = true; T r = 0; while (*__ir >= '0' && *__ir <= '9') r = r * 10 + *__ir - '0', ++__ir, c(); ++__ir; return m ? -r : r; } string readString() { auto c = [&]() { if (__ir == __er) std::fill(__bur, __bur + __BS, 0), cin.read(__bur, __BS), __ir = __bur; }; string r; c(); while (*__ir && isspace(*__ir)) ++__ir, c(); while (!isspace(*__ir)) r.push_back(*__ir), ++__ir, c(); ++__ir; return r; } char readChar() { auto c = [&]() { if (__ir == __er) std::fill(__bur, __bur + __BS, 0), cin.read(__bur, __BS), __ir = __bur; }; c(); while (*__ir && isspace(*__ir)) ++__ir, c(); return *__ir++; } static char __buw[__BS + 20], *__iw = __buw, *__ew = __buw + __BS; template <class T> void writeInt(T x, char endc = '\n') { if (x < 0) *__iw++ = '-', x = -x; if (x == 0) *__iw++ = '0'; char* s = __iw; while (x) { T t = x / 10; char c = x - 10 * t + '0'; *__iw++ = c; x = t; } char* f = __iw - 1; while (s < f) swap(*s, *f), ++s, --f; if (__iw > __ew) cout.write(__buw, __iw - __buw), __iw = __buw; if (endc) { *__iw++ = endc; } } template <class T> void writeStr(const T& str) { int i = 0; while (str[i]) { *__iw++ = str[i++]; if (__iw > __ew) cout.write(__buw, __iw - __buw), __iw = __buw; } } struct __FL__ { ~__FL__() { if (__iw != __buw) cout.write(__buw, __iw - __buw); } }; static __FL__ __flushVar__; template <class T1, class T2> ostream& operator<<(ostream& os, const pair<T1, T2>& p); template <class T> ostream& operator<<(ostream& os, const vector<T>& v); template <class T1, class T2> ostream& operator<<(ostream& os, const set<T1, T2>& v); template <class T1, class T2> ostream& operator<<(ostream& os, const multiset<T1, T2>& v); template <class T1, class T2> ostream& operator<<(ostream& os, priority_queue<T1, T2> v); template <class T1, class T2, class T3> ostream& operator<<(ostream& os, const map<T1, T2, T3>& v); template <class T1, class T2, class T3> ostream& operator<<(ostream& os, const multimap<T1, T2, T3>& v); template <class T1, class T2, class T3> ostream& operator<<(ostream& os, const triple<T1, T2, T3>& t); template <class T, size_t N> ostream& operator<<(ostream& os, const array<T, N>& v); template <class T1, class T2> ostream& operator<<(ostream& os, const pair<T1, T2>& p) { return os << "(" << p.first << ", " << p.second << ")"; } template <class T> ostream& operator<<(ostream& os, const vector<T>& v) { bool f = 1; os << "["; for (auto& i : v) { if (!f) os << ", "; os << i; f = 0; } return os << "]"; } template <class T, size_t N> ostream& operator<<(ostream& os, const array<T, N>& v) { bool f = 1; os << "["; for (auto& i : v) { if (!f) os << ", "; os << i; f = 0; } return os << "]"; } template <class T1, class T2> ostream& operator<<(ostream& os, const set<T1, T2>& v) { bool f = 1; os << "["; for (auto& i : v) { if (!f) os << ", "; os << i; f = 0; } return os << "]"; } template <class T1, class T2> ostream& operator<<(ostream& os, const multiset<T1, T2>& v) { bool f = 1; os << "["; for (auto& i : v) { if (!f) os << ", "; os << i; f = 0; } return os << "]"; } template <class T1, class T2, class T3> ostream& operator<<(ostream& os, const map<T1, T2, T3>& v) { bool f = 1; os << "["; for (auto& ii : v) { if (!f) os << ", "; os << "(" << ii.first << " -> " << ii.second << ") "; f = 0; } return os << "]"; } template <class T1, class T2> ostream& operator<<(ostream& os, const multimap<T1, T2>& v) { bool f = 1; os << "["; for (auto& ii : v) { if (!f) os << ", "; os << "(" << ii.first << " -> " << ii.second << ") "; f = 0; } return os << "]"; } template <class T1, class T2> ostream& operator<<(ostream& os, priority_queue<T1, T2> v) { bool f = 1; os << "["; while (!v.empty()) { auto x = v.top(); v.pop(); if (!f) os << ", "; f = 0; os << x; } return os << "]"; } template <class T1, class T2, class T3> ostream& operator<<(ostream& os, const triple<T1, T2, T3>& t) { return os << "(" << t.a << ", " << t.b << ", " << t.c << ")"; } template <class T1, class T2> void printarray(const T1& a, T2 sz, T2 beg = 0) { for (T2 i = beg; i < sz; i++) cout << a[i] << " "; cout << endl; } template <class T1, class T2, class T3> inline istream& operator>>(istream& os, triple<T1, T2, T3>& t); template <class T1, class T2> inline istream& operator>>(istream& os, pair<T1, T2>& p) { return os >> p.first >> p.second; } template <class T> inline istream& operator>>(istream& os, vector<T>& v) { if (v.size()) for (T& t : v) os >> t; else { string s; T obj; while (s.empty()) { getline(os, s); if (!os) return os; } stringstream ss(s); while (ss >> obj) v.push_back(obj); } return os; } template <class T1, class T2, class T3> inline istream& operator>>(istream& os, triple<T1, T2, T3>& t) { return os >> t.a >> t.b >> t.c; } template <class T1, class T2> inline pair<T1, T2> operator+(const pair<T1, T2>& p1, const pair<T1, T2>& p2) { return pair<T1, T2>(p1.first + p2.first, p1.second + p2.second); } template <class T1, class T2> inline pair<T1, T2>& operator+=(pair<T1, T2>& p1, const pair<T1, T2>& p2) { p1.first += p2.first, p1.second += p2.second; return p1; } template <class T1, class T2> inline pair<T1, T2> operator-(const pair<T1, T2>& p1, const pair<T1, T2>& p2) { return pair<T1, T2>(p1.first - p2.first, p1.second - p2.second); } template <class T1, class T2> inline pair<T1, T2>& operator-=(pair<T1, T2>& p1, const pair<T1, T2>& p2) { p1.first -= p2.first, p1.second -= p2.second; return p1; } const int USUAL_MOD = 1000000007; template <class T> inline void normmod(T& x, T m = USUAL_MOD) { x %= m; if (x < 0) x += m; } template <class T1, class T2> inline T2 summodfast(T1 x, T1 y, T2 m = USUAL_MOD) { T2 res = x + y; if (res >= m) res -= m; return res; } template <class T1, class T2, class T3 = int> inline void addmodfast(T1& x, T2 y, T3 m = USUAL_MOD) { x += y; if (x >= m) x -= m; } template <class T1, class T2, class T3 = int> inline void submodfast(T1& x, T2 y, T3 m = USUAL_MOD) { x -= y; if (x < 0) x += m; } inline long long mulmod(long long x, long long n, long long m) { x %= m; n %= m; long long r = x * n - long long(long double(x) * long double(n) / long double(m)) * m; while (r < 0) r += m; while (r >= m) r -= m; return r; } inline long long powmod(long long x, long long n, long long m) { long long r = 1; normmod(x, m); while (n) { if (n & 1) r *= x; x *= x; r %= m; x %= m; n /= 2; } return r; } inline long long powmulmod(long long x, long long n, long long m) { long long res = 1; normmod(x, m); while (n) { if (n & 1) res = mulmod(res, x, m); x = mulmod(x, x, m); n /= 2; } return res; } template <class T> inline T gcd(T a, T b) { while (b) { T t = a % b; a = b; b = t; } return a; } template <class T> T fast_gcd(T u, T v) { int shl = 0; while (u && v && u != v) { T eu = u & 1; u >>= eu ^ 1; T ev = v & 1; v >>= ev ^ 1; shl += (~(eu | ev) & 1); T d = u & v & 1 ? (u + v) >> 1 : 0; T dif = (u - v) >> (sizeof(T) * 8 - 1); u -= d & ~dif; v -= d & dif; } return std::max(u, v) << shl; } inline long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } template <class T> inline T gcd(T a, T b, T c) { return gcd(gcd(a, b), c); } long long gcdex(long long a, long long b, long long& x, long long& y) { if (!a) { x = 0; y = 1; return b; } long long y1; long long d = gcdex(b % a, a, y, y1); x = y1 - (b / a) * y; return d; } bool isPrime(long long n) { if (n <= (1 << 14)) { int x = (int)n; if (x <= 4 || x % 2 == 0 || x % 3 == 0) return x == 2 || x == 3; for (int i = 5; i * i <= x; i += 6) if (x % i == 0 || x % (i + 2) == 0) return 0; return 1; } long long s = n - 1; int t = 0; while (s % 2 == 0) { s /= 2; ++t; } for (int a : {2, 325, 9375, 28178, 450775, 9780504, 1795265022}) { if (!(a %= n)) return true; long long f = powmulmod(a, s, n); if (f == 1 || f == n - 1) continue; for (int i = 1; i < t; ++i) if ((f = mulmod(f, f, n)) == n - 1) goto nextp; return false; nextp:; } return true; } int32_t solve(); int32_t main(int argc, char** argv) { ios_base::sync_with_stdio(0); cin.tie(0); return solve(); } int a[101001]; vector<int> positions[101010]; int tr[404040]; int b[101010]; int dp[404040]; long long getSum(int* __restrict b, int* __restrict dp, int n) { long long res = 0; for (int j = 0; j < n; ++j) { res += b[j] > 0 ? 0 : dp[j]; } return res; } int solve() { int n, k; cin >> n >> k; for (int i = 0; i < (n); ++i) { b[i] -= k; } for (int i = 0; i < (n); ++i) { cin >> a[i]; --a[i]; } dp[0] = 1; for (int i = 0; i < n; ++i) { int x = a[i]; if (!positions[x].empty()) { int to = positions[x].back(); int from = positions[x].size() > 1 ? *--positions[x].rbegin() : 0; for (int j = from; j <= to; ++j) { b[j]--; } } int from = positions[x].empty() ? 0 : (positions[x].back() + 1); for (int j = from; j <= i; ++j) { b[j]++; } long long res = getSum(b, dp, i + 1); dp[i + 1] = res % 998244353; positions[x].push_back(i); } cout << dp[n] << endl; return 0; }
1129_D. Isolation
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once. Since the answer can be large, find it modulo 998 244 353. Input The first line contains two space-separated integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the number of elements in the array a and the restriction from the statement. The following line contains n space-separated integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n) β€” elements of the array a. Output The first and only line contains the number of ways to divide an array a modulo 998 244 353. Examples Input 3 1 1 1 2 Output 3 Input 5 2 1 1 2 1 3 Output 14 Input 5 5 1 2 3 4 5 Output 16 Note In the first sample, the three possible divisions are as follows. * [[1], [1], [2]] * [[1, 1], [2]] * [[1, 1, 2]] Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
{ "input": [ "5 5\n1 2 3 4 5\n", "3 1\n1 1 2\n", "5 2\n1 1 2 1 3\n" ], "output": [ "16", "3", "14" ] }
{ "input": [ "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n", "100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n", "10 1\n4 2 9 2 1 4 4 1 4 10\n", "100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n", "1 1\n1\n", "100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n", "2 2\n2 1\n", "2 1\n2 2\n" ], "output": [ "16", "726975503", "24", "244208148", "1", "1", "2", "2" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> namespace IOStream { const int MAXR = 10000000; char _READ_[MAXR], _PRINT_[MAXR]; int _READ_POS_, _PRINT_POS_, _READ_LEN_; inline char readc() { if (!_READ_POS_) _READ_LEN_ = fread(_READ_, 1, MAXR, stdin); char c = _READ_[_READ_POS_++]; if (_READ_POS_ == MAXR) _READ_POS_ = 0; if (_READ_POS_ > _READ_LEN_) return 0; return c; } template <typename T> inline void read(T &x) { x = 0; register int flag = 1, c; while (((c = readc()) < '0' || c > '9') && c != '-') ; if (c == '-') flag = -1; else x = c - '0'; while ((c = readc()) >= '0' && c <= '9') x = x * 10 - '0' + c; x *= flag; } template <typename T1, typename... T2> inline void read(T1 &a, T2 &...x) { read(a), read(x...); } inline int reads(char *s) { register int len = 0, c; while (isspace(c = readc()) || !c) ; s[len++] = c; while (!isspace(c = readc()) && c) s[len++] = c; s[len] = 0; return len; } inline void ioflush() { fwrite(_PRINT_, 1, _PRINT_POS_, stdout), _PRINT_POS_ = 0; fflush(stdout); } inline void printc(char c) { if (!c) return; _PRINT_[_PRINT_POS_++] = c; if (_PRINT_POS_ == MAXR) ioflush(); } inline void prints(const char *s, char c) { for (int i = 0; s[i]; i++) printc(s[i]); printc(c); } template <typename T> inline void print(T x, char c = '\n') { if (x < 0) printc('-'), x = -x; if (x) { static char sta[20]; register int tp = 0; for (; x; x /= 10) sta[tp++] = x % 10 + '0'; while (tp > 0) printc(sta[--tp]); } else printc('0'); printc(c); } template <typename T1, typename... T2> inline void print(T1 x, T2... y) { print(x, ' '), print(y...); } } // namespace IOStream using namespace IOStream; using namespace std; const int MAXN = 100005, MOD = 998244353; int f[MAXN], lst[MAXN], now[MAXN], num[MAXN], sum[MAXN], n, K; int main() { read(n, K); f[0] = sum[0] = 1; for (int i = 1; i <= n; i++) { int t; read(t); long long s = (lst[t] ? sum[lst[t] - 1] : 0); for (int j = lst[t] + 1; j <= now[t]; j++) { if ((--num[j]) <= K) s += f[j - 1]; sum[j - 1] = s; } for (int j = now[t] + 1; j <= i; j++) { if ((++num[j]) <= K) s += f[j - 1]; sum[j - 1] = s; } f[i] = s % MOD; sum[i] = (sum[i - 1] + f[i]) % MOD; lst[t] = now[t], now[t] = i; } printf("%d\n", f[n]); return 0; }
1129_D. Isolation
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once. Since the answer can be large, find it modulo 998 244 353. Input The first line contains two space-separated integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the number of elements in the array a and the restriction from the statement. The following line contains n space-separated integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n) β€” elements of the array a. Output The first and only line contains the number of ways to divide an array a modulo 998 244 353. Examples Input 3 1 1 1 2 Output 3 Input 5 2 1 1 2 1 3 Output 14 Input 5 5 1 2 3 4 5 Output 16 Note In the first sample, the three possible divisions are as follows. * [[1], [1], [2]] * [[1, 1], [2]] * [[1, 1, 2]] Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
{ "input": [ "5 5\n1 2 3 4 5\n", "3 1\n1 1 2\n", "5 2\n1 1 2 1 3\n" ], "output": [ "16", "3", "14" ] }
{ "input": [ "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n", "100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n", "10 1\n4 2 9 2 1 4 4 1 4 10\n", "100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n", "1 1\n1\n", "100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n", "2 2\n2 1\n", "2 1\n2 2\n" ], "output": [ "16", "726975503", "24", "244208148", "1", "1", "2", "2" ] }
IN-CORRECT
cpp
#include<bits/stdc++.h> #define LL long long #define rep(i, s, t) for(register int i = s, i##end = t; i <= i##end; ++i) #define dwn(i, s, t) for(register int i = s, i##end = t; i >= i##end; --i) using namespace std; inline int read() { int x = 0, f = 1; char ch = getchar(); for(;!isdigit(ch);ch=getchar())if(ch == '-')f = -f; for(;isdigit(ch);ch=getchar())x = 10 * x + ch - '0'; return x * f; } const int mod = 998244353,maxn = 1e5 + 10; int n,k; int a[maxn], b[maxn], nx[maxn], cur[maxn], f[maxn], bl[maxn]; inline void inc(int &x, int y) {x += y; if(x >= mod) x -= mod;} struct block { int l, r, sum, sumf[700], sz; void calsum() { memset(sumf, 0, sizeof(sumf)); int cur = 0; dwn(i, r, l) { cur += b[i]; inc(sumf[sz + cur], f[i - 1]); } rep(i, 1, sz + sz) inc(sumf[i], sumf[i - 1]); } int query(int pos) { if(pos + sz < 0)return 0; return sumf[min(pos + sz, sz + sz)]; } }bs[350]; inline void update(int pos, int val) { f[pos] = val; bs[bl[pos + 1]].calsum(); } int query(int x) { int ans = bs[bl[x]].query(k), cursum = bs[bl[x]].sum; dwn(i, bl[x]-1, 1) { inc(ans, bs[i].query(k - cursum)); cursum += bs[i].sum; } return ans; } int main() { n = read(), k = read(); rep(i, 1, n) a[i] = read(); rep(i, 1, n) nx[i] = cur[a[i]], cur[a[i]] = i; f[0] = 1; int kk = sqrt(n); rep(i, 1, n) bl[i] = (i - 1) / kk + 1; rep(i, 1, bl[n]) { bs[i].r = min(i * kk, n); bs[i].l = bs[i-1].r + 1; bs[i].sz = bs[i].r - bs[i].l + 1; }bs[1].calsum(); rep(i, 1, n) { if(nx[nx[i]]) { int pos = nx[nx[i]]; b[pos]++; bs[bl[pos]].sum++; bs[bl[pos]].calsum(); } else if(nx[i]) { int pos = nx[i]; b[pos] -= 2; bs[bl[pos]].sum -= 2; bs[bl[pos]].calsum(); } b[i]++; bs[bl[i]].sum++; bs[bl[i]].calsum(); update(i, (f[i] = query(i))); } cout << f[n] << endl; }
1129_D. Isolation
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once. Since the answer can be large, find it modulo 998 244 353. Input The first line contains two space-separated integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the number of elements in the array a and the restriction from the statement. The following line contains n space-separated integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n) β€” elements of the array a. Output The first and only line contains the number of ways to divide an array a modulo 998 244 353. Examples Input 3 1 1 1 2 Output 3 Input 5 2 1 1 2 1 3 Output 14 Input 5 5 1 2 3 4 5 Output 16 Note In the first sample, the three possible divisions are as follows. * [[1], [1], [2]] * [[1, 1], [2]] * [[1, 1, 2]] Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
{ "input": [ "5 5\n1 2 3 4 5\n", "3 1\n1 1 2\n", "5 2\n1 1 2 1 3\n" ], "output": [ "16", "3", "14" ] }
{ "input": [ "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n", "100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n", "10 1\n4 2 9 2 1 4 4 1 4 10\n", "100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n", "1 1\n1\n", "100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n", "2 2\n2 1\n", "2 1\n2 2\n" ], "output": [ "16", "726975503", "24", "244208148", "1", "1", "2", "2" ] }
IN-CORRECT
python3
print (16)
1129_D. Isolation
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once. Since the answer can be large, find it modulo 998 244 353. Input The first line contains two space-separated integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the number of elements in the array a and the restriction from the statement. The following line contains n space-separated integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n) β€” elements of the array a. Output The first and only line contains the number of ways to divide an array a modulo 998 244 353. Examples Input 3 1 1 1 2 Output 3 Input 5 2 1 1 2 1 3 Output 14 Input 5 5 1 2 3 4 5 Output 16 Note In the first sample, the three possible divisions are as follows. * [[1], [1], [2]] * [[1, 1], [2]] * [[1, 1, 2]] Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
{ "input": [ "5 5\n1 2 3 4 5\n", "3 1\n1 1 2\n", "5 2\n1 1 2 1 3\n" ], "output": [ "16", "3", "14" ] }
{ "input": [ "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n", "100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n", "10 1\n4 2 9 2 1 4 4 1 4 10\n", "100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n", "1 1\n1\n", "100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n", "2 2\n2 1\n", "2 1\n2 2\n" ], "output": [ "16", "726975503", "24", "244208148", "1", "1", "2", "2" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> #pragma comment(linker, "/stack:200000000") #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,avx,avx2") using namespace std; template <class T1, class T2, class T3> struct triple { T1 a; T2 b; T3 c; triple() : a(T1()), b(T2()), c(T3()){}; triple(T1 _a, T2 _b, T3 _c) : a(_a), b(_b), c(_c) {} }; template <class T1, class T2, class T3> bool operator<(const triple<T1, T2, T3>& t1, const triple<T1, T2, T3>& t2) { if (t1.a != t2.a) return t1.a < t2.a; else if (t1.b != t2.b) return t1.b < t2.b; else return t1.c < t2.c; } template <class T1, class T2, class T3> bool operator>(const triple<T1, T2, T3>& t1, const triple<T1, T2, T3>& t2) { if (t1.a != t2.a) return t1.a > t2.a; else if (t1.b != t2.b) return t1.b > t2.b; else return t1.c > t2.c; } inline int countBits(unsigned int v) { v = v - ((v >> 1) & 0x55555555); v = (v & 0x33333333) + ((v >> 2) & 0x33333333); return ((v + (v >> 4) & 0xF0F0F0F) * 0x1010101) >> 24; } inline int countBits(unsigned long long v) { unsigned int t = v >> 32; unsigned int p = (v & ((1ULL << 32) - 1)); return countBits(t) + countBits(p); } inline int countBits(long long v) { return countBits((unsigned long long)v); } inline int countBits(int v) { return countBits((unsigned int)v); } unsigned int reverseBits(unsigned int x) { x = (((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1)); x = (((x & 0xcccccccc) >> 2) | ((x & 0x33333333) << 2)); x = (((x & 0xf0f0f0f0) >> 4) | ((x & 0x0f0f0f0f) << 4)); x = (((x & 0xff00ff00) >> 8) | ((x & 0x00ff00ff) << 8)); return ((x >> 16) | (x << 16)); } template <class T> inline int sign(T x) { return x > 0 ? 1 : x < 0 ? -1 : 0; } inline bool isPowerOfTwo(int x) { return (x != 0 && (x & (x - 1)) == 0); } constexpr long long power(long long x, int p) { return p == 0 ? 1 : (x * power(x, p - 1)); } template <class T> inline bool inRange(const T& val, const T& min, const T& max) { return min <= val && val <= max; } unsigned long long rdtsc() { unsigned long long ret = 0; return __builtin_readcyclecounter(); asm volatile("rdtsc" : "=A"(ret) : :); return ret; } const int __BS = 4096; static char __bur[__BS + 16], *__er = __bur + __BS, *__ir = __er; template <class T = int> T readInt() { auto c = [&]() { if (__ir == __er) std::fill(__bur, __bur + __BS, 0), cin.read(__bur, __BS), __ir = __bur; }; c(); while (*__ir && (*__ir < '0' || *__ir > '9') && *__ir != '-') ++__ir; c(); bool m = false; if (*__ir == '-') ++__ir, c(), m = true; T r = 0; while (*__ir >= '0' && *__ir <= '9') r = r * 10 + *__ir - '0', ++__ir, c(); ++__ir; return m ? -r : r; } string readString() { auto c = [&]() { if (__ir == __er) std::fill(__bur, __bur + __BS, 0), cin.read(__bur, __BS), __ir = __bur; }; string r; c(); while (*__ir && isspace(*__ir)) ++__ir, c(); while (!isspace(*__ir)) r.push_back(*__ir), ++__ir, c(); ++__ir; return r; } char readChar() { auto c = [&]() { if (__ir == __er) std::fill(__bur, __bur + __BS, 0), cin.read(__bur, __BS), __ir = __bur; }; c(); while (*__ir && isspace(*__ir)) ++__ir, c(); return *__ir++; } static char __buw[__BS + 20], *__iw = __buw, *__ew = __buw + __BS; template <class T> void writeInt(T x, char endc = '\n') { if (x < 0) *__iw++ = '-', x = -x; if (x == 0) *__iw++ = '0'; char* s = __iw; while (x) { T t = x / 10; char c = x - 10 * t + '0'; *__iw++ = c; x = t; } char* f = __iw - 1; while (s < f) swap(*s, *f), ++s, --f; if (__iw > __ew) cout.write(__buw, __iw - __buw), __iw = __buw; if (endc) { *__iw++ = endc; } } template <class T> void writeStr(const T& str) { int i = 0; while (str[i]) { *__iw++ = str[i++]; if (__iw > __ew) cout.write(__buw, __iw - __buw), __iw = __buw; } } struct __FL__ { ~__FL__() { if (__iw != __buw) cout.write(__buw, __iw - __buw); } }; static __FL__ __flushVar__; template <class T1, class T2> ostream& operator<<(ostream& os, const pair<T1, T2>& p); template <class T> ostream& operator<<(ostream& os, const vector<T>& v); template <class T1, class T2> ostream& operator<<(ostream& os, const set<T1, T2>& v); template <class T1, class T2> ostream& operator<<(ostream& os, const multiset<T1, T2>& v); template <class T1, class T2> ostream& operator<<(ostream& os, priority_queue<T1, T2> v); template <class T1, class T2, class T3> ostream& operator<<(ostream& os, const map<T1, T2, T3>& v); template <class T1, class T2, class T3> ostream& operator<<(ostream& os, const multimap<T1, T2, T3>& v); template <class T1, class T2, class T3> ostream& operator<<(ostream& os, const triple<T1, T2, T3>& t); template <class T, size_t N> ostream& operator<<(ostream& os, const array<T, N>& v); template <class T1, class T2> ostream& operator<<(ostream& os, const pair<T1, T2>& p) { return os << "(" << p.first << ", " << p.second << ")"; } template <class T> ostream& operator<<(ostream& os, const vector<T>& v) { bool f = 1; os << "["; for (auto& i : v) { if (!f) os << ", "; os << i; f = 0; } return os << "]"; } template <class T, size_t N> ostream& operator<<(ostream& os, const array<T, N>& v) { bool f = 1; os << "["; for (auto& i : v) { if (!f) os << ", "; os << i; f = 0; } return os << "]"; } template <class T1, class T2> ostream& operator<<(ostream& os, const set<T1, T2>& v) { bool f = 1; os << "["; for (auto& i : v) { if (!f) os << ", "; os << i; f = 0; } return os << "]"; } template <class T1, class T2> ostream& operator<<(ostream& os, const multiset<T1, T2>& v) { bool f = 1; os << "["; for (auto& i : v) { if (!f) os << ", "; os << i; f = 0; } return os << "]"; } template <class T1, class T2, class T3> ostream& operator<<(ostream& os, const map<T1, T2, T3>& v) { bool f = 1; os << "["; for (auto& ii : v) { if (!f) os << ", "; os << "(" << ii.first << " -> " << ii.second << ") "; f = 0; } return os << "]"; } template <class T1, class T2> ostream& operator<<(ostream& os, const multimap<T1, T2>& v) { bool f = 1; os << "["; for (auto& ii : v) { if (!f) os << ", "; os << "(" << ii.first << " -> " << ii.second << ") "; f = 0; } return os << "]"; } template <class T1, class T2> ostream& operator<<(ostream& os, priority_queue<T1, T2> v) { bool f = 1; os << "["; while (!v.empty()) { auto x = v.top(); v.pop(); if (!f) os << ", "; f = 0; os << x; } return os << "]"; } template <class T1, class T2, class T3> ostream& operator<<(ostream& os, const triple<T1, T2, T3>& t) { return os << "(" << t.a << ", " << t.b << ", " << t.c << ")"; } template <class T1, class T2> void printarray(const T1& a, T2 sz, T2 beg = 0) { for (T2 i = beg; i < sz; i++) cout << a[i] << " "; cout << endl; } template <class T1, class T2, class T3> inline istream& operator>>(istream& os, triple<T1, T2, T3>& t); template <class T1, class T2> inline istream& operator>>(istream& os, pair<T1, T2>& p) { return os >> p.first >> p.second; } template <class T> inline istream& operator>>(istream& os, vector<T>& v) { if (v.size()) for (T& t : v) os >> t; else { string s; T obj; while (s.empty()) { getline(os, s); if (!os) return os; } stringstream ss(s); while (ss >> obj) v.push_back(obj); } return os; } template <class T1, class T2, class T3> inline istream& operator>>(istream& os, triple<T1, T2, T3>& t) { return os >> t.a >> t.b >> t.c; } template <class T1, class T2> inline pair<T1, T2> operator+(const pair<T1, T2>& p1, const pair<T1, T2>& p2) { return pair<T1, T2>(p1.first + p2.first, p1.second + p2.second); } template <class T1, class T2> inline pair<T1, T2>& operator+=(pair<T1, T2>& p1, const pair<T1, T2>& p2) { p1.first += p2.first, p1.second += p2.second; return p1; } template <class T1, class T2> inline pair<T1, T2> operator-(const pair<T1, T2>& p1, const pair<T1, T2>& p2) { return pair<T1, T2>(p1.first - p2.first, p1.second - p2.second); } template <class T1, class T2> inline pair<T1, T2>& operator-=(pair<T1, T2>& p1, const pair<T1, T2>& p2) { p1.first -= p2.first, p1.second -= p2.second; return p1; } const int USUAL_MOD = 1000000007; template <class T> inline void normmod(T& x, T m = USUAL_MOD) { x %= m; if (x < 0) x += m; } template <class T1, class T2> inline T2 summodfast(T1 x, T1 y, T2 m = USUAL_MOD) { T2 res = x + y; if (res >= m) res -= m; return res; } template <class T1, class T2, class T3 = int> inline void addmodfast(T1& x, T2 y, T3 m = USUAL_MOD) { x += y; if (x >= m) x -= m; } template <class T1, class T2, class T3 = int> inline void submodfast(T1& x, T2 y, T3 m = USUAL_MOD) { x -= y; if (x < 0) x += m; } inline long long mulmod(long long x, long long n, long long m) { x %= m; n %= m; long long r = x * n - long long(long double(x) * long double(n) / long double(m)) * m; while (r < 0) r += m; while (r >= m) r -= m; return r; } inline long long powmod(long long x, long long n, long long m) { long long r = 1; normmod(x, m); while (n) { if (n & 1) r *= x; x *= x; r %= m; x %= m; n /= 2; } return r; } inline long long powmulmod(long long x, long long n, long long m) { long long res = 1; normmod(x, m); while (n) { if (n & 1) res = mulmod(res, x, m); x = mulmod(x, x, m); n /= 2; } return res; } template <class T> inline T gcd(T a, T b) { while (b) { T t = a % b; a = b; b = t; } return a; } template <class T> T fast_gcd(T u, T v) { int shl = 0; while (u && v && u != v) { T eu = u & 1; u >>= eu ^ 1; T ev = v & 1; v >>= ev ^ 1; shl += (~(eu | ev) & 1); T d = u & v & 1 ? (u + v) >> 1 : 0; T dif = (u - v) >> (sizeof(T) * 8 - 1); u -= d & ~dif; v -= d & dif; } return std::max(u, v) << shl; } inline long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } template <class T> inline T gcd(T a, T b, T c) { return gcd(gcd(a, b), c); } long long gcdex(long long a, long long b, long long& x, long long& y) { if (!a) { x = 0; y = 1; return b; } long long y1; long long d = gcdex(b % a, a, y, y1); x = y1 - (b / a) * y; return d; } bool isPrime(long long n) { if (n <= (1 << 14)) { int x = (int)n; if (x <= 4 || x % 2 == 0 || x % 3 == 0) return x == 2 || x == 3; for (int i = 5; i * i <= x; i += 6) if (x % i == 0 || x % (i + 2) == 0) return 0; return 1; } long long s = n - 1; int t = 0; while (s % 2 == 0) { s /= 2; ++t; } for (int a : {2, 325, 9375, 28178, 450775, 9780504, 1795265022}) { if (!(a %= n)) return true; long long f = powmulmod(a, s, n); if (f == 1 || f == n - 1) continue; for (int i = 1; i < t; ++i) if ((f = mulmod(f, f, n)) == n - 1) goto nextp; return false; nextp:; } return true; } int32_t solve(); int32_t main(int argc, char** argv) { ios_base::sync_with_stdio(0); cin.tie(0); return solve(); } int a[101001]; int b[101010]; int dp[101010]; int p1[101010]; int p2[101010]; long long getSum(int* __restrict b, int* __restrict dp, int n) { long long res = 0; for (int j = 0; j < n; ++j) { res += b[j] > 0 ? 0 : dp[j]; } return res; } template <int val> void add(int* a, int n) { for (int i = 0; i < n; ++i) { a[i] += val; } } int solve() { int n = readInt(); int k = readInt(); for (int i = 0; i < (n); ++i) { b[i] -= k; } for (int i = 0; i < (n); ++i) { a[i] = readInt() - 1; p1[i] = p2[i] = -1; } dp[0] = 1; for (int i = 0; i < n; ++i) { int x = a[i]; add<-1>(b + p2[x] + 1, p1[x] - p2[x]); add<1>(b + p1[i] + 1, i - p1[i]); long long res = getSum(b, dp, i + 1); dp[i + 1] = res % 998244353; p2[x] = p1[x]; p1[x] = i; } cout << dp[n] << endl; return 0; }
1129_D. Isolation
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once. Since the answer can be large, find it modulo 998 244 353. Input The first line contains two space-separated integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the number of elements in the array a and the restriction from the statement. The following line contains n space-separated integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n) β€” elements of the array a. Output The first and only line contains the number of ways to divide an array a modulo 998 244 353. Examples Input 3 1 1 1 2 Output 3 Input 5 2 1 1 2 1 3 Output 14 Input 5 5 1 2 3 4 5 Output 16 Note In the first sample, the three possible divisions are as follows. * [[1], [1], [2]] * [[1, 1], [2]] * [[1, 1, 2]] Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
{ "input": [ "5 5\n1 2 3 4 5\n", "3 1\n1 1 2\n", "5 2\n1 1 2 1 3\n" ], "output": [ "16", "3", "14" ] }
{ "input": [ "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n", "100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n", "10 1\n4 2 9 2 1 4 4 1 4 10\n", "100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n", "1 1\n1\n", "100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n", "2 2\n2 1\n", "2 1\n2 2\n" ], "output": [ "16", "726975503", "24", "244208148", "1", "1", "2", "2" ] }
IN-CORRECT
cpp
#include<bits/stdc++.h> #define reg register #define F(i,a,b) for(reg int i=(a);i<=(b);i++) inline int read(); using namespace std; const int N=1e5+10,mod=998244353; int n,k,a[N],idx1[N],idx2[N],dp[N]; namespace block { #define val first #define id second pair<int,int>b[N]; int t,bel[N],L[N],R[N],add[N],c[N],s[N]; inline void init() { t=sqrt(n); F(i,1,t)L[i]=(i-1)*t+1,R[i]=i*t; if(R[t]<n)t++,L[t]=R[t-1]+1,R[t]=n; L[1]=0; F(i,1,t)F(j,L[i],R[i])bel[j]=i; } inline void reset(int i) { F(j,L[i],R[i])b[j]= {c[j],j}; sort(b+L[i],b+R[i]+1); s[L[i]]=dp[b[L[i]].id]; F(j,L[i]+1,R[i])s[j]=(s[j-1]+dp[b[j].id])%mod; } inline void _add(int l,int r,int ad) { if(r<l)return; int &st=bel[l],&ed=bel[r]; if(st==ed) { F(i,l,r)c[i]+=ad; return reset(st); } F(i,st+1,ed-1)add[i]+=ad; F(i,l,R[st])c[i]+=ad; F(i,L[ed],r)c[i]+=ad; reset(st),reset(ed); } inline int query(int l,int r) { if(r<l)return 0; int &st=bel[l],&ed=bel[r],ans=0; if(st==ed) { F(i,l,r)if(add[st]+c[i]<=k)ans=(ans+dp[i])%mod; return ans; } // whole F(i,st+1,ed-1) { // puts("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"); int ll=L[i],rr=R[i],pos=n+1; while(ll<=rr) { int mid=ll+rr; (add[i]+b[mid].val)<=k?pos=mid,ll=mid+1:rr=mid-1; } ans=(ans+s[pos])%mod; } //some of them F(i,l,R[st])if(add[st]+c[i]<=k)ans=(ans+dp[i])%mod; F(i,L[ed],r)if(add[ed]+c[i]<=k)ans=(ans+dp[i])%mod; return ans; } inline void show() { puts("------------------------------"); F(i,1,t)cout<<add[i]<<" "; cout<<endl<<endl; F(i,1,n)cout<<c[i]<<" "; cout<<endl<<endl; F(i,1,t)cout<<s[i]<<" "; cout<<endl; } inline void show_c(){ puts("c:"); F(i,1,n)cout<<c[i]<<" "; cout<<endl; } } int main() { n=read(),k=read(); F(i,1,n)a[i]=read(); block::init(); dp[0]=1; F(i,1,n) { // cout<<"i="<<i<<endl; // cout<<"d1="<<idx1[a[i]]<<" d2="<<idx2[a[i]]<<endl; block::_add(idx1[a[i]],i-1,1); // block::show_c(); block::_add(idx2[a[i]],idx1[a[i]]-1,-1); // block::show_c(); dp[i]=block::query(0,i-1); // cout<<"dp="<<dp[i]<<endl; idx2[a[i]]=idx1[a[i]]; idx1[a[i]]=i; // block::show(); // cout<<"d1="<<idx1[a[i]]<<" d2="<<idx2[a[i]]<<endl; } printf("%d",dp[n]); return 0; } inline int read() { reg int x=0; reg char c=getchar(); while(!isdigit(c))c=getchar(); while(isdigit(c))x=(x<<3)+(x<<1)+(c^48),c=getchar(); return x; } //dp[i]+=dp[j](s[j,i]<=k)
1129_D. Isolation
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once. Since the answer can be large, find it modulo 998 244 353. Input The first line contains two space-separated integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the number of elements in the array a and the restriction from the statement. The following line contains n space-separated integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n) β€” elements of the array a. Output The first and only line contains the number of ways to divide an array a modulo 998 244 353. Examples Input 3 1 1 1 2 Output 3 Input 5 2 1 1 2 1 3 Output 14 Input 5 5 1 2 3 4 5 Output 16 Note In the first sample, the three possible divisions are as follows. * [[1], [1], [2]] * [[1, 1], [2]] * [[1, 1, 2]] Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
{ "input": [ "5 5\n1 2 3 4 5\n", "3 1\n1 1 2\n", "5 2\n1 1 2 1 3\n" ], "output": [ "16", "3", "14" ] }
{ "input": [ "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n", "100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n", "10 1\n4 2 9 2 1 4 4 1 4 10\n", "100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n", "1 1\n1\n", "100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n", "2 2\n2 1\n", "2 1\n2 2\n" ], "output": [ "16", "726975503", "24", "244208148", "1", "1", "2", "2" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define sf scanf #define pf printf #define pb push_back #define mp make_pair #define PI ( acos(-1.0) ) #define mod 998244353 #define IN freopen("C.in","r",stdin) #define OUT freopen("output.txt","w",stdout) #define FOR(i,a,b) for(i=a ; i<=b ; i++) #define DBG pf("Hi\n") #define INF 1000000000000000000.0 #define i64 long long int #define eps (1e-100) #define xx first #define yy second #define ln 17 #define off 100002 using namespace __gnu_pbds; using namespace std ; typedef tree< i64, null_type, less<i64>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; typedef pair<i64,int> pii; #define maxn 100005 int block = 500 ; int n ; int ds[ 205 + 5 ][200005] , dp[maxn] , offset[maxn] , multiple[maxn] , sum[maxn] ; vector <int> occ[maxn] ; void add(int &x , int y) { x += y ; if( x >= mod ) x -= mod ; if( x < 0 ) x += mod ; } void addTo(int i, int val) { while( i <= n ) { if( i+block-1 <= n && multiple[i] ) { offset[i/block] += val ; i += block-1 ; } else{ add( ds[ i/block ][ sum[i]+off ] , -dp[i] ) ; sum[i] += val ; add( ds[ i/block ][ sum[i]+off ] , +dp[i] ) ; } i++ ; } } int findSum(int s,int x, int y) { x=0 ; int res = 0 ; while( x <= y ) { if( x+block-1 <= y && multiple[x] ) { add( res , ds[ x/block ][ s-offset[ x/block ] + off ] ) ; x += (block-1) ; } else{ if( s == sum[x] + offset[ x/block ] ) add( res , dp[ x ] ) ; } x++ ; } return res ; } int main() { int i , j , k , l , m ; dp[0] = 1 ; ds[0][0+off] = 1 ; int s = 0 ; scanf("%d %d",&n,&k) ; int sq = sqrt( n+0.0 ) ; sq = min(sq,200) ; block = n/sq ; for(i=0 ; i<=n ; i+=block) multiple[i] = 1 ; // printf("block: %d\n",block) ; int last = 0 ; for(i=1 ; i<=n ; i++) { int a ; scanf("%d",&a) ; int sz = (int)occ[a].size() ; int lastOcc = 0 ; if( sz!=0 ) lastOcc = occ[a].back() ; occ[a].pb( i ) ; int temp = dp[i-1] ; add(temp,dp[i-1]) ; add( temp , -findSum( s-k , lastOcc, i-2 ) ) ; add( temp , findSum( s-k , 0, lastOcc-1 ) ) ; s++ ; if( sz >= 1 ) { addTo( occ[a][sz-1] , -2 ) ; s -= 2 ; } if( sz >= 2 ) { addTo( occ[a][sz-2] , 1 ) ; s += 1 ; } if(i==1) temp = 1 ; dp[i] = temp ; add( ds[ i/block ][ s-offset[ i/block ] + off ] , dp[i] ) ; sum[i] = s - offset[ i/block ] ; // printf("%d:--> %d\n",i,dp[i]) ; } printf("%d\n" , dp[n] ) ; return 0 ; }
1129_D. Isolation
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once. Since the answer can be large, find it modulo 998 244 353. Input The first line contains two space-separated integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the number of elements in the array a and the restriction from the statement. The following line contains n space-separated integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n) β€” elements of the array a. Output The first and only line contains the number of ways to divide an array a modulo 998 244 353. Examples Input 3 1 1 1 2 Output 3 Input 5 2 1 1 2 1 3 Output 14 Input 5 5 1 2 3 4 5 Output 16 Note In the first sample, the three possible divisions are as follows. * [[1], [1], [2]] * [[1, 1], [2]] * [[1, 1, 2]] Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
{ "input": [ "5 5\n1 2 3 4 5\n", "3 1\n1 1 2\n", "5 2\n1 1 2 1 3\n" ], "output": [ "16", "3", "14" ] }
{ "input": [ "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n", "100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n", "10 1\n4 2 9 2 1 4 4 1 4 10\n", "100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n", "1 1\n1\n", "100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n", "2 2\n2 1\n", "2 1\n2 2\n" ], "output": [ "16", "726975503", "24", "244208148", "1", "1", "2", "2" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int mn = 1e5, msq = 317, mod = 998244353; int n, sq, k, a[mn + 3]; int inp[mn + 3], lw[msq + 3], rw[msq + 3]; int num[mn + 3]; int topw[mn + 3], secw[mn + 3]; long long f[mn + 3]; long long Sumf[msq + 3][2 * msq + 3]; int add[msq + 3]; void Add(int l, int r, int ad) { if (inp[l] >= inp[r]) { register int p = inp[l]; for (register int i = l; i <= r; i++) { Sumf[p][num[i]] -= f[i - 1]; num[i] += ad; Sumf[p][num[i]] += f[i - 1]; } return; } register int p = inp[l]; for (register int i = l; i <= rw[p]; i++) { Sumf[p][num[i]] -= f[i - 1]; num[i] += ad; Sumf[p][num[i]] += f[i - 1]; } for (p = inp[l] + 1; p <= inp[r] - 1; p++) add[p] += ad; p = inp[r]; for (register int i = lw[p]; i <= r; i++) { Sumf[p][num[i]] -= f[i - 1]; num[i] += ad; Sumf[p][num[i]] += f[i - 1]; } } int main() { cin >> n >> k; if (n > 1000) return 0; sq = sqrt(n); for (int i = 1; i <= n; i++) scanf("%d", a + i); int totp = 0; for (int i = 1; i <= n; i++) { if ((i - 1) % sq == 0) lw[++totp] = i; if (i % sq == 0 || i == n) rw[totp] = i; inp[i] = totp; } f[0] = 1; for (register int i = 1; i <= n; i++) { Sumf[inp[i]][0] += f[i - 1]; Add(topw[a[i]] + 1, i, 1); if (topw[a[i]]) Add(secw[a[i]] + 1, topw[a[i]], -1); for (register int p = 1; p < inp[i]; p++) for (register int nu = 0; nu <= k - add[p]; nu++) f[i] += Sumf[p][nu], f[i] %= mod; register int p = inp[i]; for (register int w = lw[p]; w <= i; w++) if (num[w] + add[p] <= k) f[i] += f[w - 1], f[i] %= mod; f[i] = (f[i] + mod) % mod; secw[a[i]] = topw[a[i]]; topw[a[i]] = i; } cout << f[n]; return 0; }
1129_D. Isolation
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once. Since the answer can be large, find it modulo 998 244 353. Input The first line contains two space-separated integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the number of elements in the array a and the restriction from the statement. The following line contains n space-separated integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n) β€” elements of the array a. Output The first and only line contains the number of ways to divide an array a modulo 998 244 353. Examples Input 3 1 1 1 2 Output 3 Input 5 2 1 1 2 1 3 Output 14 Input 5 5 1 2 3 4 5 Output 16 Note In the first sample, the three possible divisions are as follows. * [[1], [1], [2]] * [[1, 1], [2]] * [[1, 1, 2]] Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
{ "input": [ "5 5\n1 2 3 4 5\n", "3 1\n1 1 2\n", "5 2\n1 1 2 1 3\n" ], "output": [ "16", "3", "14" ] }
{ "input": [ "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n", "100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n", "10 1\n4 2 9 2 1 4 4 1 4 10\n", "100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n", "1 1\n1\n", "100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n", "2 2\n2 1\n", "2 1\n2 2\n" ], "output": [ "16", "726975503", "24", "244208148", "1", "1", "2", "2" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> //#pragma GCC optimize("unroll-loops") //#pragma GCC optimize("-O3") //#pragma GCC optimize("Ofast") #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #include <ext/rope> #define sz(x) int(x.size()) #define all(x) x.begin(),x.end() #define pii pair<int,int> #define PB push_back #define pll pair<ll,ll> #define pii pair<int,int> #define pil pair<int,ll> #define pli pair<ll, int> #define pi4 pair<pii, pii> #define pib pair<int, bool> #define ft first #define sd second using namespace std; using namespace __gnu_cxx; using namespace __gnu_pbds; typedef long long ll; typedef long double ld; template<class T> using ordered_set = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>; const int N = 100100; //const int N = 110; const int M = (1 << 10); const int oo = 2e9; const ll OO = 1e18; const int PW = 20; const int md = int(1e9) + 7; const int BLOCK = 750; //const int BLOCK = 2; const int K = N / BLOCK; int f[N], SQ[K][2 * N], mid[K], sum[K], a[N], n, k, ppr[N], pr[N], vl[N]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); // freopen("in.txt","r",stdin); cin >> n >> k; for (int i = 0; i < n; i++) { cin >> a[i]; a[i]--; } fill(mid, mid + K, N); for (int i = 0; i < n; i++){ ppr[i] = pr[i] = -1; } for (int i = 0; i < n; i++){ int l = ppr[a[i]], r = pr[a[i]]; swap(ppr[a[i]], pr[a[i]]); pr[a[i]] = i; int nm = i / BLOCK; for (int j = max(r + 1, BLOCK * nm); j <= i; j++){ SQ[nm][mid[nm] + vl[j]] -= (j ? f[j - 1] : 1); vl[j]++; SQ[nm][mid[nm] + vl[j]] += (j ? f[j - 1] : 1); if (vl[j] == k + 1) sum[nm] -= (j ? f[j - 1] : 1); } if ((r + 1) / BLOCK != nm){ nm--; int nd = (r + 1) / BLOCK; for (; nm > nd; nm--){ sum[nm] -= SQ[nm][mid[nm] + k]; mid[nm]--; } for (int j = r + 1; j < (nm + 1) * BLOCK; j++){ SQ[nm][mid[nm] + vl[j]] -= (j ? f[j - 1] : 1); vl[j]++; SQ[nm][mid[nm] + vl[j]] += (j ? f[j - 1] : 1); if (vl[j] == k + 1) sum[nm] -= (j ? f[j - 1] : 1); } } if (r >= 0){ int nd = (l + 1) / BLOCK; if (nd < nm){ for (int j = BLOCK * nm; j < r + 1; j++){ SQ[nm][mid[nm] + vl[j]] -= (j ? f[j - 1] : 1); vl[j]--; SQ[nm][mid[nm] + vl[j]] += (j ? f[j - 1] : 1); if (vl[j] == k) sum[nm] += (j ? f[j - 1] : 1); } nm--; } for (; nm > nd; nm--){ sum[nm] += SQ[nm][mid[nm] + k + 1]; mid[nm]--; } for (int j = l + 1; j <= r; j++){ SQ[nm][mid[nm] + vl[j]] -= (j ? f[j - 1] : 1); vl[j]--; SQ[nm][mid[nm] + vl[j]] += (j ? f[j - 1] : 1); if (vl[j] == k) sum[nm] += (j ? f[j - 1] : 1); } } for (int j = 0; j * BLOCK <= i; j++) f[i] += sum[j]; f[i] += (i ? f[i - 1] : 1); nm = i / BLOCK; SQ[nm][mid[nm] + vl[i]] += (i ? f[i - 1] : 1); if (vl[i] <= k) sum[nm] += (i ? f[i - 1] : 1); } cout << f[n - 1]; return 0; }
1129_D. Isolation
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once. Since the answer can be large, find it modulo 998 244 353. Input The first line contains two space-separated integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the number of elements in the array a and the restriction from the statement. The following line contains n space-separated integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n) β€” elements of the array a. Output The first and only line contains the number of ways to divide an array a modulo 998 244 353. Examples Input 3 1 1 1 2 Output 3 Input 5 2 1 1 2 1 3 Output 14 Input 5 5 1 2 3 4 5 Output 16 Note In the first sample, the three possible divisions are as follows. * [[1], [1], [2]] * [[1, 1], [2]] * [[1, 1, 2]] Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
{ "input": [ "5 5\n1 2 3 4 5\n", "3 1\n1 1 2\n", "5 2\n1 1 2 1 3\n" ], "output": [ "16", "3", "14" ] }
{ "input": [ "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n", "100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n", "10 1\n4 2 9 2 1 4 4 1 4 10\n", "100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n", "1 1\n1\n", "100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n", "2 2\n2 1\n", "2 1\n2 2\n" ], "output": [ "16", "726975503", "24", "244208148", "1", "1", "2", "2" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int MAX = 2e5 + 7; const int MOD = 1e9 + 7; const int PIERW = 300; int ciag[MAX]; int ostatnie[MAX]; int poprzedni[MAX]; long long pomocnicza[MAX]; long long DP[MAX]; struct Blok { int lewa, prawa; long long suma; long long sumaDP[PIERW * 2 + 1]; void Aktualizuj() { for (int i = 0; i <= 2 * PIERW; ++i) sumaDP[i] = 0; suma = 0; for (int i = prawa; i >= lewa; --i) { suma += pomocnicza[i]; sumaDP[suma + PIERW] = (sumaDP[suma + PIERW] + DP[i - 1]) % MOD; } for (int i = 1; i <= 2 * PIERW; i++) sumaDP[i] = (sumaDP[i] + sumaDP[i - 1]) % MOD; } }; Blok bloki[MAX / PIERW + 7]; int main() { int n, k; cin >> n >> k; DP[0] = 1; for (int i = 0; i * PIERW <= n; ++i) { bloki[i].lewa = i * PIERW + 1; bloki[i].prawa = (i + 1) * PIERW; bloki[i].Aktualizuj(); } for (int i = 1; i <= n; ++i) { cin >> ciag[i]; poprzedni[i] = ostatnie[ciag[i]]; ostatnie[ciag[i]] = i; pomocnicza[i] = 1; pomocnicza[poprzedni[i]] = -1; int t = poprzedni[poprzedni[i]]; pomocnicza[t] = 0; bloki[(i + 1) / PIERW].Aktualizuj(); bloki[(poprzedni[i] + 1) / PIERW].Aktualizuj(); bloki[(t + 1) / PIERW].Aktualizuj(); int blok = (i + 1) / PIERW; int suma = 0; for (int j = i; j >= bloki[blok].lewa; j--) { if (suma <= k) DP[i] = (DP[i] + DP[j - 1]) % MOD; suma += pomocnicza[j]; } for (int j = blok - 1; j >= 0; --j) { int x = k - suma; if (-PIERW <= x && x <= PIERW) DP[i] = (DP[i] + bloki[j].sumaDP[x + PIERW]) % MOD; else if (x < PIERW) DP[i] = (DP[i] + bloki[j].sumaDP[2 * PIERW]) % MOD; suma += bloki[j].suma; } } cout << DP[n] << "\n"; return 0; }
1129_D. Isolation
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once. Since the answer can be large, find it modulo 998 244 353. Input The first line contains two space-separated integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the number of elements in the array a and the restriction from the statement. The following line contains n space-separated integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n) β€” elements of the array a. Output The first and only line contains the number of ways to divide an array a modulo 998 244 353. Examples Input 3 1 1 1 2 Output 3 Input 5 2 1 1 2 1 3 Output 14 Input 5 5 1 2 3 4 5 Output 16 Note In the first sample, the three possible divisions are as follows. * [[1], [1], [2]] * [[1, 1], [2]] * [[1, 1, 2]] Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
{ "input": [ "5 5\n1 2 3 4 5\n", "3 1\n1 1 2\n", "5 2\n1 1 2 1 3\n" ], "output": [ "16", "3", "14" ] }
{ "input": [ "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n", "100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n", "10 1\n4 2 9 2 1 4 4 1 4 10\n", "100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n", "1 1\n1\n", "100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n", "2 2\n2 1\n", "2 1\n2 2\n" ], "output": [ "16", "726975503", "24", "244208148", "1", "1", "2", "2" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> #pragma comment(linker, "/stack:200000000") #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,avx,avx2") using namespace std; template <class T1, class T2, class T3> struct triple { T1 a; T2 b; T3 c; triple() : a(T1()), b(T2()), c(T3()){}; triple(T1 _a, T2 _b, T3 _c) : a(_a), b(_b), c(_c) {} }; template <class T1, class T2, class T3> bool operator<(const triple<T1, T2, T3>& t1, const triple<T1, T2, T3>& t2) { if (t1.a != t2.a) return t1.a < t2.a; else if (t1.b != t2.b) return t1.b < t2.b; else return t1.c < t2.c; } template <class T1, class T2, class T3> bool operator>(const triple<T1, T2, T3>& t1, const triple<T1, T2, T3>& t2) { if (t1.a != t2.a) return t1.a > t2.a; else if (t1.b != t2.b) return t1.b > t2.b; else return t1.c > t2.c; } inline int countBits(unsigned int v) { v = v - ((v >> 1) & 0x55555555); v = (v & 0x33333333) + ((v >> 2) & 0x33333333); return ((v + (v >> 4) & 0xF0F0F0F) * 0x1010101) >> 24; } inline int countBits(unsigned long long v) { unsigned int t = v >> 32; unsigned int p = (v & ((1ULL << 32) - 1)); return countBits(t) + countBits(p); } inline int countBits(long long v) { return countBits((unsigned long long)v); } inline int countBits(int v) { return countBits((unsigned int)v); } unsigned int reverseBits(unsigned int x) { x = (((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1)); x = (((x & 0xcccccccc) >> 2) | ((x & 0x33333333) << 2)); x = (((x & 0xf0f0f0f0) >> 4) | ((x & 0x0f0f0f0f) << 4)); x = (((x & 0xff00ff00) >> 8) | ((x & 0x00ff00ff) << 8)); return ((x >> 16) | (x << 16)); } template <class T> inline int sign(T x) { return x > 0 ? 1 : x < 0 ? -1 : 0; } inline bool isPowerOfTwo(int x) { return (x != 0 && (x & (x - 1)) == 0); } constexpr long long power(long long x, int p) { return p == 0 ? 1 : (x * power(x, p - 1)); } template <class T> inline bool inRange(const T& val, const T& min, const T& max) { return min <= val && val <= max; } unsigned long long rdtsc() { unsigned long long ret = 0; return __builtin_readcyclecounter(); asm volatile("rdtsc" : "=A"(ret) : :); return ret; } const int __BS = 4096; static char __bur[__BS + 16], *__er = __bur + __BS, *__ir = __er; template <class T = int> T readInt() { auto c = [&]() { if (__ir == __er) std::fill(__bur, __bur + __BS, 0), cin.read(__bur, __BS), __ir = __bur; }; c(); while (*__ir && (*__ir < '0' || *__ir > '9') && *__ir != '-') ++__ir; c(); bool m = false; if (*__ir == '-') ++__ir, c(), m = true; T r = 0; while (*__ir >= '0' && *__ir <= '9') r = r * 10 + *__ir - '0', ++__ir, c(); ++__ir; return m ? -r : r; } string readString() { auto c = [&]() { if (__ir == __er) std::fill(__bur, __bur + __BS, 0), cin.read(__bur, __BS), __ir = __bur; }; string r; c(); while (*__ir && isspace(*__ir)) ++__ir, c(); while (!isspace(*__ir)) r.push_back(*__ir), ++__ir, c(); ++__ir; return r; } char readChar() { auto c = [&]() { if (__ir == __er) std::fill(__bur, __bur + __BS, 0), cin.read(__bur, __BS), __ir = __bur; }; c(); while (*__ir && isspace(*__ir)) ++__ir, c(); return *__ir++; } static char __buw[__BS + 20], *__iw = __buw, *__ew = __buw + __BS; template <class T> void writeInt(T x, char endc = '\n') { if (x < 0) *__iw++ = '-', x = -x; if (x == 0) *__iw++ = '0'; char* s = __iw; while (x) { T t = x / 10; char c = x - 10 * t + '0'; *__iw++ = c; x = t; } char* f = __iw - 1; while (s < f) swap(*s, *f), ++s, --f; if (__iw > __ew) cout.write(__buw, __iw - __buw), __iw = __buw; if (endc) { *__iw++ = endc; } } template <class T> void writeStr(const T& str) { int i = 0; while (str[i]) { *__iw++ = str[i++]; if (__iw > __ew) cout.write(__buw, __iw - __buw), __iw = __buw; } } struct __FL__ { ~__FL__() { if (__iw != __buw) cout.write(__buw, __iw - __buw); } }; static __FL__ __flushVar__; template <class T1, class T2> ostream& operator<<(ostream& os, const pair<T1, T2>& p); template <class T> ostream& operator<<(ostream& os, const vector<T>& v); template <class T1, class T2> ostream& operator<<(ostream& os, const set<T1, T2>& v); template <class T1, class T2> ostream& operator<<(ostream& os, const multiset<T1, T2>& v); template <class T1, class T2> ostream& operator<<(ostream& os, priority_queue<T1, T2> v); template <class T1, class T2, class T3> ostream& operator<<(ostream& os, const map<T1, T2, T3>& v); template <class T1, class T2, class T3> ostream& operator<<(ostream& os, const multimap<T1, T2, T3>& v); template <class T1, class T2, class T3> ostream& operator<<(ostream& os, const triple<T1, T2, T3>& t); template <class T, size_t N> ostream& operator<<(ostream& os, const array<T, N>& v); template <class T1, class T2> ostream& operator<<(ostream& os, const pair<T1, T2>& p) { return os << "(" << p.first << ", " << p.second << ")"; } template <class T> ostream& operator<<(ostream& os, const vector<T>& v) { bool f = 1; os << "["; for (auto& i : v) { if (!f) os << ", "; os << i; f = 0; } return os << "]"; } template <class T, size_t N> ostream& operator<<(ostream& os, const array<T, N>& v) { bool f = 1; os << "["; for (auto& i : v) { if (!f) os << ", "; os << i; f = 0; } return os << "]"; } template <class T1, class T2> ostream& operator<<(ostream& os, const set<T1, T2>& v) { bool f = 1; os << "["; for (auto& i : v) { if (!f) os << ", "; os << i; f = 0; } return os << "]"; } template <class T1, class T2> ostream& operator<<(ostream& os, const multiset<T1, T2>& v) { bool f = 1; os << "["; for (auto& i : v) { if (!f) os << ", "; os << i; f = 0; } return os << "]"; } template <class T1, class T2, class T3> ostream& operator<<(ostream& os, const map<T1, T2, T3>& v) { bool f = 1; os << "["; for (auto& ii : v) { if (!f) os << ", "; os << "(" << ii.first << " -> " << ii.second << ") "; f = 0; } return os << "]"; } template <class T1, class T2> ostream& operator<<(ostream& os, const multimap<T1, T2>& v) { bool f = 1; os << "["; for (auto& ii : v) { if (!f) os << ", "; os << "(" << ii.first << " -> " << ii.second << ") "; f = 0; } return os << "]"; } template <class T1, class T2> ostream& operator<<(ostream& os, priority_queue<T1, T2> v) { bool f = 1; os << "["; while (!v.empty()) { auto x = v.top(); v.pop(); if (!f) os << ", "; f = 0; os << x; } return os << "]"; } template <class T1, class T2, class T3> ostream& operator<<(ostream& os, const triple<T1, T2, T3>& t) { return os << "(" << t.a << ", " << t.b << ", " << t.c << ")"; } template <class T1, class T2> void printarray(const T1& a, T2 sz, T2 beg = 0) { for (T2 i = beg; i < sz; i++) cout << a[i] << " "; cout << endl; } template <class T1, class T2, class T3> inline istream& operator>>(istream& os, triple<T1, T2, T3>& t); template <class T1, class T2> inline istream& operator>>(istream& os, pair<T1, T2>& p) { return os >> p.first >> p.second; } template <class T> inline istream& operator>>(istream& os, vector<T>& v) { if (v.size()) for (T& t : v) os >> t; else { string s; T obj; while (s.empty()) { getline(os, s); if (!os) return os; } stringstream ss(s); while (ss >> obj) v.push_back(obj); } return os; } template <class T1, class T2, class T3> inline istream& operator>>(istream& os, triple<T1, T2, T3>& t) { return os >> t.a >> t.b >> t.c; } template <class T1, class T2> inline pair<T1, T2> operator+(const pair<T1, T2>& p1, const pair<T1, T2>& p2) { return pair<T1, T2>(p1.first + p2.first, p1.second + p2.second); } template <class T1, class T2> inline pair<T1, T2>& operator+=(pair<T1, T2>& p1, const pair<T1, T2>& p2) { p1.first += p2.first, p1.second += p2.second; return p1; } template <class T1, class T2> inline pair<T1, T2> operator-(const pair<T1, T2>& p1, const pair<T1, T2>& p2) { return pair<T1, T2>(p1.first - p2.first, p1.second - p2.second); } template <class T1, class T2> inline pair<T1, T2>& operator-=(pair<T1, T2>& p1, const pair<T1, T2>& p2) { p1.first -= p2.first, p1.second -= p2.second; return p1; } const int USUAL_MOD = 1000000007; template <class T> inline void normmod(T& x, T m = USUAL_MOD) { x %= m; if (x < 0) x += m; } template <class T1, class T2> inline T2 summodfast(T1 x, T1 y, T2 m = USUAL_MOD) { T2 res = x + y; if (res >= m) res -= m; return res; } template <class T1, class T2, class T3 = int> inline void addmodfast(T1& x, T2 y, T3 m = USUAL_MOD) { x += y; if (x >= m) x -= m; } template <class T1, class T2, class T3 = int> inline void submodfast(T1& x, T2 y, T3 m = USUAL_MOD) { x -= y; if (x < 0) x += m; } inline long long mulmod(long long x, long long n, long long m) { x %= m; n %= m; long long r = x * n - long long(long double(x) * long double(n) / long double(m)) * m; while (r < 0) r += m; while (r >= m) r -= m; return r; } inline long long powmod(long long x, long long n, long long m) { long long r = 1; normmod(x, m); while (n) { if (n & 1) r *= x; x *= x; r %= m; x %= m; n /= 2; } return r; } inline long long powmulmod(long long x, long long n, long long m) { long long res = 1; normmod(x, m); while (n) { if (n & 1) res = mulmod(res, x, m); x = mulmod(x, x, m); n /= 2; } return res; } template <class T> inline T gcd(T a, T b) { while (b) { T t = a % b; a = b; b = t; } return a; } template <class T> T fast_gcd(T u, T v) { int shl = 0; while (u && v && u != v) { T eu = u & 1; u >>= eu ^ 1; T ev = v & 1; v >>= ev ^ 1; shl += (~(eu | ev) & 1); T d = u & v & 1 ? (u + v) >> 1 : 0; T dif = (u - v) >> (sizeof(T) * 8 - 1); u -= d & ~dif; v -= d & dif; } return std::max(u, v) << shl; } inline long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } template <class T> inline T gcd(T a, T b, T c) { return gcd(gcd(a, b), c); } long long gcdex(long long a, long long b, long long& x, long long& y) { if (!a) { x = 0; y = 1; return b; } long long y1; long long d = gcdex(b % a, a, y, y1); x = y1 - (b / a) * y; return d; } bool isPrime(long long n) { if (n <= (1 << 14)) { int x = (int)n; if (x <= 4 || x % 2 == 0 || x % 3 == 0) return x == 2 || x == 3; for (int i = 5; i * i <= x; i += 6) if (x % i == 0 || x % (i + 2) == 0) return 0; return 1; } long long s = n - 1; int t = 0; while (s % 2 == 0) { s /= 2; ++t; } for (int a : {2, 325, 9375, 28178, 450775, 9780504, 1795265022}) { if (!(a %= n)) return true; long long f = powmulmod(a, s, n); if (f == 1 || f == n - 1) continue; for (int i = 1; i < t; ++i) if ((f = mulmod(f, f, n)) == n - 1) goto nextp; return false; nextp:; } return true; } int32_t solve(); int32_t main(int argc, char** argv) { ios_base::sync_with_stdio(0); cin.tie(0); return solve(); } int a[101001]; vector<int> positions[101010]; int tr[404040]; int b[101010]; int dp[404040]; long long getSum(int* __restrict b, int* __restrict dp, int n) { long long res = 0; for (int j = 0; j < n; ++j) { res += b[j] > 0 ? 0 : dp[j]; } return res; } int solve() { int n, k; cin >> n >> k; for (int i = 0; i < (n); ++i) { b[i] -= k; } for (int i = 0; i < (n); ++i) { cin >> a[i]; --a[i]; } dp[0] = 1; for (int i = 0; i < n; ++i) { int x = a[i]; if (!positions[x].empty()) { int to = positions[x].back(); int from = positions[x].size() > 0 ? *--positions[x].rbegin() : 0; for (int j = from; j <= to; ++j) { b[j]--; } } int from = positions[x].empty() ? 0 : (positions[x].back() + 1); for (int j = from; j <= i; ++j) { b[j]++; } long long res = getSum(b, dp, i + 1); dp[i + 1] = res % 998244353; positions[x].push_back(i); } cout << dp[n] << endl; return 0; }
1129_D. Isolation
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once. Since the answer can be large, find it modulo 998 244 353. Input The first line contains two space-separated integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the number of elements in the array a and the restriction from the statement. The following line contains n space-separated integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n) β€” elements of the array a. Output The first and only line contains the number of ways to divide an array a modulo 998 244 353. Examples Input 3 1 1 1 2 Output 3 Input 5 2 1 1 2 1 3 Output 14 Input 5 5 1 2 3 4 5 Output 16 Note In the first sample, the three possible divisions are as follows. * [[1], [1], [2]] * [[1, 1], [2]] * [[1, 1, 2]] Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
{ "input": [ "5 5\n1 2 3 4 5\n", "3 1\n1 1 2\n", "5 2\n1 1 2 1 3\n" ], "output": [ "16", "3", "14" ] }
{ "input": [ "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n", "100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n", "10 1\n4 2 9 2 1 4 4 1 4 10\n", "100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n", "1 1\n1\n", "100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n", "2 2\n2 1\n", "2 1\n2 2\n" ], "output": [ "16", "726975503", "24", "244208148", "1", "1", "2", "2" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <class T> inline void gn(T &first) { char c, sg = 0; while (c = getchar(), (c > '9' || c < '0') && c != '-') ; for ((c == '-' ? sg = 1, c = getchar() : 0), first = 0; c >= '0' && c <= '9'; c = getchar()) first = (first << 1) + (first << 3) + c - '0'; if (sg) first = -first; } template <class T1, class T2> inline void gn(T1 &x1, T2 &x2) { gn(x1), gn(x2); } int power(int a, int b, int m, int ans = 1) { for (; b; b >>= 1, a = 1LL * a * a % m) if (b & 1) ans = 1LL * ans * a % m; return ans; } int ans[100010], sum[321], cnt[100010]; int dp[321][100010]; int val[321]; int a[100010], pre[100010]; int L[321], R[321], id[100010]; int ID[100010]; int n, k; void build(int n) { for (int i = 1; i < 100010; i++) id[i] = sqrt(i); for (int i = 1; i < 100010; i++) { if (!L[id[i]]) L[id[i]] = i; R[id[i]] = i; } L[1] = 0; id[0] = 1; for (int i = 1; i <= n; i++) { pre[i] = ID[a[i]]; ID[a[i]] = i; } for (int i = 0; i < 100010; i++) cnt[i] = -1; } inline int add_val(int &u, int v) { u += v; if (u >= 998244353) u -= 998244353; } inline int add(int st, int ed, int s) { if (st > ed) return 0; int u = id[st]; for (int i = L[u]; i <= R[u]; i++) { if (cnt[i] < 0) break; add_val(dp[u][cnt[i]], 998244353 - ans[i]); cnt[i] += sum[u]; if (cnt[i] <= k) add_val(val[u], 998244353 - ans[i]); if (i >= st and i <= ed) cnt[i] += s; add_val(dp[u][cnt[i]], ans[i]); if (cnt[i] <= k) add_val(val[u], ans[i]); } sum[u] = 0; if (id[st] == id[ed]) return 0; u = id[ed]; for (int i = L[u]; i <= R[u]; i++) { if (cnt[i] < 0) break; add_val(dp[u][cnt[i]], 998244353 - ans[i]); cnt[i] += sum[u]; if (cnt[i] <= k) add_val(val[u], 998244353 - ans[i]); if (i >= st and i <= ed) cnt[i] += s; add_val(dp[u][cnt[i]], ans[i]); if (cnt[i] <= k) add_val(val[u], ans[i]); } for (int i = id[st] + 1; i < id[ed]; i++) { if (s == 1 and k >= sum[i]) add_val(val[i], 998244353 - dp[i][k - sum[i]]); if (s == -1 and k + 1 >= sum[i]) add_val(val[i], dp[i][k + 1 - sum[i]]); add_val(sum[i], s); } return 0; } int main() { gn(n, k); for (int i = 1; i <= n; i++) gn(a[i]); build(n); cnt[0] = 0; dp[1][0] = ans[0] = val[1] = 1; for (int i = 1; i <= n; i++) { add(pre[i], i - 1, 1); add(pre[pre[i]], pre[i] - 1, -1); ans[i] = 0; cnt[i] = 0; int u = id[i]; for (int j = 1; j <= u; j++) { add_val(ans[i], val[j]); } add_val(val[u], ans[i]); add_val(dp[u][cnt[i]], ans[i]); } cout << ans[n] << endl; }
1129_D. Isolation
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once. Since the answer can be large, find it modulo 998 244 353. Input The first line contains two space-separated integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the number of elements in the array a and the restriction from the statement. The following line contains n space-separated integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n) β€” elements of the array a. Output The first and only line contains the number of ways to divide an array a modulo 998 244 353. Examples Input 3 1 1 1 2 Output 3 Input 5 2 1 1 2 1 3 Output 14 Input 5 5 1 2 3 4 5 Output 16 Note In the first sample, the three possible divisions are as follows. * [[1], [1], [2]] * [[1, 1], [2]] * [[1, 1, 2]] Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
{ "input": [ "5 5\n1 2 3 4 5\n", "3 1\n1 1 2\n", "5 2\n1 1 2 1 3\n" ], "output": [ "16", "3", "14" ] }
{ "input": [ "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n", "100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n", "10 1\n4 2 9 2 1 4 4 1 4 10\n", "100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n", "1 1\n1\n", "100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n", "2 2\n2 1\n", "2 1\n2 2\n" ], "output": [ "16", "726975503", "24", "244208148", "1", "1", "2", "2" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int MOD = 998244353; template <typename T> struct modular { constexpr modular() : val(0) {} constexpr modular(const modular<T>& _m) : val(_m.val) {} template <typename U> constexpr modular(const U& _r = U()) { val = -MOD <= _r && _r < MOD ? _r : _r % MOD; if (val < 0) { val += MOD; } } const T operator()() { return val; } template <typename U> explicit operator U() const { return static_cast<U>(val); } modular<T>& operator+=(const modular<T>& _m) { if ((val += _m.val) >= MOD) { val -= MOD; } return *this; } modular<T>& operator-=(const modular<T>& _m) { if ((val -= _m.val) < 0) { val += MOD; } return *this; } modular<T>& operator*=(const modular<T>& _m) { val = modular<T>(static_cast<int64_t>(val) * static_cast<int64_t>(_m.val)) .val; return *this; } modular<T>& operator/=(const modular<T>& _m) { T a = _m.val, b = MOD, u = 0, v = 1; while (a != 0) { T q = b / a; b -= q * a; swap(a, b); u -= q * v; swap(u, v); } return *this *= u; } modular<T>& operator=(const modular<T>& _m) { val = _m.val; return *this; } template <typename U> modular<T>& operator+=(const U& _r) { return *this += modular<T>(_r); } template <typename U> modular<T>& operator-=(const U& _r) { return *this -= modular<T>(_r); } template <typename U> modular<T>& operator*=(const U& _r) { return *this *= modular<T>(_r); } template <typename U> modular<T>& operator/=(const U& _r) { return *this /= modular<T>(_r); } template <typename U> modular<T>& operator=(const U& _r) { val = modular<T>(_r).val; return *this; } modular<T> operator-() { return modular<T>(-val); } template <typename U> friend bool operator==(const modular<U>&, const modular<U>&); friend std::istream& operator>>(std::istream& os, modular<T>& _m) { os >> _m.val; _m *= 1; return os; } friend std::ostream& operator<<(std::ostream& os, const modular<T>& _m) { return os << _m.val; } template <typename U> modular<T> exp(U e) { modular<T> res = 1; modular<T> b = val; if (e < 0) { b = 1 / b; e *= -1; } for (; e; e >>= 1) { if (e & 1) { res *= b; } b *= b; } return res; } private: T val; }; template <typename T> inline modular<T> operator+(const modular<T>& _lhs, const modular<T>& _rhs) { return modular<T>(_lhs) += _rhs; } template <typename T, typename U> inline modular<T> operator+(const modular<T>& _lhs, const U& _rhs) { return modular<T>(_lhs) += _rhs; } template <typename T, typename U> inline modular<T> operator+(const U& _lhs, const modular<T>& _rhs) { return modular<T>(_lhs) += _rhs; } template <typename T> inline modular<T> operator-(const modular<T>& _lhs, const modular<T>& _rhs) { return modular<T>(_lhs) -= _rhs; } template <typename T, typename U> inline modular<T> operator-(const modular<T>& _lhs, const U& _rhs) { return modular<T>(_lhs) -= _rhs; } template <typename T, typename U> inline modular<T> operator-(const U& _lhs, const modular<T>& _rhs) { return modular<T>(_lhs) -= _rhs; } template <typename T> inline modular<T> operator*(const modular<T>& _lhs, const modular<T>& _rhs) { return modular<T>(_lhs) *= _rhs; } template <typename T, typename U> inline modular<T> operator*(const modular<T>& _lhs, const U& _rhs) { return modular<T>(_lhs) *= _rhs; } template <typename T, typename U> inline modular<T> operator*(const U& _lhs, const modular<T>& _rhs) { return modular<T>(_lhs) *= _rhs; } template <typename T> inline modular<T> operator/(const modular<T>& _lhs, const modular<T>& _rhs) { return modular<T>(_lhs) /= _rhs; } template <typename T, typename U> inline modular<T> operator/(const modular<T>& _lhs, const U& _rhs) { return modular<T>(_lhs) /= _rhs; } template <typename T, typename U> inline modular<T> operator/(const U& _lhs, const modular<T>& _rhs) { return modular<T>(_lhs) /= _rhs; } template <typename T> inline bool operator==(const modular<T>& _lhs, const modular<T>& _rhs) { return _lhs.val == _rhs.val; } template <typename T, typename U> inline bool operator==(const modular<T>& _lhs, const U& _rhs) { return _lhs == modular<T>(_rhs); } template <typename T, typename U> inline bool operator==(const U& _lhs, const modular<T>& _rhs) { return modular<T>(_lhs) == _rhs; } template <typename T> inline bool operator!=(const modular<T>& _lhs, const modular<T>& _rhs) { return !(_lhs == _rhs); } template <typename T, typename U> inline bool operator!=(const modular<T>& _lhs, const U& _rhs) { return !(_lhs == _rhs); } template <typename T, typename U> inline bool operator!=(const U& _lhs, const modular<T>& _rhs) { return !(_lhs == _rhs); } const int N = 100005; modular<int> dp[N]; int K; const int BLOCK_SIZE = 256; struct Block { int L, R; int suff[BLOCK_SIZE], offset, height; modular<int> sum[BLOCK_SIZE]; Block() {} Block(int _L, int _R) : L(_L), R(_R), offset(0), height(0) { memset(suff, 0, sizeof suff); memset(sum, 0, sizeof sum); } void update(int l, int r, int x) { assert(L <= l && r <= R); if (L == l && r == R) { offset += x; return; } for (int i = l; i < r; i++) { suff[i - L] += x; } int delta = *min_element(suff, suff + BLOCK_SIZE); offset += delta; for (int i = L; i < R; i++) { suff[i - L] -= delta; } height = *max_element(suff, suff + BLOCK_SIZE); memset(sum, 0, sizeof sum); for (int i = L; i < R; i++) { sum[suff[i - L]] += dp[i]; } for (int i = 1; i <= height; i++) { sum[i] += sum[i - 1]; } } modular<int> query() { int x = K - offset; return x < 0 ? 0 : sum[min(x, height)]; } void add(int l) { for (int i = suff[l - L]; i <= height; i++) { sum[i] += dp[l]; } } }; vector<Block> bs; void update_all(int l, int r, int x) { if (l >= r) return; for (int i = l / BLOCK_SIZE; l < r; i++) { int _r = min(r, bs[i].R); bs[i].update(l, _r, x); l = _r; } } modular<int> query_all(int r) { modular<int> res = 0; for (auto& b : bs) { if (r > b.R) break; res += b.query(); } return res; } void solve() { int n; cin >> n >> K; for (int l = 0; l <= n; l += BLOCK_SIZE) { bs.emplace_back(l, min(l + BLOCK_SIZE, n + 1)); } dp[0] = 1; bs[0].add(0); vector<int> pre(n + 1), las(n + 1); for (int i = 1; i <= n; i++) { int x; cin >> x; int p = las[x]; las[x] = i; pre[i] = p; update_all(p, i, 1); int s = pre[p]; update_all(s, p, -1); dp[i] = query_all(i); bs[i / BLOCK_SIZE].add(i); } cout << dp[n]; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); solve(); cout << endl; }
1129_D. Isolation
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once. Since the answer can be large, find it modulo 998 244 353. Input The first line contains two space-separated integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the number of elements in the array a and the restriction from the statement. The following line contains n space-separated integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n) β€” elements of the array a. Output The first and only line contains the number of ways to divide an array a modulo 998 244 353. Examples Input 3 1 1 1 2 Output 3 Input 5 2 1 1 2 1 3 Output 14 Input 5 5 1 2 3 4 5 Output 16 Note In the first sample, the three possible divisions are as follows. * [[1], [1], [2]] * [[1, 1], [2]] * [[1, 1, 2]] Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
{ "input": [ "5 5\n1 2 3 4 5\n", "3 1\n1 1 2\n", "5 2\n1 1 2 1 3\n" ], "output": [ "16", "3", "14" ] }
{ "input": [ "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n", "100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n", "10 1\n4 2 9 2 1 4 4 1 4 10\n", "100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n", "1 1\n1\n", "100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n", "2 2\n2 1\n", "2 1\n2 2\n" ], "output": [ "16", "726975503", "24", "244208148", "1", "1", "2", "2" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long INF = 0x3f3f3f3f; const int N = 2e5 + 10; const int M = 11; const double PI = acos(-1.0); const int blo = 320; inline void add(int &a, int b) { a += b; if (a >= 998244353) a -= 998244353; } int v[N]; int cnt[blo][(blo << 1) + 10], sum[blo]; int vc[N], w[N], pre[N], l[N], dd[3] = {1, -1, 0}; void up(int x, int y) { vc[x] = y; int id = x / blo; memset(cnt[id], 0, sizeof(cnt[id])); sum[id] = 0; for (int i = blo - 1, be = id * blo; i >= 0; --i) { cnt[id][sum[id]] += w[be + i]; sum[id] += vc[be + i]; } for (int i = 1; i <= blo * 2; ++i) add(cnt[id][i], cnt[id][i - 1]); } void qu(int x, int k) { int id = x / blo; for (int i = id; i >= 0; --i) { add(w[x], cnt[i][k]); k -= sum[i]; } } int main() { int n, k; scanf("%d%d", &n, &k); for (int i = 1; i <= n; ++i) scanf("%d", &v[i]); w[0] = 1; for (int i = 1; i <= blo * 2; ++i) add(cnt[0][i], cnt[0][i - 1]); for (int i = 1; i <= n; ++i) { pre[i] = l[v[i]], l[v[i]] = i; for (int j = 0, now = i; j < 3 && now; ++j, now = pre[now]) up(now, dd[j]); qu(i, k); } printf("%d\n", w[n]); return 0; }
1129_D. Isolation
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once. Since the answer can be large, find it modulo 998 244 353. Input The first line contains two space-separated integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the number of elements in the array a and the restriction from the statement. The following line contains n space-separated integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n) β€” elements of the array a. Output The first and only line contains the number of ways to divide an array a modulo 998 244 353. Examples Input 3 1 1 1 2 Output 3 Input 5 2 1 1 2 1 3 Output 14 Input 5 5 1 2 3 4 5 Output 16 Note In the first sample, the three possible divisions are as follows. * [[1], [1], [2]] * [[1, 1], [2]] * [[1, 1, 2]] Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
{ "input": [ "5 5\n1 2 3 4 5\n", "3 1\n1 1 2\n", "5 2\n1 1 2 1 3\n" ], "output": [ "16", "3", "14" ] }
{ "input": [ "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n", "100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n", "10 1\n4 2 9 2 1 4 4 1 4 10\n", "100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n", "1 1\n1\n", "100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n", "2 2\n2 1\n", "2 1\n2 2\n" ], "output": [ "16", "726975503", "24", "244208148", "1", "1", "2", "2" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <typename T> void maxtt(T& t1, T t2) { t1 = max(t1, t2); } template <typename T> void mintt(T& t1, T t2) { t1 = min(t1, t2); } bool debug = 0; int n, m, k; int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0}; string direc = "URDL"; long long ln, lk, lm; void etp(bool f = 0) { puts(f ? "YES" : "NO"); exit(0); } void addmod(int& x, int y, int mod = 998244353) { assert(y >= 0); x += y; if (x >= mod) x -= mod; assert(x >= 0 && x < mod); } void et(int x = -1) { printf("%d\n", x); exit(0); } long long fastPow(long long x, long long y, int mod = 998244353) { long long ans = 1; while (y > 0) { if (y & 1) ans = (x * ans) % mod; x = x * x % mod; y >>= 1; } return ans; } long long gcd1(long long x, long long y) { return y ? gcd1(y, x % y) : x; } int a[100035], pre[100035], pos[100035], dp[100035]; int SZ, ps[320][100035], S, add[320]; int pfx[100035]; void bd(int bid, int pos, int val) { int z = pfx[pos] + add[bid]; addmod(ps[bid][z], 998244353 - dp[pos - 1]); if (z <= k) addmod(S, 998244353 - dp[pos - 1]); pfx[pos] += val; z = pfx[pos] + add[bid]; addmod(ps[bid][z], dp[pos - 1]); if (z <= k) addmod(S, dp[pos - 1]); } int gid(int pos) { return (pos + SZ - 1) / SZ; } void upt(int l, int r, int val) { int lid = gid(l), rid = gid(r); if (lid == rid) { for (int i = l; i <= r; i++) bd(lid, i, val); return; } for (int i = l; i <= n; i++) { bd(lid, i, val); if (i % SZ == 0) break; } for (int z = lid + 1; z < rid; z++) { if (val == 1) { if (k >= add[z]) addmod(S, 998244353 - ps[z][k - add[z]]); add[z]++; } else { if (k + 1 >= add[z]) addmod(S, ps[z][k + 1 - add[z]]); add[z]--; } } for (int i = (rid - 1) * SZ + 1; i <= r; i++) { bd(rid, i, val); } } void fmain(int tid) { scanf("%d%d", &n, &k); for (int(i) = 1; (i) <= (int)(n); (i)++) { scanf("%d", a + i); pre[i] = pos[a[i]]; pos[a[i]] = i; } dp[0] = 1; S = 1; SZ = sqrt(n); ps[1][0] = 1; for (int(i) = 1; (i) <= (int)(n); (i)++) { upt(pre[i] + 1, i, 1); if (pre[i]) upt(pre[pre[i]] + 1, pre[i], -1); dp[i] = S; int z = gid(i + 1); addmod(ps[z][0], S); addmod(S, S); } printf("%d\n", dp[n]); } int main() { int t = 1; for (int(i) = 1; (i) <= (int)(t); (i)++) { fmain(i); } return 0; }
1129_D. Isolation
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once. Since the answer can be large, find it modulo 998 244 353. Input The first line contains two space-separated integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the number of elements in the array a and the restriction from the statement. The following line contains n space-separated integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n) β€” elements of the array a. Output The first and only line contains the number of ways to divide an array a modulo 998 244 353. Examples Input 3 1 1 1 2 Output 3 Input 5 2 1 1 2 1 3 Output 14 Input 5 5 1 2 3 4 5 Output 16 Note In the first sample, the three possible divisions are as follows. * [[1], [1], [2]] * [[1, 1], [2]] * [[1, 1, 2]] Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
{ "input": [ "5 5\n1 2 3 4 5\n", "3 1\n1 1 2\n", "5 2\n1 1 2 1 3\n" ], "output": [ "16", "3", "14" ] }
{ "input": [ "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n", "100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n", "10 1\n4 2 9 2 1 4 4 1 4 10\n", "100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n", "1 1\n1\n", "100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n", "2 2\n2 1\n", "2 1\n2 2\n" ], "output": [ "16", "726975503", "24", "244208148", "1", "1", "2", "2" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 100100; const int B = 300; const int Mod = 998244353; int n, k; int x[N]; vector<int> v[N]; int Cnt[N]; int Offset[B]; int Pref[B][B]; int Dp[N]; void add_self(int& a, int b) { a += b; if (a >= Mod) a -= Mod; assert(0 <= a && a < Mod); } void min_self(int& a, int b) { a = min(a, b); } void ReBuild(int Block) { int a = Block * B; int b = (Block + 1) * B - 1; if (b > n) b = n - 1; for (int i = a; i <= b; i++) Cnt[i] += Offset[Block]; Offset[Block] = Cnt[a]; for (int i = a; i <= b; i++) min_self(Offset[Block], Cnt[i]); for (int i = a; i <= b; i++) Cnt[i] -= Offset[Block], assert(0 <= Cnt[i] && Cnt[i] < B); for (int i = a; i <= b; i++) add_self(Pref[Block][Cnt[i]], Dp[i]); for (int i = 1; i < B; i++) add_self(Pref[Block][i], Pref[Block][i - 1]); } void Add(int a, int b, int c) { assert(a <= b); if (a / B == b / B) { for (int i = a; i <= b; i++) Cnt[i] += c; return ReBuild(a / B); } for (int i = a / B + 1; i < b / B; i++) Offset[i] += c; for (int i = a; i < n && i / B == a / B; i++) Cnt[i] += c; for (int i = b; i >= 0 && i / B == b / B; i--) Cnt[i] += c; ReBuild(a / B); ReBuild(b / B); } int Query(int a) { int Res = 0; for (int i = 0; i < a / B; i++) if (k - Offset[i] >= 0) add_self(Res, Pref[i][min(k - Offset[i], B - 1)]); for (int i = (a / B) * B; i <= a; i++) if (Cnt[i] + Offset[i / B] <= k) add_self(Res, Dp[i]); return Res; } int main() { cin >> n >> k; for (int i = 0; i < n; i++) scanf("%d", x + i); for (int i = 1; i <= n; i++) v[i].push_back(-1); Dp[0] = 1; ReBuild(0); for (int R = 0; R < n; R++) { vector<int>& vec = v[x[R]]; if (vec.size() >= 2) Add(vec.end()[-2] + 1, vec.back(), -1); Add(vec.back() + 1, R, +1); vec.push_back(R); add_self(Dp[R + 1], Query(R)); } cout << Dp[n]; }
1129_D. Isolation
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once. Since the answer can be large, find it modulo 998 244 353. Input The first line contains two space-separated integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the number of elements in the array a and the restriction from the statement. The following line contains n space-separated integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n) β€” elements of the array a. Output The first and only line contains the number of ways to divide an array a modulo 998 244 353. Examples Input 3 1 1 1 2 Output 3 Input 5 2 1 1 2 1 3 Output 14 Input 5 5 1 2 3 4 5 Output 16 Note In the first sample, the three possible divisions are as follows. * [[1], [1], [2]] * [[1, 1], [2]] * [[1, 1, 2]] Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
{ "input": [ "5 5\n1 2 3 4 5\n", "3 1\n1 1 2\n", "5 2\n1 1 2 1 3\n" ], "output": [ "16", "3", "14" ] }
{ "input": [ "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n", "100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n", "10 1\n4 2 9 2 1 4 4 1 4 10\n", "100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n", "1 1\n1\n", "100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n", "2 2\n2 1\n", "2 1\n2 2\n" ], "output": [ "16", "726975503", "24", "244208148", "1", "1", "2", "2" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> //#pragma GCC optimize("unroll-loops") //#pragma GCC optimize("-O3") //#pragma GCC optimize("Ofast") #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #include <ext/rope> #define sz(x) int(x.size()) #define all(x) x.begin(),x.end() #define pii pair<int,int> #define PB push_back #define pll pair<ll,ll> #define pii pair<int,int> #define pil pair<int,ll> #define pli pair<ll, int> #define pi4 pair<pii, pii> #define pib pair<int, bool> #define ft first #define sd second using namespace std; using namespace __gnu_cxx; using namespace __gnu_pbds; typedef long long ll; typedef long double ld; template<class T> using ordered_set = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>; //const int N = 100100; const int N = 2010; const int M = (1 << 10); const int oo = 2e9; const ll OO = 1e18; const int PW = 20; const int md = int(1e9) + 7; //const int BLOCK = 750; const int BLOCK = 2; const int K = N / BLOCK; int f[N], SQ[K][2 * N], mid[K], sum[K], a[N], n, k, ppr[N], pr[N], vl[N]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); // freopen("in.txt","r",stdin); cin >> n >> k; for (int i = 0; i < n; i++) { cin >> a[i]; a[i]--; } fill(mid, mid + K, N); for (int i = 0; i < n; i++){ ppr[i] = pr[i] = -1; } for (int i = 0; i < n; i++){ int l = ppr[a[i]], r = pr[a[i]]; swap(ppr[a[i]], pr[a[i]]); pr[a[i]] = i; int nm = i / BLOCK; for (int j = max(r + 1, BLOCK * nm); j <= i; j++){ SQ[nm][mid[nm] + vl[j]] -= (j ? f[j - 1] : 1); vl[j]++; SQ[nm][mid[nm] + vl[j]] += (j ? f[j - 1] : 1); if (vl[j] == k + 1) sum[nm] -= (j ? f[j - 1] : 1); } if ((r + 1) / BLOCK != nm){ nm--; int nd = (r + 1) / BLOCK; for (; nm > nd; nm--){ sum[nm] -= SQ[nm][mid[nm] + k]; mid[nm]--; } for (int j = r + 1; j < (nm + 1) * BLOCK; j++){ SQ[nm][mid[nm] + vl[j]] -= (j ? f[j - 1] : 1); vl[j]++; SQ[nm][mid[nm] + vl[j]] += (j ? f[j - 1] : 1); if (vl[j] == k + 1) sum[nm] -= (j ? f[j - 1] : 1); } } if (r >= 0){ int nd = (l + 1) / BLOCK; if (nd < nm){ for (int j = BLOCK * nm; j < r + 1; j++){ SQ[nm][mid[nm] + vl[j]] -= (j ? f[j - 1] : 1); vl[j]--; SQ[nm][mid[nm] + vl[j]] += (j ? f[j - 1] : 1); if (vl[j] == k) sum[nm] += (j ? f[j - 1] : 1); } nm--; } for (; nm > nd; nm--){ sum[nm] += SQ[nm][mid[nm] + k + 1]; mid[nm]--; } for (int j = l + 1; j <= r; j++){ SQ[nm][mid[nm] + vl[j]] -= (j ? f[j - 1] : 1); vl[j]--; SQ[nm][mid[nm] + vl[j]] += (j ? f[j - 1] : 1); if (vl[j] == k) sum[nm] += (j ? f[j - 1] : 1); } } for (int j = 0; j * BLOCK <= i; j++) f[i] += sum[j]; f[i] += (i ? f[i - 1] : 1); nm = i / BLOCK; SQ[nm][mid[nm] + vl[i]] += (i ? f[i - 1] : 1); if (vl[i] <= k) sum[nm] += (i ? f[i - 1] : 1); } cout << f[n - 1]; return 0; }
1129_D. Isolation
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once. Since the answer can be large, find it modulo 998 244 353. Input The first line contains two space-separated integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the number of elements in the array a and the restriction from the statement. The following line contains n space-separated integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n) β€” elements of the array a. Output The first and only line contains the number of ways to divide an array a modulo 998 244 353. Examples Input 3 1 1 1 2 Output 3 Input 5 2 1 1 2 1 3 Output 14 Input 5 5 1 2 3 4 5 Output 16 Note In the first sample, the three possible divisions are as follows. * [[1], [1], [2]] * [[1, 1], [2]] * [[1, 1, 2]] Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
{ "input": [ "5 5\n1 2 3 4 5\n", "3 1\n1 1 2\n", "5 2\n1 1 2 1 3\n" ], "output": [ "16", "3", "14" ] }
{ "input": [ "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n", "100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n", "10 1\n4 2 9 2 1 4 4 1 4 10\n", "100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n", "1 1\n1\n", "100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n", "2 2\n2 1\n", "2 1\n2 2\n" ], "output": [ "16", "726975503", "24", "244208148", "1", "1", "2", "2" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; pair<int, int> operator+(const pair<int, int> x, const int y) { return make_pair(x.first + y, x.second); } const int mxn = 131072, siz = 1, md = 998244353; int n, k, pl[mxn], pr[mxn], d[mxn], f[mxn], bl[mxn], cnt[512][256], tag[512]; pair<int, int> a[mxn], b[mxn], c[mxn]; void mrg(pair<int, int> *x, int nx, pair<int, int> *y, int ny, pair<int, int> *z) { int px = 0, py = 0, pz = 0; for (; px < nx && py < ny;) if (x[px] < y[py]) z[pz++] = x[px++]; else z[pz++] = y[py++]; for (; px < nx; z[pz++] = x[px++]) ; for (; py < ny; z[pz++] = y[py++]) ; } void reb(int pos, int l, int r) { int mn = n + 1; for (int i = l; i <= r; ++i) mn = min(mn, a[i].first); for (int i = l; i <= r; ++i) a[i].first -= mn; for (int i = 0; i <= siz; ++i) cnt[pos][i] = 0; for (int i = l; i <= r; ++i) (cnt[pos][a[i].first] += f[a[i].second]) %= md; for (int i = 1; i <= siz; ++i) (cnt[pos][i] += cnt[pos][i - 1]) %= md; tag[pos] += mn; } void add(int l, int r, int x) { int pb = 0, pc = 0; for (int i = (bl[l] - 1) * siz + 1; i <= min(bl[l] * siz, n); ++i) if (a[i].second < l || a[i].second > r) b[pb++] = a[i]; else c[pc++] = a[i] + x; mrg(b, pb, c, pc, a + (bl[l] - 1) * siz + 1); reb(bl[l], (bl[l] - 1) * siz + 1, min(bl[l] * siz, n)); if (bl[l] != bl[r]) { pb = 0, pc = 0; for (int i = (bl[r] - 1) * siz + 1; i <= min(bl[r] * siz, n); ++i) if (a[i].second < l || a[i].second > r) b[pb++] = a[i]; else c[pc++] = a[i] + x; mrg(b, pb, c, pc, a + (bl[r] - 1) * siz + 1); reb(bl[r], (bl[r] - 1) * siz + 1, min(bl[r] * siz, n)); } for (int i = bl[l] + 1; i <= bl[r] - 1; ++i) tag[i] += x; } void init() { for (int i = 1; i <= n; ++i) bl[i] = (i - 1) / siz + 1; for (int i = 1; i <= n; ++i) a[i] = make_pair(0, i); for (int i = 1; i <= bl[n]; ++i) reb(i, (i - 1) * siz + 1, min(i * siz, n)); } int main() { scanf("%d%d", &n, &k), ++n, f[1] = 1; for (int i = 2; i <= n; ++i) scanf("%d", &d[i]); init(); for (int i = 2; i <= n; ++i) { if (pr[d[i]]) add(pl[d[i]] + 1, pr[d[i]], -1); pl[d[i]] = pr[d[i]], pr[d[i]] = i - 1; add(pl[d[i]] + 1, pr[d[i]], 1); for (int j = 1; j <= bl[i]; ++j) if (k - tag[j] >= 0 && k - tag[j] <= siz) (f[i] += cnt[j][k - tag[j]]) %= md; reb(bl[i], (bl[i] - 1) * siz + 1, min(bl[i] * siz, n)); } printf("%d\n", f[n]); return 0; }
1129_D. Isolation
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once. Since the answer can be large, find it modulo 998 244 353. Input The first line contains two space-separated integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the number of elements in the array a and the restriction from the statement. The following line contains n space-separated integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n) β€” elements of the array a. Output The first and only line contains the number of ways to divide an array a modulo 998 244 353. Examples Input 3 1 1 1 2 Output 3 Input 5 2 1 1 2 1 3 Output 14 Input 5 5 1 2 3 4 5 Output 16 Note In the first sample, the three possible divisions are as follows. * [[1], [1], [2]] * [[1, 1], [2]] * [[1, 1, 2]] Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
{ "input": [ "5 5\n1 2 3 4 5\n", "3 1\n1 1 2\n", "5 2\n1 1 2 1 3\n" ], "output": [ "16", "3", "14" ] }
{ "input": [ "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n", "100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n", "10 1\n4 2 9 2 1 4 4 1 4 10\n", "100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n", "1 1\n1\n", "100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n", "2 2\n2 1\n", "2 1\n2 2\n" ], "output": [ "16", "726975503", "24", "244208148", "1", "1", "2", "2" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; pair<int, int> operator+(const pair<int, int> x, const int y) { return make_pair(x.first + y, x.second); } const int mxn = 131072, siz = 250, md = 998244353; int n, k, pl[mxn], pr[mxn], d[mxn], f[mxn], bl[mxn], cnt[512][256], tag[512]; pair<int, int> a[mxn], b[mxn], c[mxn]; void mrg(pair<int, int> *x, int nx, pair<int, int> *y, int ny, pair<int, int> *z) { int px = 0, py = 0, pz = 0; for (; px < nx && py < ny;) if (x[px] < y[py]) z[pz++] = x[px++]; else z[pz++] = y[py++]; for (; px < nx; z[pz++] = x[px++]) ; for (; py < ny; z[pz++] = y[py++]) ; } void reb(int pos, int l, int r) { int mn = n + 1; for (int i = l; i <= r; ++i) mn = min(mn, a[i].first); for (int i = l; i <= r; ++i) a[i].first -= mn; for (int i = 0; i <= siz; ++i) cnt[pos][i] = 0; for (int i = l; i <= r; ++i) (cnt[pos][a[i].first] += f[a[i].second]) %= md; for (int i = 1; i <= siz; ++i) (cnt[pos][i] += cnt[pos][i - 1]) %= md; tag[pos] += mn; } void add(int l, int r, int x) { int pb = 0, pc = 0; for (int i = (bl[l] - 1) * siz + 1; i <= min(bl[l] * siz, n); ++i) if (a[i].second < l || a[i].second > r) b[pb++] = a[i]; else c[pc++] = a[i] + x; mrg(b, pb, c, pc, a + (bl[l] - 1) * siz + 1); reb(bl[l], (bl[l] - 1) * siz + 1, min(bl[l] * siz, n)); if (bl[l] != bl[r]) { pb = 0, pc = 0; for (int i = (bl[r] - 1) * siz + 1; i <= min(bl[r] * siz, n); ++i) if (a[i].second < l || a[i].second > r) b[pb++] = a[i]; else c[pc++] = a[i] + x; mrg(b, pb, c, pc, a + (bl[r] - 1) * siz + 1); reb(bl[r], (bl[r] - 1) * siz + 1, min(bl[r] * siz, n)); } for (int i = bl[l] + 1; i <= bl[r] - 1; ++i) tag[i] += x; } void init() { for (int i = 1; i <= n; ++i) bl[i] = (i - 1) / siz + 1; for (int i = 1; i <= n; ++i) a[i] = make_pair(0, i); for (int i = 1; i <= bl[n]; ++i) reb(i, (i - 1) * siz + 1, min(i * siz, n)); } int main() { scanf("%d%d", &n, &k), ++n, f[1] = 1; for (int i = 2; i <= n; ++i) scanf("%d", &d[i]); init(); for (int i = 2; i <= n; ++i) { if (pr[d[i]]) add(pl[d[i]] + 1, pr[d[i]], -1); pl[d[i]] = pr[d[i]], pr[d[i]] = i - 1; add(pl[d[i]] + 1, pr[d[i]], 1); for (int j = 1; j <= bl[i]; ++j) if (k - tag[j] >= 0) (f[i] += cnt[j][k - tag[j]]) %= md; reb(bl[i], (bl[i] - 1) * siz + 1, min(bl[i] * siz, n)); } printf("%d\n", f[n]); return 0; }
1129_D. Isolation
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once. Since the answer can be large, find it modulo 998 244 353. Input The first line contains two space-separated integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the number of elements in the array a and the restriction from the statement. The following line contains n space-separated integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n) β€” elements of the array a. Output The first and only line contains the number of ways to divide an array a modulo 998 244 353. Examples Input 3 1 1 1 2 Output 3 Input 5 2 1 1 2 1 3 Output 14 Input 5 5 1 2 3 4 5 Output 16 Note In the first sample, the three possible divisions are as follows. * [[1], [1], [2]] * [[1, 1], [2]] * [[1, 1, 2]] Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
{ "input": [ "5 5\n1 2 3 4 5\n", "3 1\n1 1 2\n", "5 2\n1 1 2 1 3\n" ], "output": [ "16", "3", "14" ] }
{ "input": [ "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n", "100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n", "10 1\n4 2 9 2 1 4 4 1 4 10\n", "100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n", "1 1\n1\n", "100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n", "2 2\n2 1\n", "2 1\n2 2\n" ], "output": [ "16", "726975503", "24", "244208148", "1", "1", "2", "2" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> #pragma comment(linker, "/stack:200000000") #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,avx,avx2") using namespace std; template <class T1, class T2, class T3> struct triple { T1 a; T2 b; T3 c; triple() : a(T1()), b(T2()), c(T3()){}; triple(T1 _a, T2 _b, T3 _c) : a(_a), b(_b), c(_c) {} }; template <class T1, class T2, class T3> bool operator<(const triple<T1, T2, T3>& t1, const triple<T1, T2, T3>& t2) { if (t1.a != t2.a) return t1.a < t2.a; else if (t1.b != t2.b) return t1.b < t2.b; else return t1.c < t2.c; } template <class T1, class T2, class T3> bool operator>(const triple<T1, T2, T3>& t1, const triple<T1, T2, T3>& t2) { if (t1.a != t2.a) return t1.a > t2.a; else if (t1.b != t2.b) return t1.b > t2.b; else return t1.c > t2.c; } inline int countBits(unsigned int v) { v = v - ((v >> 1) & 0x55555555); v = (v & 0x33333333) + ((v >> 2) & 0x33333333); return ((v + (v >> 4) & 0xF0F0F0F) * 0x1010101) >> 24; } inline int countBits(unsigned long long v) { unsigned int t = v >> 32; unsigned int p = (v & ((1ULL << 32) - 1)); return countBits(t) + countBits(p); } inline int countBits(long long v) { return countBits((unsigned long long)v); } inline int countBits(int v) { return countBits((unsigned int)v); } unsigned int reverseBits(unsigned int x) { x = (((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1)); x = (((x & 0xcccccccc) >> 2) | ((x & 0x33333333) << 2)); x = (((x & 0xf0f0f0f0) >> 4) | ((x & 0x0f0f0f0f) << 4)); x = (((x & 0xff00ff00) >> 8) | ((x & 0x00ff00ff) << 8)); return ((x >> 16) | (x << 16)); } template <class T> inline int sign(T x) { return x > 0 ? 1 : x < 0 ? -1 : 0; } inline bool isPowerOfTwo(int x) { return (x != 0 && (x & (x - 1)) == 0); } constexpr long long power(long long x, int p) { return p == 0 ? 1 : (x * power(x, p - 1)); } template <class T> inline bool inRange(const T& val, const T& min, const T& max) { return min <= val && val <= max; } unsigned long long rdtsc() { unsigned long long ret = 0; return __builtin_readcyclecounter(); asm volatile("rdtsc" : "=A"(ret) : :); return ret; } const int __BS = 4096; static char __bur[__BS + 16], *__er = __bur + __BS, *__ir = __er; template <class T = int> T readInt() { auto c = [&]() { if (__ir == __er) std::fill(__bur, __bur + __BS, 0), cin.read(__bur, __BS), __ir = __bur; }; c(); while (*__ir && (*__ir < '0' || *__ir > '9') && *__ir != '-') ++__ir; c(); bool m = false; if (*__ir == '-') ++__ir, c(), m = true; T r = 0; while (*__ir >= '0' && *__ir <= '9') r = r * 10 + *__ir - '0', ++__ir, c(); ++__ir; return m ? -r : r; } string readString() { auto c = [&]() { if (__ir == __er) std::fill(__bur, __bur + __BS, 0), cin.read(__bur, __BS), __ir = __bur; }; string r; c(); while (*__ir && isspace(*__ir)) ++__ir, c(); while (!isspace(*__ir)) r.push_back(*__ir), ++__ir, c(); ++__ir; return r; } char readChar() { auto c = [&]() { if (__ir == __er) std::fill(__bur, __bur + __BS, 0), cin.read(__bur, __BS), __ir = __bur; }; c(); while (*__ir && isspace(*__ir)) ++__ir, c(); return *__ir++; } static char __buw[__BS + 20], *__iw = __buw, *__ew = __buw + __BS; template <class T> void writeInt(T x, char endc = '\n') { if (x < 0) *__iw++ = '-', x = -x; if (x == 0) *__iw++ = '0'; char* s = __iw; while (x) { T t = x / 10; char c = x - 10 * t + '0'; *__iw++ = c; x = t; } char* f = __iw - 1; while (s < f) swap(*s, *f), ++s, --f; if (__iw > __ew) cout.write(__buw, __iw - __buw), __iw = __buw; if (endc) { *__iw++ = endc; } } template <class T> void writeStr(const T& str) { int i = 0; while (str[i]) { *__iw++ = str[i++]; if (__iw > __ew) cout.write(__buw, __iw - __buw), __iw = __buw; } } struct __FL__ { ~__FL__() { if (__iw != __buw) cout.write(__buw, __iw - __buw); } }; static __FL__ __flushVar__; template <class T1, class T2> ostream& operator<<(ostream& os, const pair<T1, T2>& p); template <class T> ostream& operator<<(ostream& os, const vector<T>& v); template <class T1, class T2> ostream& operator<<(ostream& os, const set<T1, T2>& v); template <class T1, class T2> ostream& operator<<(ostream& os, const multiset<T1, T2>& v); template <class T1, class T2> ostream& operator<<(ostream& os, priority_queue<T1, T2> v); template <class T1, class T2, class T3> ostream& operator<<(ostream& os, const map<T1, T2, T3>& v); template <class T1, class T2, class T3> ostream& operator<<(ostream& os, const multimap<T1, T2, T3>& v); template <class T1, class T2, class T3> ostream& operator<<(ostream& os, const triple<T1, T2, T3>& t); template <class T, size_t N> ostream& operator<<(ostream& os, const array<T, N>& v); template <class T1, class T2> ostream& operator<<(ostream& os, const pair<T1, T2>& p) { return os << "(" << p.first << ", " << p.second << ")"; } template <class T> ostream& operator<<(ostream& os, const vector<T>& v) { bool f = 1; os << "["; for (auto& i : v) { if (!f) os << ", "; os << i; f = 0; } return os << "]"; } template <class T, size_t N> ostream& operator<<(ostream& os, const array<T, N>& v) { bool f = 1; os << "["; for (auto& i : v) { if (!f) os << ", "; os << i; f = 0; } return os << "]"; } template <class T1, class T2> ostream& operator<<(ostream& os, const set<T1, T2>& v) { bool f = 1; os << "["; for (auto& i : v) { if (!f) os << ", "; os << i; f = 0; } return os << "]"; } template <class T1, class T2> ostream& operator<<(ostream& os, const multiset<T1, T2>& v) { bool f = 1; os << "["; for (auto& i : v) { if (!f) os << ", "; os << i; f = 0; } return os << "]"; } template <class T1, class T2, class T3> ostream& operator<<(ostream& os, const map<T1, T2, T3>& v) { bool f = 1; os << "["; for (auto& ii : v) { if (!f) os << ", "; os << "(" << ii.first << " -> " << ii.second << ") "; f = 0; } return os << "]"; } template <class T1, class T2> ostream& operator<<(ostream& os, const multimap<T1, T2>& v) { bool f = 1; os << "["; for (auto& ii : v) { if (!f) os << ", "; os << "(" << ii.first << " -> " << ii.second << ") "; f = 0; } return os << "]"; } template <class T1, class T2> ostream& operator<<(ostream& os, priority_queue<T1, T2> v) { bool f = 1; os << "["; while (!v.empty()) { auto x = v.top(); v.pop(); if (!f) os << ", "; f = 0; os << x; } return os << "]"; } template <class T1, class T2, class T3> ostream& operator<<(ostream& os, const triple<T1, T2, T3>& t) { return os << "(" << t.a << ", " << t.b << ", " << t.c << ")"; } template <class T1, class T2> void printarray(const T1& a, T2 sz, T2 beg = 0) { for (T2 i = beg; i < sz; i++) cout << a[i] << " "; cout << endl; } template <class T1, class T2, class T3> inline istream& operator>>(istream& os, triple<T1, T2, T3>& t); template <class T1, class T2> inline istream& operator>>(istream& os, pair<T1, T2>& p) { return os >> p.first >> p.second; } template <class T> inline istream& operator>>(istream& os, vector<T>& v) { if (v.size()) for (T& t : v) os >> t; else { string s; T obj; while (s.empty()) { getline(os, s); if (!os) return os; } stringstream ss(s); while (ss >> obj) v.push_back(obj); } return os; } template <class T1, class T2, class T3> inline istream& operator>>(istream& os, triple<T1, T2, T3>& t) { return os >> t.a >> t.b >> t.c; } template <class T1, class T2> inline pair<T1, T2> operator+(const pair<T1, T2>& p1, const pair<T1, T2>& p2) { return pair<T1, T2>(p1.first + p2.first, p1.second + p2.second); } template <class T1, class T2> inline pair<T1, T2>& operator+=(pair<T1, T2>& p1, const pair<T1, T2>& p2) { p1.first += p2.first, p1.second += p2.second; return p1; } template <class T1, class T2> inline pair<T1, T2> operator-(const pair<T1, T2>& p1, const pair<T1, T2>& p2) { return pair<T1, T2>(p1.first - p2.first, p1.second - p2.second); } template <class T1, class T2> inline pair<T1, T2>& operator-=(pair<T1, T2>& p1, const pair<T1, T2>& p2) { p1.first -= p2.first, p1.second -= p2.second; return p1; } const int USUAL_MOD = 1000000007; template <class T> inline void normmod(T& x, T m = USUAL_MOD) { x %= m; if (x < 0) x += m; } template <class T1, class T2> inline T2 summodfast(T1 x, T1 y, T2 m = USUAL_MOD) { T2 res = x + y; if (res >= m) res -= m; return res; } template <class T1, class T2, class T3 = int> inline void addmodfast(T1& x, T2 y, T3 m = USUAL_MOD) { x += y; if (x >= m) x -= m; } template <class T1, class T2, class T3 = int> inline void submodfast(T1& x, T2 y, T3 m = USUAL_MOD) { x -= y; if (x < 0) x += m; } inline long long mulmod(long long x, long long n, long long m) { x %= m; n %= m; long long r = x * n - long long(long double(x) * long double(n) / long double(m)) * m; while (r < 0) r += m; while (r >= m) r -= m; return r; } inline long long powmod(long long x, long long n, long long m) { long long r = 1; normmod(x, m); while (n) { if (n & 1) r *= x; x *= x; r %= m; x %= m; n /= 2; } return r; } inline long long powmulmod(long long x, long long n, long long m) { long long res = 1; normmod(x, m); while (n) { if (n & 1) res = mulmod(res, x, m); x = mulmod(x, x, m); n /= 2; } return res; } template <class T> inline T gcd(T a, T b) { while (b) { T t = a % b; a = b; b = t; } return a; } template <class T> T fast_gcd(T u, T v) { int shl = 0; while (u && v && u != v) { T eu = u & 1; u >>= eu ^ 1; T ev = v & 1; v >>= ev ^ 1; shl += (~(eu | ev) & 1); T d = u & v & 1 ? (u + v) >> 1 : 0; T dif = (u - v) >> (sizeof(T) * 8 - 1); u -= d & ~dif; v -= d & dif; } return std::max(u, v) << shl; } inline long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } template <class T> inline T gcd(T a, T b, T c) { return gcd(gcd(a, b), c); } long long gcdex(long long a, long long b, long long& x, long long& y) { if (!a) { x = 0; y = 1; return b; } long long y1; long long d = gcdex(b % a, a, y, y1); x = y1 - (b / a) * y; return d; } bool isPrime(long long n) { if (n <= (1 << 14)) { int x = (int)n; if (x <= 4 || x % 2 == 0 || x % 3 == 0) return x == 2 || x == 3; for (int i = 5; i * i <= x; i += 6) if (x % i == 0 || x % (i + 2) == 0) return 0; return 1; } long long s = n - 1; int t = 0; while (s % 2 == 0) { s /= 2; ++t; } for (int a : {2, 325, 9375, 28178, 450775, 9780504, 1795265022}) { if (!(a %= n)) return true; long long f = powmulmod(a, s, n); if (f == 1 || f == n - 1) continue; for (int i = 1; i < t; ++i) if ((f = mulmod(f, f, n)) == n - 1) goto nextp; return false; nextp:; } return true; } int32_t solve(); int32_t main(int argc, char** argv) { ios_base::sync_with_stdio(0); cin.tie(0); return solve(); } int a[101001]; int b[101010]; int dp[101010]; int p1[101010]; int p2[101010]; long long ans = 0; int k; template <int val> void add(int* __restrict a, int* __restrict dp, int n) { for (int i = 0; i < n; ++i) { if (val == -1) { ans += a[i] == 0 ? dp[i] : 0; } a[i] += val; if (val == 1) { ans -= a[i] == 0 ? dp[i] : 0; } } } int solve() { int n = readInt(); k = readInt(); for (int i = 0; i < (n); ++i) { b[i] = -k - 1; } for (int i = 0; i < (n); ++i) { a[i] = readInt() - 1; p1[i] = p2[i] = -1; } dp[0] = 1; ans = 1; for (int i = 0; i < n; ++i) { int x = a[i]; add<-1>(b + p2[x] + 1, dp + p2[x] + 1, p1[x] - p2[x]); add<1>(b + p1[x] + 1, dp + p1[x] + 1, i - p1[x]); ans %= 998244353; dp[i + 1] = (int)ans; ans *= 2; p2[x] = p1[x]; p1[x] = i; } cout << ans / 2 << endl; return 0; }
1129_D. Isolation
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once. Since the answer can be large, find it modulo 998 244 353. Input The first line contains two space-separated integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the number of elements in the array a and the restriction from the statement. The following line contains n space-separated integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n) β€” elements of the array a. Output The first and only line contains the number of ways to divide an array a modulo 998 244 353. Examples Input 3 1 1 1 2 Output 3 Input 5 2 1 1 2 1 3 Output 14 Input 5 5 1 2 3 4 5 Output 16 Note In the first sample, the three possible divisions are as follows. * [[1], [1], [2]] * [[1, 1], [2]] * [[1, 1, 2]] Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
{ "input": [ "5 5\n1 2 3 4 5\n", "3 1\n1 1 2\n", "5 2\n1 1 2 1 3\n" ], "output": [ "16", "3", "14" ] }
{ "input": [ "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n", "100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n", "10 1\n4 2 9 2 1 4 4 1 4 10\n", "100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n", "1 1\n1\n", "100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n", "2 2\n2 1\n", "2 1\n2 2\n" ], "output": [ "16", "726975503", "24", "244208148", "1", "1", "2", "2" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int MAXSIZE = 360; class Solution { public: int count(vector<int>& nums, int k) { int n = nums.size(); init(n, k); dp[0] = 1; for (int i = 0; i < n; ++i) { count(nums[i], i); } return dp[n]; } private: struct Block { Block() : left(0), right(-1), sum(0) { fill(begin(sums), end(sums), 0); } int left, right; int sum; long long sums[MAXSIZE * 2 + 1]; long long count(int x) { long long res = 0; int sz = right - left; if (x < -sz) { return 0; } x = min(x, sz); res = get(x); return res; } long long get(int i) { return sums[MAXSIZE + i]; } void put(int i, long long v) { sums[MAXSIZE + i] = v; } void add(int i, long long v) { sums[MAXSIZE + i] += v; } void update(const vector<int>& nums, const vector<int>& cnts) { int sz = right - left; for (int i = -sz; i <= sz; ++i) { put(i, 0); } sum = 0; for (int i = right - 1; i >= left; --i) { sum += nums[i]; put(sum, cnts[i]); } long long preSum = 0; for (int i = -sz; i <= sz; ++i) { preSum += get(i); put(i, preSum); } } }; int N; int K; vector<Block> blocks; vector<int> dp; vector<int> marks; vector<pair<int, int>> pos; vector<int> splitPoints; void init(int n, int k) { N = n; K = k; int sz = min(MAXSIZE, (int)sqrt(n) + 1); int bn = (n + sz - 1) / sz; blocks.clear(); blocks.resize(bn); splitPoints.clear(); splitPoints.reserve(bn); for (int i = 0, l = 0; i < bn; ++i) { splitPoints.push_back(l); int r = min(l + sz, n); blocks[i].left = l; blocks[i].right = r; l = r; } dp.clear(); dp.resize(n + 1, 0); marks.clear(); marks.resize(n + 1, 0); pos.clear(); pos.resize(n + 1, make_pair(-1, -1)); } void update(int num, int p) { if (pos[num].second >= 0) { updateBlocks(pos[num].second, 0); pos[num].second = pos[num].first; } if (pos[num].first >= 0) { updateBlocks(pos[num].first, -1); } pos[num].first = p; updateBlocks(p, 1); } pair<int, int> locate(int p) { if (p < 0 || p >= N) { return {-1, -1}; } auto bIdx = upper_bound(splitPoints.begin(), splitPoints.end(), p) - splitPoints.begin(); --bIdx; int idx = p - blocks[bIdx].left; return {bIdx, idx}; } void updateBlocks(int p, int val) { auto pos = locate(p); if (pos.first == -1) { assert(0); return; } int bIdx = pos.first; marks[p] = val; blocks[bIdx].update(marks, dp); } int count(int num, int p) { update(num, p); auto pp = locate(p); int bIdx = pp.first; int left = blocks[bIdx].left; int t = 0; long long cnt = 0; for (int i = p; i >= left; --i) { t += marks[i]; if (t <= K) { cnt += dp[i]; } } while (--bIdx >= 0) { cnt += blocks[bIdx].count(K - t); t += blocks[bIdx].sum; } dp[p + 1] = cnt % M; return dp[p + 1]; } const static int M = 998244353; }; int main(int argc, char** argv) { ios::sync_with_stdio(false); cin.tie(0); Solution sol; int n, k; cin >> n >> k; vector<int> nums(n); for (int i = 0; i < n; ++i) { cin >> nums[i]; --nums[i]; } cout << sol.count(nums, k) << "\n"; return 0; }
1129_D. Isolation
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once. Since the answer can be large, find it modulo 998 244 353. Input The first line contains two space-separated integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the number of elements in the array a and the restriction from the statement. The following line contains n space-separated integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n) β€” elements of the array a. Output The first and only line contains the number of ways to divide an array a modulo 998 244 353. Examples Input 3 1 1 1 2 Output 3 Input 5 2 1 1 2 1 3 Output 14 Input 5 5 1 2 3 4 5 Output 16 Note In the first sample, the three possible divisions are as follows. * [[1], [1], [2]] * [[1, 1], [2]] * [[1, 1, 2]] Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
{ "input": [ "5 5\n1 2 3 4 5\n", "3 1\n1 1 2\n", "5 2\n1 1 2 1 3\n" ], "output": [ "16", "3", "14" ] }
{ "input": [ "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n", "100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n", "10 1\n4 2 9 2 1 4 4 1 4 10\n", "100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n", "1 1\n1\n", "100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n", "2 2\n2 1\n", "2 1\n2 2\n" ], "output": [ "16", "726975503", "24", "244208148", "1", "1", "2", "2" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int f[100010], val[100010], sep[100010], cnt, k, h[100010], last[100010], n; int read() { int tmp = 0; char c = getchar(); while (c < '0' || c > '9') c = getchar(); while (c >= '0' && c <= '9') { tmp = tmp * 10 + c - '0'; c = getchar(); } return tmp; } struct block { int l, r, tag, xs[320], val[320], diff[320], pre[320], cur, cnt; int id[2][320], merge[320]; void change(int tl, int tr, int c) { int sum[2], p[2], now = 0; sum[0] = sum[1] = 0; p[0] = p[1] = 1; for (int i = 1; i <= r - l + 1; i++) if (merge[i] >= tl - l + 1 && merge[i] <= tr - l + 1) { id[1][++sum[1]] = merge[i]; xs[merge[i]] += c; } else id[0][++sum[0]] = merge[i]; while (p[0] <= sum[0] || p[1] <= sum[1]) { int ch; if (p[0] > sum[0]) ch = 1; else if (p[1] > sum[1]) ch = 0; else if (xs[id[0][p[0]]] < xs[id[1][p[1]]] + c) ch = 0; else ch = 1; merge[++now] = id[ch][p[ch]]; p[ch]++; } cnt = 0; for (int i = 1; i <= r - l + 1; i++) if (i != 1 && xs[merge[i]] == xs[merge[i - 1]]) pre[cnt] = (pre[cnt] + val[merge[i]]) % 998244353; else { cnt++; diff[cnt] = xs[merge[i]]; pre[cnt] = val[merge[i]]; } cur = 0; for (int i = 1; i <= cnt; i++) { if (diff[i] + tag <= k) cur = i; pre[i] = (pre[i - 1] + pre[i]) % 998244353; } } void update() { if (cur && diff[cur] + tag > k) cur--; if (cur != cnt && diff[cur + 1] + tag <= k) cur++; } } B[320]; void modify(int l, int r, int x) { if (sep[l] == sep[r]) { B[sep[l]].change(l, r, x); return; } for (int i = sep[l] + 1; i <= sep[r] - 1; i++) { B[i].tag++; B[i].update(); } B[sep[l]].change(l, B[sep[l]].r, x); B[sep[r]].change(B[sep[r]].l, r, x); } int main() { n = read(); k = read(); int S = sqrt(n); for (int i = 1; i <= n; i++) val[i] = read(); for (int cur = 0; cur <= n; cur += S) { int nxt = min(cur + S - 1, n); cnt++; B[cnt].l = cur; B[cnt].r = nxt; for (int i = cur; i <= nxt; i++) { sep[i] = cnt; B[cnt].merge[i - cur + 1] = i - cur + 1; } B[cnt].cnt = B[cnt].cur = 1; } f[0] = 1; B[sep[0]].val[1] = f[0]; B[1].change(1, 0, 0); for (int i = 1; i <= n; i++) { last[i] = h[val[i]]; h[val[i]] = i; int tmp = last[i]; modify(tmp, i - 1, 1); if (tmp != 0) modify(last[tmp], tmp - 1, -1); for (int j = 1; j <= sep[i] - 1; j++) f[i] = (f[i] + B[j].pre[B[j].cur]) % 998244353; int c = sep[i]; for (int j = B[c].l; j <= i - 1; j++) if (B[c].tag + B[c].xs[j - B[c].l + 1] <= k) f[i] = (f[i] + f[j]) % 998244353; B[c].val[i - B[c].l + 1] = f[i]; B[c].change(i, i, 0); } printf("%d\n", f[n]); return 0; }
1129_D. Isolation
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once. Since the answer can be large, find it modulo 998 244 353. Input The first line contains two space-separated integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the number of elements in the array a and the restriction from the statement. The following line contains n space-separated integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n) β€” elements of the array a. Output The first and only line contains the number of ways to divide an array a modulo 998 244 353. Examples Input 3 1 1 1 2 Output 3 Input 5 2 1 1 2 1 3 Output 14 Input 5 5 1 2 3 4 5 Output 16 Note In the first sample, the three possible divisions are as follows. * [[1], [1], [2]] * [[1, 1], [2]] * [[1, 1, 2]] Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
{ "input": [ "5 5\n1 2 3 4 5\n", "3 1\n1 1 2\n", "5 2\n1 1 2 1 3\n" ], "output": [ "16", "3", "14" ] }
{ "input": [ "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n", "100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n", "10 1\n4 2 9 2 1 4 4 1 4 10\n", "100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n", "1 1\n1\n", "100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n", "2 2\n2 1\n", "2 1\n2 2\n" ], "output": [ "16", "726975503", "24", "244208148", "1", "1", "2", "2" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; inline void read(long long &x) { char ch; bool flag = false; for (ch = getchar(); !isdigit(ch); ch = getchar()) if (ch == '-') flag = true; for (x = 0; isdigit(ch); x = x * 10 + ch - '0', ch = getchar()) ; x = flag ? -x : x; } inline void write(long long x) { static const long long maxlen = 100; static char s[maxlen]; if (x < 0) { putchar('-'); x = -x; } if (!x) { putchar('0'); return; } long long len = 0; for (; x; x /= 10) s[len++] = x % 10 + '0'; for (long long i = len - 1; i >= 0; --i) putchar(s[i]); } const long long MAXN = 110000; const long long MAX_block = 336; const long long MAX_val = 2000; const long long AVG_val = 779; long long belong[MAXN]; long long l[MAX_block], r[MAX_block], cnt; long long add[MAX_block]; long long num[MAX_block][MAX_val]; long long val[MAXN]; long long n, lim; void prepare() { long long N = sqrt(n); for (long long i = 1; i <= n; i += N) { ++cnt; l[cnt] = i; r[cnt] = i + N - 1; } r[cnt] = min(r[cnt], n); for (long long i = 1; i <= cnt; i++) for (long long j = l[i]; j <= r[i]; j++) belong[j] = i; } const long long P = 998244353; long long a[MAXN]; long long f[MAXN]; void modify_block(int a, int b, int tag) { long long now = belong[a]; int flag = 0; for (long long i = a; i <= b; i++) { if (tag == 1) { long long &x = num[now][val[i] + AVG_val]; x = (x - f[i - 1] + P) % P; } else { long long &x = num[now][val[i] + AVG_val - 1]; x = (x + f[i - 1]) % P; } val[i] += tag; if (val[i] >= AVG_val) flag = 1; if (val[i] <= -AVG_val) flag = -1; } if (flag == 3) { add[now]++; add[now + 1]--; for (int i = 0; i < MAX_val - 1; i++) num[now][i] = num[now][i + 1]; for (int i = l[now]; i <= r[now]; i++) { val[i]--; if (val[i] <= -AVG_val) for (;;) ; } } if (flag == 4) { add[now]--; add[now + 1]++; for (int i = MAX_val - 2; i >= 0; i--) num[now][i + 1] = num[now][i]; num[now][0] = 0; for (int i = l[now]; i <= r[now]; i++) { val[i]++; if (val[i] >= AVG_val) for (;;) ; } } } void modify(long long a, long long b, long long tag) { if (belong[a] == belong[b]) { modify_block(a, b, tag); return; } add[belong[a] + 1] += tag; add[belong[b]] -= tag; modify(a, r[belong[a]], tag); modify(l[belong[b]], b, tag); } long long query() { long long sum = 0; long long kt = 0; for (long long i = 1; i <= cnt; i++) { kt += add[i]; int tmp = min(lim - kt + AVG_val, MAX_val - 1); if (tmp < 0) continue; sum = (sum + num[i][tmp]) % P; } return sum; } long long last[MAXN]; long long pre[MAXN]; int main() { read(n); read(lim); for (long long i = 1; i <= n; i++) read(a[i]); prepare(); f[0] = 1; for (long long i = 1; i <= n; i++) { pre[i] = last[a[i]]; long long now = belong[i]; for (long long j = AVG_val; j < MAX_val; j++) num[now][j] = (f[i - 1] + num[now][j]) % P; if (pre[i] == 0) { add[1]++; add[now]--; modify(l[now], i, 1); } else modify(pre[i] + 1, i, 1); if (pre[i] != 0) { modify(pre[pre[i]] + 1, pre[i], -1); } f[i] = query(); last[a[i]] = i; } cout << f[n] << endl; return 0; }
1129_D. Isolation
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once. Since the answer can be large, find it modulo 998 244 353. Input The first line contains two space-separated integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the number of elements in the array a and the restriction from the statement. The following line contains n space-separated integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n) β€” elements of the array a. Output The first and only line contains the number of ways to divide an array a modulo 998 244 353. Examples Input 3 1 1 1 2 Output 3 Input 5 2 1 1 2 1 3 Output 14 Input 5 5 1 2 3 4 5 Output 16 Note In the first sample, the three possible divisions are as follows. * [[1], [1], [2]] * [[1, 1], [2]] * [[1, 1, 2]] Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
{ "input": [ "5 5\n1 2 3 4 5\n", "3 1\n1 1 2\n", "5 2\n1 1 2 1 3\n" ], "output": [ "16", "3", "14" ] }
{ "input": [ "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n", "100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n", "10 1\n4 2 9 2 1 4 4 1 4 10\n", "100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n", "1 1\n1\n", "100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n", "2 2\n2 1\n", "2 1\n2 2\n" ], "output": [ "16", "726975503", "24", "244208148", "1", "1", "2", "2" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> #pragma GCC optimize(3) using namespace std; int n, k, a[100005]; int last[100005], prelast[100005], dp[100005]; const int blocks = 400; const int blocknum = 300; void add(int &a, int b) { a += b; if (a >= 998244353) a -= 998244353; } void dec(int &a, int b) { a -= b; if (a < 0) a += 998244353; } int sum[blocknum]; int shift[blocknum]; int res[blocknum][blocks]; int cnt[blocknum][2 * 100005]; int origin; void point_modify(int pos, int v) { int b = (pos - 1) / blocks + 1; int obj = res[b][pos % blocks] + shift[b]; res[b][pos % blocks] += v; dec(cnt[b][origin + obj - shift[b]], dp[pos]); add(cnt[b][origin + obj - shift[b] + v], dp[pos]); if (obj == k && v > 0) dec(sum[b], dp[pos]); else if (obj == k + 1 && v < 0) add(sum[b], dp[pos]); } void block_modify(int id, int v) { if (v > 0) { if (k + 1 >= shift[id]) { dec(sum[id], cnt[id][origin + k - shift[id]]); } } else { if (k >= shift[id]) add(sum[id], cnt[id][origin + k + 1 - shift[id]]); } shift[id] += v; } int point_query(int pos) { int b = (pos - 1) / blocks + 1; if (res[b][pos % blocks] + shift[b] <= k) return dp[pos]; else return 0; } int block_query(int id) { return sum[id]; } void modify(int l, int r, int x) { while (l <= r && r % blocks != 0) point_modify(r, x), r--; while (l <= r && l % blocks != 1) point_modify(l, x), l++; while (l <= r) { int b = (l - 1) / blocks + 1; block_modify(b, x); l += blocks; } } int query(int l, int r) { int ans = 0; while (l <= r && r % blocks != 0) add(ans, point_query(r)), r--; while (l <= r && l % blocks != 1) add(ans, point_query(l)), l++; while (l <= r) { int b = (l - 1) / blocks + 1; add(ans, block_query(b)); l += blocks; } return ans; } int main() { scanf("%d%d", &n, &k); origin = n; for (int i = 1; i <= n; i++) scanf("%d", &a[i]); dp[1] = 1; cnt[1][1] = 1; sum[1] = 1; for (int i = 1; i <= n; i++) { modify(last[a[i]] + 1, i, 1); if (prelast[a[i]] < last[a[i]]) modify(prelast[a[i]] + 1, last[a[i]], -1); prelast[a[i]] = last[a[i]]; last[a[i]] = i; dp[i + 1] = query(1, i); add(sum[i / blocks + 1], dp[i + 1]); add(cnt[i / blocks + 1][origin - shift[i / blocks + 1]], dp[i + 1]); } printf("%d\n", dp[n + 1]); return 0; }
1129_D. Isolation
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once. Since the answer can be large, find it modulo 998 244 353. Input The first line contains two space-separated integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the number of elements in the array a and the restriction from the statement. The following line contains n space-separated integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n) β€” elements of the array a. Output The first and only line contains the number of ways to divide an array a modulo 998 244 353. Examples Input 3 1 1 1 2 Output 3 Input 5 2 1 1 2 1 3 Output 14 Input 5 5 1 2 3 4 5 Output 16 Note In the first sample, the three possible divisions are as follows. * [[1], [1], [2]] * [[1, 1], [2]] * [[1, 1, 2]] Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
{ "input": [ "5 5\n1 2 3 4 5\n", "3 1\n1 1 2\n", "5 2\n1 1 2 1 3\n" ], "output": [ "16", "3", "14" ] }
{ "input": [ "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n", "100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n", "10 1\n4 2 9 2 1 4 4 1 4 10\n", "100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n", "1 1\n1\n", "100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n", "2 2\n2 1\n", "2 1\n2 2\n" ], "output": [ "16", "726975503", "24", "244208148", "1", "1", "2", "2" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <class X, class Y> void amax(X &x, const Y &y) { if (x < y) x = y; } template <class X, class Y> void amin(X &x, const Y &y) { if (x > y) x = y; } const int INF = 1e9 + 10; const long long INFL = (long long)1e18 + 10; const int MAX = 1e5 + 10; const int MAXS = 320; const int MOD = 998244353; int n, k, sn; int a[MAX], prv[MAX], last[MAX], dp[MAX], q[MAXS][2 * MAXS]; int c[MAX], sc[MAXS]; int gb(int i) { return (i - 1) / sn + 1; } int bl(int i) { if (i <= n / sn) return sn; return n - (n / sn) * sn; } pair<int, int> itv(int i) { return make_pair((i - 1) * sn + 1, min(i * sn, n)); } int add(int a, int b) { return (a + b) % MOD; } int gq(int i, int j) { return q[i][j + MAXS]; } void uq(int i, int j, int v) { q[i][j + MAXS] = add(q[i][j + MAXS], v); } void calcq(int i) { memset(q[i], 0, 2 * MAXS * sizeof(q[i][0])); int l = itv(i).first, r = itv(i).second; int t = 0; for (int j = r; j >= l; j--) { t += c[i]; if (t < -bl(i)) uq(i, -bl(i), dp[j - 1]); else if (t <= bl(i)) uq(i, t, dp[j - 1]); } for (int j = -bl(i) + 1; j <= bl(i); j++) uq(i, j, gq(i, j - 1)); sc[i] = t; } void process() { cin >> n >> k; for (int i = 1; i <= n; i++) cin >> a[i]; sn = max((int)sqrt(n), 1); for (int i = 1; i <= n; i++) { prv[i] = last[a[i]]; last[a[i]] = i; } dp[0] = 1; for (int i = 1; i <= n; i++) { if (i > 0 && gb(i) > gb(i - 1)) calcq(gb(i - 1)); if (prv[prv[i]] != 0) { c[prv[prv[i]]] = 0; if (gb(prv[prv[i]]) < gb(i)) calcq(gb(prv[prv[i]])); } if (prv[i] != 0) { c[prv[i]] = -1; if (gb(prv[i]) < gb(i)) calcq(gb(prv[i])); } c[i] = 1; int l = itv(gb(i)).first; int t = 0; for (int j = i; j >= l; j--) { t += c[j]; if (t <= k) dp[i] = add(dp[i], dp[j - 1]); } int x = k - t; for (int j = gb(i) - 1; j >= 1; j--) { if (x >= -bl(j)) { if (x <= bl(j)) dp[i] = add(dp[i], gq(j, x)); else dp[i] = add(dp[i], gq(j, bl(j))); } x += sc[j]; } } cout << dp[n]; } int main() { ios_base::sync_with_stdio(false); process(); }
1129_D. Isolation
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once. Since the answer can be large, find it modulo 998 244 353. Input The first line contains two space-separated integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the number of elements in the array a and the restriction from the statement. The following line contains n space-separated integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n) β€” elements of the array a. Output The first and only line contains the number of ways to divide an array a modulo 998 244 353. Examples Input 3 1 1 1 2 Output 3 Input 5 2 1 1 2 1 3 Output 14 Input 5 5 1 2 3 4 5 Output 16 Note In the first sample, the three possible divisions are as follows. * [[1], [1], [2]] * [[1, 1], [2]] * [[1, 1, 2]] Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
{ "input": [ "5 5\n1 2 3 4 5\n", "3 1\n1 1 2\n", "5 2\n1 1 2 1 3\n" ], "output": [ "16", "3", "14" ] }
{ "input": [ "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n", "100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n", "10 1\n4 2 9 2 1 4 4 1 4 10\n", "100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n", "1 1\n1\n", "100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n", "2 2\n2 1\n", "2 1\n2 2\n" ], "output": [ "16", "726975503", "24", "244208148", "1", "1", "2", "2" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const double PI = acos(-1.0); const long long INF = 1000 * 1000 * 1000 + 7; const int mod = 998244353; const long long LINF = (long long)INF * INF; double eps = 1e-10; const int MAX = 100005; const int MAGIC = 400; const int zsuv = 400; int n, k; int dp[MAX]; int a[MAX]; int b[MAX]; int q[MAGIC][MAGIC * 2 + 5]; int prefB[MAX]; int last[MAX]; int de[MAX]; int global[MAGIC]; inline int add(int A, int B) { return A + B >= mod ? A + B - mod : A + B; } void getLast() { memset(last, -1, sizeof last); memset(de, -1, sizeof de); for (int i = 0; i < n; i++) { last[i] = de[a[i]]; de[a[i]] = i; } } void findQ(int pos) { memset(q[pos], 0, sizeof q[pos]); for (int i = pos * MAGIC + MAGIC - 1; i >= pos * MAGIC; i--) { q[pos][prefB[i] + zsuv] = add(q[pos][prefB[i] + zsuv], dp[i]); } for (int i = 0; i < 2 * MAGIC + 4; i++) q[pos][i + 1] = add(q[pos][i + 1], q[pos][i]); } void update(int pos, int delta) { for (int i = pos / MAGIC - 1; i >= 0; i--) { global[i] += delta; } for (int i = pos; i >= (pos / MAGIC) * MAGIC; i--) { prefB[i] += delta; } findQ(pos / MAGIC); } void doday(int pos, int value) { update(pos, 1); if (last[pos] == -1) return; pos = last[pos]; update(pos, -2); if (last[pos] == -1) return; pos = last[pos]; update(pos, 1); } int suma(int pos) { int K = k - global[pos]; if (K >= MAGIC) { return q[pos][10]; } if (K <= -MAGIC) return 0; return q[pos][K + zsuv]; } int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); cin >> n >> k; for (int i = 0; i < n; i++) { cin >> a[i]; a[i]--; } getLast(); dp[0] = 1; for (int r = 0; r < n; r++) { doday(r, a[r]); for (int l = r; l >= (r / MAGIC) * MAGIC; l--) { if (prefB[l] <= k) { dp[r + 1] = add(dp[r + 1], dp[l]); } } for (int i = r / MAGIC - 1; i >= 0; i--) { dp[r + 1] = add(dp[r + 1], suma(i)); } if (r % MAGIC == MAGIC - 1) findQ(r / MAGIC); } for (int i = 1; i <= n; i++) cout << dp[i] << " "; cout << endl; return 0; }
1129_D. Isolation
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once. Since the answer can be large, find it modulo 998 244 353. Input The first line contains two space-separated integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the number of elements in the array a and the restriction from the statement. The following line contains n space-separated integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n) β€” elements of the array a. Output The first and only line contains the number of ways to divide an array a modulo 998 244 353. Examples Input 3 1 1 1 2 Output 3 Input 5 2 1 1 2 1 3 Output 14 Input 5 5 1 2 3 4 5 Output 16 Note In the first sample, the three possible divisions are as follows. * [[1], [1], [2]] * [[1, 1], [2]] * [[1, 1, 2]] Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
{ "input": [ "5 5\n1 2 3 4 5\n", "3 1\n1 1 2\n", "5 2\n1 1 2 1 3\n" ], "output": [ "16", "3", "14" ] }
{ "input": [ "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n", "100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n", "10 1\n4 2 9 2 1 4 4 1 4 10\n", "100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n", "1 1\n1\n", "100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n", "2 2\n2 1\n", "2 1\n2 2\n" ], "output": [ "16", "726975503", "24", "244208148", "1", "1", "2", "2" ] }
IN-CORRECT
cpp
#pragma GCC optimize ("O3") #pragma GCC target ("sse4") #include <bits/stdc++.h> #include <ext/pb_ds/tree_policy.hpp> #include <ext/pb_ds/assoc_container.hpp> #include <ext/rope> using namespace std; using namespace __gnu_pbds; using namespace __gnu_cxx; random_device(rd); mt19937 rng(rd()); const long long FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); struct custom_hash { template<class T> unsigned long long operator()(T v) const { unsigned long long x = v; x += FIXED_RANDOM; // x += 11400714819323198485ull; // x = (x ^ (x >> 30)) * 13787848793156543929ull; x = (x ^ (x >> 27)) * 10723151780598845931ull; return x ^ (x >> 31); } }; template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; template<class T, class U> using hash_table = gp_hash_table<T, U, custom_hash>; template<class T> T randomize(T mod) { return (uniform_int_distribution<T>(0, mod - 1))(rng); } template<class T> void readi(T &x) { x = 0; bool negative = false; char c = ' '; while (c < '-') { c = getchar(); } if (c == '-') { negative = true; c = getchar(); } while (c >= '0') { x = x * 10 + (c - '0'); c = getchar(); } if (negative) { x = -x; } } template<class T> void printi(T output) { if (output == 0) { putchar('0'); return; } if (output < 0) { putchar('-'); output = -output; } int buf[20], n = 0; while(output) { buf[n] = ((output % 10)); output /= 10; n++; } for (n--; n >= 0; n--) { putchar(buf[n] + '0'); } return; } template<class T> void ckmin(T &a, T b) { a = min(a, b); } template<class T> void ckmax(T &a, T b) { a = max(a, b); } long long expo(long long a, long long e, long long mod) { return ((e == 0) ? 1 : ((expo(a * a % mod, e >> 1, mod)) * ((e & 1) ? a : 1) % mod)); } template<class T, class U> void nmod(T &x, U mod) { if (x >= mod) x -= mod; } template<class T> T gcd(T a, T b) { return (b ? gcd(b, a % b) : a); } #define y0 ___y0 #define y1 ___y1 #define MP make_pair #define PB push_back #define LB lower_bound #define UB upper_bound #define fi first #define se second #define DBG(x) cerr << #x << " = " << (x) << endl #define SZ(x) ((int) ((x).size())) #define FOR(i, a, b) for (auto i = (a); i < (b); i++) #define FORD(i, a, b) for (auto i = (a) - 1; i >= (b); i--) #define ALL(x) (x).begin(), (x).end() const long double PI = 4.0 * atan(1.0); const long double EPS = 1e-9; #define MAGIC 347 #define SINF 10007 #define CO 1000007 #define INF 998244353 #define BIG 1000000931 #define LARGE 1696969696967ll #define GIANT 2564008813937411ll #define LLINF 2696969696969696969ll #define MAXN 100013 typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<ld, ld> pdd; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<ld> vd; typedef vector<pii> vpi; typedef vector<pll> vpl; typedef vector<pdd> vpd; int N, K; int arr[MAXN]; int dp[MAXN]; int lt[MAXN]; int indiv[MAXN]; int go[MAGIC][MAXN]; int need[MAGIC]; int res[MAGIC]; int ans; //min - max in a specific range can't differ by more than sqrt(N) //so how about void upd(int L, int R, int v) { // cerr << "update " << L << ' ' << R << ' ' << v << endl; int BLOCK; if (L / MAGIC == R / MAGIC) { BLOCK = L / MAGIC; FOR(i, L, R + 1) { go[BLOCK][indiv[i]] += (INF - dp[i]); nmod(go[BLOCK][indiv[i]], INF); indiv[i] += v; go[BLOCK][indiv[i]] += dp[i]; nmod(go[BLOCK][indiv[i]], INF); if (v == 1 && indiv[i] == need[BLOCK] + 1) { res[BLOCK] += (INF - dp[i]); nmod(res[BLOCK], INF); } if (v == -1 && indiv[i] == need[BLOCK]) { res[BLOCK] += dp[i]; nmod(res[BLOCK], INF); } } } else { BLOCK = L / MAGIC; for (int i = L; i % MAGIC; i++) { go[BLOCK][indiv[i]] += (INF - dp[i]); nmod(go[BLOCK][indiv[i]], INF); indiv[i] += v; go[BLOCK][indiv[i]] += dp[i]; nmod(go[BLOCK][indiv[i]], INF); if (v == 1 && indiv[i] == need[BLOCK] + 1) { res[BLOCK] += (INF - dp[i]); nmod(res[BLOCK], INF); } if (v == -1 && indiv[i] == need[BLOCK]) { res[BLOCK] += dp[i]; nmod(res[BLOCK], INF); } } BLOCK = R / MAGIC; for (int i = (R / MAGIC) * MAGIC; i <= R; i++) { go[BLOCK][indiv[i]] += (INF - dp[i]); nmod(go[BLOCK][indiv[i]], INF); indiv[i] += v; go[BLOCK][indiv[i]] += dp[i]; nmod(go[BLOCK][indiv[i]], INF); if (v == 1 && indiv[i] == need[BLOCK] + 1) { res[BLOCK] += (INF - dp[i]); nmod(res[BLOCK], INF); } if (v == -1 && indiv[i] == need[BLOCK]) { res[BLOCK] += dp[i]; nmod(res[BLOCK], INF); } } FOR(i, L / MAGIC, R / MAGIC) { if (v == 1) { if (need[i] > 0) { res[i] += (INF - go[i][need[i]]); nmod(res[i], INF); } need[i]--; } if (v == -1) { need[i]++; if (need[i] > 0) { res[i] += go[i][need[i]]; nmod(res[i], INF); } } } } } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); // cout << fixed << setprecision(10); // cerr << fixed << setprecision(10); // if (fopen("file.in", "r")) // { // freopen ("file.in", "r", stdin); // freopen ("file.out", "w", stdout); // } cin >> N >> K; FOR(i, 0, N) { cin >> arr[i]; arr[i]--; } FOR(i, 0, N) dp[i] = -1; FOR(i, 0, N) { lt[i] = dp[arr[i]]; dp[arr[i]] = i; } FOR(i, 0, N) dp[i] = 0; FOR(i, 0, MAGIC) need[i] = K; //dp[i] is the # of ways to finish up to i - 1 dp[0] = 1; go[0][0]++; res[0] = 1; FOR(i, 0, N) { // cerr << "SOLVE\n"; int L = lt[i] + 1, R = i; //increment(L...R) upd(L, R, 1); if (L != 0) { R = L - 1; L = lt[L - 1] + 1; //decrement (L...R) upd(L, R, -1); } FOR(j, 0, i / MAGIC + 1) { dp[i + 1] += res[j]; nmod(dp[i + 1], INF); } //u ned to insert i + 1 go[(i + 1) / MAGIC][0] += dp[i + 1]; nmod(go[(i + 1) / MAGIC][0], INF); res[(i + 1) / MAGIC] += dp[i + 1]; nmod(res[(i + 1) / MAGIC], INF); // DBG(dp[i + 1]); } cout << dp[N] << '\n'; // cerr << "time elapsed = " << (clock() / (CLOCKS_PER_SEC / 1000)) << " ms" << endl; return 0; } /* READ READ READ * int overflow, maxn too small, special cases (n=1?, two distinct?), cin.tie() interactive * reread the problem, try small cases * note down possible sources of error as you go * do smth instead of nothing */
1129_D. Isolation
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once. Since the answer can be large, find it modulo 998 244 353. Input The first line contains two space-separated integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the number of elements in the array a and the restriction from the statement. The following line contains n space-separated integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n) β€” elements of the array a. Output The first and only line contains the number of ways to divide an array a modulo 998 244 353. Examples Input 3 1 1 1 2 Output 3 Input 5 2 1 1 2 1 3 Output 14 Input 5 5 1 2 3 4 5 Output 16 Note In the first sample, the three possible divisions are as follows. * [[1], [1], [2]] * [[1, 1], [2]] * [[1, 1, 2]] Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
{ "input": [ "5 5\n1 2 3 4 5\n", "3 1\n1 1 2\n", "5 2\n1 1 2 1 3\n" ], "output": [ "16", "3", "14" ] }
{ "input": [ "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n", "100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n", "10 1\n4 2 9 2 1 4 4 1 4 10\n", "100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n", "1 1\n1\n", "100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n", "2 2\n2 1\n", "2 1\n2 2\n" ], "output": [ "16", "726975503", "24", "244208148", "1", "1", "2", "2" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int mod = 998244353; const int len = 300; int last[100002], prelast[100002], a[100000], dp[100000]; int mag[100000]; int block[100000 / 300][2 * len + 1]; int blsum[100000 / len]; template <typename T1, typename T2> ostream& operator<<(ostream& ostr, const pair<T1, T2>& a) { ostr << '{' << a.first << ' ' << a.second << '}'; } void Set(int l, int r) { vector<pair<int, int> > suff(r - l + 1); int bl = l / len; blsum[bl] = 0; for (int i = r; i >= l; --i) { suff[i - l].first = mag[i] + (i < r ? suff[i - l + 1].first : 0); suff[i - l].second = (i ? dp[i - 1] : 1); blsum[bl] += mag[i]; } sort(suff.begin(), suff.end()); for (int i = 1; i < r - l + 1; ++i) { suff[i].second += suff[i - 1].second; if (suff[i].second >= mod) suff[i].second -= mod; } for (int sum = -len, i = 0; sum <= len; ++sum) { while (i + 1 < r - l + 1 && suff[i + 1].first <= sum) ++i; if (suff[i].first <= sum) block[bl][len + sum] = suff[i].second; else block[bl][len + sum] = 0; } } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, k; cin >> n >> k; fill(last + 1, last + n + 1, -1); fill(prelast + 1, prelast + n + 1, -1); for (int i = 0; i < n; ++i) cin >> a[i]; for (int i = 0; i < n; ++i) { if (prelast[a[i]] != -1) { int j = prelast[a[i]]; mag[j] = 0; int bl = j / len; Set(bl * len, min(i, bl * len + len)); } if (last[a[i]] != -1) { int j = last[a[i]]; mag[j] = -1; int bl = j / len; Set(bl * len, min(i, bl * len + len)); } int j = i; mag[j] = 1; int bl = j / len; Set(bl * len, min(i, bl * len + len)); long long sum = 0; for (int bl = i / len; bl >= 0; --bl) { if (k - sum >= -len) { dp[i] += block[bl][min((long long)len, k - sum) + len]; if (dp[i] >= mod) dp[i] -= mod; } sum += blsum[bl]; } prelast[a[i]] = last[a[i]]; last[a[i]] = i; } cout << dp[n - 1]; }
1129_D. Isolation
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once. Since the answer can be large, find it modulo 998 244 353. Input The first line contains two space-separated integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the number of elements in the array a and the restriction from the statement. The following line contains n space-separated integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n) β€” elements of the array a. Output The first and only line contains the number of ways to divide an array a modulo 998 244 353. Examples Input 3 1 1 1 2 Output 3 Input 5 2 1 1 2 1 3 Output 14 Input 5 5 1 2 3 4 5 Output 16 Note In the first sample, the three possible divisions are as follows. * [[1], [1], [2]] * [[1, 1], [2]] * [[1, 1, 2]] Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
{ "input": [ "5 5\n1 2 3 4 5\n", "3 1\n1 1 2\n", "5 2\n1 1 2 1 3\n" ], "output": [ "16", "3", "14" ] }
{ "input": [ "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n", "100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n", "10 1\n4 2 9 2 1 4 4 1 4 10\n", "100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n", "1 1\n1\n", "100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n", "2 2\n2 1\n", "2 1\n2 2\n" ], "output": [ "16", "726975503", "24", "244208148", "1", "1", "2", "2" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; const int N = 2e5 + 5; const int M = 505; const ll mod = 998244353; ll add(ll a, ll b) { return (a + b >= mod ? a + b - mod : a + b); } ll sub(ll a, ll b) { return (a - b < 0 ? a - b + mod : a - b); } ll mul(ll a, ll b) { return (a * b) % mod; } int n, k; int a[N]; int last[N], preLast[N]; ll dp[N]; int inDex[N], head[M]; int magic; struct block { int sz; int Min, Max; int bad[M]; ll val[M], sum[M]; void init(int n) { sz = n; Min = Max = 0; for (int i = 0; i <= sz; i++) { val[i] = bad[i] = sum[i] = 0; } } void updAll(int x) { Min += x; Max += x; } void upd(int l, int r, int x) { for (int i = l; i <= r; i++) { bad[i] += x; } Min = 1e9; Max = -1e9; for (int i = 0; i < sz; i++) { Min = min(Min, bad[i]); Max = max(Max, bad[i]); } for (int i = 0; i <= Max - Min; i++) sum[i] = 0; for (int i = 0; i < sz; i++) { sum[bad[i] - Min] = add(sum[bad[i] - Min], val[i]); } for (int i = 1; i <= Max - Min; i++) { sum[i] = add(sum[i], sum[i - 1]); } } void updVal(int id, ll x) { val[id] = x; for (int i = bad[id] - Min; i <= Max - Min; i++) { sum[i] = add(sum[i], x); } } ll get(int x) { if (Min > x) return 0; if (Max < x) return sum[Max - Min]; return sum[x - Min]; } } block[M]; void update(int l, int r, ll x) { int L = inDex[l], R = inDex[r]; if (L == R) { block[L].upd(l - head[L], r - head[L], x); return; } block[L].upd(l - head[L], head[L + 1] - head[L] - 1, x); block[R].upd(0, r - head[R], x); for (int i = L + 1; i < R; i++) { block[i].updAll(x); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> k; for (int i = 1; i <= n; i++) { cin >> a[i]; preLast[i] = -1; } magic = sqrt(n); int now = 1, cnt = 0; for (int i = 1; i <= n; i++) { if (head[now] == 0) head[now] = i; inDex[i] = now; cnt++; if (cnt == magic || i == n) { block[now].init(i - head[now] + 1); now++; cnt = 0; } } head[now] = n + 1; dp[0] = 1; for (int i = 1; i <= n; i++) { update(last[a[i]] + 1, i, 1); if (preLast[a[i]] != -1) { update(preLast[a[i]] + 1, last[a[i]], -1); } preLast[a[i]] = last[a[i]]; last[a[i]] = i; block[inDex[i]].updVal(i - head[inDex[i]], dp[i - 1]); for (int j = 1; j <= inDex[i]; j++) { dp[i] = add(dp[i], block[j].get(k)); } } cout << dp[n]; }
1129_D. Isolation
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once. Since the answer can be large, find it modulo 998 244 353. Input The first line contains two space-separated integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the number of elements in the array a and the restriction from the statement. The following line contains n space-separated integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n) β€” elements of the array a. Output The first and only line contains the number of ways to divide an array a modulo 998 244 353. Examples Input 3 1 1 1 2 Output 3 Input 5 2 1 1 2 1 3 Output 14 Input 5 5 1 2 3 4 5 Output 16 Note In the first sample, the three possible divisions are as follows. * [[1], [1], [2]] * [[1, 1], [2]] * [[1, 1, 2]] Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
{ "input": [ "5 5\n1 2 3 4 5\n", "3 1\n1 1 2\n", "5 2\n1 1 2 1 3\n" ], "output": [ "16", "3", "14" ] }
{ "input": [ "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n", "100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n", "10 1\n4 2 9 2 1 4 4 1 4 10\n", "100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n", "1 1\n1\n", "100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n", "2 2\n2 1\n", "2 1\n2 2\n" ], "output": [ "16", "726975503", "24", "244208148", "1", "1", "2", "2" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> //#pragma GCC optimize("unroll-loops") //#pragma GCC optimize("-O3") //#pragma GCC optimize("Ofast") #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #include <ext/rope> #define sz(x) int(x.size()) #define all(x) x.begin(),x.end() #define pii pair<int,int> #define PB push_back #define pll pair<ll,ll> #define pii pair<int,int> #define pil pair<int,ll> #define pli pair<ll, int> #define pi4 pair<pii, pii> #define pib pair<int, bool> #define ft first #define sd second using namespace std; using namespace __gnu_cxx; using namespace __gnu_pbds; typedef long long ll; typedef long double ld; template<class T> using ordered_set = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>; //const int N = 100100; const int N = 2010; const int M = (1 << 10); const int oo = 2e9; const ll OO = 1e18; const int PW = 20; const int md = int(1e9) + 7; //const int BLOCK = 750; const int BLOCK = 2; const int K = N / BLOCK; int f[N], SQ[K][2 * N], mid[K], sum[K], a[N], n, k, ppr[N], pr[N], vl[N]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); // freopen("in.txt","r",stdin); cin >> n >> k; for (int i = 0; i < n; i++) { cin >> a[i]; a[i]--; } fill(mid, mid + K, N); for (int i = 0; i < n; i++){ ppr[i] = pr[i] = -1; } for (int i = 0; i < n; i++){ int l = ppr[a[i]], r = pr[a[i]]; swap(ppr[a[i]], pr[a[i]]); pr[a[i]] = i; int nm = i / BLOCK; sum[nm] += (i ? f[i - 1] : 1); SQ[nm][mid[nm]] += (i ? f[i - 1] : 1); for (int j = max(r + 1, BLOCK * nm); j <= i; j++){ SQ[nm][mid[nm] + vl[j]] -= (j ? f[j - 1] : 1); vl[j]++; SQ[nm][mid[nm] + vl[j]] += (j ? f[j - 1] : 1); if (vl[j] == k + 1) sum[nm] -= (j ? f[j - 1] : 1); } if ((r + 1) / BLOCK != nm){ nm--; int nd = (r + 1) / BLOCK; for (; nm > nd; nm--){ sum[nm] -= SQ[nm][mid[nm] + k]; mid[nm]--; } for (int j = r + 1; j < (nm + 1) * BLOCK; j++){ SQ[nm][mid[nm] + vl[j]] -= (j ? f[j - 1] : 1); vl[j]++; SQ[nm][mid[nm] + vl[j]] += (j ? f[j - 1] : 1); if (vl[j] == k + 1) sum[nm] -= (j ? f[j - 1] : 1); } } if (r >= 0){ int nd = (l + 1) / BLOCK; if (nd < nm){ for (int j = BLOCK * nm; j < r + 1; j++){ SQ[nm][mid[nm] + vl[j]] -= (j ? f[j - 1] : 1); vl[j]--; SQ[nm][mid[nm] + vl[j]] += (j ? f[j - 1] : 1); if (vl[j] == k) sum[nm] += (j ? f[j - 1] : 1); } nm--; } for (; nm > nd; nm--){ sum[nm] += SQ[nm][mid[nm] + k + 1]; mid[nm]++; } for (int j = l + 1; j <= r; j++){ SQ[nm][mid[nm] + vl[j]] -= (j ? f[j - 1] : 1); vl[j]--; SQ[nm][mid[nm] + vl[j]] += (j ? f[j - 1] : 1); if (vl[j] == k) sum[nm] += (j ? f[j - 1] : 1); } } for (int j = 0; j * BLOCK <= i; j++) f[i] += sum[j]; nm = i / BLOCK; // SQ[nm][mid[nm] + vl[i]] += (i ? f[i - 1] : 1); } cout << f[n - 1]; return 0; }
1129_D. Isolation
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once. Since the answer can be large, find it modulo 998 244 353. Input The first line contains two space-separated integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the number of elements in the array a and the restriction from the statement. The following line contains n space-separated integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n) β€” elements of the array a. Output The first and only line contains the number of ways to divide an array a modulo 998 244 353. Examples Input 3 1 1 1 2 Output 3 Input 5 2 1 1 2 1 3 Output 14 Input 5 5 1 2 3 4 5 Output 16 Note In the first sample, the three possible divisions are as follows. * [[1], [1], [2]] * [[1, 1], [2]] * [[1, 1, 2]] Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
{ "input": [ "5 5\n1 2 3 4 5\n", "3 1\n1 1 2\n", "5 2\n1 1 2 1 3\n" ], "output": [ "16", "3", "14" ] }
{ "input": [ "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n", "100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n", "10 1\n4 2 9 2 1 4 4 1 4 10\n", "100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n", "1 1\n1\n", "100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n", "2 2\n2 1\n", "2 1\n2 2\n" ], "output": [ "16", "726975503", "24", "244208148", "1", "1", "2", "2" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int mod = 998244353; const int b = 325; int dp[100005]; int a[100005], cnt[100005]; int s[100005], e[100005]; vector<int> v[100005]; struct bucket { int val[2 * b + 5]; int sum = 0; void rebuild(int s, int e) { for (int i = 0; i <= 2 * b; i++) val[i] = 0; sum = 0; for (int i = e; i >= s; i--) { sum += cnt[i]; val[b + sum] = (val[b + sum] + dp[i - 1]) % mod; } for (int i = 1; i <= 2 * b; i++) val[i] = (val[i] + val[i - 1]) % mod; } } bucket[b + 5]; int main() { int n, k, a; scanf("%d%d", &n, &k); for (int i = 1; i <= n; i++) { int bi = i / b; e[bi] = i; if (s[bi] == 0) s[bi] = i; } dp[0] = 1; for (int i = 1; i <= n; i++) { scanf("%d", &a); if (v[a].size() >= 1) { cnt[v[a][v[a].size() - 1]] = -1; int idx = (v[a].size() - 1) / b; bucket[idx].rebuild(s[idx], e[idx]); if (v[a].size() >= 2) { cnt[v[a][v[a].size() - 2]] = 0; int idx = (v[a].size() - 2) / b; bucket[idx].rebuild(s[idx], e[idx]); } } v[a].push_back(i); cnt[i] = 1; int idx = i / b; bucket[idx].rebuild(s[idx], e[idx]); int l = 1, r = i; int bl = l / b, br = r / b; if (bl == br) { int sum = 0; for (int j = r; j >= l; j--) { sum += cnt[j]; if (sum <= k) dp[i] = (dp[i] + dp[j - 1]) % mod; } } else { int sum = 0; for (int j = r; j >= s[br]; j--) { sum += cnt[j]; if (sum <= k) dp[i] = (dp[i] + dp[j - 1]) % mod; } for (int j = br - 1; j >= bl + 1; j--) { int d = min(2 * b, k - sum + b); if (d >= 0) dp[i] = (dp[i] + bucket[j].val[d]) % mod; sum += bucket[j].sum; } for (int j = e[bl]; j >= l; j--) { sum += cnt[j]; if (sum <= k) dp[i] = (dp[i] + dp[j - 1]) % mod; } } } printf("%d\n", dp[n]); }
1129_D. Isolation
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once. Since the answer can be large, find it modulo 998 244 353. Input The first line contains two space-separated integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the number of elements in the array a and the restriction from the statement. The following line contains n space-separated integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n) β€” elements of the array a. Output The first and only line contains the number of ways to divide an array a modulo 998 244 353. Examples Input 3 1 1 1 2 Output 3 Input 5 2 1 1 2 1 3 Output 14 Input 5 5 1 2 3 4 5 Output 16 Note In the first sample, the three possible divisions are as follows. * [[1], [1], [2]] * [[1, 1], [2]] * [[1, 1, 2]] Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
{ "input": [ "5 5\n1 2 3 4 5\n", "3 1\n1 1 2\n", "5 2\n1 1 2 1 3\n" ], "output": [ "16", "3", "14" ] }
{ "input": [ "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n", "100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n", "10 1\n4 2 9 2 1 4 4 1 4 10\n", "100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n", "1 1\n1\n", "100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n", "2 2\n2 1\n", "2 1\n2 2\n" ], "output": [ "16", "726975503", "24", "244208148", "1", "1", "2", "2" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int P = 998244353; const int N = 100010, B = 300; int n, b, c; int a[N], ans[N], lst[N], prv[N], tag[N]; int lazy[B]; struct Fw { int fw[N * 2]; void ch(int k, int x) { k += N; for (; k < N * 2; k += k & -k) if ((fw[k] += x) >= P) fw[k] -= P; } int qry(int k) { int ret = 0; k += N; for (; k; k -= k & -k) if ((ret += fw[k]) >= P) ret -= P; return ret; } } sum[B]; void ch(int l, int v) { int f = l / b; for (int i = f + 1; i < c; ++i) lazy[i] += v; for (int j = l; j < (f + 1) * b; ++j) { int x = ans[j]; if (x) { sum[f].ch(tag[j], P - x); sum[f].ch(tag[j] += v, x); } else tag[j] += v; } } int qry(int k) { int ret = 0; for (int i = 0; i < c; ++i) if ((ret += sum[i].qry(k - lazy[i])) >= P) ret -= P; return ret; } int main() { int k; scanf("%d%d", &n, &k); for (int i = 1; i <= n; ++i) scanf("%d", &a[i]); b = sqrt(n); if (n / b > 295) b = n / 295; c = n / b + 1; ans[0] = 1; sum[0].ch(0, 1); for (int i = 1; i <= n; ++i) { int cur = lst[a[i]]; lst[a[i]] = i; prv[i] = cur; if (cur) { ch(prv[cur], -1); ch(cur, 1); } ch(cur, 1); ch(i, -1); ans[i] = qry(k); sum[i / b].ch(tag[i], ans[i]); } printf("%d\n", ans[n]); return 0; }
1129_D. Isolation
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once. Since the answer can be large, find it modulo 998 244 353. Input The first line contains two space-separated integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the number of elements in the array a and the restriction from the statement. The following line contains n space-separated integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n) β€” elements of the array a. Output The first and only line contains the number of ways to divide an array a modulo 998 244 353. Examples Input 3 1 1 1 2 Output 3 Input 5 2 1 1 2 1 3 Output 14 Input 5 5 1 2 3 4 5 Output 16 Note In the first sample, the three possible divisions are as follows. * [[1], [1], [2]] * [[1, 1], [2]] * [[1, 1, 2]] Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
{ "input": [ "5 5\n1 2 3 4 5\n", "3 1\n1 1 2\n", "5 2\n1 1 2 1 3\n" ], "output": [ "16", "3", "14" ] }
{ "input": [ "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n", "100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n", "10 1\n4 2 9 2 1 4 4 1 4 10\n", "100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n", "1 1\n1\n", "100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n", "2 2\n2 1\n", "2 1\n2 2\n" ], "output": [ "16", "726975503", "24", "244208148", "1", "1", "2", "2" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 5; const int inf = 0x3f3f3f3f; const int mod = 1e9 + 7; int a[maxn]; int n, k, sum; int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> k; int lim = 1000000; if (k & 1) { n = 1999; a[1] = 0, a[2] = -1; sum = (k + 2001) / 2; for (int i = 3; i <= n; ++i) { if (sum > lim) a[i] = lim, sum -= lim; else a[i] = sum, sum = 0; } } else { n = 2000; a[1] = 0, a[2] = -1; sum = (k + 2002) / 2; for (int i = 3; i <= n; ++i) { if (sum > lim) a[i] = lim, sum -= lim; else a[i] = sum, sum = 0; } } cout << n << endl; for (int i = 1; i <= n; ++i) cout << a[i] << " "; return 0; }
1129_D. Isolation
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once. Since the answer can be large, find it modulo 998 244 353. Input The first line contains two space-separated integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the number of elements in the array a and the restriction from the statement. The following line contains n space-separated integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n) β€” elements of the array a. Output The first and only line contains the number of ways to divide an array a modulo 998 244 353. Examples Input 3 1 1 1 2 Output 3 Input 5 2 1 1 2 1 3 Output 14 Input 5 5 1 2 3 4 5 Output 16 Note In the first sample, the three possible divisions are as follows. * [[1], [1], [2]] * [[1, 1], [2]] * [[1, 1, 2]] Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
{ "input": [ "5 5\n1 2 3 4 5\n", "3 1\n1 1 2\n", "5 2\n1 1 2 1 3\n" ], "output": [ "16", "3", "14" ] }
{ "input": [ "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n", "100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n", "10 1\n4 2 9 2 1 4 4 1 4 10\n", "100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n", "1 1\n1\n", "100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n", "2 2\n2 1\n", "2 1\n2 2\n" ], "output": [ "16", "726975503", "24", "244208148", "1", "1", "2", "2" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> int q, sum[320][2 * 320]; int dp[100009], last[100009], last2[100009], v[100009]; inline void add(int &a, int b) { a += b; if (a >= 1000000007) a -= 1000000007; } inline void update(int t) { memset(sum[t], 0, sizeof sum[t]); int cnt = 0; for (int i = t * q + q; i > t * q; i--) { cnt += v[i]; add(sum[t][cnt + q], dp[i - 1]); } for (int i = 1; i <= 2 * q; i++) add(sum[t][i], sum[t][i - 1]); } int main() { int n, k; fscanf(stdin, "%d%d", &n, &k); dp[0] = 1; q = sqrt(n); for (int i = 1; i <= n; i++) { int x; fscanf(stdin, "%d", &x); v[i] = 1; if (last2[x]) { v[last2[x]] = 0; if ((last2[x] - 1) / q < (i - 1) / q) update((last2[x] - 1) / q); } if (last[x]) { v[last[x]] = -1; if ((last[x] - 1) / q < (i - 1) / q) update((last[x] - 1) / q); } last2[x] = last[x]; last[x] = i; int cnt = 0, lim = (i - 1) / q * q; for (int j = i; j > lim; j--) { cnt += v[j]; if (cnt <= k) add(dp[i], dp[j - 1]); } for (int p = (i - 1) / q - 1; p >= 0; p--) { if (k - cnt >= -q) { if (k - cnt <= q) add(dp[i], sum[p][k - cnt + q]); else add(dp[i], sum[p][2 * q]); } } if (i % q == 0) update((i - 1) / q); } fprintf(stdout, "%d\n", dp[n]); fclose(stdin); fclose(stdout); return 0; }
1129_D. Isolation
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once. Since the answer can be large, find it modulo 998 244 353. Input The first line contains two space-separated integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the number of elements in the array a and the restriction from the statement. The following line contains n space-separated integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n) β€” elements of the array a. Output The first and only line contains the number of ways to divide an array a modulo 998 244 353. Examples Input 3 1 1 1 2 Output 3 Input 5 2 1 1 2 1 3 Output 14 Input 5 5 1 2 3 4 5 Output 16 Note In the first sample, the three possible divisions are as follows. * [[1], [1], [2]] * [[1, 1], [2]] * [[1, 1, 2]] Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
{ "input": [ "5 5\n1 2 3 4 5\n", "3 1\n1 1 2\n", "5 2\n1 1 2 1 3\n" ], "output": [ "16", "3", "14" ] }
{ "input": [ "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n", "100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n", "10 1\n4 2 9 2 1 4 4 1 4 10\n", "100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n", "1 1\n1\n", "100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n", "2 2\n2 1\n", "2 1\n2 2\n" ], "output": [ "16", "726975503", "24", "244208148", "1", "1", "2", "2" ] }
IN-CORRECT
cpp
#pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse4,sse3,avx,avx2") #include <iostream> #include <immintrin.h> using namespace std; using ll = long long; int a[101001]; int b[101010]; int dp[101010]; int p1[101010]; int p2[101010]; int ans = 0; int n, k; #define mod 998244353 void add(int* __restrict a, int* __restrict dp, int n) { int sum = 0; while (n % 8) { a[0]++; sum -= a[0] == 0 ? dp[0] : 0; sum += sum < 0 ? mod : 0; ++a; ++dp; --n; } n /= 4; __m256i x7 = _mm256_set1_epi32(0); __m256i x0 = _mm256_set1_epi32(0); __m256i x3 = _mm256_set1_epi32(1); __m256i x4 = _mm256_set1_epi32(mod); while (n--) { __m256i x1 = _mm256_loadu_si256((__m256i*)a); x1 = _mm256_add_epi32(x1, x3); _mm256_storeu_si256((__m256i*)a, x1); __m256i x2 = _mm256_loadu_si256((__m256i*)dp); x7 = _mm256_sub_epi32(x7, _mm256_and_si256(x2, _mm256_cmpeq_epi32(x1, x0))); x7 = _mm256_add_epi32(x7, _mm256_and_si256(x4, _mm256_cmpgt_epi32(x0, x7))); a += 4; dp += 4; } int res[8]; _mm256_storeu_si256((__m256i*)res, x7); for (int i = 0; i < 8; ++i) { sum += res[i]; if (sum >= mod) sum -= mod; } ans += sum; if (ans >= mod) ans -= mod; } void sub(int* __restrict a, int* __restrict dp, int n) { int sum = 0; while (n % 8) { sum -= a[0] == 0 ? dp[0] : 0; sum += sum < 0 ? mod : 0; a[0]--; ++a; ++dp; --n; } n /= 4; __m256i x7 = _mm256_set1_epi32(0); __m256i x0 = _mm256_set1_epi32(0); __m256i x3 = _mm256_set1_epi32(1); __m256i x4 = _mm256_set1_epi32(mod); while (n--) { __m256i x1 = _mm256_loadu_si256((__m256i*)a); __m256i x2 = _mm256_loadu_si256((__m256i*)dp); x7 = _mm256_sub_epi32(x7, _mm256_and_si256(x2, _mm256_cmpeq_epi32(x1, x0))); x7 = _mm256_add_epi32(x7, _mm256_and_si256(x4, _mm256_cmpgt_epi32(x0, x7))); x1 = _mm256_sub_epi32(x1, x3); _mm256_storeu_si256((__m256i*)a, x1); a += 8; dp += 8; } int res[8]; _mm256_storeu_si256((__m256i*)res, x7); for (int i = 0; i < 8; ++i) { sum += res[i]; if (sum >= mod) sum -= mod; } ans -= sum; ans += ans < 0 ? mod : 0; } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> k; for (int i = 0; i < n; ++i) { b[i] = -k - 1; cin >> a[i]; --a[i]; p1[i] = p2[i] = -1; } dp[0] = 1; ans = 1; for (int i = 0; i < n; ++i) { if (i) ans = (ans + ans) % mod; int x = a[i]; sub(b + p2[x] + 1, dp + p2[x] + 1, p1[x] - p2[x]); add(b + p1[x] + 1, dp + p1[x] + 1, i - p1[x]); dp[i + 1] = ans; p2[x] = p1[x]; p1[x] = i; } cout << ans << endl; return 0; }
1129_D. Isolation
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once. Since the answer can be large, find it modulo 998 244 353. Input The first line contains two space-separated integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the number of elements in the array a and the restriction from the statement. The following line contains n space-separated integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n) β€” elements of the array a. Output The first and only line contains the number of ways to divide an array a modulo 998 244 353. Examples Input 3 1 1 1 2 Output 3 Input 5 2 1 1 2 1 3 Output 14 Input 5 5 1 2 3 4 5 Output 16 Note In the first sample, the three possible divisions are as follows. * [[1], [1], [2]] * [[1, 1], [2]] * [[1, 1, 2]] Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
{ "input": [ "5 5\n1 2 3 4 5\n", "3 1\n1 1 2\n", "5 2\n1 1 2 1 3\n" ], "output": [ "16", "3", "14" ] }
{ "input": [ "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n", "100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n", "10 1\n4 2 9 2 1 4 4 1 4 10\n", "100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n", "1 1\n1\n", "100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n", "2 2\n2 1\n", "2 1\n2 2\n" ], "output": [ "16", "726975503", "24", "244208148", "1", "1", "2", "2" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n, k, unit, tot; int be[(100005)], st[(100005)], en[(100005)], a[(100005)], sum[355][355], f[(100005)], pre[(100005)], now[(100005)], lazy[(100005)], S[(100005)]; const int P = 998244353; template <typename T> void read(T& t) { t = 0; bool fl = true; char p = getchar(); while (!isdigit(p)) { if (p == '-') fl = false; p = getchar(); } do { (t *= 10) += p - 48; p = getchar(); } while (isdigit(p)); if (!fl) t = -t; } inline int Inc(int a, int b) { return (a + b >= P) ? (a + b - P) : (a + b); } void reset(int u) { for (int i = st[u]; i <= en[u]; i++) S[i] += lazy[u]; lazy[u] = 0; int minn = S[st[u]]; for (int i = st[u]; i <= en[u]; i++) minn = min(minn, S[i]); for (int i = st[u]; i <= en[u]; i++) S[i] -= minn; memset(sum[u], 0, sizeof(sum[u])); for (int i = st[u]; i <= en[u]; i++) sum[u][S[i]] = Inc(sum[u][S[i]], f[i]); for (int i = unit - 1; i >= 0; i--) sum[u][i] = Inc(sum[u][i], sum[u][i + 1]); lazy[u] = minn; } void change(int L, int R, int data) { if (be[L] == be[R]) { for (int i = L; i <= R; i++) S[i] += data; reset(be[L]); } else { for (int i = be[L] + 1; i <= be[R] - 1; i++) lazy[i] += data; for (int i = L; i <= en[be[L]]; i++) S[i] += data; reset(be[L]); for (int i = st[be[R]]; i <= R; i++) S[i] += data; reset(be[R]); } } int query(int R, int lim) { if (R == 0) return 0; int ret = 0; for (int i = 1; i < be[R]; i++) { ret = Inc(ret, sum[i][max(lim - lazy[i], 0)]); } for (int i = st[be[R]]; i <= R; i++) { if (S[i] + lazy[be[i]] >= lim) ret = Inc(ret, f[i]); } return ret; } int main() { read(n), read(k); unit = sqrt(n); for (int i = 1; i <= n; i++) { read(a[i]); pre[i] = now[a[i]]; now[a[i]] = i; } for (int i = 1; i <= n; i++) { be[i] = (i - 1) / unit + 1; if (be[i] != be[i - 1]) { en[be[i - 1]] = i - 1; st[be[i]] = i; } } tot = be[n]; en[tot] = n; f[0] = 1; S[0] = 0; for (int i = 1; i <= n; i++) { S[i] = S[i - 1] + 1 + lazy[be[i - 1]]; if (pre[pre[i]]) change(pre[pre[i]], i, 1); if (pre[i]) change(pre[i], i, -2); f[i] = query(i - 1, S[i] + lazy[be[i]] - k); if (S[i] + lazy[be[i]] <= k) f[i]++; if (i == en[be[i]]) reset(be[i]); } printf("%d", f[n]); return 0; }
1129_D. Isolation
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once. Since the answer can be large, find it modulo 998 244 353. Input The first line contains two space-separated integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the number of elements in the array a and the restriction from the statement. The following line contains n space-separated integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n) β€” elements of the array a. Output The first and only line contains the number of ways to divide an array a modulo 998 244 353. Examples Input 3 1 1 1 2 Output 3 Input 5 2 1 1 2 1 3 Output 14 Input 5 5 1 2 3 4 5 Output 16 Note In the first sample, the three possible divisions are as follows. * [[1], [1], [2]] * [[1, 1], [2]] * [[1, 1, 2]] Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
{ "input": [ "5 5\n1 2 3 4 5\n", "3 1\n1 1 2\n", "5 2\n1 1 2 1 3\n" ], "output": [ "16", "3", "14" ] }
{ "input": [ "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n", "100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n", "10 1\n4 2 9 2 1 4 4 1 4 10\n", "100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n", "1 1\n1\n", "100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n", "2 2\n2 1\n", "2 1\n2 2\n" ], "output": [ "16", "726975503", "24", "244208148", "1", "1", "2", "2" ] }
IN-CORRECT
cpp
#include<cstdio> #define mod 998244353 #define N 100039 #define W 139 #define ll long long #define I inline #define re register using namespace std; int lazy[N],k,ks=100,n,a[N],b[N],g[N],pre[N];ll dp[N],f[W][N],tot; I void add(int x){ re int i,now=x/ks,fr=x/ks*ks,en=x;tot+=dp[x]; for(i=fr;i<en;i++)f[now][b[i]]=0; for(i=fr;i<en;i++) b[i]+=lazy[now];lazy[now]=0; for(i=fr;i<=en;i++) f[now][b[i]]+=dp[i]; } I void gets(int l,int r){ re int now=l/ks,i; for(i=l;i<=r;i++) tot-=(b[i]+lazy[now]==k?dp[i]:0),f[now][b[i]]-=dp[i],b[i]++,f[now][b[i]]+=dp[i]; } I void get(int l,int r){ if(l/ks==r/ks)return gets(l,r);re int i;gets(l,l/ks*ks+ks-1);gets(r/ks*ks,r); for(i=l/ks+1;i<r/ks;i++) (k>=lazy[i])&&(tot-=f[i][k-lazy[i]]),lazy[i]++; } I void dels(int l,int r){ re int now=l/ks,i; for(i=l;i<=r;i++) b[i]--,tot+=(b[i]+lazy[now]==k?dp[i]:0),f[now][b[i]]-=dp[i],f[now][b[i]]+=dp[i]; } I void del(int l,int r){ if(l/ks==r/ks)return dels(l,r);re int i;dels(l,l/ks*ks+ks-1);dels(r/ks*ks,r); for(i=l/ks+1;i<r/ks;i++) lazy[i]--,(k>=lazy[i])&&(tot+=f[i][k-lazy[i]]); } int main(){ // freopen("1.in","r",stdin); re int i;scanf("%d%d",&n,&k); for(i=1;i<=n;i++) scanf("%d",&a[i]); for(i=1;i<=n;i++) pre[i]=g[a[i]],g[a[i]]=i;dp[0]=1;add(0); for(i=1;i<=n;i++)pre[i]&&(del(0,pre[i]-1),0),get(pre[i],i-1),tot%=mod,dp[i]=tot,add(i); printf("%lld\n",dp[n]); }
1129_D. Isolation
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once. Since the answer can be large, find it modulo 998 244 353. Input The first line contains two space-separated integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the number of elements in the array a and the restriction from the statement. The following line contains n space-separated integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n) β€” elements of the array a. Output The first and only line contains the number of ways to divide an array a modulo 998 244 353. Examples Input 3 1 1 1 2 Output 3 Input 5 2 1 1 2 1 3 Output 14 Input 5 5 1 2 3 4 5 Output 16 Note In the first sample, the three possible divisions are as follows. * [[1], [1], [2]] * [[1, 1], [2]] * [[1, 1, 2]] Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
{ "input": [ "5 5\n1 2 3 4 5\n", "3 1\n1 1 2\n", "5 2\n1 1 2 1 3\n" ], "output": [ "16", "3", "14" ] }
{ "input": [ "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n", "100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n", "10 1\n4 2 9 2 1 4 4 1 4 10\n", "100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n", "1 1\n1\n", "100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n", "2 2\n2 1\n", "2 1\n2 2\n" ], "output": [ "16", "726975503", "24", "244208148", "1", "1", "2", "2" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long MOD = 998244353; long long mpow(long long a, long long b) { if (b == 0) return 1; long long t1 = mpow(a, b / 2); t1 *= t1; t1 %= MOD; if (b % 2) t1 *= a; t1 %= MOD; return t1; } const long long N = 1e5 + 5; const long long block = 315; vector<long long> mcount(N, 0); vector<long long> offset(block, 0); vector<long long> mprev(N, 0), dp(N, 0); vector<long long> recent(N, 0); long long n, k; long long ans; vector<vector<long long> > sum(block, vector<long long>(N, 0)); void update(long long st, long long la, long long val) { if (st <= 0 || la <= 0) return; for (long long i = 0; i < block; i++) { long long stb = i * block + 1, lab = (i + 1) * block; if (stb > la || lab < st) continue; else if (stb >= st && la >= lab) { if (val == 1 && k - offset[i] >= 0) ans -= sum[i][k - offset[i]]; else if (val == -1 && k + 1 - offset[i] >= 0) ans += sum[i][k + 1 - offset[i]]; offset[i] += val; } else { for (long long j = stb; j < lab + 1; j++) { if (j < st || j > la) continue; if (val == 1 && offset[i] + mcount[j] == k) { ans -= dp[j - 1]; } else if (val == -1 && offset[i] + mcount[j] == k + 1) ans += dp[j - 1]; sum[i][mcount[j]] -= dp[j - 1]; mcount[j] += val; sum[i][mcount[j]] += dp[j - 1]; } } } } void solve() { cin >> n >> k; dp[0] = 1; for (long long i = 1; i < n + 1; i++) { long long a; cin >> a; ans += dp[i - 1]; sum[(i - 1) / block][0] += dp[i - 1]; mprev[i] = recent[a]; update(mprev[i] + 1, i, 1); update(mprev[mprev[i]] + 1, mprev[i], -1); recent[a] = i; dp[i] = ans; } cout << ans << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long t = 1; for (long long i = 0; i < t; i++) { solve(); } return 0; }
1129_D. Isolation
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once. Since the answer can be large, find it modulo 998 244 353. Input The first line contains two space-separated integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the number of elements in the array a and the restriction from the statement. The following line contains n space-separated integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n) β€” elements of the array a. Output The first and only line contains the number of ways to divide an array a modulo 998 244 353. Examples Input 3 1 1 1 2 Output 3 Input 5 2 1 1 2 1 3 Output 14 Input 5 5 1 2 3 4 5 Output 16 Note In the first sample, the three possible divisions are as follows. * [[1], [1], [2]] * [[1, 1], [2]] * [[1, 1, 2]] Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
{ "input": [ "5 5\n1 2 3 4 5\n", "3 1\n1 1 2\n", "5 2\n1 1 2 1 3\n" ], "output": [ "16", "3", "14" ] }
{ "input": [ "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n", "100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n", "10 1\n4 2 9 2 1 4 4 1 4 10\n", "100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n", "1 1\n1\n", "100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n", "2 2\n2 1\n", "2 1\n2 2\n" ], "output": [ "16", "726975503", "24", "244208148", "1", "1", "2", "2" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 100100; const int B = 300; const int Mod = 998244353; int n, k; int x[N]; vector<int> v[N]; void add_self(int& a, int b) { a += b; if (a >= Mod) a -= Mod; } void min_self(int& a, int b) { a = min(a, b); } int Offset[B]; int Cnt[N]; int Pref[B][B]; int Dp[N]; void ReBuild(int Block) { int a = Block * B; int b = (Block + 1) * B - 1; if (b > n) b = n - 1; for (int i = a; i <= b; i++) Cnt[i] += Offset[Block]; Offset[Block] = Cnt[a]; for (int i = a; i <= b; i++) min_self(Offset[Block], Cnt[i]); for (int i = a; i <= b; i++) Cnt[i] -= Offset[Block]; for (int i = a; i <= b; i++) add_self(Pref[Block][Cnt[i]], Dp[i]); for (int i = 1; i < B; i++) add_self(Pref[Block][i], Pref[Block][i - 1]); } void Add(int a, int b, int c) { if (a / B == b / B) { for (int i = a; i <= b; i++) Cnt[i] += c; return ReBuild(a / B); } for (int i = a / B + 1; i < b / B; i++) Offset[i] += c; for (int i = a; i < n && i / B == a / B; i++) Cnt[i] += c; for (int i = b; i >= 0 && i / B == b / B; i--) Cnt[i] += c; ReBuild(a / B); ReBuild(b / B); } int Query(int a) { int Res = 0; for (int i = 0; i < a / B; i++) if (k - Offset[i] >= 0) add_self(Res, Pref[i][k - Offset[i]]); for (int i = (a / B) * B; i <= a; i++) if (Cnt[i] + Offset[i / B] <= k) add_self(Res, Dp[i]); return Res; } int main() { cin >> n >> k; for (int i = 0; i < n; i++) scanf("%d", x + i); for (int i = 1; i <= n; i++) v[i].push_back(-1); Dp[0] = 1; for (int R = 0; R < n; R++) { vector<int>& vec = v[x[R]]; if (vec.size() >= 2) Add(vec.end()[-2], vec.back(), -1); Add(vec.back() + 1, R, +1); vec.push_back(R); add_self(Dp[R + 1], Query(R)); } cout << Dp[n]; }
1129_D. Isolation
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once. Since the answer can be large, find it modulo 998 244 353. Input The first line contains two space-separated integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the number of elements in the array a and the restriction from the statement. The following line contains n space-separated integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n) β€” elements of the array a. Output The first and only line contains the number of ways to divide an array a modulo 998 244 353. Examples Input 3 1 1 1 2 Output 3 Input 5 2 1 1 2 1 3 Output 14 Input 5 5 1 2 3 4 5 Output 16 Note In the first sample, the three possible divisions are as follows. * [[1], [1], [2]] * [[1, 1], [2]] * [[1, 1, 2]] Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
{ "input": [ "5 5\n1 2 3 4 5\n", "3 1\n1 1 2\n", "5 2\n1 1 2 1 3\n" ], "output": [ "16", "3", "14" ] }
{ "input": [ "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n", "100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n", "10 1\n4 2 9 2 1 4 4 1 4 10\n", "100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n", "1 1\n1\n", "100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n", "2 2\n2 1\n", "2 1\n2 2\n" ], "output": [ "16", "726975503", "24", "244208148", "1", "1", "2", "2" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> #pragma GCC optimize(3) using namespace std; int n, k, a[100005]; int last[100005], prelast[100005], dp[100005]; const int blocks = 300; const int blocknum = 400; void add(int &a, int b) { a += b; if (a >= 998244353) a -= 998244353; } void dec(int &a, int b) { a -= b; if (a < 0) a += 998244353; } int sum[blocks]; int shift[blocks]; int res[blocks][blocknum]; int cnt[blocks][2 * 100005]; int origin; void point_modify(int pos, int v) { int b = (pos - 1) / blocks + 1; int obj = res[b][pos % blocks] + shift[b]; res[b][pos % blocks] += v; dec(cnt[b][origin + obj - shift[b]], dp[pos]); add(cnt[b][origin + obj - shift[b] + v], dp[pos]); if (obj == k && v > 0) dec(sum[b], dp[pos]); else if (obj == k + 1 && v < 0) add(sum[b], dp[pos]); } void block_modify(int id, int v) { if (v > 0) { if (k + 1 >= shift[id]) { dec(sum[id], cnt[id][origin + k - shift[id]]); } } else { if (k >= shift[id]) add(sum[id], cnt[id][origin + k + 1 - shift[id]]); } shift[id] += v; } int point_query(int pos) { int b = (pos - 1) / blocks + 1; if (res[b][pos % blocks] + shift[b] <= k) return dp[pos]; else return 0; } int block_query(int id) { return sum[id]; } void modify(int l, int r, int x) { while (l <= r && r % blocks != 0) point_modify(r, x), r--; while (l <= r && l % blocks != 1) point_modify(l, x), l++; while (l <= r) { int b = (l - 1) / blocks + 1; block_modify(b, x); l += blocks; } } int query(int l, int r) { int ans = 0; while (l <= r && r % blocks != 0) add(ans, point_query(r)), r--; while (l <= r && l % blocks != 1) add(ans, point_query(l)), l++; while (l <= r) { int b = (l - 1) / blocks + 1; add(ans, block_query(b)); l += blocks; } return ans; } int main() { scanf("%d%d", &n, &k); origin = n; for (int i = 1; i <= n; i++) scanf("%d", &a[i]); dp[1] = 1; cnt[1][1] = 1; sum[1] = 1; for (int i = 1; i <= n; i++) { modify(last[a[i]] + 1, i, 1); if (prelast[a[i]] < last[a[i]]) modify(prelast[a[i]] + 1, last[a[i]], -1); prelast[a[i]] = last[a[i]]; last[a[i]] = i; dp[i + 1] = query(1, i); add(sum[i / blocks + 1], dp[i + 1]); add(cnt[i / blocks + 1][origin - shift[i / blocks + 1]], dp[i + 1]); } printf("%d\n", dp[n + 1]); return 0; }
1129_D. Isolation
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once. Since the answer can be large, find it modulo 998 244 353. Input The first line contains two space-separated integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the number of elements in the array a and the restriction from the statement. The following line contains n space-separated integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n) β€” elements of the array a. Output The first and only line contains the number of ways to divide an array a modulo 998 244 353. Examples Input 3 1 1 1 2 Output 3 Input 5 2 1 1 2 1 3 Output 14 Input 5 5 1 2 3 4 5 Output 16 Note In the first sample, the three possible divisions are as follows. * [[1], [1], [2]] * [[1, 1], [2]] * [[1, 1, 2]] Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
{ "input": [ "5 5\n1 2 3 4 5\n", "3 1\n1 1 2\n", "5 2\n1 1 2 1 3\n" ], "output": [ "16", "3", "14" ] }
{ "input": [ "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n", "100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n", "10 1\n4 2 9 2 1 4 4 1 4 10\n", "100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n", "1 1\n1\n", "100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n", "2 2\n2 1\n", "2 1\n2 2\n" ], "output": [ "16", "726975503", "24", "244208148", "1", "1", "2", "2" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int mod = 998244353; const int MAXN = 1e5; const int B = 315; int cnt[1 + MAXN], dp[1 + MAXN]; vector<int> occ[1 + MAXN]; void add_self(int &x, int y) { x += y; if (x >= mod) x -= mod; } void min_self(int &x, int y) { x = min(x, y); } struct SQRT { int id, offset, pref_sum[B]; void rebuild() { int st = id * B, dr = (id + 1) * B - 1, minn = 0x3f3f3f3f; for (int i = st; i <= dr; ++i) min_self(minn, offset + cnt[i]); for (int i = st; i <= dr; ++i) cnt[i] -= minn - offset; offset = minn; for (int i = 0; i < B; ++i) pref_sum[i] = 0; for (int i = st; i <= dr; ++i) add_self(pref_sum[cnt[i]], dp[i]); for (int i = 1; i < B; ++i) add_self(pref_sum[i], pref_sum[i - 1]); } } a[MAXN / B + 1]; int get_bucket(int index) { return index / B; } void update(int l, int r, short t) { int bl = get_bucket(l), br = get_bucket(r); for (int i = l; i <= r && get_bucket(i) == bl; ++i) cnt[i] += t; a[bl].rebuild(); if (bl == br) return; for (int i = bl + 1; i < br; ++i) a[i].offset += t; for (int i = r; get_bucket(i) == br; --i) cnt[i] += t; a[br].rebuild(); } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int n, k; cin >> n >> k; for (int i = 0; i <= get_bucket(n); ++i) a[i].id = i; for (int i = 1; i <= n; ++i) occ[i].emplace_back(-1); dp[0] = 1; a[0].rebuild(); for (int r = 0; r < n; ++r) { int x; cin >> x; vector<int> &vec = occ[x]; if (static_cast<int>(vec.size()) >= 2) update(vec.end()[-2] + 1, vec.back(), -1); update(vec.back() + 1, r, 1); vec.emplace_back(r); int val = 0; for (int i = 0; i <= get_bucket(r); ++i) { int at_most = k - a[i].offset; if (at_most >= 0) add_self(val, a[i].pref_sum[min(at_most, B - 1)]); } dp[r + 1] = val; a[get_bucket(r + 1)].rebuild(); } cout << dp[n] << '\n'; return 0; }
1129_D. Isolation
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once. Since the answer can be large, find it modulo 998 244 353. Input The first line contains two space-separated integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the number of elements in the array a and the restriction from the statement. The following line contains n space-separated integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n) β€” elements of the array a. Output The first and only line contains the number of ways to divide an array a modulo 998 244 353. Examples Input 3 1 1 1 2 Output 3 Input 5 2 1 1 2 1 3 Output 14 Input 5 5 1 2 3 4 5 Output 16 Note In the first sample, the three possible divisions are as follows. * [[1], [1], [2]] * [[1, 1], [2]] * [[1, 1, 2]] Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
{ "input": [ "5 5\n1 2 3 4 5\n", "3 1\n1 1 2\n", "5 2\n1 1 2 1 3\n" ], "output": [ "16", "3", "14" ] }
{ "input": [ "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n", "100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n", "10 1\n4 2 9 2 1 4 4 1 4 10\n", "100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n", "1 1\n1\n", "100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n", "2 2\n2 1\n", "2 1\n2 2\n" ], "output": [ "16", "726975503", "24", "244208148", "1", "1", "2", "2" ] }
IN-CORRECT
cpp
#pragma comment(linker, "/stack:200000000") #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,avx,avx2") #define _CRT_SECURE_NO_WARNINGS # include <iostream> # include <cmath> # include <algorithm> # include <stdio.h> # include <cstdint> # include <cstring> # include <string> # include <cstdlib> # include <vector> # include <bitset> # include <map> # include <queue> # include <ctime> # include <stack> # include <set> # include <list> # include <random> # include <deque> # include <functional> # include <iomanip> # include <sstream> # include <fstream> # include <complex> # include <numeric> # include <immintrin.h> # include <cassert> # include <array> # include <tuple> # include <unordered_map> # include <unordered_set> //#ifdef LOCAL //# include <opencv2/core/core.hpp> //# include <opencv2/highgui/highgui.hpp> //# include <opencv2/imgproc/imgproc.hpp> //#endif using namespace std; #define VA_NUM_ARGS(...) VA_NUM_ARGS_IMPL_((0,__VA_ARGS__, 6,5,4,3,2,1)) #define VA_NUM_ARGS_IMPL_(tuple) VA_NUM_ARGS_IMPL tuple #define VA_NUM_ARGS_IMPL(_0,_1,_2,_3,_4,_5,_6,N,...) N #define macro_dispatcher(macro, ...) macro_dispatcher_(macro, VA_NUM_ARGS(__VA_ARGS__)) #define macro_dispatcher_(macro, nargs) macro_dispatcher__(macro, nargs) #define macro_dispatcher__(macro, nargs) macro_dispatcher___(macro, nargs) #define macro_dispatcher___(macro, nargs) macro ## nargs #define DBN1(a) cerr<<#a<<"="<<(a)<<"\n" #define DBN2(a,b) cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<"\n" #define DBN3(a,b,c) cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<"\n" #define DBN4(a,b,c,d) cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<"\n" #define DBN5(a,b,c,d,e) cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<", "<<#e<<"="<<(e)<<"\n" #define DBN6(a,b,c,d,e,f) cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<", "<<#e<<"="<<(e)<<", "<<#f<<"="<<(f)<<"\n" #define DBN(...) macro_dispatcher(DBN, __VA_ARGS__)(__VA_ARGS__) #define DA(a,n) cerr<<#a<<"=["; printarray(a,n); cerr<<"]\n" #define DAR(a,n,s) cerr<<#a<<"["<<s<<"-"<<n-1<<"]=["; printarray(a,n,s); cerr<<"]\n" #ifdef _MSC_VER #define PREFETCH(ptr, rw, level) ((void)0) #else #define PREFETCH(ptr, rw, level) __builtin_prefetch(ptr, rw, level) #endif #if defined _MSC_VER #define ASSUME(condition) ((void)0) #define __restrict #elif defined __clang__ #define ASSUME(condition) __builtin_assume(condition) #elif defined __GNUC__ [[noreturn]] __attribute__((always_inline)) inline void undefinedBehaviour() {} #define ASSUME(condition) ((condition) ? true : (undefinedBehaviour(), false)) #endif #ifdef LOCAL #define CURTIME() cerr << clock() * 1.0 / CLOCKS_PER_SEC << endl #else #define CURTIME() #endif typedef long long ll; typedef long double ld; typedef unsigned int uint; typedef unsigned long long ull; typedef pair<int, int> pii; typedef pair<long long, long long> pll; typedef vector<int> vi; typedef vector<long long> vll; typedef int itn; template<class T1, class T2, class T3> struct triple{ T1 a; T2 b; T3 c; triple() : a(T1()), b(T2()), c(T3()) {}; triple(T1 _a, T2 _b, T3 _c) :a(_a), b(_b), c(_c){} }; template<class T1, class T2, class T3> bool operator<(const triple<T1,T2,T3>&t1,const triple<T1,T2,T3>&t2){if(t1.a!=t2.a)return t1.a<t2.a;else if(t1.b!=t2.b)return t1.b<t2.b;else return t1.c<t2.c;} template<class T1, class T2, class T3> bool operator>(const triple<T1,T2,T3>&t1,const triple<T1,T2,T3>&t2){if(t1.a!=t2.a)return t1.a>t2.a;else if(t1.b!=t2.b)return t1.b>t2.b;else return t1.c>t2.c;} #define tri triple<int,int,int> #define trll triple<ll,ll,ll> #define FI(n) for(int i=0;i<(n);++i) #define FJ(n) for(int j=0;j<(n);++j) #define FK(n) for(int k=0;k<(n);++k) #define FL(n) for(int l=0;l<(n);++l) #define FQ(n) for(int q=0;q<(n);++q) #define FOR(i,a,b) for(int i = (a), __e = (int) (b); i < __e; ++i) #define all(a) std::begin(a), std::end(a) #define reunique(v) v.resize(std::unique(v.begin(), v.end()) - v.begin()) #define sqr(x) ((x) * (x)) #define sqrt(x) sqrt(1.0 * (x)) #define pow(x, n) pow(1.0 * (x), n) #define COMPARE(obj) [&](const std::decay_t<decltype(obj)>& a, const std::decay_t<decltype(obj)>& b) #define COMPARE_BY(obj, field) [&](const std::decay_t<decltype(obj)>& a, const std::decay_t<decltype(obj)>& b) { return a.field < b.field; } #define checkbit(n, b) (((n) >> (b)) & 1) #define setbit(n, b) ((n) | (static_cast<std::decay<decltype(n)>::type>(1) << (b))) #define removebit(n, b) ((n) & ~(static_cast<std::decay<decltype(n)>::type>(1) << (b))) #define flipbit(n, b) ((n) ^ (static_cast<std::decay<decltype(n)>::type>(1) << (b))) inline int countBits(uint v){v=v-((v>>1)&0x55555555);v=(v&0x33333333)+((v>>2)&0x33333333);return((v+(v>>4)&0xF0F0F0F)*0x1010101)>>24;} inline int countBits(ull v){uint t=v>>32;uint p=(v & ((1ULL << 32) - 1)); return countBits(t) + countBits(p); } inline int countBits(ll v){return countBits((ull)v); } inline int countBits(int v){return countBits((uint)v); } unsigned int reverseBits(uint x){ x = (((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1)); x = (((x & 0xcccccccc) >> 2) | ((x & 0x33333333) << 2)); x = (((x & 0xf0f0f0f0) >> 4) | ((x & 0x0f0f0f0f) << 4)); x = (((x & 0xff00ff00) >> 8) | ((x & 0x00ff00ff) << 8)); return((x >> 16) | (x << 16)); } template<class T> inline int sign(T x){ return x > 0 ? 1 : x < 0 ? -1 : 0; } inline bool isPowerOfTwo(int x){ return (x != 0 && (x&(x - 1)) == 0); } constexpr ll power(ll x, int p) { return p == 0 ? 1 : (x * power(x, p - 1)); } template<class T> inline bool inRange(const T& val, const T& min, const T& max) { return min <= val && val <= max; } //template<class T1, class T2, class T3> T1 inline clamp(T1 x, const T2& a, const T3& b) { if (x < a) return a; else if (x > b) return b; else return x; } unsigned long long rdtsc() { unsigned long long ret = 0; #ifdef __clang__ return __builtin_readcyclecounter(); #endif #ifndef _MSC_VER asm volatile("rdtsc" : "=A" (ret) : :); #endif return ret; } // Fast IO ******************************************************************************************************** const int __BS = 4096; static char __bur[__BS + 16], *__er = __bur + __BS, *__ir = __er; template<class T = int> T readInt() { auto c = [&]() { if (__ir == __er) std::fill(__bur, __bur + __BS, 0), cin.read(__bur, __BS), __ir = __bur; }; c(); while (*__ir && (*__ir < '0' || *__ir > '9') && *__ir != '-') ++__ir; c(); bool m = false; if (*__ir == '-') ++__ir, c(), m = true; T r = 0; while (*__ir >= '0' && *__ir <= '9') r = r * 10 + *__ir - '0', ++__ir, c(); ++__ir; return m ? -r : r; } string readString() { auto c = [&]() { if (__ir == __er) std::fill(__bur, __bur + __BS, 0), cin.read(__bur, __BS), __ir = __bur; }; string r; c(); while (*__ir && isspace(*__ir)) ++__ir, c(); while (!isspace(*__ir)) r.push_back(*__ir), ++__ir, c(); ++__ir; return r; } char readChar() { auto c = [&]() { if (__ir == __er) std::fill(__bur, __bur + __BS, 0), cin.read(__bur, __BS), __ir = __bur; }; c(); while (*__ir && isspace(*__ir)) ++__ir, c(); return *__ir++; } static char __buw[__BS + 20], *__iw = __buw, *__ew = __buw + __BS; template<class T> void writeInt(T x, char endc = '\n') { if (x < 0) *__iw++ = '-', x = -x; if (x == 0) *__iw++ = '0'; char* s = __iw; while (x) { T t = x / 10; char c = x - 10 * t + '0'; *__iw++ = c; x = t; } char* f = __iw - 1; while (s < f) swap(*s, *f), ++s, --f; if (__iw > __ew) cout.write(__buw, __iw - __buw), __iw = __buw; if (endc) { *__iw++ = endc; } } template<class T> void writeStr(const T& str) { int i = 0; while (str[i]) { *__iw++ = str[i++]; if (__iw > __ew) cout.write(__buw, __iw - __buw), __iw = __buw; } } struct __FL__ { ~__FL__() { if (__iw != __buw) cout.write(__buw, __iw - __buw); } }; static __FL__ __flushVar__; //STL output ***************************************************************************************************** #define TT1 template<class T> #define TT1T2 template<class T1, class T2> #define TT1T2T3 template<class T1, class T2, class T3> TT1T2 ostream& operator << (ostream& os, const pair<T1, T2>& p); TT1 ostream& operator << (ostream& os, const vector<T>& v); TT1T2 ostream& operator << (ostream& os, const set<T1, T2>&v); TT1T2 ostream& operator << (ostream& os, const multiset<T1, T2>&v); TT1T2 ostream& operator << (ostream& os, priority_queue<T1, T2> v); TT1T2T3 ostream& operator << (ostream& os, const map<T1, T2, T3>& v); TT1T2T3 ostream& operator << (ostream& os, const multimap<T1, T2, T3>& v); TT1T2T3 ostream& operator << (ostream& os, const triple<T1, T2, T3>& t); template<class T, size_t N> ostream& operator << (ostream& os, const array<T, N>& v); TT1T2 ostream& operator << (ostream& os, const pair<T1, T2>& p){ return os <<"("<<p.first<<", "<< p.second<<")"; } TT1 ostream& operator << (ostream& os, const vector<T>& v){ bool f=1;os<<"[";for(auto& i : v) { if (!f)os << ", ";os<<i;f=0;}return os << "]"; } template<class T, size_t N> ostream& operator << (ostream& os, const array<T, N>& v) { bool f=1;os<<"[";for(auto& i : v) { if (!f)os << ", ";os<<i;f=0;}return os << "]"; } TT1T2 ostream& operator << (ostream& os, const set<T1, T2>&v){ bool f=1;os<<"[";for(auto& i : v) { if (!f)os << ", ";os<<i;f=0;}return os << "]"; } TT1T2 ostream& operator << (ostream& os, const multiset<T1,T2>&v){bool f=1;os<<"[";for(auto& i : v) { if (!f)os << ", ";os<<i;f=0;}return os << "]"; } TT1T2T3 ostream& operator << (ostream& os, const map<T1,T2,T3>& v){ bool f = 1; os << "["; for (auto& ii : v) { if (!f)os << ", "; os << "(" << ii.first << " -> " << ii.second << ") "; f = 0; }return os << "]"; } TT1T2 ostream& operator << (ostream& os, const multimap<T1, T2>& v){ bool f = 1; os << "["; for (auto& ii : v) { if (!f)os << ", "; os << "(" << ii.first << " -> " << ii.second << ") "; f = 0; }return os << "]"; } TT1T2 ostream& operator << (ostream& os, priority_queue<T1, T2> v) { bool f = 1; os << "["; while (!v.empty()) { auto x = v.top(); v.pop(); if (!f) os << ", "; f = 0; os << x; } return os << "]"; } TT1T2T3 ostream& operator << (ostream& os, const triple<T1, T2, T3>& t){ return os << "(" << t.a << ", " << t.b << ", " << t.c << ")"; } TT1T2 void printarray(const T1& a, T2 sz, T2 beg = 0){ for (T2 i = beg; i<sz; i++) cout << a[i] << " "; cout << endl; } //STL input ***************************************************************************************************** TT1T2T3 inline istream& operator >> (istream& os, triple<T1, T2, T3>& t); TT1T2 inline istream& operator >> (istream& os, pair<T1, T2>& p) { return os >> p.first >> p.second; } TT1 inline istream& operator >> (istream& os, vector<T>& v) { if (v.size()) for (T& t : v) os >> t; else { string s; T obj; while (s.empty()) {getline(os, s); if (!os) return os;} stringstream ss(s); while (ss >> obj) v.push_back(obj); } return os; } TT1T2T3 inline istream& operator >> (istream& os, triple<T1, T2, T3>& t) { return os >> t.a >> t.b >> t.c; } //Pair magic ***************************************************************************************************** #define PT1T2 pair<T1, T2> TT1T2 inline PT1T2 operator+(const PT1T2 &p1 , const PT1T2 &p2) { return PT1T2(p1.first + p2.first, p1.second + p2.second); } TT1T2 inline PT1T2& operator+=(PT1T2 &p1 , const PT1T2 &p2) { p1.first += p2.first, p1.second += p2.second; return p1; } TT1T2 inline PT1T2 operator-(const PT1T2 &p1 , const PT1T2 &p2) { return PT1T2(p1.first - p2.first, p1.second - p2.second); } TT1T2 inline PT1T2& operator-=(PT1T2 &p1 , const PT1T2 &p2) { p1.first -= p2.first, p1.second -= p2.second; return p1; } #undef TT1 #undef TT1T2 #undef TT1T2T3 #define FREIN(FILE) freopen(FILE, "rt", stdin) #define FREOUT(FILE) freopen(FILE, "wt", stdout) #ifdef LOCAL #define BEGIN_PROFILE(idx, name) int profileIdx = idx; profileName[profileIdx] = name; totalTime[profileIdx] -= rdtsc() / 1e3; #define END_PROFILE totalTime[profileIdx] += rdtsc() / 1e3; totalCount[profileIdx]++; #else #define BEGIN_PROFILE(idx, name) #define END_PROFILE #endif const int USUAL_MOD = 1000000007; template<class T> inline void normmod(T &x, T m = USUAL_MOD) { x %= m; if (x < 0) x += m; } template<class T1, class T2> inline T2 summodfast(T1 x, T1 y, T2 m = USUAL_MOD) { T2 res = x + y; if (res >= m) res -= m; return res; } template<class T1, class T2, class T3 = int> inline void addmodfast(T1 &x, T2 y, T3 m = USUAL_MOD) { x += y; if (x >= m) x -= m; } template<class T1, class T2, class T3 = int> inline void submodfast(T1 &x, T2 y, T3 m = USUAL_MOD) { x -= y; if (x < 0) x += m; } inline ll mulmod(ll x, ll n, ll m){ x %= m; n %= m; ll r = x * n - ll(ld(x)*ld(n) / ld(m)) * m; while (r < 0) r += m; while (r >= m) r -= m; return r; } inline ll powmod(ll x, ll n, ll m){ ll r = 1; normmod(x, m); while (n){ if (n & 1) r *= x; x *= x; r %= m; x %= m; n /= 2; }return r; } inline ll powmulmod(ll x, ll n, ll m) { ll res = 1; normmod(x, m); while (n){ if (n & 1)res = mulmod(res, x, m); x = mulmod(x, x, m); n /= 2; } return res; } template<class T> inline T gcd(T a, T b) { while (b) { T t = a % b; a = b; b = t; } return a; } template<class T> T fast_gcd(T u, T v) { int shl = 0; while ( u && v && u != v) { T eu = u & 1; u >>= eu ^ 1; T ev = v & 1; v >>= ev ^ 1; shl += (~(eu | ev) & 1); T d = u & v & 1 ? (u + v) >> 1 : 0; T dif = (u - v) >> (sizeof(T) * 8 - 1); u -= d & ~dif; v -= d & dif; } return std::max(u, v) << shl; } inline ll lcm(ll a, ll b){ return a / gcd(a, b) * b; } template<class T> inline T gcd(T a, T b, T c){ return gcd(gcd(a, b), c); } ll gcdex(ll a, ll b, ll& x, ll& y) { if (!a) { x = 0; y = 1; return b; } ll y1; ll d = gcdex(b % a, a, y, y1); x = y1 - (b / a) * y; return d; } bool isPrime(long long n) { if (n <= (1 << 14)) { int x = (int) n; if (x <= 4 || x % 2 == 0 || x % 3 == 0) return x == 2 || x == 3; for (int i = 5; i * i <= x; i += 6) if (x % i == 0 || x % (i + 2) == 0) return 0; return 1; } long long s = n - 1; int t = 0; while (s % 2 == 0) { s /= 2; ++t; } for (int a : {2, 325, 9375, 28178, 450775, 9780504, 1795265022}) { if (!(a %= n)) return true; long long f = powmulmod(a, s, n); if (f == 1 || f == n - 1) continue; for (int i = 1; i < t; ++i) if ((f = mulmod(f, f, n)) == n - 1) goto nextp; return false; nextp:; } return true; } // Useful constants //int some_primes[7] = {24443, 100271, 1000003, 1000333, 5000321, 98765431, #define T9 1000000000 #define T18 1000000000000000000LL #define INF 1011111111 #define LLINF 1000111000111000111LL #define mod 1000000007 #define fftmod 998244353 #define EPS 1e-10 #define PI 3.14159265358979323846264 #define link himomisubmitted #define rank thatsnoteasytask //************************************************************************************* int32_t solve(); int32_t main(int argc, char** argv) { ios_base::sync_with_stdio(0);cin.tie(0); #ifdef LOCAL // FREIN("input.txt"); // FREOUT("out.txt"); #endif return solve(); } int a[101001]; int b[101010]; int dp[104040]; vector<int> positions[101010]; __m256i _mm256_load_pi32x4(const int *x) { return _mm256_cvtepi32_epi64(_mm_load_si128((const __m128i*)x)); } __m256i _mm256_set_pi32x4(int x) { return _mm256_cvtepi32_epi64(_mm_set1_epi32(x)); } ll getSum(int* __restrict b, int* __restrict dp, int n) { ll res = 0; __m256i y0 = _mm256_set_pi32x4(0); __m256i y7 = _mm256_set_pi32x4(0); while (n % 16) { --n; res += b[0] > 0 ? 0 : dp[0]; ++b; ++dp; } n /= 4; for (int j = 0; j < n; ++j) { __m256i y1 = _mm256_load_pi32x4(b); __m256i y2 = _mm256_load_pi32x4(dp); y1 = _mm256_cmpgt_epi64(y1, y0); y7 = _mm256_add_epi64(y7, _mm256_and_si256(y1, y2)); b += 4; dp += 4; } ll arr[4]; _mm256_store_si256((__m256i*)arr, y7); return res + arr[0] + arr[1] + arr[2] + arr[3]; } int solve() { int n = readInt(); int k = readInt(); FI(n) { b[i] -= k; } FI(n) { a[i] = readInt(); --a[i]; positions[i].push_back(-1); positions[i].push_back(-1); } dp[0] = 1; for (int i = 0; i < n; ++i) { int x = a[i]; { int to = positions[x].back(); int from = *++positions[x].rbegin() + 1; for (int j = from; j <= to; ++j) { b[j]--; } } int from = positions[x].back() + 1; for (int j = from; j <= i; ++j) { b[j]++; } ll res = getSum(b, dp, i + 1); dp[i + 1] = res % fftmod; positions[x].push_back(i); } cout << dp[n] << endl; return 0; }
1129_D. Isolation
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once. Since the answer can be large, find it modulo 998 244 353. Input The first line contains two space-separated integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the number of elements in the array a and the restriction from the statement. The following line contains n space-separated integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n) β€” elements of the array a. Output The first and only line contains the number of ways to divide an array a modulo 998 244 353. Examples Input 3 1 1 1 2 Output 3 Input 5 2 1 1 2 1 3 Output 14 Input 5 5 1 2 3 4 5 Output 16 Note In the first sample, the three possible divisions are as follows. * [[1], [1], [2]] * [[1, 1], [2]] * [[1, 1, 2]] Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
{ "input": [ "5 5\n1 2 3 4 5\n", "3 1\n1 1 2\n", "5 2\n1 1 2 1 3\n" ], "output": [ "16", "3", "14" ] }
{ "input": [ "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n", "100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n", "10 1\n4 2 9 2 1 4 4 1 4 10\n", "100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n", "1 1\n1\n", "100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n", "2 2\n2 1\n", "2 1\n2 2\n" ], "output": [ "16", "726975503", "24", "244208148", "1", "1", "2", "2" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <class T> inline void gn(T &first) { char c, sg = 0; while (c = getchar(), (c > '9' || c < '0') && c != '-') ; for ((c == '-' ? sg = 1, c = getchar() : 0), first = 0; c >= '0' && c <= '9'; c = getchar()) first = (first << 1) + (first << 3) + c - '0'; if (sg) first = -first; } template <class T1, class T2> inline void gn(T1 &x1, T2 &x2) { gn(x1), gn(x2); } int power(int a, int b, int m, int ans = 1) { for (; b; b >>= 1, a = 1LL * a * a % m) if (b & 1) ans = 1LL * ans * a % m; return ans; } int ans[100010], sum[321], cnt[100010]; int dp[321][100010]; int val[321]; int a[100010], pre[100010]; int L[321], R[321], id[100010]; int ID[100010]; int n, k; void build(int n) { for (int i = 1; i < 100010; i++) id[i] = sqrt(i); for (int i = 1; i < 100010; i++) { if (!L[id[i]]) L[id[i]] = i; R[id[i]] = i; } L[1] = 0, id[0] = 1; for (int i = 1; i <= n; i++) { pre[i] = ID[a[i]]; ID[a[i]] = i; } for (int i = 0; i < 100010; i++) cnt[i] = -1; } inline int add_val(int &u, int v) { u += v; if (u >= 998244353) u -= 998244353; } inline int add(int st, int ed, int s) { if (st > ed) return 0; int u = id[st]; for (int i = L[u]; i <= R[u]; i++) { if (cnt[i] < 0) break; add_val(dp[u][cnt[i]], 998244353 - ans[i]); cnt[i] += sum[u]; if (cnt[i] <= k) add_val(val[u], 998244353 - ans[i]); if (i >= st and i <= ed) cnt[i] += s; add_val(dp[u][cnt[i]], ans[i]); if (cnt[i] <= k) add_val(val[u], ans[i]); } sum[u] = 0; if (id[st] == id[ed]) return 0; u = id[ed]; for (int i = L[u]; i <= R[u]; i++) { if (cnt[i] < 0) break; add_val(dp[u][cnt[i]], 998244353 - ans[i]); cnt[i] += sum[u]; if (cnt[i] <= k) add_val(val[u], 998244353 - ans[i]); if (i >= st and i <= ed) cnt[i] += s; add_val(dp[u][cnt[i]], ans[i]); if (cnt[i] <= k) add_val(val[u], ans[i]); } for (int i = id[st] + 1; i < id[ed]; i++) { if (s == 1 and k >= sum[i]) add_val(val[i], 998244353 - dp[i][k - sum[i]]); if (s == -1 and k + 1 >= sum[i]) add_val(val[i], dp[i][k + 1 - sum[i]]); add_val(sum[i], s); } return 0; } int main() { gn(n, k); for (int i = 1; i <= n; i++) gn(a[i]); build(n); cnt[0] = 0; dp[1][0] = ans[0] = val[1] = 1; for (int i = 1; i <= n; i++) { add(pre[i], i - 1, 1); add(pre[pre[i]], pre[i] - 1, -1); ans[i] = 0; cnt[i] = 0; int u = id[i]; for (int j = 1; j <= u; j++) { add_val(ans[i], val[j]); } add_val(val[u], ans[i]); add_val(dp[u][cnt[i]], ans[i]); } cout << ans[n] << endl; }
1129_D. Isolation
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once. Since the answer can be large, find it modulo 998 244 353. Input The first line contains two space-separated integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the number of elements in the array a and the restriction from the statement. The following line contains n space-separated integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n) β€” elements of the array a. Output The first and only line contains the number of ways to divide an array a modulo 998 244 353. Examples Input 3 1 1 1 2 Output 3 Input 5 2 1 1 2 1 3 Output 14 Input 5 5 1 2 3 4 5 Output 16 Note In the first sample, the three possible divisions are as follows. * [[1], [1], [2]] * [[1, 1], [2]] * [[1, 1, 2]] Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
{ "input": [ "5 5\n1 2 3 4 5\n", "3 1\n1 1 2\n", "5 2\n1 1 2 1 3\n" ], "output": [ "16", "3", "14" ] }
{ "input": [ "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n", "100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n", "10 1\n4 2 9 2 1 4 4 1 4 10\n", "100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n", "1 1\n1\n", "100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n", "2 2\n2 1\n", "2 1\n2 2\n" ], "output": [ "16", "726975503", "24", "244208148", "1", "1", "2", "2" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> constexpr int sqrt(int n) { int r = 0; while (r * r < n) { r++; } return r; } const int N = 100001; const int B = sqrt(N); const int MOD = 998244353; void update(int &x, int a) { x += a; if (x >= MOD) { x -= MOD; } } int n, limit, blocks, bdelta[B], value[N], sum[B], sufsum[B][N], dp[N], last[N][2]; template <int d> void add(int l, int r) { for (int bid = l / blocks, bbegin = bid * blocks; bid <= (r - 1) / blocks; ++bid, bbegin += blocks) { int bend = bbegin + blocks; if (l <= bbegin && bend <= r) { bdelta[bid] += d; } else { for (int i = std::max(l, bbegin); i < std::min(r, bend); ++i) { if (d == -1) { update(sufsum[bid][value[i]], MOD - dp[i]); update(sufsum[bid][--value[i]], dp[i]); } else { update(sufsum[bid][++value[i]], dp[i]); } } } } } int query() { int result = 0; for (int bid = 0; bid < blocks; ++bid) { update(result, sum[bid]); update(result, MOD - sufsum[bid][std::min(std::max(limit + 1 - bdelta[bid], 0), n)]); } return result; } int main() { scanf("%d%d", &n, &limit); blocks = sqrt(n); dp[0] = 1; for (int i = 1, a; i <= n; ++i) { scanf("%d", &a); int bid = (i - 1) / blocks; assert(bdelta[bid] == 0); update(sum[bid], dp[i - 1]); update(sufsum[bid][0], dp[i - 1]); if (last[a][1]) { add<-1>(last[a][0], last[a][1]); } add<1>(last[a][1], i); dp[i] = query(); last[a][0] = last[a][1]; last[a][1] = i; } printf("%d\n", dp[n]); }
1129_D. Isolation
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once. Since the answer can be large, find it modulo 998 244 353. Input The first line contains two space-separated integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the number of elements in the array a and the restriction from the statement. The following line contains n space-separated integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n) β€” elements of the array a. Output The first and only line contains the number of ways to divide an array a modulo 998 244 353. Examples Input 3 1 1 1 2 Output 3 Input 5 2 1 1 2 1 3 Output 14 Input 5 5 1 2 3 4 5 Output 16 Note In the first sample, the three possible divisions are as follows. * [[1], [1], [2]] * [[1, 1], [2]] * [[1, 1, 2]] Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
{ "input": [ "5 5\n1 2 3 4 5\n", "3 1\n1 1 2\n", "5 2\n1 1 2 1 3\n" ], "output": [ "16", "3", "14" ] }
{ "input": [ "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n", "100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n", "10 1\n4 2 9 2 1 4 4 1 4 10\n", "100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n", "1 1\n1\n", "100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n", "2 2\n2 1\n", "2 1\n2 2\n" ], "output": [ "16", "726975503", "24", "244208148", "1", "1", "2", "2" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ii = pair<int, int>; using ll = long long; using db = long double; const int N = 1e5 + 5, K = 20, MOD = 1e9 + 7, MOD2 = 998244353; const ll MX = 1e15; const int blkSz = 320; int n, k, sq, a[N]; int dp[N], lz[blkSz]; int sweeped[N]; deque<int> dq[blkSz]; deque<int> vals[N]; int calc[blkSz]; bool isBuilt[blkSz]; void buildBlock(int idx) { int l = idx * sq; int r = l + sq; int mx = *max_element(sweeped + l, sweeped + r); for (int j = 0; j <= mx; j++) { dq[idx].push_back(0); } for (int i = l; i < r; i++) { dq[idx][sweeped[i]] += dp[i]; if (dq[idx][sweeped[i]] >= MOD) dq[idx][sweeped[i]] -= MOD; } for (int i = 0; i < min(k + 1, (int)dq[idx].size()); i++) { calc[idx] += dq[idx][i]; if (calc[idx] >= MOD) calc[idx] -= MOD; } } void reBuildBlock(int idx) { int l = idx * sq; int r = l + sq; for (int i = l; i < r; i++) { sweeped[i] += lz[idx]; } lz[idx] = 0; } void modifyBlock(int idx, bool push) { lz[idx] += (push ? 1 : -1); if (push) { if (dq[idx].size() > k) { calc[idx] -= dq[idx][k]; if (calc[idx] < 0) calc[idx] += MOD; } dq[idx].push_front(0); } else { dq[idx].pop_front(); if (dq[idx].size() > k) { calc[idx] += dq[idx][k]; if (calc[idx] >= MOD) calc[idx] -= MOD; } } } void incPrefix(int idx) { --idx; int org = idx / sq; if (isBuilt[org]) { reBuildBlock(org); } for (; idx >= 0 && (!isBuilt[idx / sq] || idx % sq != sq - 1); --idx) { if (isBuilt[org]) { dq[idx / sq][sweeped[idx]] -= dp[idx]; if (dq[idx / sq][sweeped[idx]] < 0) dq[idx / sq][sweeped[idx]] += MOD; if (sweeped[idx] <= k) { calc[idx / sq] -= dp[idx]; if (calc[idx / sq] < 0) calc[idx / sq] += MOD; } } sweeped[idx]++; if (isBuilt[org]) { if (dq[idx / sq].size() <= sweeped[idx]) dq[idx / sq].push_back(0); dq[idx / sq][sweeped[idx]] += dp[idx]; if (dq[idx / sq][sweeped[idx]] >= MOD) dq[idx / sq][sweeped[idx]] -= MOD; if (sweeped[idx] <= k) { calc[idx / sq] += dp[idx]; if (calc[idx / sq] >= MOD) calc[idx / sq] -= MOD; } } } for (; idx >= 0; idx -= sq) { modifyBlock(idx / sq, 1); } } void decPrefix(int idx) { --idx; int org = idx / sq; if (isBuilt[org]) { reBuildBlock(org); } for (; idx >= 0 && (!isBuilt[idx / sq] || idx % sq != sq - 1); --idx) { if (isBuilt[org]) { dq[idx / sq][sweeped[idx]] -= dp[idx]; if (dq[idx / sq][sweeped[idx]] < 0) dq[idx / sq][sweeped[idx]] += MOD; if (sweeped[idx] <= k) { calc[idx / sq] -= dp[idx]; if (calc[idx / sq] < 0) calc[idx / sq] += MOD; } } sweeped[idx]--; if (isBuilt[org]) { if (dq[idx / sq].size() <= sweeped[idx]) dq[idx / sq].push_back(0); dq[idx / sq][sweeped[idx]] += dp[idx]; if (dq[idx / sq][sweeped[idx]] >= MOD) dq[idx / sq][sweeped[idx]] -= MOD; if (sweeped[idx] <= k) { calc[idx / sq] += dp[idx]; if (calc[idx / sq] >= MOD) calc[idx / sq] -= MOD; } } } for (; idx >= 0; idx -= sq) { modifyBlock(idx / sq, 0); } } void calcDP(int idx) { int o = idx; --idx; for (; idx >= 0 && !isBuilt[idx / sq]; --idx) { if (sweeped[idx] <= k) { dp[o] += dp[idx]; if (dp[o] >= MOD) dp[o] -= MOD; } } for (; idx >= 0; idx -= sq) { dp[o] += calc[idx / sq]; if (dp[o] >= MOD) dp[o] -= MOD; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> k; dp[0] = 1; for (int i = 1; i <= n; i++) cin >> a[i]; sq = sqrt(n + 1); if (sq * sq != n + 1) ++sq; for (int i = 1; i <= n; i++) { vals[a[i]].push_back(i); incPrefix(i); if (vals[a[i]].size() >= 2) { decPrefix(vals[a[i]][vals[a[i]].size() - 2]); decPrefix(vals[a[i]][vals[a[i]].size() - 2]); } if (vals[a[i]].size() == 3) { incPrefix(vals[a[i]].front()); vals[a[i]].pop_front(); } if (i % sq == 0) { buildBlock(i / sq - 1); } sweeped[i] = 0; calcDP(i); } cout << dp[n] << '\n'; }
1129_D. Isolation
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once. Since the answer can be large, find it modulo 998 244 353. Input The first line contains two space-separated integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the number of elements in the array a and the restriction from the statement. The following line contains n space-separated integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n) β€” elements of the array a. Output The first and only line contains the number of ways to divide an array a modulo 998 244 353. Examples Input 3 1 1 1 2 Output 3 Input 5 2 1 1 2 1 3 Output 14 Input 5 5 1 2 3 4 5 Output 16 Note In the first sample, the three possible divisions are as follows. * [[1], [1], [2]] * [[1, 1], [2]] * [[1, 1, 2]] Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
{ "input": [ "5 5\n1 2 3 4 5\n", "3 1\n1 1 2\n", "5 2\n1 1 2 1 3\n" ], "output": [ "16", "3", "14" ] }
{ "input": [ "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n", "100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n", "10 1\n4 2 9 2 1 4 4 1 4 10\n", "100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n", "1 1\n1\n", "100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n", "2 2\n2 1\n", "2 1\n2 2\n" ], "output": [ "16", "726975503", "24", "244208148", "1", "1", "2", "2" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; pair<int, int> operator+(const pair<int, int> x, const int y) { return make_pair(x.first + y, x.second); } const int mxn = 131072, siz = 250, md = 998244353; int n, k, pl[mxn], pr[mxn], d[mxn], f[mxn], bl[mxn], cnt[512][256], tag[512]; pair<int, int> a[mxn], b[mxn], c[mxn]; void mrg(pair<int, int> *x, int nx, pair<int, int> *y, int ny, pair<int, int> *z) { int px = 0, py = 0, pz = 0; for (; px < nx && py < ny;) if (x[px] < y[py]) z[pz++] = x[px++]; else z[pz++] = y[py++]; for (; px < nx; z[pz++] = x[px++]) ; for (; py < ny; z[pz++] = y[py++]) ; } void reb(int pos, int l, int r) { int mn = n + 1; for (int i = l; i <= r; ++i) mn = min(mn, a[i].first); for (int i = l; i <= r; ++i) a[i].first -= mn; for (int i = 0; i <= siz; ++i) cnt[pos][i] = 0; for (int i = l; i <= r; ++i) (cnt[pos][a[i].first] += f[a[i].second]) %= md; for (int i = 1; i <= siz; ++i) (cnt[pos][i] += cnt[pos][i - 1]) %= md; tag[pos] += mn; } void add(int l, int r, int x) { int pb = 0, pc = 0; for (int i = (bl[l] - 1) * siz + 1; i <= min(bl[l] * siz, n); ++i) if (a[i].second < l || a[i].second > r) b[pb++] = a[i]; else c[pc++] = a[i] + x; mrg(b, pb, c, pc, a + (bl[l] - 1) * siz + 1); reb(bl[l], (bl[l] - 1) * siz + 1, min(bl[l] * siz, n)); if (bl[l] != bl[r]) { pb = 0, pc = 0; for (int i = (bl[r] - 1) * siz + 1; i <= min(bl[r] * siz, n); ++i) if (a[i].second < l || a[i].second > r) b[pb++] = a[i]; else c[pc++] = a[i] + x; mrg(b, pb, c, pc, a + (bl[r] - 1) * siz + 1); reb(bl[r], (bl[r] - 1) * siz + 1, min(bl[r] * siz, n)); } for (int i = bl[l] + 1; i <= bl[r] - 1; ++i) tag[i] += x; } void init() { for (int i = 1; i <= n; ++i) bl[i] = (i - 1) / siz + 1; for (int i = 1; i <= n; ++i) a[i] = make_pair(0, i); for (int i = 1; i <= bl[n]; ++i) reb(i, (i - 1) * siz + 1, min(i * siz, n)); } int main() { scanf("%d%d", &n, &k), ++n, f[1] = 1; for (int i = 2; i <= n; ++i) scanf("%d", &d[i]); init(); for (int i = 2; i <= n; ++i) { if (pr[d[i]]) add(pl[d[i]] + 1, pr[d[i]], -1); pl[d[i]] = pr[d[i]], pr[d[i]] = i - 1; add(pl[d[i]] + 1, pr[d[i]], 1); for (int j = 1; j <= bl[i]; ++j) if (k - tag[j] >= 0 && k - tag[j] <= siz) (f[i] += cnt[j][k - tag[j]]) %= md; reb(bl[i], (bl[i] - 1) * siz + 1, min(bl[i] * siz, n)); } printf("%d\n", f[n]); return 0; }
1129_D. Isolation
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once. Since the answer can be large, find it modulo 998 244 353. Input The first line contains two space-separated integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the number of elements in the array a and the restriction from the statement. The following line contains n space-separated integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n) β€” elements of the array a. Output The first and only line contains the number of ways to divide an array a modulo 998 244 353. Examples Input 3 1 1 1 2 Output 3 Input 5 2 1 1 2 1 3 Output 14 Input 5 5 1 2 3 4 5 Output 16 Note In the first sample, the three possible divisions are as follows. * [[1], [1], [2]] * [[1, 1], [2]] * [[1, 1, 2]] Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
{ "input": [ "5 5\n1 2 3 4 5\n", "3 1\n1 1 2\n", "5 2\n1 1 2 1 3\n" ], "output": [ "16", "3", "14" ] }
{ "input": [ "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n", "100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n", "10 1\n4 2 9 2 1 4 4 1 4 10\n", "100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n", "1 1\n1\n", "100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n", "2 2\n2 1\n", "2 1\n2 2\n" ], "output": [ "16", "726975503", "24", "244208148", "1", "1", "2", "2" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse4,sse3,avx,avx2") using namespace std; using ll = long long; int a[101001]; int b[101010]; int dp[101010]; int p1[101010]; int p2[101010]; int ans = 0; int n, k; void add(int* __restrict a, int* __restrict dp, int n) { int sum = 0; while (n % 4) { a[0]++; sum -= a[0] == 0 ? dp[0] : 0; sum += sum < 0 ? 998244353 : 0; ++a; ++dp; --n; } n /= 4; __m128i x7 = _mm_set1_epi32(0); __m128i x0 = _mm_set1_epi32(0); __m128i x3 = _mm_set1_epi32(1); __m128i x4 = _mm_set1_epi32(998244353); while (n--) { __m128i x1 = _mm_loadu_si128((__m128i*)a); x1 = _mm_add_epi32(x1, x3); _mm_storeu_si128((__m128i*)a, x1); __m128i x2 = _mm_loadu_si128((__m128i*)dp); x7 = _mm_sub_epi32(x7, _mm_and_si128(x2, _mm_cmpeq_epi32(x1, x0))); x7 = _mm_add_epi32(x7, _mm_and_si128(x4, _mm_cmplt_epi32(x7, x0))); a += 4; dp += 4; } int res[4]; _mm_storeu_si128((__m128i*)res, x7); sum += res[0]; if (sum >= 998244353) sum -= 998244353; sum += res[1]; if (sum >= 998244353) sum -= 998244353; sum += res[2]; if (sum >= 998244353) sum -= 998244353; sum += res[3]; if (sum >= 998244353) sum -= 998244353; ans += sum; if (ans >= 998244353) ans -= 998244353; } void sub(int* __restrict a, int* __restrict dp, int n) { int sum = 0; while (n % 4) { a[0]++; sum -= a[0] == 0 ? dp[0] : 0; sum += sum < 0 ? 998244353 : 0; ++a; ++dp; --n; } n /= 4; __m128i x7 = _mm_set1_epi32(0); __m128i x0 = _mm_set1_epi32(0); __m128i x3 = _mm_set1_epi32(1); __m128i x4 = _mm_set1_epi32(998244353); while (n--) { __m128i x1 = _mm_loadu_si128((__m128i*)a); __m128i x2 = _mm_loadu_si128((__m128i*)dp); x7 = _mm_sub_epi32(x7, _mm_and_si128(x2, _mm_cmpeq_epi32(x1, x0))); x7 = _mm_add_epi32(x7, _mm_and_si128(x4, _mm_cmplt_epi32(x7, x0))); x1 = _mm_add_epi32(x1, x3); _mm_storeu_si128((__m128i*)a, x1); a += 4; dp += 4; } int res[4]; _mm_storeu_si128((__m128i*)res, x7); sum += res[0]; if (sum >= 998244353) sum -= 998244353; sum += res[1]; if (sum >= 998244353) sum -= 998244353; sum += res[2]; if (sum >= 998244353) sum -= 998244353; sum += res[3]; if (sum >= 998244353) sum -= 998244353; ans -= sum; ans += ans < 0 ? 998244353 : 0; } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> k; for (int i = 0; i < n; ++i) { b[i] = -k - 1; cin >> a[i]; --a[i]; p1[i] = p2[i] = -1; } dp[0] = 1; ans = 1; for (int i = 0; i < n; ++i) { if (i) ans = (ans + ans) % 998244353; int x = a[i]; sub(b + p2[x] + 1, dp + p2[x] + 1, p1[x] - p2[x]); add(b + p1[x] + 1, dp + p1[x] + 1, i - p1[x]); dp[i + 1] = ans; p2[x] = p1[x]; p1[x] = i; } cout << ans << endl; return 0; }
1129_D. Isolation
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once. Since the answer can be large, find it modulo 998 244 353. Input The first line contains two space-separated integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the number of elements in the array a and the restriction from the statement. The following line contains n space-separated integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n) β€” elements of the array a. Output The first and only line contains the number of ways to divide an array a modulo 998 244 353. Examples Input 3 1 1 1 2 Output 3 Input 5 2 1 1 2 1 3 Output 14 Input 5 5 1 2 3 4 5 Output 16 Note In the first sample, the three possible divisions are as follows. * [[1], [1], [2]] * [[1, 1], [2]] * [[1, 1, 2]] Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
{ "input": [ "5 5\n1 2 3 4 5\n", "3 1\n1 1 2\n", "5 2\n1 1 2 1 3\n" ], "output": [ "16", "3", "14" ] }
{ "input": [ "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n", "100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n", "10 1\n4 2 9 2 1 4 4 1 4 10\n", "100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n", "1 1\n1\n", "100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n", "2 2\n2 1\n", "2 1\n2 2\n" ], "output": [ "16", "726975503", "24", "244208148", "1", "1", "2", "2" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 2; const int magic = 350; const int mod = 998244353; const int inf = 1e9 + 7; vector<int> pos[N]; int n, ar[N], dp[N], sum[magic][magic], off[magic], pter[N], wow[N]; int ceil(int x, int y) { if (x % y == 0) { return x / y; } return x / y + 1; } void add(int x, int y, int val) { int i, j, grp1 = ceil(x, magic), grp2 = ceil(y, magic), min1 = inf, min2 = inf; for (i = grp1 + 1; i < grp2; i++) { off[i] += val; } if (grp1 != grp2) { for (i = (grp1 - 1) * magic + 1; i <= min(n, grp1 * magic); i++) { if (i >= x) { wow[i] += val; } min1 = min(min1, wow[i]); } for (i = (grp2 - 1) * magic + 1; i <= min(n, grp2 * magic); i++) { if (i <= y) { wow[i] += val; } min2 = min(min2, wow[i]); } off[grp1] += min1; off[grp2] += min2; for (i = 0; i < magic; i++) { sum[grp1][i] = 0; sum[grp2][i] = 0; } for (i = (grp1 - 1) * magic + 1; i <= min(n, grp1 * magic); i++) { wow[i] -= min1; sum[grp1][wow[i]] += dp[i - 1]; if (sum[grp1][wow[i]] >= mod) { sum[grp1][wow[i]] -= mod; } } for (i = (grp2 - 1) * magic + 1; i <= min(n, grp2 * magic); i++) { wow[i] -= min2; sum[grp2][wow[i]] += dp[i - 1]; if (sum[grp2][wow[i]] >= mod) { sum[grp2][wow[i]] -= mod; } } for (i = 1; i < magic; i++) { sum[grp1][i] += sum[grp1][i - 1]; if (sum[grp1][i] >= mod) { sum[grp1][i] -= mod; } sum[grp2][i] += sum[grp2][i - 1]; if (sum[grp2][i] >= mod) { sum[grp2][i] -= mod; } } } else { for (i = (grp1 - 1) * magic + 1; i <= min(n, grp1 * magic); i++) { if (i >= x && i <= y) { wow[i] += val; } min1 = min(min1, wow[i]); } off[grp1] += min1; for (i = 0; i < magic; i++) { sum[grp1][i] = 0; } for (i = (grp1 - 1) * magic + 1; i <= grp1 * magic; i++) { wow[i] -= min1; sum[grp1][wow[i]] += dp[i - 1]; if (sum[grp1][wow[i]] >= mod) { sum[grp1][wow[i]] -= mod; } } for (i = 1; i < magic; i++) { sum[grp1][i] += sum[grp1][i - 1]; if (sum[grp1][i] >= mod) { sum[grp1][i] -= mod; } } } } signed main() { ios::sync_with_stdio(0); cin.tie(0); int i, j, k, l, grp, num; cin >> n >> num; for (i = 1; i <= n; i++) { cin >> ar[i]; if (!pos[ar[i]].size()) { pos[ar[i]].push_back(0); } pos[ar[i]].push_back(i); } dp[0] = 1; for (i = 1; i <= n; i++) { grp = ceil(i, magic); pter[ar[i]]++; add(pos[ar[i]][pter[ar[i]] - 1] + 1, pos[ar[i]][pter[ar[i]]], 1); if (pter[ar[i]] - 2 >= 0) { add(pos[ar[i]][pter[ar[i]] - 2] + 1, pos[ar[i]][pter[ar[i]] - 1], -1); } for (j = 1; j <= grp; j++) { if (num >= off[j]) { dp[i] += sum[j][min(num - off[j], magic - 1)]; if (dp[i] >= mod) { dp[i] -= mod; } } } } cout << dp[n]; }
1129_D. Isolation
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once. Since the answer can be large, find it modulo 998 244 353. Input The first line contains two space-separated integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the number of elements in the array a and the restriction from the statement. The following line contains n space-separated integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n) β€” elements of the array a. Output The first and only line contains the number of ways to divide an array a modulo 998 244 353. Examples Input 3 1 1 1 2 Output 3 Input 5 2 1 1 2 1 3 Output 14 Input 5 5 1 2 3 4 5 Output 16 Note In the first sample, the three possible divisions are as follows. * [[1], [1], [2]] * [[1, 1], [2]] * [[1, 1, 2]] Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
{ "input": [ "5 5\n1 2 3 4 5\n", "3 1\n1 1 2\n", "5 2\n1 1 2 1 3\n" ], "output": [ "16", "3", "14" ] }
{ "input": [ "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n", "100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n", "10 1\n4 2 9 2 1 4 4 1 4 10\n", "100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n", "1 1\n1\n", "100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n", "2 2\n2 1\n", "2 1\n2 2\n" ], "output": [ "16", "726975503", "24", "244208148", "1", "1", "2", "2" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int P = 998244353; int n, k; inline int ad(int& x, int& y) { return (x + y > P ? x + y - P : x + y); } int A[200006], las[200006], pr[200006]; const int blo = 600; int bel[200006]; pair<int, int> B[200006]; int S[200006]; int val[200006], gv[200006], lz[200006], cur; void rebuild(int b) { int l = b * blo - blo + 1, r = b * blo; for (int i = (l), iend = (r); i <= iend; ++i) val[i] = val[i] + lz[b]; lz[b] = 0; for (int i = (l), iend = (r); i <= iend; ++i) B[i] = make_pair(val[i], gv[i]); sort(B + l, B + r + 1); S[l] = B[l].second; for (int i = (l + 1), iend = (r); i <= iend; ++i) S[i] = ad(S[i - 1], B[i].second); } void add(int l, int r, int c) { ++l, ++r; if (bel[l] == bel[r]) { int b = bel[l]; for (int i = (l), iend = (r); i <= iend; ++i) val[i] += c; rebuild(b); return; } for (int i = (l), iend = (bel[l] * blo); i <= iend; ++i) val[i] += c; rebuild(bel[l]); for (int i = (r), iend = (bel[r] * blo - blo + 1); i >= iend; --i) val[i] += c; rebuild(bel[r]); for (int i = (bel[l] + 1), iend = (bel[r] - 1); i <= iend; ++i) lz[i] += c; } int que() { int as = 0; for (int b = (1), bend = (bel[cur]); b <= bend; ++b) { int l = (b - 1) * blo + 1, r = b * blo; int ps = upper_bound(B + l, B + r + 1, make_pair(k - lz[b], 0x3f3f3f3f)) - B - 1; if (ps < l) continue; else as = ad(as, S[ps]); } return as; } void gt(int p, int c) { ++p; gv[p] = c; rebuild(bel[p]); } int dp[200006]; void solve() { cin >> n >> k; for (int i = (1), iend = (n); i <= iend; ++i) scanf("%d", A + i), las[i] = pr[A[i]], pr[A[i]] = i; for (int i = (1), iend = (n + 1); i <= iend; ++i) bel[i] = (i - 1) / blo + 1; dp[0] = 1; for (int i = (1), iend = (n); i <= iend; ++i) { cur = i; add(las[i] + 1, i, 1); if (las[i]) add(las[las[i]] + 1, las[i], -1); gt(i, dp[i - 1]); dp[i] = que(); } printf("%d\n", dp[n]); } signed main() { solve(); }
1129_D. Isolation
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once. Since the answer can be large, find it modulo 998 244 353. Input The first line contains two space-separated integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the number of elements in the array a and the restriction from the statement. The following line contains n space-separated integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n) β€” elements of the array a. Output The first and only line contains the number of ways to divide an array a modulo 998 244 353. Examples Input 3 1 1 1 2 Output 3 Input 5 2 1 1 2 1 3 Output 14 Input 5 5 1 2 3 4 5 Output 16 Note In the first sample, the three possible divisions are as follows. * [[1], [1], [2]] * [[1, 1], [2]] * [[1, 1, 2]] Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
{ "input": [ "5 5\n1 2 3 4 5\n", "3 1\n1 1 2\n", "5 2\n1 1 2 1 3\n" ], "output": [ "16", "3", "14" ] }
{ "input": [ "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n", "100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n", "10 1\n4 2 9 2 1 4 4 1 4 10\n", "100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n", "1 1\n1\n", "100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n", "2 2\n2 1\n", "2 1\n2 2\n" ], "output": [ "16", "726975503", "24", "244208148", "1", "1", "2", "2" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> const int N = 1e5 + 5, M = N / 183 + 3; int bel[N], f[N], g[N], tag[M], s[M][183 + 3 << 1]; inline int read() { int now = 0; register char c = getchar(); for (; !isdigit(c); c = getchar()) ; for (; isdigit(c); now = now * 10 + c - 48, c = getchar()) ; return now; } void Update(int p, int v) { int *s = ::s[bel[p]]; for (int i = 183; i <= 183 << 1; ++i) (s[i] += v) >= 998244353 && (s[i] -= 998244353); } void Modify(int p, int v) { int bel = ::bel[p], *s = ::s[bel]; tag[bel] += v; for (int i = bel * 183 + 1; i <= p; ++i) { if (v == 1) (s[g[i] + 183] += 998244353 - f[i - 1]) >= 998244353 && (s[g[i] + 183] -= 998244353); else (s[g[i] - 1 + 183] += f[i - 1]) >= 998244353 && (s[g[i] - 1 + 183] -= 998244353), (s[g[i] - 2 + 183] += f[i - 1]) >= 998244353 && (s[g[i] - 2 + 183] -= 998244353); g[i] += v; } } int Query(int p, int K) { int bel = ::bel[p], sum = tag[bel]; long long res = 0; for (int i = bel * 183 + 1; i <= p; ++i) g[i] <= K && (res += f[i - 1]); while (bel--) { assert(sum >= 0); if (sum <= K) res += s[bel][std::min(183 << 1, K - sum + 183)]; sum += tag[bel]; } return res % 998244353; } int main() { static int las[N], pre[N]; int n = read(), K = read(); for (int i = 1; i <= n; ++i) bel[i] = (i - 1) / 183; f[0] = 1; for (int i = 1; i <= n; ++i) { int a = read(); las[i] = pre[a], pre[a] = i; Update(i, f[i - 1]), Modify(i, 1); if (las[i]) { Modify(las[i], -2); if (las[las[i]]) Modify(las[las[i]], 1); } f[i] = Query(i, K); } printf("%d\n", f[n]); return 0; }
1129_D. Isolation
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once. Since the answer can be large, find it modulo 998 244 353. Input The first line contains two space-separated integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the number of elements in the array a and the restriction from the statement. The following line contains n space-separated integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n) β€” elements of the array a. Output The first and only line contains the number of ways to divide an array a modulo 998 244 353. Examples Input 3 1 1 1 2 Output 3 Input 5 2 1 1 2 1 3 Output 14 Input 5 5 1 2 3 4 5 Output 16 Note In the first sample, the three possible divisions are as follows. * [[1], [1], [2]] * [[1, 1], [2]] * [[1, 1, 2]] Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
{ "input": [ "5 5\n1 2 3 4 5\n", "3 1\n1 1 2\n", "5 2\n1 1 2 1 3\n" ], "output": [ "16", "3", "14" ] }
{ "input": [ "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n", "100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n", "10 1\n4 2 9 2 1 4 4 1 4 10\n", "100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n", "1 1\n1\n", "100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n", "2 2\n2 1\n", "2 1\n2 2\n" ], "output": [ "16", "726975503", "24", "244208148", "1", "1", "2", "2" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int mod = 998244353; int n, k, a[100010], pre[350], L[350], R[350], b[100010]; int c[2][100010], sum[350][100010], f[100010], u[350]; bool vis[350]; void change(int l, int r, int c) { int i = l; while (i <= r) { int N = pre[i]; if (L[N] == i && R[N] <= r) u[N] += c, i = R[N]; else { if (vis[N]) { if (c == 1) (sum[N][b[i]] -= f[i - 1]) %= mod; else (sum[N][b[i] - 1] += f[i - 1]) %= mod; } b[i] += c; } i++; } } int get(int r) { int i = 1, ans = 0; while (i <= r) { int N = pre[i]; if (L[N] == i && R[N] <= r) { if (k >= u[N]) (ans += sum[N][k - u[N]]) %= mod; i = R[N]; } else if (b[i] + u[N] <= k) (ans += f[i - 1]) %= mod; i++; } return ans; } void make(int N) { vis[N] = true; for (int i = L[N]; i <= R[N]; i++) (sum[N][b[i]] += f[i - 1]) %= mod; for (int i = 1; i <= n; i++) (sum[N][i] += sum[N][i - 1]) %= mod; } int main() { scanf("%d %d", &n, &k); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); int len = sqrt(n); for (int i = 1; i <= n; i++) { pre[i] = i / len + 1; if (i == 1 || pre[i] != pre[i - 1]) L[pre[i]] = i, R[pre[i - 1]] = i - 1; } R[pre[n]] = n; memset(vis, false, sizeof(vis)); f[0] = 1; for (int i = 1; i <= n; i++) { if (i == R[pre[i]]) make(pre[i]); change(c[0][a[i]] + 1, c[1][a[i]], -1); c[0][a[i]] = c[1][a[i]]; c[1][a[i]] = i; change(c[0][a[i]] + 1, c[1][a[i]], 1); f[i] = get(i); } if (n == 1000) { for (int i = 1; i <= n; i++) printf("%d ", (f[i] + mod) % mod); printf("\n"); } printf("%d", (f[n] + mod) % mod); }
1129_D. Isolation
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once. Since the answer can be large, find it modulo 998 244 353. Input The first line contains two space-separated integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the number of elements in the array a and the restriction from the statement. The following line contains n space-separated integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n) β€” elements of the array a. Output The first and only line contains the number of ways to divide an array a modulo 998 244 353. Examples Input 3 1 1 1 2 Output 3 Input 5 2 1 1 2 1 3 Output 14 Input 5 5 1 2 3 4 5 Output 16 Note In the first sample, the three possible divisions are as follows. * [[1], [1], [2]] * [[1, 1], [2]] * [[1, 1, 2]] Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
{ "input": [ "5 5\n1 2 3 4 5\n", "3 1\n1 1 2\n", "5 2\n1 1 2 1 3\n" ], "output": [ "16", "3", "14" ] }
{ "input": [ "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n", "100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n", "10 1\n4 2 9 2 1 4 4 1 4 10\n", "100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n", "1 1\n1\n", "100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n", "2 2\n2 1\n", "2 1\n2 2\n" ], "output": [ "16", "726975503", "24", "244208148", "1", "1", "2", "2" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> #pragma GCC optimize("Ofast", "unroll-loops") using namespace std; using ll = long long; const int N = 1e5 + 1, mod = 998244353; int n, k; int a[N], dp[N], pre[N], pre2[N], cnt[N]; int32_t main() { cin.tie(0)->sync_with_stdio(0); cin >> n >> k; for (int i = 1; i <= n; i++) { cin >> a[i]; } int sum = 1; dp[0] = 1; for (int i = 1, j; i <= n; i++) { j = pre2[a[i]] - 1; while (++j < pre[a[i]]) { sum += --cnt[j] == k ? dp[j] : 0; sum += sum >= mod ? mod : 0; } j = pre[a[i]] - 1; while (++j < i) { sum -= cnt[j]++ == k ? dp[j] : 0; sum += sum < 0 ? mod : 0; } dp[i] = sum; pre2[a[i]] = pre[a[i]]; pre[a[i]] = i; sum = (sum + dp[i]) % mod; } cout << dp[n]; }
1129_D. Isolation
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once. Since the answer can be large, find it modulo 998 244 353. Input The first line contains two space-separated integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the number of elements in the array a and the restriction from the statement. The following line contains n space-separated integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n) β€” elements of the array a. Output The first and only line contains the number of ways to divide an array a modulo 998 244 353. Examples Input 3 1 1 1 2 Output 3 Input 5 2 1 1 2 1 3 Output 14 Input 5 5 1 2 3 4 5 Output 16 Note In the first sample, the three possible divisions are as follows. * [[1], [1], [2]] * [[1, 1], [2]] * [[1, 1, 2]] Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
{ "input": [ "5 5\n1 2 3 4 5\n", "3 1\n1 1 2\n", "5 2\n1 1 2 1 3\n" ], "output": [ "16", "3", "14" ] }
{ "input": [ "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n", "100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n", "10 1\n4 2 9 2 1 4 4 1 4 10\n", "100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n", "1 1\n1\n", "100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n", "2 2\n2 1\n", "2 1\n2 2\n" ], "output": [ "16", "726975503", "24", "244208148", "1", "1", "2", "2" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int BASE = 998244353; const int SZ = 2; struct BlockData { int sumC; vector<long long> sumF; BlockData() {} }; int n, k, c[100100]; long long f[100100]; BlockData blocks[SZ]; long long calc(int i) { long long res = 0; int sumC = 0, block = i / SZ; for (int j = i; j >= block * SZ; j--) { sumC += c[j]; if (sumC <= k) res += j ? f[j - 1] : 1; } for (int j = block - 1; j >= 0; j--) { res += blocks[j].sumF[max(min(k - sumC, SZ), 0) + SZ]; sumC += blocks[j].sumC; } return res % BASE; } void constructBlock(int block) { BlockData &data = blocks[block]; data.sumC = 0; data.sumF = vector<long long>(SZ * 2 + 1, 0); for (int i = block * SZ + SZ - 1; i >= block * SZ; i--) { data.sumC += c[i]; data.sumF[data.sumC + SZ] += i ? f[i - 1] : 1; } for (int i = 0; i <= SZ * 2; i++) { if (i) data.sumF[i] += data.sumF[i - 1]; data.sumF[i] %= BASE; } } int main() { ios::sync_with_stdio(0); cin.tie(0); int x; cin >> n >> k; vector<int> last(n + 1, -1); for (int i = 0; i < n; i++) { cin >> x; c[i]++; if (last[x] >= 0) { c[last[x]] = -1; if (last[x] / SZ != i / SZ) constructBlock(last[x] / SZ); } last[x] = i; f[i] = calc(i); if (i % SZ == SZ - 1) constructBlock(i / SZ); } cout << f[n - 1] << endl; }
1129_D. Isolation
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once. Since the answer can be large, find it modulo 998 244 353. Input The first line contains two space-separated integers n and k (1 ≀ k ≀ n ≀ 10^5) β€” the number of elements in the array a and the restriction from the statement. The following line contains n space-separated integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n) β€” elements of the array a. Output The first and only line contains the number of ways to divide an array a modulo 998 244 353. Examples Input 3 1 1 1 2 Output 3 Input 5 2 1 1 2 1 3 Output 14 Input 5 5 1 2 3 4 5 Output 16 Note In the first sample, the three possible divisions are as follows. * [[1], [1], [2]] * [[1, 1], [2]] * [[1, 1, 2]] Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
{ "input": [ "5 5\n1 2 3 4 5\n", "3 1\n1 1 2\n", "5 2\n1 1 2 1 3\n" ], "output": [ "16", "3", "14" ] }
{ "input": [ "50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n", "100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n", "10 1\n4 2 9 2 1 4 4 1 4 10\n", "100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n", "1 1\n1\n", "100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n", "2 2\n2 1\n", "2 1\n2 2\n" ], "output": [ "16", "726975503", "24", "244208148", "1", "1", "2", "2" ] }
IN-CORRECT
java
import java.io.*; import java.util.*; public class D { static class ModularArithmetic { private final int module; public ModularArithmetic(int module) { this.module = module; } public ModularArithmetic() { this(1_000_000_007); } final int sum(int a, int b) { int sum = a + b; return sum >= module ? sum - module : sum; } final int difference(int a, int b) { return sum(a, module - b); } final int product(int a, int b) { return (int)((long)a * b % module); } final int power(int x, int k) { int result = 1; while (k > 0) { if (k % 2 == 1) { result = product(result, x); } x = product(x, x); k /= 2; } return result; } final int inverse(int x) { return power(x, module - 2); } } public static void main(String[] args) throws IOException { try (Input input = new StandardInput(); PrintWriter writer = new PrintWriter(System.out)) { int n = input.nextInt(), k = input.nextInt(); int[] a = input.readIntArray(n); ModularArithmetic modular = new ModularArithmetic(998244353); class Segment { private final int from, to; private int evicted; private Segment(int from, int to) { this.from = from; this.to = to; } } Map<Integer, Segment> numberToLastSegment = new HashMap<>(); List<Segment> segments = new ArrayList<>(); for (int i = 0; i < n; i++) { Segment previousSegment = numberToLastSegment.get(a[i]); if (previousSegment != null) { previousSegment.evicted = i; } Segment newSegment = new Segment(previousSegment == null ? 0 : previousSegment.to, i + 1); numberToLastSegment.put(a[i], newSegment); segments.add(newSegment); } for (Segment segment : numberToLastSegment.values()) { segment.evicted = n; } List[] add = new List[n + 1], remove = new List[n + 1]; for (int i = 0; i <= n; i++) { add[i] = new ArrayList<Segment>(); remove[i] = new ArrayList<Segment>(); } for (Segment segment : segments) { add[segment.from].add(segment); remove[segment.to].add(segment); } class SegmentTree { private int size = 1; { while (size <= n) { size *= 2; } } private final int[] a = new int[2 * size]; private void add(int i, int value) { for (i += size; i > 0; i /= 2) { a[i] += value; } } private int kthNumber(int node, int k) { // k starts from 0 if (node >= size) { return node - size; } if (a[2 * node] <= k) { return kthNumber(2 * node + 1, k - a[2 * node]); } return kthNumber(2 * node, k); } private int kthNumber(int k) { return kthNumber(1, k); } } class SumSegmentTree { private int size = 1; { while (size <= n) { size *= 2; } } private final int[] a = new int[2 * size]; private void add(int l, int r, int value) { for (l += size, r += size; l < r; l /= 2, r /= 2) { if (l % 2 == 1) { a[l] = modular.sum(a[l], value); l++; } if (r % 2 == 1) { --r; a[r] = modular.sum(a[r], value); } } } private int getValue(int i) { int result = 0; for (i += size; i > 0; i /= 2) { result = modular.sum(result, a[i]); } return result; } } SumSegmentTree dp = new SumSegmentTree(); SegmentTree segmentStarts = new SegmentTree(), segmentEnds = new SegmentTree(); dp.add(0, 1, 1); int segmentsCount = 0; for (int i = 0; i < n; i++) { int value = dp.getValue(i); for (Object o : add[i]) { Segment s = (Segment)o; segmentsCount++; segmentStarts.add(s.to, 1); segmentEnds.add(s.evicted, 1); } for (Object o : remove[i]) { Segment s = (Segment)o; segmentsCount--; segmentStarts.add(s.to, -1); segmentEnds.add(s.evicted, -1); } if (segmentsCount <= k) { dp.add(i + 1, n + 1, value); } else { int kthStart = segmentStarts.kthNumber(k); dp.add(i + 1, kthStart, value); int needToEvict = segmentsCount - k; int evictionTime = Math.max(segmentEnds.kthNumber(needToEvict - 1) + 1, kthStart); dp.add(evictionTime, n + 1, value); } } writer.println(dp.getValue(n)); } } interface Input extends Closeable { String next() throws IOException; default int nextInt() throws IOException { return Integer.parseInt(next()); } default long nextLong() throws IOException { return Long.parseLong(next()); } default int[] readIntArray() throws IOException { return readIntArray(nextInt()); } default int[] readIntArray(int size) throws IOException { int[] array = new int[size]; for (int i = 0; i < array.length; i++) { array[i] = nextInt(); } return array; } } private static class StandardInput implements Input { private final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); private StringTokenizer stringTokenizer; @Override public void close() throws IOException { reader.close(); } @Override public String next() throws IOException { if (stringTokenizer == null || !stringTokenizer.hasMoreTokens()) { stringTokenizer = new StringTokenizer(reader.readLine()); } return stringTokenizer.nextToken(); } } }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 100; int n, m, h[N], lev[N], Xor[N], cnt[N], deg[N]; vector<int> nxt[N], rnxt[N], vec[N]; set<int> s; queue<int> q; int main() { cin >> n >> m; for (int i = 1; i <= n; ++i) cin >> h[i]; for (int i = 1; i <= m; ++i) { int x, y; cin >> x >> y; rnxt[y].push_back(x); nxt[x].push_back(y); ++deg[x]; } for (int i = 1; i <= n; ++i) if (!deg[i]) q.push(i); while (!q.empty()) { int u = q.front(); q.pop(); for (auto v : nxt[u]) ++cnt[lev[v]]; while (cnt[lev[u]]) ++lev[u]; Xor[lev[u]] ^= h[u]; for (auto v : nxt[u]) --cnt[lev[v]]; for (auto v : rnxt[u]) if (!--deg[v]) q.push(v); } for (int i = n; ~i; --i) if (Xor[i]) { puts("WIN"); for (int u = 1; u <= n; ++u) if (lev[u] == i && h[u] > (h[u] ^ Xor[i])) { h[u] ^= Xor[i]; for (auto v : nxt[u]) if (Xor[lev[v]]) h[v] ^= Xor[lev[v]], Xor[lev[v]] = 0; break; } for (int j = 1; j <= n; ++j) cout << h[j] << " "; puts(""); return 0; } puts("LOSE"); return 0; }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> #pragma GCC optimize("O2") #pragma GCC optimize("unroll-loops") using namespace std; const long double eps = 1e-7; const int inf = 1000000010; const long long INF = 10000000000000010LL; const int mod = 1000000007; const int MAXN = 200010, LOG = 20; int n, m, k, u, v, x, y, t, a, b, ans; int A[MAXN], g[MAXN]; int val[MAXN]; int mark[MAXN]; vector<int> G[MAXN], topol; void dfs(int node) { mark[node] = 1; for (int v : G[node]) if (!mark[v]) dfs(v); topol.push_back(node); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> m; for (int i = 1; i <= n; i++) cin >> A[i]; while (m--) { cin >> u >> v; G[u].push_back(v); } for (int v = 1; v <= n; v++) if (!mark[v]) dfs(v); memset(mark, 0, sizeof(mark)); for (int v : topol) { for (int u : G[v]) mark[g[u]] = v; while (mark[g[v]] == v) g[v]++; val[g[v]] ^= A[v]; } for (int i = n; ~i; i--) if (val[i]) { for (int v = 1; v <= n; v++) if (g[v] == i && (A[v] ^ val[i]) < A[v]) { A[v] ^= val[i]; for (int u : G[v]) { A[u] ^= val[g[u]]; val[g[u]] = 0; } break; } cout << "WIN\n"; for (int v = 1; v <= n; v++) cout << A[v] << ' '; cout << '\n'; return 0; } cout << "LOSE\n"; return 0; }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int he[200010], ver[2 * 200010], nxt[2 * 200010], tot, in[200010]; void add(int x, int y) { ver[++tot] = y; nxt[tot] = he[x]; he[x] = tot; } int que[200010], cnt; long long w[200010]; vector<int> v[200010]; long long ans[200010]; int s[200010]; bool vis[200010]; int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) { scanf("%lld", &w[i]); } for (int i = 1; i <= m; i++) { int x, y; scanf("%d%d", &x, &y); add(y, x); in[x]++; } queue<int> q; for (int i = 1; i <= n; i++) { if (in[i] == 0) q.push(i); } while (!q.empty()) { int x = q.front(); q.pop(); que[++cnt] = x; for (int i = he[x]; i; i = nxt[i]) { in[ver[i]]--; if (!in[ver[i]]) q.push(ver[i]); } } int maxx = 0; for (int i = 1; i <= cnt; i++) { int x = que[i]; int stp = 0; if (!v[x].size()) ans[0] ^= w[x]; else { sort(v[x].begin(), v[x].end()); s[x] = 0; for (int j = 0; j < v[x].size(); j++) { if (v[x][j] == s[x]) s[x]++; } stp = s[x]; ans[stp] ^= w[x]; maxx = max(maxx, stp); } s[x] = stp; for (int j = he[x]; j; j = nxt[j]) v[ver[j]].push_back(stp); } bool xx = false; for (int i = 0; i <= maxx; i++) { if (ans[i] != 0) { xx = true; break; } } if (xx) { puts("WIN"); for (int i = maxx; i >= 0; i--) { if (ans[i]) { maxx = i; break; } } int tmp = 0; for (int i = 1; i <= n; i++) { if (s[i] == maxx && ((w[i] ^ ans[s[i]]) < w[i])) { tmp = i; break; } } w[tmp] = ans[maxx] ^ w[tmp]; for (int i = 1; i <= n; i++) { if (i == tmp || s[i] >= s[tmp]) continue; for (int j = he[i]; j; j = nxt[j]) { if (ver[j] == tmp && vis[s[i]] == 0) { w[i] = ans[s[i]] ^ w[i]; vis[s[i]] = 1; break; } } } for (int i = 1; i <= n; i++) printf("%lld ", w[i]); } else puts("LOSE"); }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using i64 = long long; int n, m, h[223456], xors[223456], sg[223456], tag[223456]; int u, v, t, deg[223456], q[223456]; vector<int> e[223456]; int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) { scanf("%d", &h[i]); } for (int i = 0; i < m; i++) { scanf("%d%d", &u, &v); e[u].push_back(v); ++deg[v]; } for (int i = 1; i <= n; i++) if (deg[i] == 0) q[++t] = i; for (int i = 1; i <= t; i++) { int u = q[i]; for (auto v : e[u]) { --deg[v]; if (deg[v] == 0) q[++t] = v; } } int maxL = 0; for (int i = n; i >= 1; i--) { int u = q[i]; for (auto v : e[u]) tag[sg[v]] = i; while (tag[sg[u]] == i) ++sg[u]; maxL = max(maxL, sg[u]); xors[sg[u]] ^= h[u]; } int lev = -1; for (int i = 0; i <= maxL; i++) if (xors[i] != 0) { lev = i; } if (lev == -1) { puts("LOSE"); return 0; } puts("WIN"); for (int u = 1; u <= n; u++) { if (sg[u] == lev && (h[u] ^ xors[lev]) < h[u]) { h[u] = h[u] ^ xors[lev]; for (auto v : e[u]) { h[v] ^= xors[sg[v]]; xors[sg[v]] = 0; } break; } } for (int i = 1; i <= n; i++) printf("%d ", h[i]); puts(""); }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 10; vector<int> e[N]; int n, m, h[N], sg[N], sum[N], dg[N], q[N], vis[N]; void topsort() { int h = 1, t = 0; for (int i = 1; i <= n; i++) if (!dg[i]) q[++t] = i; while (h <= t) { int u = q[h++]; for (int v : e[u]) if (!--dg[v]) q[++t] = v; } } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%d", &h[i]); for (int i = 1; i <= m; i++) { int u, v; scanf("%d%d", &u, &v); e[u].push_back(v); dg[v]++; } topsort(); for (int i = n; i; i--) { int u = q[i]; for (int v : e[u]) vis[sg[v]] = i; while (vis[sg[u]] == i) sg[u]++; sum[sg[u]] ^= h[u]; } for (int i = n; ~i; i--) if (sum[i]) { int pos; for (int j = 1; j <= n; j++) if (sg[j] == i && h[j] > (sum[i] ^ h[j])) pos = j; h[pos] ^= sum[i]; for (int v : e[pos]) h[v] ^= sum[sg[v]], sum[sg[v]] = 0; printf("WIN\n"); for (int j = 1; j <= n; j++) printf("%d ", h[j]); printf("\n"); return 0; } printf("LOSE"); }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; inline long long read() { long long x = 0, y = 0, c = getchar(); while (!isdigit(c)) y = c, c = getchar(); while (isdigit(c)) x = (x << 1) + (x << 3) + (c ^ '0'), c = getchar(); return y == '-' ? -x : x; } inline void print(long long q) { if (q < 0) putchar('-'), q = -q; if (q >= 10) print(q / 10); putchar(q % 10 + '0'); } long long s1 = 19260817, s2 = 23333333, s3 = 998244353, srd; inline long long rd() { return srd = (srd * s1 + s2 + rand()) % s3; } void file() { freopen(".in", "r", stdin); freopen(".out", "w", stdout); } int n, m, a[200010]; struct edge { int to, nxt; } e[200010], ee[200010]; int cnt, fir[200010], ff[200010], ds[200010]; inline void ins(int u, int v) { e[++cnt].to = v; e[cnt].nxt = fir[u]; fir[u] = cnt; ee[cnt].to = u; ee[cnt].nxt = ff[v]; ff[v] = cnt; ++ds[u]; } int jie[200010], vl[200010], mx; bool tp[200010]; vector<int> b[200010]; int q[200010], h, t; int main() { srand(time(0)); rd(); int i, j, k, u, v; n = read(); m = read(); for (i = 1; i <= n; ++i) a[i] = read(); for (i = 1; i <= m; ++i) { u = read(); v = read(); ins(u, v); } for (i = 1; i <= n; ++i) if (!ds[i]) q[++t] = i; while (h < t) { j = q[++h]; for (i = ff[j]; i; i = ee[i].nxt) { --ds[ee[i].to]; if (!ds[ee[i].to]) q[++t] = ee[i].to; } } for (i = 1; i <= n; ++i) { for (j = fir[q[i]]; j; j = e[j].nxt) tp[jie[e[j].to]] = 1; for (j = 0;; ++j) if (!tp[j]) { jie[q[i]] = j; b[j].push_back(q[i]); mx = max(mx, j); vl[j] ^= a[q[i]]; break; } for (j = fir[q[i]]; j; j = e[j].nxt) tp[jie[e[j].to]] = 0; } int mm = mx + 1; for (i = mx; i >= 0; --i) if (vl[i]) { mm = i; break; } if (mm == mx + 1) { puts("LOSE"); return 0; } puts("WIN"); for (i = 0; i < b[mm].size(); ++i) { j = b[mm][i]; k = vl[mm] ^ a[j]; if (k < a[j]) { a[j] = k; vl[mm] = 0; for (u = fir[j]; u; u = e[u].nxt) { a[e[u].to] ^= vl[jie[e[u].to]]; vl[jie[e[u].to]] = 0; } break; } } for (i = 1; i <= n; ++i) print(a[i]), putchar(' '); return 0; }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <typename T> void maxtt(T& t1, T t2) { t1 = max(t1, t2); } template <typename T> void mintt(T& t1, T t2) { t1 = min(t1, t2); } bool debug = 0; int n, m, k; int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0}; string direc = "URDL"; long long ln, lk, lm; void etp(bool f = 0) { puts(f ? "YES" : "NO"); exit(0); } void addmod(int& x, int y, int mod = 998244353) { assert(y >= 0); x += y; if (x >= mod) x -= mod; assert(x >= 0 && x < mod); } void et(int x = -1) { printf("%d\n", x); exit(0); } long long fastPow(long long x, long long y, int mod = 998244353) { long long ans = 1; while (y > 0) { if (y & 1) ans = (x * ans) % mod; x = x * x % mod; y >>= 1; } return ans; } long long gcd1(long long x, long long y) { return y ? gcd1(y, x % y) : x; } vector<int> mp[200135]; int h[200135], sg[200135], s[200135]; int spg(int x) { int& tmp = sg[x]; if (tmp != -1) return tmp; vector<int> vp; for (int c : mp[x]) vp.push_back(spg(c)); sort(vp.begin(), vp.end()); int N = unique(vp.begin(), vp.end()) - vp.begin(); for (int(i) = 0; (i) < (int)(N); (i)++) if (i != vp[i]) { tmp = i; return tmp; } tmp = N; return tmp; } void fmain(int tid) { scanf("%d%d", &n, &m); for (int(i) = 1; (i) <= (int)(n); (i)++) scanf("%d", h + i); for (int(i) = 1; (i) <= (int)(m); (i)++) { int u, v; scanf("%d%d", &u, &v); mp[u].push_back(v); } memset(sg, -1, sizeof sg); for (int(i) = 1; (i) <= (int)(n); (i)++) { s[spg(i)] ^= h[i]; } int tar = -1; for (int i = n; i >= 0; i--) if (s[i] != 0) { tar = i; break; } if (tar == -1) { puts("LOSE"); return; } puts("WIN"); int w = -1; for (int(i) = 1; (i) <= (int)(n); (i)++) if (sg[i] == tar) { if ((s[tar] ^ h[i]) < h[i]) { w = i; break; } } h[w] = s[tar] ^ h[w]; for (int c : mp[w]) { h[c] = s[sg[c]] ^ h[c]; s[sg[c]] = 0; } for (int(i) = 1; (i) <= (int)(n); (i)++) printf("%d ", h[i]); } int main() { int t = 1; for (int(i) = 1; (i) <= (int)(t); (i)++) { fmain(i); } return 0; }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long head[400005], ver[400005], nxt[400005], cnt; void add(long long a, long long b) { ver[++cnt] = b, nxt[cnt] = head[a], head[a] = cnt; } vector<long long> e[400005], has[400005]; long long n, m, cd[400005]; long long a[400005], sg[400005]; long long pos[400005], mex[400005], mx; bool vis[400005]; queue<long long> q, ans; int main() { scanf("%d%d", &n, &m); for (long long i = 1; i <= n; i++) scanf("%lld", &a[i]); for (long long i = 1, a, b; i <= m; i++) { scanf("%d%d", &a, &b); add(a, b), e[b].push_back(a), cd[a]++; } for (long long i = 1; i <= n; i++) if (!cd[i]) q.push(i); while (!q.empty()) { long long now = q.front(); q.pop(), cnt = 0; for (long long i = head[now]; i; i = nxt[i]) mex[++cnt] = pos[ver[i]]; pos[now] = 1; sort(mex + 1, mex + 1 + cnt); for (long long i = 1; i <= cnt; i++) if (mex[i] == pos[now]) pos[now]++; sg[pos[now]] ^= a[now]; has[pos[now]].push_back(now); mx = max(mx, pos[now]); for (long long i = 0; i < e[now].size(); i++) { cd[e[now][i]]--; if (!cd[e[now][i]]) q.push(e[now][i]); } } for (long long o = mx; o >= 1; o--) { if (sg[o] != 0) { long long now; for (long long i = 0; i < has[o].size(); i++) if ((a[has[o][i]] ^ sg[o]) <= a[has[o][i]]) { now = has[o][i]; break; } a[now] ^= sg[o]; for (long long i = head[now]; i; i = nxt[i]) { long long to = ver[i]; if (vis[pos[to]]) continue; vis[pos[to]] = 1; a[to] = sg[pos[to]] ^ a[to]; } printf("WIN\n"); for (long long i = 1; i <= n; i++) printf("%lld ", a[i]); return 0; } } printf("LOSE"); }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; constexpr int kMaxN = 2e5 + 3; vector<int> edges[kMaxN]; bitset<kMaxN> visited; int nim[kMaxN]; int mext[kMaxN]; int levels[kMaxN]; vector<int> nim_nodes[kMaxN]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m; vector<int> h(n); for (int i = 0; i < n; ++i) { cin >> h[i]; } for (int i = 0; i < m; ++i) { int u, v; cin >> u >> v; --u, --v; edges[u].emplace_back(v); } memset(mext, -1, sizeof(mext)); function<void(int)> dfs = [&](int u) { if (visited[u]) { return; } visited[u] = true; for (int v : edges[u]) { dfs(v); } for (int v : edges[u]) { mext[nim[v]] = u; } int k = 0; while (mext[k] == u) { ++k; } nim[u] = k; levels[k] ^= h[u]; nim_nodes[k].emplace_back(u); }; for (int i = 0; i < n; ++i) { dfs(i); } if (count(levels, levels + kMaxN, 0) == kMaxN) { cout << "LOSE\n"; return 0; } cout << "WIN\n"; int last_level = n; while (levels[last_level] == 0) { --last_level; } assert(last_level >= 0); int node = -1; for (int u : nim_nodes[last_level]) { if ((h[u] ^ levels[nim[u]]) <= h[u]) { h[u] ^= levels[nim[u]]; node = u; levels[nim[u]] = 0; break; } } assert(node != -1); for (int v : edges[node]) { h[v] ^= levels[nim[v]]; levels[nim[v]] = 0; } assert(count(levels, levels + kMaxN, 0) == kMaxN); for (int i = 0; i < n; ++i) { cout << h[i] << ' '; } cout << '\n'; return 0; }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <class T> inline void chkmax(T &a, T b) { if (a < b) a = b; } template <class T> inline void chkmin(T &a, T b) { if (a > b) a = b; } inline int read() { int s = 0, f = 1; char ch = getchar(); while (!isdigit(ch) && ch != '-') ch = getchar(); if (ch == '-') ch = getchar(), f = -1; while (isdigit(ch)) s = s * 10 + ch - '0', ch = getchar(); return ~f ? s : -s; } const int maxn = 3e5 + 20; vector<int> ed[maxn]; int h[maxn]; int n, m, sg[maxn], rd[maxn]; int rk[maxn], tp; inline void get_topx() { queue<int> q; for (int i = (1), _end_ = (n); i <= _end_; i++) if (!rd[i]) q.push(i); while (!q.empty()) { int u = q.front(); q.pop(); rk[++tp] = u; for (int v : ed[u]) if (!(--rd[v])) q.push(v); } } inline void init() { n = read(); m = read(); for (int i = (1), _end_ = (n); i <= _end_; i++) h[i] = read(); for (int i = (1), _end_ = (m); i <= _end_; i++) { int u = read(), v = read(); ed[u].push_back(v); rd[v]++; } get_topx(); } int vis[maxn]; int sum[maxn]; inline void doing() { for (int i = (n), _end_ = (1); i >= _end_; i--) { int x = rk[i]; for (int y : ed[x]) vis[sg[y]]++; while (vis[sg[x]]) ++sg[x]; for (int y : ed[x]) vis[sg[y]]--; } for (int i = (1), _end_ = (n); i <= _end_; i++) sum[sg[i]] ^= h[i]; for (int i = (n), _end_ = (0); i >= _end_; i--) if (sum[i]) { puts("WIN"); for (int j = (1), _end_ = (n); j <= _end_; j++) if (sg[j] == i && (h[j] ^ sum[i]) < h[j]) { h[j] = h[j] ^ sum[i]; for (int k : ed[j]) if (sum[sg[k]]) h[k] ^= sum[sg[k]], sum[sg[k]] = 0; break; } for (int j = (1), _end_ = (n); j <= _end_; j++) printf("%d ", h[j]); return; } puts("LOSE"); } int main() { init(); doing(); return 0; }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; int n, m; bool tmp[N]; int h[N], f[N], b[N]; vector<int> G[N]; int deg[N]; vector<int> ord; void topusort() { queue<int> Q; for (int i = 1; i <= n; i++) { if (!deg[i]) { Q.push(i); } } while (!Q.empty()) { int p = Q.front(); Q.pop(); ord.push_back(p); for (auto e : G[p]) { if (!--deg[e]) { Q.push(e); } } } } int qwq(int x) { int t = 0; while (x) x >>= 1, t++; return t; } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) { scanf("%d", h + i); } for (int i = 1, u, v; i <= m; i++) { scanf("%d%d", &u, &v); G[u].emplace_back(v); deg[v]++; } topusort(); reverse(ord.begin(), ord.end()); for (auto p : ord) { int t = G[p].size(); fill(tmp, tmp + t + 1, false); for (auto e : G[p]) { tmp[f[e]] = true; } while (tmp[f[p]]) f[p]++; } for (int i = 1; i <= n; i++) { b[f[i]] ^= h[i]; } int mx = n; while (mx--) { if (b[mx]) { break; } } if (mx < 0) { puts("LOSE"); } else { puts("WIN"); int p; for (p = 1; p <= n; p++) { if (f[p] == mx && (h[p] ^ b[mx]) < h[p]) { break; } } assert(p <= n); h[p] ^= b[mx]; b[mx] = 0; for (auto e : G[p]) { h[e] ^= b[f[e]]; b[f[e]] = 0; } for (int i = 1; i <= n; i++) { printf("%d ", h[i]); } } return 0; }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int maxN = 2e5 + 10; int n, m, clk; int h[maxN + 1], vis[maxN + 1]; int p[maxN + 1], sum[maxN + 1]; vector<int> G[maxN + 1]; inline int read() { int num = 0, f = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') f = -1; ch = getchar(); } while (isdigit(ch)) num = (num << 3) + (num << 1) + (ch ^ 48), ch = getchar(); return num * f; } inline void dfs(int u) { if (p[u] != -1) return; for (int i = 0; i < G[u].size(); i++) dfs(G[u][i]); clk++; for (int i = 0; i < G[u].size(); i++) vis[p[G[u][i]]] = clk; p[u] = 0; while (vis[p[u]] == clk) p[u]++; sum[p[u]] ^= h[u]; } int main() { n = read(), m = read(); for (int i = 1; i <= n; i++) h[i] = read(); for (int i = 1; i <= m; i++) { int x = read(), y = read(); G[x].push_back(y); } fill(p + 1, p + n + 1, -1); for (int i = 1; i <= n; i++) dfs(i); int id = -1; for (int i = 1; i <= n; i++) if (sum[p[i]]) id = max(id, p[i]); if (id == -1) { puts("LOSE"); return 0; } puts("WIN"); int x; for (int i = 1; i <= n; i++) if (p[i] == id && (h[i] ^ sum[id]) < h[i]) { x = i; break; } h[x] ^= sum[p[x]]; for (int i = 0; i < G[x].size(); i++) { int y = G[x][i]; h[y] ^= sum[p[y]]; sum[p[y]] = 0; } for (int i = 1; i <= n; i++) printf("%d ", h[i]); puts(""); return 0; }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> const int N = 200007; int a[N], deg[N], pos[N], vis[N], sg[N], sum[N]; std::queue<int> q; std::vector<int> e[N]; int read() { int x = 0, c = getchar(); while (isspace(c)) c = getchar(); while (isdigit(c)) (x *= 10) += c & 15, c = getchar(); return x; } int main() { int n = read(), m = read(); for (int i = 1; i <= n; ++i) a[i] = read(); for (int i = 1, u, v; i <= m; ++i) u = read(), v = read(), e[u].push_back(v), ++deg[v]; for (int i = 1; i <= n; ++i) if (!deg[i]) q.push(i); for (int u, c = 0; !q.empty();) { u = q.front(), q.pop(), pos[++c] = u; for (int v : e[u]) if (!--deg[v]) q.push(v); } for (int i = n, u; i; --i) { u = pos[i]; for (int v : e[u]) vis[sg[v]] = i; while (vis[sg[u]] == i) ++sg[u]; sum[sg[u]] ^= a[u]; } for (int i = n, u; ~i; --i) if (sum[i]) { for (int j = 1; j <= n; ++j) if (sg[j] == i && a[j] > (sum[i] ^ a[j])) u = j; a[u] ^= sum[i]; for (int v : e[u]) a[v] ^= sum[sg[v]], sum[sg[v]] = 0; puts("WIN"); for (int j = 1; j <= n; ++j) printf("%d ", a[j]); puts(""); return 0; } puts("LOSE"); }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> inline void read(int& x) { char ch = getchar(); x = 0; while (!isdigit(ch)) ch = getchar(); while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar(); } const int N = 201000; struct Edge { int v, nxt; } a[N + N]; int head[N], Head[N]; int h[N], xr[N], deg[N], q[N]; int tng[N], sg[N]; int n, m, _; inline void ad() { static int x, y; read(x), read(y), ++deg[x]; a[++_].v = y, a[_].nxt = head[x], head[x] = _; a[++_].v = x, a[_].nxt = Head[y], Head[y] = _; } int main() { read(n), read(m); for (int i = 1; i <= n; ++i) read(h[i]); for (int i = 1; i <= m; ++i) ad(); int he = 1, ta = 0; for (int x = 1; x <= n; ++x) if (!deg[x]) q[++ta] = x; while (he <= ta) { int x = q[he++]; for (int i = head[x]; i; i = a[i].nxt) tng[sg[a[i].v]] = 1; for (int i = 0; !sg[x]; ++i) if (!tng[i]) sg[x] = i; for (int i = head[x]; i; i = a[i].nxt) tng[sg[a[i].v]] = 0; for (int i = Head[x]; i; i = a[i].nxt) if (!(--deg[a[i].v])) q[++ta] = a[i].v; } m = 0; for (int i = 1; i <= n; ++i) if (sg[i] > m) m = sg[i]; for (int i = 1; i <= n; ++i) xr[sg[i]] ^= h[i]; int d = 0; for (int i = 1; i <= m; ++i) if (xr[i]) d = i; if (!d) return puts("LOSE"), 0; puts("WIN"); for (int x = 1; x <= n; ++x) if (sg[x] == d and (xr[sg[x]] ^ h[x]) < h[x]) { h[x] = (xr[sg[x]] ^ h[x]); xr[sg[x]] = 0; for (int i = head[x]; i; i = a[i].nxt) if (xr[sg[a[i].v]]) { h[a[i].v] ^= xr[sg[a[i].v]]; xr[sg[a[i].v]] = 0; } for (int i = 1; i <= n; ++i) printf("%d ", h[i]); putchar(10); return 0; } }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; vector<vector<int> > adj(200001); int c[200001], mex[200001], h[200001], b[200001]; void DFS(int i) { if (mex[i] >= 0) { return; } vector<int> v; for (int j : adj[i]) { DFS(j); v.push_back(mex[j]); } for (int x : v) { c[x] = i; } int &k = mex[i]; for (k = 0; c[k] == i; ++k) ; } int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 1; i <= n; ++i) { scanf("%d", &h[i]); } while (m--) { int i, j; scanf("%d%d", &i, &j); adj[i].push_back(j); } for (int i = 1; i <= n; ++i) { mex[i] = -1; } for (int i = 1; i <= n; ++i) { DFS(i); } for (int i = 1; i <= n; ++i) { b[mex[i]] ^= h[i]; } int root = 0; mex[root] = -1; for (int i = 1; i <= n; ++i) { if (mex[i] > mex[root] && ((b[mex[i]] ^ h[i]) < h[i])) { root = i; } } bool win = false; for (int i = 0; i <= mex[root] && !win; ++i) { win = (b[i] != 0); } if (!win) { printf("LOSE\n"); return 0; } c[mex[root]] = root; for (int j : adj[root]) { c[mex[j]] = j; } for (int i = 0; i <= mex[root]; ++i) { int j = c[i]; h[j] ^= b[i]; } printf("WIN\n"); for (int i = 1; i <= n; ++i) { printf("%d ", h[i]); } printf("\n"); return 0; }
1149_E. Election Promises
In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are n cities in Byteland, connected by m one-way roads. Interestingly enough, the road network has no cycles β€” it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the i-th city had to pay h_i bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city v, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from v using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers n, m (1 ≀ n ≀ 200 000, 0 ≀ m ≀ 200 000) β€” the number of cities and roads in Byteland. The next line contains n space-separated integers h_1, h_2, ..., h_n (0 ≀ h_i ≀ 10^9); h_i denotes the amount of taxes paid in the i-th city. Each of the following m lines contains two integers (1 ≀ u, v ≀ n, u β‰  v), and describes a one-way road leading from the city u to the city v. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain n non-negative integers h'_1, h'_2, ..., h'_n (0 ≀ h'_i ≀ 2 β‹… 10^{18}) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than 2 β‹… 10^{18} bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 4 2 2 1 1 5 1 2 3 4 Output WIN 1 5 1 5 Input 4 2 1 5 1 5 1 2 3 4 Output LOSE Input 3 3 314 159 265 1 2 1 3 3 2 Output WIN 0 0 0 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Output LOSE Note In the first example, Wrong Answer Party should hold the convention in the city 1. The party will decrease the taxes in this city to 1 bourle. As the city 2 is directly reachable from 1, we can arbitrarily modify the taxes in this city. The party should change the tax to 5 bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately.
{ "input": [ "6 4\n2 2 5 5 6 6\n1 3\n2 4\n3 5\n4 6\n", "3 3\n314 159 265\n1 2\n1 3\n3 2\n", "4 2\n2 1 1 5\n1 2\n3 4\n", "4 2\n1 5 1 5\n1 2\n3 4\n" ], "output": [ "LOSE\n", "WIN\n0 0 0 \n", "WIN\n1 5 1 5 \n", "LOSE\n" ] }
{ "input": [ "2 1\n1000000000 1000000000\n2 1\n", "3 2\n123 345 567\n1 2\n3 2\n", "3 0\n4 1 4\n", "3 2\n1 0 1\n3 2\n2 1\n", "3 0\n1 2 3\n", "3 2\n1 1 1\n1 2\n2 3\n", "2 0\n1000000000 1000000000\n", "2 0\n123456789 987654321\n", "3 2\n123 345 123\n1 2\n3 2\n", "1 0\n271828182\n", "2 1\n1000000000 1000000000\n1 2\n", "6 3\n678678678 395063145 789789 2 3 4\n4 1\n5 2\n6 3\n", "3 2\n2 3 4\n1 2\n1 3\n", "1 0\n0\n" ], "output": [ "WIN\n0 0 \n", "WIN\n123 0 123 \n", "WIN\n4 0 4 \n", "LOSE\n", "LOSE\n", "WIN\n1 0 1 \n", "LOSE\n", "WIN\n123456789 123456789 \n", "WIN\n123 0 123 \n", "WIN\n0 \n", "WIN\n0 0 \n", "WIN\n678678678 395063145 1073741823 2 3 1 \n", "WIN\n0 4 4 \n", "LOSE\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5; int n, m, h[maxn + 3], xo[maxn + 3], deg[maxn + 3], sg[maxn + 3], vis[maxn + 3], res[maxn + 3]; vector<int> G[maxn + 3], H[maxn + 3]; int main() { scanf("%d %d", &n, &m); for (int i = (1); i <= int(n); i++) scanf("%d", &h[i]); for (int i = (1); i <= int(m); i++) { int u, v; scanf("%d %d", &u, &v); G[u].push_back(v), H[v].push_back(u); } for (int i = (1); i <= int(n); i++) deg[i] = G[i].size(); queue<int> Q; for (int i = (1); i <= int(n); i++) if (!deg[i]) Q.push(i); for (int k = (1); k <= int(n); k++) { int u = Q.front(); Q.pop(); for (int i = (0); i <= int(G[u].size() - 1); i++) { int v = G[u][i]; vis[sg[v]] = k; } for (int i = (0); i <= int(n); i++) if (vis[i] != k) { sg[u] = i; break; } xo[sg[u]] ^= h[u]; for (int i = (0); i <= int(H[u].size() - 1); i++) { int v = H[u][i]; if (!--deg[v]) { Q.push(v); } } } bool flag = true; for (int i = (0); i <= int(n - 1); i++) flag &= xo[i] == 0; if (flag) { puts("LOSE"); } else { puts("WIN"); for (int i = (1); i <= int(n); i++) res[i] = h[i]; int x = -1, y = -1; for (int i = (n - 1); i >= int(0); i--) if (xo[i]) { x = i; break; } for (int i = (1); i <= int(n); i++) if (sg[i] == x && (h[i] ^ xo[x]) < h[i]) { res[i] ^= xo[x], y = i; break; } for (int i = (0); i <= int(G[y].size() - 1); i++) { int v = G[y][i]; if (sg[v] < x && xo[sg[v]]) { res[v] ^= xo[sg[v]], xo[sg[v]] = 0; } } for (int i = (1); i <= int(n); i++) printf("%d%c", res[i], " \n"[i == n]); } return 0; }