2023 rcpc

This commit is contained in:
2024-04-22 16:45:09 +03:00
parent a0ddb657b7
commit 156202ada0
61 changed files with 4462 additions and 0 deletions

View File

@@ -0,0 +1,62 @@
/// Gheorghies Alexandru
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;
const ll NMAX=5e5+5,MOD=1e9+7;
ll dp[NMAX],col[NMAX],prv[NMAX],ans[NMAX],p[NMAX],subtreeSize[NMAX];
vector<ll> edg[NMAX];
void dfs(ll u){
ll formerprv=prv[col[p[u]]];
subtreeSize[u]=1;
prv[col[p[u]]]=u;
for(auto it : edg[u]){
if(it!=p[u]){
p[it]=u;
dfs(it);
subtreeSize[u]+=subtreeSize[it];
}
}
ll v=prv[col[u]];
if(v==1)
ans[col[u]]-=subtreeSize[u];
else
dp[v]-=subtreeSize[u];
prv[col[p[u]]]=formerprv;
dp[u]+=subtreeSize[u];
}
void tc(){
ll n,k;
cin>>n>>k;
for(ll i=1;i<=n;i++){
dp[i]=p[i]=subtreeSize[i]=0,prv[i]=1,edg[i].clear();
ans[i]=n;
cin>>col[i];
}
for(ll i=1;i<n;i++){
ll u,v;
cin>>u>>v;
edg[u].push_back(v);
edg[v].push_back(u);
}
dfs(1);
for(ll i=1;i<=k;i++) ans[i]*=ans[i];
for(ll i=2;i<=n;i++){
ans[col[p[i]]]+=dp[i]*dp[i];
}
for(ll i=1;i<=k;i++)
cout<<n*n-ans[i]<<" \n"[i==k];
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
ll t; cin>>t; while(t--)
tc();
return 0;
}

View File

@@ -0,0 +1,38 @@
/// Gheorghies Alexandru
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;
bool isPrime(ll num){
if(num < 2)
return false;
for(ll divisor = 2; divisor * divisor <= num; divisor++){
if(num % divisor == 0)
return false;
}
return true;
}
int main()
{
ios_base::sync_with_stdio(false); cin.tie(0);
ll k;
cin>>k;
for(ll i = 0; i < k; i++){
ll n;
cin>>n;
if(n == 9)
cout<<"Second\n";
else if(n == 3)
cout<<"First\n";
else if(isPrime(n))
cout<<"Second\n";
else
cout<<"First\n";
}
return 0;
}

View File

