QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#336183 | #964. Excluded Min | bachbeo2007 | WA | 3ms | 17956kb | C++20 | 5.4kb | 2024-02-24 13:48:49 | 2024-02-24 13:48:50 |
Judging History
answer
// Judges with GCC >= 12 only needs Ofast
// #pragma GCC optimize("O3,no-stack-protector,fast-math,unroll-loops,tree-vectorize")
// MLE optimization
// #pragma GCC optimize("conserve-stack")
// Old judges
// #pragma GCC target("sse4.2,popcnt,lzcnt,abm,mmx,fma,bmi,bmi2")
// New judges. Test with assert(__builtin_cpu_supports("avx2"));
// #pragma GCC target("avx2,popcnt,lzcnt,abm,bmi,bmi2,fma,tune=native")
// Atcoder
// #pragma GCC target("avx2,popcnt,lzcnt,abm,bmi,bmi2,fma")
/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
- insert(x),erase(x)
- find_by_order(k): return iterator to the k-th smallest element
- order_of_key(x): the number of elements that are strictly smaller
*/
#include<bits/stdc++.h>
using namespace std;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
uniform_real_distribution<> pp(0.0,1.0);
#define int long long
#define ld long double
#define pii pair<int,int>
#define piii pair<int,pii>
#define mpp make_pair
#define fi first
#define se second
const int inf=1e18;
const int mod=998244353;
const int maxn=500005;
const int bl=650;
const int maxs=655;
const int maxm=200005;
const int maxq=1000005;
const int maxl=25;
const int maxa=500000;
const int root=3;
int power(int a,int n){
int res=1;
while(n){
if(n&1) res=res*a%mod;
a=a*a%mod;n>>=1;
}
return res;
}
const int iroot=power(3,mod-2);
const int base=101;
struct Query{
int L,R,id;
bool operator<(Query o){
if(L!=o.L) return L<o.L;
else return R>o.R;
}
}qq[maxn];
vector<int> edge[maxn];
int par[maxn],f[maxn],revf[maxn],cnt;
void dfs(int u){
f[u]=++cnt;revf[cnt]=u;
for(int v:edge[u]) dfs(v);
}
int n,q,a[maxn];
int res[maxn];
vector<int> pos[maxn];
namespace BIT{
int bit[maxn];
void update(int x,int val){
for(int i=x;i<=n;i+=(i&(-i))) bit[i]+=val;
}
int query(int x){
int ans=0;
for(int i=x;i>=1;i-=(i&(-i))) ans+=bit[i];
return ans;
}
}
namespace Segtree{
int tree[4*maxn],lazy[4*maxn];
void getnew(int id,int val){
lazy[id]+=val;
tree[id]-=val;
}
void pushdown(int id){
if(!lazy[id]) return;
getnew(id<<1,lazy[id]);
getnew(id<<1|1,lazy[id]);
lazy[id]=0;
}
void change(int l,int r,int id,int p,int val){
if(l==r){
tree[id]=val;
return;
}
pushdown(id);
int mid=(l+r)>>1;
if(p<=mid) change(l,mid,id<<1,p,val);
else change(mid+1,r,id<<1|1,p,val);
tree[id]=max(tree[id<<1],tree[id<<1|1]);
}
void update(int l,int r,int id,int tl,int tr){
if(tr<l || r<tl) return;
if(tl<=l && r<=tr){
getnew(id,1);
return;
}
pushdown(id);
int mid=(l+r)>>1;
update(l,mid,id<<1,tl,tr);update(mid+1,r,id<<1|1,tl,tr);
tree[id]=max(tree[id<<1],tree[id<<1|1]);
}
int query(int l,int r,int id,int val){
if(l==r){
tree[id]=0;
return l;
}
pushdown(id);
int mid=(l+r)>>1,ans=-1;
if(tree[id<<1]>val) ans=query(l,mid,id<<1,val);
else ans=query(mid+1,r,id<<1|1,val);
tree[id]=max(tree[id<<1],tree[id<<1|1]);
return ans;
}
}
void solve(){
cin >> n >> q;
for(int i=1;i<=n;i++){
cin >> a[i];
pos[a[i]].push_back(i);
}
for(int i=1;i<=q;i++){
cin >> qq[i].L >> qq[i].R;
qq[i].id=i;
if(i==28) cout << qq[i].L << ' ' << qq[i].R << '\n';
}
sort(qq+1,qq+q+1);
int p=0;qq[0].L=1;qq[0].R=n;
for(int i=1;i<=q;i++){
while(qq[i].R>qq[p].R) p=par[p];
//cout << p << ' ' << i << '\n';
edge[p].push_back(i);par[i]=p;p=i;
}
for(int v:edge[0]) dfs(v);
for(int i=1;i<=n;i++) BIT::update(i,1);
set<pii> sL,sR;
for(int v:edge[0]){
sL.insert({qq[v].L,f[v]});
sR.insert({qq[v].R,f[v]});
Segtree::change(1,q,1,f[v],qq[v].R-qq[v].L+1);
}
for(int i=maxa;i>=0;i--){
//cout << Segtree::tree[1] << ' ' << i << '\n';
while(Segtree::tree[1]>i){
int u=revf[Segtree::query(1,q,1,i)];
res[qq[u].id]=i+1;
sL.erase({qq[u].L,f[u]});
sR.erase({qq[u].R,f[u]});
//cout << "query " << i << ' ' << u << '\n';
for(int v:edge[u]){
sL.insert({qq[v].L,f[v]});
sR.insert({qq[v].R,f[v]});
//cout << "add " << f[v] << ' ' << BIT::query(qq[v].R)-BIT::query(qq[v].L-1) << '\n';
Segtree::change(1,q,1,f[v],BIT::query(qq[v].R)-BIT::query(qq[v].L-1));
}
}
for(int id:pos[i]){
BIT::update(id,-1);
auto it=sL.upper_bound({id,inf});
int r=(it==sL.end()?q:it->se-1);
it=sR.lower_bound({id,0});
int l=(it==sR.end()?q+1:it->se);
//cout << id << ' ' << l << ' ' << r << '\n';
if(l<=r) Segtree::update(1,q,1,l,r);
}
}
for(int i=1;i<=q;i++) cout << res[i] << '\n';
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);cout.tie(NULL);
int test=1;//cin >> test;
while(test--) solve();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 17956kb
input:
3 3 0 0 2 1 3 2 3 3 3
output:
3 1 0
result:
ok 3 number(s): "3 1 0"
Test #2:
score: 0
Accepted
time: 0ms
memory: 15920kb
input:
3 2 1 2 2 1 2 1 3
output:
0 3
result:
ok 2 number(s): "0 3"
Test #3:
score: 0
Accepted
time: 0ms
memory: 10012kb
input:
10 10 3 0 3 3 9 7 9 6 0 7 3 3 9 9 4 6 1 9 3 4 2 4 7 10 3 7 5 7 2 7
output:
0 1 0 5 0 1 1 0 0 1
result:
ok 10 numbers
Test #4:
score: 0
Accepted
time: 0ms
memory: 9720kb
input:
10 10 3 0 0 0 5 1 1 1 2 1 1 2 8 8 5 7 1 7 2 2 1 5 5 6 4 6 3 10 6 8
output:
1 0 2 7 1 4 0 2 8 3
result:
ok 10 numbers
Test #5:
score: -100
Wrong Answer
time: 3ms
memory: 9848kb
input:
100 100 15 82 7 39 63 55 64 99 71 63 32 99 67 94 3 43 15 75 45 55 53 4 35 36 15 40 82 20 62 43 6 83 27 95 27 45 98 44 35 98 39 7 17 32 76 7 40 16 40 63 13 8 22 27 11 12 61 14 19 45 100 97 90 88 22 59 25 63 4 25 65 16 22 92 84 44 99 66 89 26 93 97 35 97 92 1 32 40 97 97 78 43 7 12 23 53 61 74 33 90 1...
output:
6 80 68 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 19 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 28 0 0 0 0 0 0 0 0 0 0 48 0 0 0 0 0 0 0 0 0 0 0 0 0 28 0 0 0 19 0 0 0 0 0 0 0 0 0 0 23 0 0 0 0 0 0 0 0
result:
wrong answer 1st numbers differ - expected: '68', found: '6'