Submission #406686

#TimeUsernameProblemLanguageResultExecution timeMemory
406686amunduzbaevPainting Walls (APIO20_paint)C++14
51 / 100
410 ms468548 KiB
#include "paint.h"
#include "bits/stdc++.h"
#ifndef EVAL
#include "grader.cpp"
#endif
using namespace std;

#define int long long
#define sz(x) (int)x.size()
#define pb push_back
#define pii pair<int, int>
#define ff first
#define ss second

const int mod = 1e9+7;
const int N = 2e4+5;
const int MM = 4e3+5;
const int M = 1e5+5;
int gg[MM][N], n, m, k, is[N];
vector<int> pos[M];
int tree[4*N], p[4*N];

void push(int x, int lx, int rx){
	if(!p[x] || lx == rx) return;
	tree[x<<1] += p[x], tree[x<<1|1] += p[x];
	p[x<<1] += p[x], p[x<<1|1] += p[x];
	p[x] = 0;
}

void sett(int l, int r, int v, int lx = 0, int rx = n-1, int x = 1){
	push(x, lx, rx);
	if(lx > r || rx < l) return;
	if(lx >= l && rx <= r) { p[x] += v, tree[x] += v; return; }
	int m = (lx + rx)>>1;
	sett(l, r, v, lx, m, x<<1), sett(l, r, v, m+1, rx, x<<1|1);
	tree[x] = min(tree[x<<1], tree[x<<1|1]);
}

int find(int l, int r, int lx = 0, int rx = n-1, int x = 1){
	push(x, lx, rx);
	if(lx > r || rx < l) return mod;
	if(lx >= l && rx <= r) return tree[x];
	int m = (lx + rx)>>1;
	return min(find(l, r, lx, m, x<<1), find(l, r, m+1, rx, x<<1|1));
}

/*

8 3 5
3 3 1 3 4 4 2 2
3 0 1 2
2 2 3
2 3 4

10 4 4
1 2 3 4 3 4 1 2 3 4
1 1
1 2
1 3
1 4

*/

int ss[M], pw[M], res ;

bool check(){
	if(n <= N) return 0;
	return 1;
}

int bp(int a, int b){
	if(b == 1) return a % mod;
	if(b & 1) return (bp(a, b - 1) * a) % mod;
	int tt = bp(a, b>>1);
	return (tt * tt) % mod;
}

int32_t minimumInstructions(int32_t N, int32_t M, int32_t K, vector<int32_t> c,
	vector<int32_t> a, vector<vector<int32_t>> b) {
	n = N, m = M, k = K;
	if(check()){
		for(int i=0;i<m;i++){
			for(int j=0;j<a[i];j++){
				ss[b[i][j]] = i+1;
			}
		} pw[0] = 1;
		for(int i=1;i<n;i++) pw[i] = pw[i-1] * 73 % mod;
		map<int, int> hh;
		int hash = 0;
		for(int i=0;i<m;i++) hash = (hash + pw[i] * i) % mod;
		hh[hash];
		int inv = bp(73ll, mod - 2);
		for(int i=0;i<m;i++){
			hash = (hash - i + mod) % mod;
			hash = (hash * inv) % mod;
			hash = (hash + i * pw[m-1]) % mod;
			hh[hash];
		} hash = 0;
		for(int i=0;i<m;i++){
			if(!ss[c[i]]) return -1;		
			hash = (hash + pw[i] * (ss[c[i]] - 1)) % mod;
		} if(hh.count(hash)) is[0] = 1;
		for(int i=0;i<m;i++){
			for(int j=0;j<a[i];j++) ss[b[i][j]]--;
		} for(int i=m;i<n;i++){
			hash = (hash - ss[c[i - m]] + mod) % mod;
			hash = (hash * inv) % mod;
			hash = (hash + pw[m - 1] * ss[c[i]]) % mod;
			if(hh.count(hash)) is[i - m + 1] = 1;
		} res = 0;
		for(int i=0;i<n;i++) if(is[i]) sett(i, i+m-1, 1), res++;
		if(tree[1] == 0) return -1;
		for(int i=0;i<n;i++){
			if(!is[i]) continue;
			if(find(i, i+m-1) >= 2) sett(i, i+m-1, -1), res--;
		} return res;
	}
	for(int i=0;i<n;i++) pos[c[i]].pb(i);
	for(int i=0;i<m;i++){
		for(int j=0;j<a[i];j++){
			for(auto x : pos[b[i][j]]) gg[i][x] = 1;
		}
	} for(int i=0;i<m;i++){
		for(int j=0;j<a[i];j++){
			for(auto x : pos[b[i][j]]) gg[i+m][x] = 1;
		}
	}
	for(int i=0;i<2*m;i++){
		for(int j=0;j<n;j++){
			if(i > 0 && j > 0 && gg[i][j]) gg[i][j] += gg[i-1][j-1];
			if(gg[i][j] >= m) is[j - m + 1] = 1;
		}
	} res = 0;
	for(int i=0;i<n;i++) if(is[i]) sett(i, i+m-1, 1), res++;
	if(tree[1] == 0) return -1;
	for(int i=0;i<n;i++){
		if(!is[i]) continue;
		if(find(i, i+m-1) >= 2) sett(i, i+m-1, -1), res--;
	} return res;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...