@@ -0,0 +1,225 @@
/// Lungu Alexandru
#include <vector>
#include <set>
#include <map>
#include <iostream>
#include <cassert>
#include <cmath>
using namespace std;
void debug_out() { cerr << endl; }
template<class T> ostream& prnt(ostream& out, T v) { out << v.size() << '\n'; for(auto e : v) out << e << ' '; return out;}
template<class T> ostream& operator<<(ostream& out, vector <T> v) { return prnt(out, v); }
template<class T> ostream& operator<<(ostream& out, set <T> v) { return prnt(out, v); }
template<class T1, class T2> ostream& operator<<(ostream& out, map <T1, T2> v) { return prnt(out, v); }
template<class T1, class T2> ostream& operator<<(ostream& out, pair<T1, T2> p) { return out << '(' << p.first << ' ' << p.second << ')'; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << H; debug_out(T...);}
#define dbg(...) cerr << #__VA_ARGS__ << " ->", debug_out(__VA_ARGS__)
#define dbg_v(x, n) do{cerr<<#x"[]: ";for(int _=0;_<n;++_)cerr<<x[_]<<" ";cerr<<'\n';}while(0)
#define dbg_ok cerr<<"OK!\n"
#define ll long long
#define ld long double
#define ull unsigned long long
#define pii pair<int,int>
#define MOD 1000000007
#define zeros(x) x&(x-1)^x
#define fi first
#define se second
const long double PI = acos(-1);
const bool ASSERT = true;
const int NMAX = 2 * 1e5;
int n;
ll totalCost, dp[2 * NMAX + 5], cost[2 * NMAX + 5];
bool vis[2 * NMAX + 5];
vector<int> v[2 * NMAX + 5];
void check(ll partA, ll partB, ll& answer)
{
// dbg(partA, partB, answer);
if (ASSERT)
{
assert(partA > 0 && "Part A is 0");
assert(partB > 0 && "Part A is 0");
assert(partA + partB == totalCost && "Parts are invalid");
}
answer = min(answer, abs(partA - partB));
}
int dfs(int x, vector<int>& path, vector<int>& cycle, int prev = -1)
{
path.push_back(x);
vis[x] = 1;
for (int next : v[x])
{
if (next == prev) continue;
if (vis[next])
{
while (path.back() != next)
{
cycle.push_back(path.back());
path.pop_back();
}
cycle.push_back(next);
return 1;
}
if (dfs(next, path, cycle, x)) return 1;
}
path.pop_back();
return 0;
}
ll solve1_subtree(int root, int prev, ll& answer)
{
if (ASSERT)
{
assert(prev != root && "Invalid parameters solve1 (2)");
// assert(find(v[root].begin(), v[root].end(), prev) != v[root].end() && "Invalid cycle invariant in solve1 (3)");
}
ll subTreeCost = cost[root];
for (int next : v[root])
{
if (next == prev) continue;
subTreeCost += solve1_subtree(next, root, answer);
}
check(subTreeCost, totalCost - subTreeCost, answer);
dp[root] = subTreeCost;
return subTreeCost;
}
ll solve1(int x, int prev, int next)
{
if (ASSERT)
{
assert(prev != next && prev != x && x != next && "Invalid parameters solve1 (1)");
// assert(find(v[x].begin(), v[x].end(), prev) != v[x].end() && "Invalid cycle invariant in solve1 (1)");
// assert(find(v[x].begin(), v[x].end(), next) != v[x].end() && "Invalid cycle invariant in solve1 (2)");
}
dp[x] = cost[x];
if (v[x].size() == 2)
{
return 1e18;
}
ll answer = 1e18;
for (int root : v[x])
{
if (root == prev || root == next) continue;
ll subTreeCost = solve1_subtree(root, x, answer);
dp[x] += subTreeCost;
}
if (ASSERT)
{
assert(answer < 1e18 && "Invalid answer after solve1");
}
return answer;
}
int main()
{
ios::sync_with_stdio(false);
cin >> n;
ll answer = 1e18;
for (int i = 0; i < n; i++)
{
cin >> cost[i + 1];
totalCost += cost[i + 1];
}
for (int i = 0; i < n; i++)
{
int x, y;
cin >> x >> y;
v[x].push_back(y);
v[y].push_back(x);
}
vector<int> cycle;
vector<int> path;
dfs(1, path, cycle);
if (ASSERT)
{
assert(cycle.size() >= 3 && "Cycle size is too small");
for (int i = 0; i < cycle.size(); i++)
{
int now = cycle[i];
int next = i + 1 == cycle.size() ? cycle[0] : cycle[i + 1];
// assert(find(v[now].begin(), v[now].end(), next) != v[now].end() && "Cycle is invalid.");
}
}
// case 1: edge from cycle + edge from outer-ring
for (int i = 0; i < cycle.size(); i++)
{
int prev = i == 0 ? cycle[cycle.size() - 1] : cycle[i - 1];
int now = cycle[i];
int next = i + 1 == cycle.size() ? cycle[0] : cycle[i + 1];
answer = min(answer, solve1(now, prev, next));
}
// case 2: both edges from cycle
ll partA = dp[cycle[0]];
ll partB = totalCost - dp[cycle[0]];
int cycleSize = cycle.size();
for (int i = 0; i < cycleSize; i++)
{
cycle.push_back(cycle[i]);
}
for (int i = 0, j = 1; i < cycleSize;)
{
if (ASSERT)
{
assert(i < j && "Invalid iterators (a1)");
assert(j - i < cycleSize && "Invalid iterators (a2)");
assert(partA + partB == totalCost && "Invalid parts (a3)");
}
check(partA, partB, answer);
while (j - i < cycleSize - 1 && partA < partB)
{
partA += dp[cycle[j]];
partB -= dp[cycle[j]];
j++;
check(partA, partB, answer);
}
if (ASSERT)
{
assert(i < j && "Invalid iterators (b1)");
assert(j - i < cycleSize && "Invalid iterators (b2)");
assert(partA + partB == totalCost && "Invalid parts (b3)");
}
partA -= dp[cycle[i]];
partB += dp[cycle[i]];
i++;
if (i == j)
{
partA += dp[cycle[j]];
partB -= dp[cycle[j]];
j++;
}
}
if (ASSERT)
{
assert(answer != 1e18 && "Invalid answer");
}
cout << answer << '\n';
return 0;
}

View File

@@ -0,0 +1,87 @@
/// Gheorghies Alexandru
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pll;
typedef pair<ld,ld> pld;
const ll LGMAX=20,NMAX=5e5+5;
ll dp[NMAX],deg[NMAX];
vector<ll> edg[NMAX];
void tc(){
ll n,s=0,ans=LLONG_MAX;
cin>>n;
for(ll i=1;i<=n;i++) cin>>dp[i],s+=dp[i];
for(ll i=0;i<n;i++){
ll u,v; cin>>u>>v;
edg[u].push_back(v);
edg[v].push_back(u);
deg[u]++,deg[v]++;
}
queue<ll> q;
for(ll i=1;i<=n;i++)
if(deg[i]==1)
q.push(i);
while(!q.empty()){
deg[q.front()]=0;
ans=min(ans,abs(2*dp[q.front()]-s));
for(auto it : edg[q.front()]){
if(deg[it]>0){
dp[it]+=dp[q.front()];
deg[it]--;
if(deg[it]==1)
q.push(it);
}
}
q.pop();
}
vector<ll> cycle;
for(ll i=1;i<=n;i++){
if(deg[i]>0){
cycle={i};
deg[i]=0;
break;
}
}
while(1){
bool ok=0;
for(auto it : edg[cycle.back()]){
if(deg[it]){
cycle.push_back(it);
deg[it]=0;
ok=1;
break;
}
}
if(!ok) break;
}
n=cycle.size();
ll l=0,r=n-1,curr=0;
for(ll l=0;l<n;l++){
while(1){
ll nxt=cycle[(r+1)%n];
if((curr+dp[nxt])*2<=s)
curr+=dp[nxt],r=(r+1)%n;
else break;
}
ans=min(ans,abs(curr*2-s));
ll nxt=cycle[(r+1)%n];
ans=min(ans,abs((curr+dp[nxt])*2-s));
curr-=dp[cycle[l]];
}
cout<<ans;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif // ONLINE_JUDGE
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
//ll t; cin>>t; while(t--)
tc();
return 0;
}

View File

