首页 > 其他分享 >题解 ABC293G【Triple Index】

题解 ABC293G【Triple Index】

时间:2023-03-12 09:12:19浏览次数:76  
标签:题解 ll tot while Triple buc ABC293G now rep

莫队板子。

类似于 小 B 的询问,在移动指针过程中,维护每个数出现次数 \(cnt_i\),同时维护 \(\sum\binom{cnt_i}{3}\) 即可。

取序列分块块长 \(B=\frac{n}{\sqrt{m}}\),有最优复杂度 \(\mathcal O(n\sqrt{m})\)。

在 ABC G 看到过好几次莫队板子,ABC 怎么这么喜欢莫队板子。

// Problem: G - Triple Index
// Contest: AtCoder - AtCoder Beginner Contest 293
// URL: https://atcoder.jp/contests/abc293/tasks/abc293_g
// Memory Limit: 1024 MB
// Time Limit: 3000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

//By: OIer rui_er
#include <bits/stdc++.h>
#define rep(x,y,z) for(ll x=(y);x<=(z);x++)
#define per(x,y,z) for(ll x=(y);x>=(z);x--)
#define debug(format...) fprintf(stderr, format)
#define fileIO(s) do{freopen(s".in","r",stdin);freopen(s".out","w",stdout);}while(false)
#define likely(exp) __builtin_expect(!!(exp), 1)
#define unlikely(exp) __builtin_expect(!!(exp), 0)
using namespace std;
typedef long long ll;

mt19937 rnd(std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::system_clock::now().time_since_epoch()).count());
ll randint(ll L, ll R) {
	uniform_int_distribution<ll> dist(L, R);
	return dist(rnd);
}

template<typename T> void chkmin(T& x, T y) {if(x > y) x = y;}
template<typename T> void chkmax(T& x, T y) {if(x < y) x = y;}

const ll N = 2e5+5;

ll n, m, a[N], L[N], R[N], pos[N], sz, tot, buc[N], now, ans[N];

struct Query {
	ll l, r, id;
	Query(ll a=0, ll b=0, ll c=0) : l(a), r(b), id(c) {}
	friend bool operator<(const Query& a, const Query& b) {
		if(pos[a.l] != pos[b.l]) return a.l < b.l;
		return (pos[a.l] & 1) ? (a.r < b.r) : (a.r > b.r);
	}
}q[N];

void initBlock() {
	sz = n / sqrt(m);
	chkmax(sz, 1LL);
	chkmin(sz, n);
	while(++tot) {
		L[tot] = R[tot-1] + 1;
		R[tot] = min(sz*tot, n);
		rep(i, L[tot], R[tot]) pos[i] = tot;
		if(R[tot] == n) break;
	}
}

void ins(ll x) {
	now -= buc[x] * (buc[x] - 1) / 2 * (buc[x] - 2) / 3;
	++buc[x];
	now += buc[x] * (buc[x] - 1) / 2 * (buc[x] - 2) / 3;
}

void del(ll x) {
	now -= buc[x] * (buc[x] - 1) / 2 * (buc[x] - 2) / 3;
	--buc[x];
	now += buc[x] * (buc[x] - 1) / 2 * (buc[x] - 2) / 3;
}

int main() {
	scanf("%lld%lld", &n, &m);
	rep(i, 1, n) scanf("%lld", &a[i]);
	initBlock();
	rep(i, 1, m) scanf("%lld%lld", &q[i].l, &q[i].r), q[i].id = i;
	sort(q+1, q+1+m);
	ll l = 1, r = 0;
	rep(i, 1, m) {
		while(l > q[i].l) ins(a[--l]);
		while(r < q[i].r) ins(a[++r]);
		while(l < q[i].l) del(a[l++]);
		while(r > q[i].r) del(a[r--]);
		ans[q[i].id] = now;
	}
	rep(i, 1, m) printf("%lld\n", ans[i]);
	return 0;
}

标签:题解,ll,tot,while,Triple,buc,ABC293G,now,rep
From: https://www.cnblogs.com/ruierqwq/p/abc293g.html

相关文章

  • [ABC293E] Geometric Progression 题解
    [ABC293E]GeometricProgression题解神中神数论题目描述给定整数\(A,X,M\),求\[\sum_{i=0}^{X-1}A^i\bmodM\]\(1\leA,M\le10^9\)\(1\leX\le10^......
  • UVA12107 题解
    前言题目传送门!更好的阅读体验?很久以前的一道搜索大模拟题目,另一篇题解的写法有点鬼畜,所以就来补篇题解。题面给你一个数字谜。修改最少次数(每次修改一个数位为空格或......
  • [POI2001][HAOI2007] 反素数 题解
    前置知识:一些关于约数的小常识。唯一分解定理对于所有正整数\(n\),一定有唯一分解方式\(n=p_1^{c_1}p_2^{c_2}\cdotsp_m^{c_m}\),其中\(p_1<p_2<\cdots<p_m\),......
  • P3530[POI2012 FES-Festival] 题解
    题面链接简要题意对于数列\(\{v_n\}\),有两种约束\(v_i=v_j+1\)和\(v_i\gev_j\),问\(\{v_n\}\)最多有多少个不同的项。解法考虑先建图,注意到如果约束图是DAG,那么......
  • CF1795 G.Removal Sequences - 题解
    记\(N(u)\)表示图上与点\(u\)相邻的点,\(p_u=deg_u-a_u\),其中\(deg_u\)为无向图上点\(u\)的度数。首先要删除\(p_u=0\)的点,同时\(\forallv\inN(u),p_v......
  • CF888D Almost Identity Permutations 题解
    CF链接:AlmostIdentityPermutationsLuogu链接:AlmostIdentityPermutations${\scr\color{Aquamarine}{\text{Solution}}}$前言这好像是一道能用数学秒掉的题目但......
  • 「THUPC 2023 初赛」欺诈游戏 题解
    写点无脑做法。设走私者的策略是\(p_i\)概率选\(i\),检查官的策略是\(q_i\)概率选\(i\)。因为两者策略均最优,所以走私者选任意一个数得到的期望收益相同,检查官选任......
  • CF1802D题解
    CF1802D题解传送门更好的阅读体验简化题意:有n个商店,每个商店卖a,b两种商品,价格分别为\(a_i,b_i\),你需要在每个商店买一个商品,并且不能在所有商店都买同一种商品,最......
  • Python - Crypto 导入失败问题解决记录
    python3.7Mac安装psycopg2$pipinstallpsycopg2...Error:pg_configexecutablenotfound....出现报错:Error:pg_configexecutablenotfound.解决参考:h......
  • P5752 [NOI1999] 棋盘分割题解
    本文来自我的洛谷博客。本题解力求使用清晰易懂的图片和文字,讲解最简洁的道理。请大家耐心地看完,注意要结合图片一起哦~~2022-8-24更改了格式与错别字2022-8-28更改......