ACM模板库

ACM模板库


莫队

<pre><code class="language-cpp">#include &lt;cstdio&gt; #include &lt;cstring&gt; #include &lt;cmath&gt; #include &lt;algorithm&gt; using namespace std; const int maxn = 1010000; int n, m; int num[maxn]; namespace moQueue{ const int maxnb = sqrt(maxn) + 1; struct Query{ int l; int r; int id; Query(int _l = 0, int _r = 0, int _id = 0){ l = _l; r = _r; id = _id; } }; int cnt[maxn], belong[maxn]; int size, bnum;//一个块大小和块的数量 int now, ans[maxn]; Query q[maxn]; int cmp(Query a, Query b) { return (belong[a.l] ^ belong[b.l]) ? belong[a.l] &lt; belong[b.l] : ((belong[a.l] &amp; 1) ? a.r &lt; b.r : a.r &gt; b.r); } void add(int pos) { if(!cnt[num[pos]]) ++now; ++cnt[num[pos]]; } void del(int pos) { --cnt[num[pos]]; if(!cnt[num[pos]]) --now; } void init(){ size = sqrt(n); bnum = ceil((double)n / size); for(int i = 1; i &lt;= bnum; ++i){ for(int j = (i - 1) * size + 1; j &lt;= i * size; ++j) { belong[j] = i; } } } } int main() { scanf("%d", &amp;n); for(int i = 1; i &lt;= n; ++i) scanf("%d", &amp;num[i]); scanf("%d", &amp;m); for(int i = 1; i &lt;= m; ++i) { scanf("%d%d", &amp;moQueue::q[i].l, &amp;moQueue::q[i].r); moQueue::q[i].id = i; } sort(moQueue::q + 1, moQueue::q + m + 1, moQueue::cmp); int l = 1, r = 0; for(int i = 1; i &lt;= m; ++i) { int ql = moQueue::q[i].l, qr = moQueue::q[i].r; while(l &lt; ql) moQueue::del(l++); while(l &gt; ql) moQueue::add(--l); while(r &lt; qr) moQueue::add(++r); while(r &gt; qr) moQueue::del(r--); moQueue::ans[moQueue::q[i].id] = moQueue::now; } for(int i = 1; i &lt;= m; ++i) printf("%d ", moQueue::ans[i]); return 0; }</code></pre>

页面列表

ITEM_HTML