@@ -0,0 +1,143 @@
/// Cozma Tiberiu - Stefan
#include <bits/stdc++.h>
using namespace std;
int mod;
const int nmax = 100;
struct Mint
{
int val;
Mint(int _val = 0)
{
val = _val % mod;
}
Mint(long long _val)
{
val = _val % mod;
}
Mint operator+(Mint oth)
{
return val + oth.val;
}
Mint operator*(Mint oth)
{
return 1LL * val * oth.val;
}
Mint operator-(Mint oth)
{
return val - oth.val + mod;
}
};
Mint dp[nmax+1][nmax+1][nmax+1];
Mint sum[nmax+1][nmax+1][nmax+1];
Mint dp2[nmax+1][nmax+1];
int main()
{
cin.tie(nullptr)->sync_with_stdio(false);
int n;
cin >> n >> mod;
vector<int> a(n + 1);
for (int i = 1; i <= n; ++i)
{
cin >> a[i];
}
dp[1][1][0] = 1;
sum[1][1][0] = 1;
for (int i = 2; i <= n; ++i)
{
for (int j = 1; j <= i; ++j)
{
for (int k = 0; k < i; ++k)
{
dp[i][j][k] = sum[i - 1][i - 1][k] - sum[i - 1][j - 1][k];
if (k)
{
dp[i][j][k] = dp[i][j][k] + sum[i - 1][j - 1][k - 1];
}
sum[i][j][k] = sum[i][j - 1][k] + dp[i][j][k];
}
}
}
vector<vector<Mint>> pas(n + 1, vector<Mint>(n + 1));
pas[0][0] = 1;
for (int i = 1; i <= n; ++i)
{
for (int j = 0; j <= i; ++j)
{
if (j == 0 || j == i)
{
pas[i][j] = 1;
continue;
}
pas[i][j] = pas[i - 1][j] + pas[i - 1][j - 1];
}
}
set<int> vals;
for (int i = 1; i <= n; ++i)
{
vals.insert(i);
}
int cnt = 0;
vector<Mint> ans(n + 1);
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j < a[i]; ++j)
{
if (vals.find(j) != vals.end())
{
vals.erase(j);
cnt += vals.find(j + 1) != vals.end();
vector<int> left(1);
for (auto k : vals)
{
left.push_back(k);
}
int sz = 1;
vector<int> lg(1);
for (int i = 2; i < (int)left.size(); ++i)
{
if (left[i] == left[i - 1] + 1)
{
sz++;
}
else
{
lg.push_back(sz);
sz = 1;
}
}
lg.push_back(sz);
int m = (int)lg.size() - 1;
dp2[0][0] = 1;
int s = 0;
for (int x = 1; x <= m; ++x)
{
s += lg[x];
for (int y = 0; y < n; ++y)
{
dp2[x][y].val = 0;
for (int z = 0; z <= min(y, lg[x]); ++z)
{
dp2[x][y] = dp2[x][y] + dp2[x - 1][y - z] * pas[s][lg[x]] * sum[lg[x]][lg[x]][z];
}
}
}
for (int k = 0; k < n; ++k)
{
int need = k - cnt;
if (need >= 0)
{
ans[k] = ans[k] + dp2[m][need];
}
}
vals.insert(j);
cnt -= vals.find(j + 1) != vals.end();
}
}
vals.erase(a[i]);
cnt += vals.find(a[i] + 1) != vals.end();
}
for (int i = 0; i < n; ++i)
{
cout << ans[i].val << ' ';
}
}

View File

@@ -0,0 +1,46 @@
/// Banu Denis - dp
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define ull unsigned long long
#define pii pair<int,int>
#define MOD 1000000007
#define zeros(x) x&(x-1)^x
#define fi first
#define se second
#define NMAX 500005
const long double PI = acos(-1);
int n,t, dp[2][4];
string s;
int main(){
ios::sync_with_stdio(false);
cin >> t;
while (t--) {
cin >> n;
cin >> s;
memset(dp,0,sizeof(dp));
int crt = 1;
for (int i=0;i<s.size();i++) {
crt = (crt^1);
if (s[i] == '0') {
dp[crt][0] = max(dp[crt^1][0] + 1, dp[crt^1][2] + 1);
dp[crt][1] = dp[crt^1][1];
dp[crt][2] = max(dp[crt^1][3] + 1, dp[crt^1][2]);
dp[crt][3] = dp[crt^1][3];
} else {
dp[crt][0] = dp[crt^1][0];
dp[crt][1] = max(dp[crt^1][0] + 1, dp[crt^1][1]);
dp[crt][2] = dp[crt^1][2];
dp[crt][3] = max(dp[crt^1][3] + 1, dp[crt^1][1] + 1);
}
}
cout << n - max({dp[crt][0], dp[crt][1], dp[crt][2], dp[crt][3]}) << '\n';
}
return 0;
}

View File

@@ -0,0 +1,38 @@
/// Gheorghies Alexandru - greedy solution
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pll;
typedef pair<ld,ld> pld;
const ll LGMAX=20,NMAX=3e5+5;
void tc(){
ll n,ans=0,len=0;
string s;
cin>>n>>s;
for(ll i=0;i<n;i++){
if(i && s[i]!=s[i-1]) len++;
else{
ans+=(len+1)/3;
len=0;
}
}
ans+=(len+1)/3;
cout<<ans<<'\n';
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif // ONLINE_JUDGE
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
ll t; cin>>t; while(t--)
tc();
return 0;
}

