This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
#include "paint.h"
#define mp make_pair
#define INF 10000000
#define MOD 1000000007
#define MID ((l+r)/2)
#define HASHMOD 2305843009213693951
#define ll long long
#define ull unsigned long long
#define F first
#define S second
typedef pair<ll, ll> ii;
typedef pair<ii, int> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef map<int, int> mii;
#define EPS 1e-6
#define FOR(i,n) for(int i=0;i<((int)(n));i++)
#define FORi(i,a,b) for(int i=((int)(a));i<((int)(b));i++)
#define FOA(v, a) for(auto v : a)
int M, N;
vi c;
map<int, vector<int>> f;
struct seg{
unordered_set<int> val;
seg *L, *R;
unordered_set<int> merge(unordered_set<int> &a, int as, unordered_set<int> &b){
unordered_set<int> ans;
FOA(v,a){
if(b.count((v + as)%M)) ans.insert(v);
}
return ans;
}
void build(int l=0, int r=N-1){
if(l==r){
FOA(v, f[c[l]]){
val.insert(v);
}
}
else{
L = new seg;
R = new seg;
L->build(l,MID);
R->build(MID+1,r);
val = merge(L->val, MID-l+1, R->val);
}
}
unordered_set<int> query(int rl, int rr, int l=0, int r=N-1){
if(rl<=l && r<=rr) return val;
else if(rr<l || r<rl) return unordered_set<int>();
else{
unordered_set<int> sl = L->query(rl,rr,l,MID);
unordered_set<int> sr = R->query(rl,rr,MID+1,r);
if(rr<=MID){
/*
cout<<"Return left "<<l<<" "<<r<<": ";
FOA(v,sl) cout<<v<<" ";
cout<<endl;*/
return sl;
}
if(rl>MID){
/*
cout<<"Return right "<<l<<" "<<r<<": ";
FOA(v,sr) cout<<v<<" ";
cout<<endl;*/
return sr;
}
//cout<<"merge "<<l<<" "<<r<<": ";
unordered_set<int> qq;
if(l<=rl) qq = merge(sl, MID-rl+1, sr);
else qq = merge(sl, MID-l+1, sr);
/*
FOA(v, sl) cout<<v<<" ";
cout<<" | ";
FOA(v, sr) cout<<v<<" ";
cout<<" | ";
FOA(v, qq) cout<<v<<" ";
cout<<endl;*/
return qq;
}
}
void print(int l=0, int r=N-1){
cout<<l<<" "<<r<<": ";
FOA(v, val) cout<<v<<" ";
cout<<endl;
if(l!=r){
L->print(l, MID);
R->print(MID+1,r);
}
}
};
int minimumInstructions(int n, int m, int K, std::vector<int> C,
vector<int> A, vector<std::vector<int>> B) {
M = m;
N = n;
c = C;
FOR(i,M){
FOA(v, B[i]){
f[v].push_back(i);
}
}
seg s;
s.build();
if(s.query(0, M-1).empty()) return -1;
int p=0;
int cp=M;
int ans = 1;
while(cp<N){
//cout<<p<<" "<<cp<<endl;
while(s.query(cp, cp+M-1).empty()) cp--;
if(cp<=p) return -1;
p = cp;
cp+=M;
ans++;
}
return ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |