洛谷P3901 数列找不同(莫队水题)
重温下手感,判断区间是否全是不同的数字有两种做法,一个长度为len的区间不同的数字,参见HH的项链,一种是区间众数,参见蒲公英,是水题没错了。明天搞数据库,然后继续自己的gre和训练计划
#include <bits/stdc++.h> using namespace std; #define limit (10005 + 5)//防止溢出 #define INF 0x3f3f3f3f #define inf 0x3f3f3f3f3f #define lowbit(i) i&(-i)//一步两步 #define EPS 1e-6 #define FASTIO ios::sync_with_stdio(false);cin.tie(0); #define ff(a) printf("%d\n",a ); #define pi(a,b) pair<a,b> #define rep(i, a, b) for(ll i = a; i <= b ; ++i) #define per(i, a, b) for(ll i = b ; i >= a ; --i) #define MOD 998244353 #define traverse(u) for(int i = head[u]; ~i ; i = edge[i].next) #define FOPEN freopen("C:\\Users\\tiany\\CLionProjects\\acm_01\\data.txt", "rt", stdin) #define FOUT freopen("C:\\Users\\tiany\\CLionProjects\\acm_01\\dabiao.txt", "wt", stdout) #define debug(x) cout<<x<<endl typedef long long ll; typedef unsigned long long ull; inline ll read(){ ll sign = 1, x = 0;char s = getchar(); while(s > '9' || s < '0' ){if(s == '-')sign = -1;s = getchar();} while(s >= '0' && s <= '9'){x = (x << 3) + (x << 1) + s - '0';s = getchar();} return x * sign; }//快读 void write(ll x){ if(x < 0) putchar('-'),x = -x; if(x / 10) write(x / 10); putchar(x % 10 + '0'); } int n, m, k; struct node{ int l, r, qid, blo; bool operator<(const node &rhs)const{ if(blo ^ rhs.blo)return l < rhs.l; return blo & 1 ? r < rhs.r : rhs.r < r; } }query[limit]; ll cnt[limit],a[limit],sum[limit]; int len[limit]; ll ans[limit]; ll res; void add(int x){ --sum[cnt[a[x]]]; ++cnt[a[x]]; res = max(res, cnt[a[x]]); ++sum[cnt[a[x]]];//加完之后 } void del(int x){ --sum[cnt[a[x]]]; if(res && !sum[cnt[a[x]]])--res; --cnt[a[x]]; ++sum[cnt[a[x]]]; } int main() { #ifdef LOCAL FOPEN; #endif n = read(), m = read(); rep(i ,1,n)a[i] = read(); int block = int(sqrt(n >= 3 ? n * (2.0 / 3) : n));//分块 rep(i, 1, m){ query[i].l = read(), query[i].r = read(), query[i].qid = i; query[i].blo = query[i].l / block;//分成块 } sort(query + 1, query + 1 + m); int l = 0 , r = 0; res = 0; rep(i ,1, m){ while(l < query[i].l)del(l++);//缩进 while(l > query[i].l)add(--l); while(r < query[i].r)add(++r); while(r > query[i].r)del(r--); ans[query[i].qid] = res; len[query[i].qid] = r - l + 1; } rep(i ,1,m)puts(ans[i] == 1 ? "Yes" : "No"); return 0; }
天才选手zerol的主页:https://zerol.me/
|
WeepingDemon的个人主页:https://weepingdemon.gitee.io/blog/