View File

@@ -0,0 +1,49 @@
/// Gheorghies Alexandru - difference arrays solution
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pll;
typedef pair<ld,ld> pld;
const ll LGMAX=20,NMAX=2e5+5;
vector<ll> smen[NMAX]; /// smen (smenul lui mars) is the romanian term for difference arrays
ll ans[NMAX];
pll v[NMAX];
void tc(){
ll n,k;
cin>>n>>k;
for(ll i=0;i<n;i++){
cin>>v[i].first;
v[i].second=i;
}
sort(v,v+n);
ll r=0;
for(ll l=0;l<n;l++){
while(r<n && v[r].first-v[l].first<=k) r++;
smen[l].push_back(r-l);
smen[r].push_back(l-r); // -(r-l)
}
multiset<ll> cnd;
for(ll i=0;i<n;i++){
for(auto it : smen[i]){
if(it>0) cnd.insert(it);
else cnd.erase(cnd.lower_bound(-it));
}
ans[v[i].second]=*cnd.rbegin();
}
for(ll i=0;i<n;i++)
cout<<ans[i]<<' ';
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif // ONLINE_JUDGE
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
//ll t; cin>>t; while(t--)
tc();
return 0;
}

View File

@@ -0,0 +1,45 @@
/// Banu Denis - monotone stack solution
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define ull unsigned long long
#define pii pair<int,int>
#define MOD 1000000007
#define zeros(x) x&(x-1)^x
#define fi first
#define se second
#define NMAX 200005
const long double PI = acos(-1);
int n,k,ans[NMAX];
deque<pii> bst;
vector<pair<int, int> > v;
int main(){
ios::sync_with_stdio(false);
cin >> n >> k;
for (int i=1;i<=n;i++) {
int x;
cin >> x;
v.push_back({x, i});
}
sort(v.begin(), v.end());
v.push_back({int(2e9) + 3, 0});
int st = 0, dr = -1;
while (st < n) {
while (v[st].first + k >= v[dr+1].first) dr++;
while (!bst.empty() && bst.back().first <= dr-st+1) bst.pop_back();
bst.push_back({dr-st+1, dr});
while (!bst.empty() && bst.front().second < st) bst.pop_front();
ans[v[st].second] = bst.front().first;
st++;
}
for (int i=1;i<=n;i++) cout << ans[i] << ' ';
cout << '\n';
return 0;
}

View File

@@ -0,0 +1,32 @@
/// Gheorghies Alexandru
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;
const ll NMAX=3e5+5,LGMAX=17;
ll v[NMAX];
map<ll,ll> fr,prv;
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
ll n,ans=0;
cin>>n;
for(ll i=1;i<=n;i++){
cin>>v[i];
v[i]=abs(v[i]);
++fr[v[i]];
if(fr[v[i]]%2) v[i]=-v[i];
}
for(ll i=1;i<=n;i++){
ans+=(i-prv[v[i]])*(n-i+1);
prv[v[i]]=i;
}
cout<<ans;
return 0;
}

View File

@@ -0,0 +1,17 @@
/// Gheorghies Alexandru
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
void tc(){
ll n,m;
cin>>n>>m;
cout<<(n*m*2+2)/3<<'\n';
}
int main()
{
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
ll t; cin>>t; while(t--)
tc();
return 0;
}

View File

