4 条题解
-
1
首先 ,我们能自然推导出一个结论:若一个 能作为答案,那么对于 都能满足“至少有 篇题解被点赞了不少于 次”。
这是什么?单调性!
单调性?二分答案!
那么对于任意区间 ,都可以考虑使用一些方式存储其中的所有信息,然后在其中二分选择可行解。
然后 ,问题来到了选择一种合适的方式存储区间信息,考虑我们要维护的内容:任意区间 的所有数的有序权值——这是因为当足够多的更大权值存在,使得当前枚举的权值小于等于更大权值的个数,那么就可以更新答案。
权值,有序……权值线段树!(而且权值线段树的左儿子和右儿子就相当于自动二分了)
最后 ,我们又不能强行建立对每一个区间 的权值线段树……但是我们转变一下思维,若指定建立 棵权值线段树,那么对于 的权值线段树,范围为 的权值线段树就相当于它的历史版本。
所以我们只需要建立主席树(可持久化线段树),每次查询为 和 的两颗就可以了。
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int N=200100; template< typename T > inline void read(T &x){ char c=getchar();x=0;int f=0; for(;!isdigit(c);c=getchar()) f|=(c=='-'); for(;isdigit(c);c=getchar()) x=((x<<3)+(x<<1)+(c^48)); x=f?-x:x; } template< typename T > inline void write(T x){ if(x<0) putchar('-'),x=-x; if(x>9) write(x/10); putchar(x%10^48); } int n,q,tot,rt[N]; struct Mtr{ int lch,rch,sum; }tr[N<<8];//权值主席树 int modify(int las,int val,int l=1,int r=N){ int root=++tot; tr[root].lch=tr[las].lch;//访问历史 tr[root].rch=tr[las].rch; tr[root].sum=tr[las].sum+1; if(l==r) return root; int mid=(l+r)>>1; if(val<=mid) tr[root].lch=modify(tr[las].lch,val,l,mid); else tr[root].rch=modify(tr[las].rch,val,mid+1,r); return root; } int query(int llim,int rlim,int l=1,int r=N,int his=0){ if(l==r) return l; int drt=tr[tr[rlim].rch].sum-tr[tr[llim].rch].sum; int mid=(l+r)>>1; if(his+drt>mid) return query(tr[llim].rch,tr[rlim].rch,mid+1,r,his); else return query(tr[llim].lch,tr[rlim].lch,l,mid,his+drt); } int main(){ freopen("yy.in","r",stdin); freopen("yy.out","w",stdout); read(n);read(q); for(int i=1,x;i<=n;++i) read(x),rt[i]=modify(rt[i-1],x); for(int i=1,l,r;i<=q;++i) read(l),read(r),write(query(rt[l-1],rt[r])),putchar('\n'); return 0; }还不错的主席树(线段树)二分的练习题。
-
0
莫队题解:
第一眼:区间处理问题。
第二眼:没有强制在线。
第三眼:不带修改
第四眼:数据
综上所述:莫队显然是较好的比赛选择(好写不易错) 莫队的思想不再赘述,想必大家都已经了解
回到题目,可以发现不仅查询数 ,而且数组中的任意元素都 ,于是在莫队的加减操作中,可以通过桶排序来实现均摊 处理(证明请见下文)
同时,本题推荐使用奇偶排序进行常数优化,本人优化后的代码以最高 通过最大测试点。不保证没有优化或者常数过大的代码通过本题
以下为本人代码:
#include<bits/stdc++.h> using namespace std; #define debug(x); cerr<<__LINE__<<" : "<<#x<<" "<<x<<"\n"; typedef long long ll; typedef pair<int, int> PII; typedef array<int, 3> ARI; const int N=2e5+5,mod=1e9+7,base=448; int s[N], sum[N], ans[N]; int now, cnt, L = 1, R = 0; void add(int id){ sum[s[id]] ++; if(s[id] >= now){cnt ++;} while(cnt - sum[now] >= now + 1){cnt -= sum[now]; now ++;} } void del(int id){ sum[s[id]] --; if(s[id] >= now){cnt --;} while(cnt < now){cnt += sum[now - 1]; now --;} } int main(){ freopen("yy.in", "r", stdin); freopen("yy.out", "w", stdout); ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, q; cin>>n>>q; for(int i=1;i<=n;i++){cin>>s[i];} vector<ARI> Q(q); for(int i=0;i<q;i++){cin>>Q[i][1]>>Q[i][2]; Q[i][0] = i;} sort(Q.begin(), Q.end(), [&](const ARI &a, const ARI &b){ if(a[1] / base != b[1] / base){return (a[1] / base) < (b[1] / base);} else{return ((a[1] / base) % 2 == 0 ? a[2] < b[2] : a[2] > b[2]);} }); for(int i=0;i<q;i++){ int l = Q[i][1], r = Q[i][2], id = Q[i][0]; while(R < r){add(++ R);} while(l < L){add(-- L);} while(R > r){del(R --);} while(l > L){del(L ++);} ans[id] = now; } for(int i=0;i<q;i++){cout<<ans[i]<<"\n";} return 0; }时间复杂度证明: 对于每一个加入或删除的点,其最多只会影响一个位置的值,一个点进行多个上越或下行最多只能释放其前面所积累的势能,故莫队的加入和删除操作均摊时间复杂度 ,总体时间复杂度 ,证毕。
-
0
值域线段树板子 判据就是假设当前答案为 , 已经大于的数有 个 。若满足 , 表明答案还可以更大 ,取右区间 ,反之 ,取做区间 。过程中 ,需要在每次更新已经大于的个数 。
#include<bits/stdc++.h> char ST; using namespace std; #define open(x) freopen(x ".in", "r", stdin);freopen(x ".out", "w", stdout); #define pb push_back #define L(i,a,b) for(int i = (a) ; i <= (b) ; ++i) #define int long long inline int read(){int f=1;int x=0;char c=getchar();while(c<'0'||c>'9'){if(c=='-'){f=-f;}c=getchar();}while(c>='0'&&c<='9'){x=(x<<1)+(x<<3)+(c^48);c=getchar();}return x*f;} inline void wr(int x){if(x<0){putchar('-');x=-x;}if(x>9){wr(x/10);}putchar(x%10+'0');} const int MAXN = 1e6 + 5; struct node{ int ls , rs; int num; }t[MAXN << 2]; int n,q; int a[MAXN]; int root[MAXN]; int cnt = 0; inline void change(int &p, int lst,int l , int r , int pos){ p = ++cnt; t[p] = t[lst]; t[p].num++; if(l == r) return; int mid = l + r >> 1; if(pos <= mid) change(t[p].ls,t[lst].ls,l,mid,pos); if(pos > mid) change(t[p].rs,t[lst].rs,mid+1,r,pos); return; } inline int ask(int x,int y,int l,int r,int v){ if(l == r) return l; int now = t[t[y].rs].num - t[t[x].rs].num; int mid = l + r >> 1; if(v + now > mid) return ask(t[x].rs,t[y].rs,mid + 1,r,v); else return ask(t[x].ls,t[y].ls,l,mid,v + now); } signed main(){ open("yy"); n = read() , q = read(); L(i,1,n) a[i] = read(); L(i,1,n) change(root[i] , root[i-1] , 1 , 200001 , a[i]); L(i,1,q){ int x = read(); int y = read(); int ans = ask(root[x - 1] , root[y] , 1 , 200001 , 0); wr(ans); printf("\n"); } return 0; } -
0
先看题,第一眼看到:“他只发表了第 篇至第 篇题解” 和 “有 个疑问” ,再回想一下~当时背诵~ OI Wiki 里对莫队的介绍:“莫队算法可以解决一类离线区间询问问题,适用性极为广泛。” ,一眼丁真,鉴定为 普通莫队 。但是类似于线段树(主席树)、二分答案、树状数组(?)等做法应当也是可行的。
如果您还不会莫队算法,请前往 普通莫队算法 - OI Wiki 进行自学,再阅读代码。
写一个巨板的莫队,改吧改吧就能用了。需要注意的地方写在注释里了。
时间复杂度
#include <bits/stdc++.h> #define int long long #define inf 0x7fffffffffffffff using namespace std; #define cin is #define cout os ifstream is("yy.in", ios::in); ofstream os("yy.out", ios::out); const int N = 202020; int n, q, block, a[N], cnt[N], ans[N]; struct node { int l, r, id; bool operator<(const node that) { return ((l / block) != (that.l / block)) ? (l < that.l) : (((l / block) & 1) ? (r < that.r) : (r > that.r)); } } query[N]; signed main( ) { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cin >> n >> q; block = (int)(ceil(powl(n, 0.5)));// 令块长等于 sqrt(n) for (int i = 1; i <= n; ++i) { cin >> a[i]; } for (int i = 1; i <= q; ++i) { cin >> query[i].l >> query[i].r; query[i].id = i; } sort(query + 1, query + q + 1); // 对于区间 [l,r] , 以 l 所在块的编号为第一关键字, r 为第二关键字从小到大排序。 int l = query[1].l, r = query[1].l - 1, k = 0, t = 0; for (int i = 1; i <= q; ++i) { // 背的莫队板子,改改就能用 while (l > query[i].l) { l--; cnt[a[l]]++; t += ((a[l] >= k) ? 1 : 0); } while (r < query[i].r) { r++; cnt[a[r]]++; t += ((a[r] >= k) ? 1 : 0); } while (l < query[i].l) { t -= ((a[l] >= k) ? 1 : 0); cnt[a[l]]--; l++; } while (r > query[i].r) { t -= ((a[r] >= k) ? 1 : 0); cnt[a[r]]--; r--; } // 由于对 l、r 的值的更改,4个while是不能随意调换位置的。 // 还需要注意的是上面的几个 l--、r++、l++、r-- 的位置。 // 当然你也可以在 l、r 调用的时候顺便更改, // 但请一定要注意自增、自减运算符的位置。 while (t - cnt[k] > k) { t -= cnt[k]; k++; } while (t < k) { k--; t += cnt[k]; } ans[query[i].id] = k; // 不要尝试在这里直接输出答案。 } for (int i = 1;i <= q;++i) { cout << ans[i] << endl; } return 0; }
- 1
信息
- ID
- 14
- 时间
- 1200ms
- 内存
- 512MiB
- 难度
- 6
- 标签
- 递交数
- 37
- 已通过
- 14
- 上传者