@@ -0,0 +1,58 @@
/// Gheorghies Alexandru
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;
const ll NMAX=1e5+5;
ll primes[NMAX],divisors[NMAX],cntp=0,cntdiv=0;
bool sieve[NMAX];
void AddDivisors(ll base, ll exponent){
ll n=cntdiv,curr=1;
for(ll j=1;j<=exponent;j++){
curr*=base;
for(ll i=0;i<n;i++){
divisors[cntdiv++]=divisors[i]*curr;
}
}
}
map<ll,ll> cache;
void tc(){
ll n,cpy;
cin>>n;
if(cache.count(n)){
cout<<cache[n]<<'\n';
return;
}
cpy=n;
divisors[0]=1;
cntdiv=1;
for(ll i=0;primes[i]*primes[i]<=n;i++){
ll e=0;
while(n%primes[i]==0) n/=primes[i],e++;
if(e) AddDivisors(primes[i],e*2);
}
if(n) AddDivisors(n,2);
ll ans=LLONG_MAX;
for(ll i=0;i<cntdiv;i++){
if(divisors[i]>cpy){
ans=min(ans,divisors[i]);
}
}
cache[cpy]=ans-cpy;
cout<<ans-cpy<<'\n';
}
int main()
{
for(ll i=2;i<NMAX;i++){
if(!sieve[i]){
primes[cntp++]=i;
for(ll j=i*i;j<NMAX;j+=i)
sieve[j]=1;
}
}
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
ll t; cin>>t; while(t--)
tc();
return 0;
}

View File

@@ -0,0 +1,26 @@
/// Gheorghies Alexandru
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;
const ll NMAX=1e5+5;
void tc(){
ll n;
bool good=1;
cin>>n;
for(ll i=1;i<=n;i++){
ll x; cin>>x;
if(x%2!=i%2) good=0;
}
cout<<(good?"YES":"NO")<<'\n';
}
int main()
{
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
ll t; cin>>t; while(t--)
tc();
return 0;
}

View File

@@ -0,0 +1,100 @@
/// Gheorghies Alexandru
/// this problem is really painful to implement
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;
const ll NMAX=1e5+5,NSQRMAX=1.6e7+5,MOD=1e9+7;
ll b[NMAX],actualnxt[NMAX],actualprv[NMAX],n,m;
ll ans[NSQRMAX],tmpans[NMAX];
ll prv[NMAX],nxt[NMAX],last[NMAX];
ll f(ll i1, ll i2, ll j){
return m*((n*(n+1)-(n-i1)*(n-i1+1))/2+i2-i1)+j;
}
void Push_back(ll p){
if(last[b[p]]==-1)
prv[p]=-1,nxt[p]=-1;
else
nxt[last[b[p]]]=p,prv[p]=last[b[p]],nxt[p]=-1;
last[b[p]]=p;
}
void Erase(ll p){
if(nxt[p]==-1 && prv[p]==-1){
last[b[p]]=-1;
return;
}
if(prv[p]!=-1) nxt[prv[p]]=nxt[p];
if(nxt[p]!=-1) prv[nxt[p]]=prv[p];
nxt[p]=prv[p]=-1;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
for(ll i=0;i<NMAX;i++) last[i]=-1;
ll q;
bool flipped=0;
cin>>n>>m;
if(n<=m){
for(ll i=0;i<n;i++)
for(ll j=0;j<m;j++)
cin>>b[i*m+j];
}
else{
for(ll i=0;i<n;i++)
for(ll j=0;j<m;j++)
cin>>b[j*n+i];
swap(n,m);
flipped=1;
}
for(ll i1=0;i1<n;i1++){
for(ll j=0;j<m;j++){
for(ll i2=i1;i2<n;i2++){
Push_back(i2*m+j);
}
}
for(ll i2=n-1;i2>=i1;i2--){
for(ll j=0;j<m;j++){
ll p=i2*m+j;
actualprv[p]=(prv[p]==-1?-1:prv[p]%m);
actualnxt[p]=(nxt[p]==-1?m:nxt[p]%m);
if(actualprv[p]==j || actualnxt[p]==j)
actualprv[p]=actualnxt[p]=j;
}
for(ll j=0;j<m;j++) Erase(i2*m+j);
}
for(ll j=0;j<=m;j++) tmpans[j]=m;
for(ll i2=i1;i2<n;i2++){
for(ll j=m-1;j>=0;j--){
ll p=i2*m+j;
if(actualprv[p]>=0)
tmpans[actualprv[p]]=min(tmpans[actualprv[p]],j);
if(actualnxt[p]<m)
tmpans[j]=min(tmpans[j],actualnxt[p]);
}
for(ll j=m-1;j>=0;j--){
tmpans[j]=min(tmpans[j],tmpans[j+1]);
ans[f(i1,i2,j)]=tmpans[j]-1;
}
}
}
cin>>q;
while(q--){
ll a,b,c,d;
cin>>a>>b>>c>>d;
if(flipped) swap(a,b),swap(c,d);
a--,b--,c--,d--;
if(ans[f(a,c,b)]>=d)
cout<<"YES\n";
else
cout<<"NO\n";
}
return 0;
}

View File

@@ -0,0 +1,168 @@
/// Ionita Alexandru
#include <bits/stdc++.h>
#define st first
#define nd second
using namespace std;
const long long N = 500100;
long long n, aib[N], a[N], prev, f[N];
string s;
const long long MOD = 1e9 + 7;
long long powMod(long long a, long long exp) {
long long b = a, res = 1;
while(exp) {
if(exp % 2) res *= b;
b = 1LL * b * b % MOD;
res %= MOD;
exp /= 2;
}
return res;
}
long long invMod(long long x) {
return powMod(x, MOD - 2);
}
long long comb(long long n, long long k) {
if(n < k) return 0;
if(n == k) return 1;
if(k == 0) return 1;
return f[n] * invMod(1LL * f[k] * f[n - k] % MOD) % MOD;
}
long long lsb(long long x) {
return x & (-x);
}
void upd(long long pos, long long val) {
for(; pos < N; pos += lsb(pos)) {
aib[pos] += val;
}
}
long long que(long long pos) {
long long ret = 0;
for(; pos; pos -= lsb(pos)) {
ret += aib[pos];
}
return ret;
}
long long que(long long l, long long r) {
if(l == r && r == 0) return 0;
if(l == 1) return que(r);
return que(r) - que(l - 1);
}
long long compute(long long pos, long long mn, long long mx) {
long long prev = pos;
long long ans = 1;
for(long long i = pos + 1; i <= n; i++) {
if(a[i] == 0) continue;
if(a[prev] > a[i]) {
// minim
long long k = mn - a[i] - 1;
long long lg = i - prev - 1;
ans = ans * comb(lg, k) % MOD;
mn = a[i];
mx += lg - k;
if(mx > n) return 0; // todo testcase
upd(a[i], -1);
} else {
long long k = a[i] - mx - 1;
long long lg = i - prev - 1;
ans = ans * comb(lg, k) % MOD;
mx = a[i];
mn -= lg - k;
if(mn < 1) return 0;
upd(a[i], -1);
}
long long banned = que(mn, mx);
if(banned != 1)
return 0;
prev = i;
}
return ans;
}
void solve() {
long long mn, mx, ans1 = 0, ans2 = 0, banned;
cin >> n;
f[0] = 1;
for(long long i = 1; i <= n + 1; i++)
if(que(i, i) == 1)
upd(i, -1);
for(long long i = 1; i <= n; i++) {
f[i] = f[i - 1] * i % MOD;
cin >> a[i];
if(a[i])
upd(a[i], 1);
}
long long prev = 0;
n++;
a[n] = n;
f[n] = f[n - 1] * n % MOD;
upd(a[n], 1);
for(long long i = 1; i <= n; i++) {
if(a[i]) {
mn = a[i];
mx = a[i] + i - 1;
if(mx <= n) {
banned = que(mn, mx);
if(banned == 1) {
ans1 = powMod(2, max(i - 2, 0LL));
ans1 = ans1 * compute(i, mn, mx) % MOD;
}
}
if(i == 1) break;
// memset(aib, 0, sizeof aib);
for(long long j = 1; j <= n; j++)
if(a[j] && que(a[j], a[j]) == 0)
upd(a[j], 1);
mx = a[i];
mn = a[i] - i + 1;
if(mn > 0) {
banned = que(mn, mx);
if(banned == 1) {
ans2 = powMod(2, max(i - 2, 0LL));
ans2 = ans2 * compute(i, mn, mx) % MOD;
}
}
break;
}
}
cout << (ans1 + ans2) % MOD << '\n';
}
int main() {
int t;
ios_base::sync_with_stdio(false);
cin >> t;
for(int i = 1; i <= t; i++) {
solve();
}
}