【莫队】luogu_2709 小B的询问

题意

∑ci2\sum{{c_i}^2}ci2的值,其中iii的值从111KKK,其中cic_ici表示数字iii[L..R][L..R][L..R]中的重复次数。

思路

莫队修改一下暴力中的两个函数即可,注意lll111开始,否则会多无用计算。

代码

#include<cmath>
#include<cstdio>
#include<algorithm>

struct node {
	int l, r, pos;
}que[500001];
int n, m, k, block, ans;
int a[500001], cnt[1000001], res[500001];

bool operator <(const node &x, const node &y) {
	return (x.l / block) ^ (y.l / block) ? x.l < y.l : (((x.l / block) & 1) ? x.r < y.r : x.r > y.r);
}

void add(int x) {
	ans += 2 * cnt[a[x]] + 1;
	cnt[a[x]]++;
}

void del(int x) {
	ans -= 2 * cnt[a[x]] - 1;
	cnt[a[x]]--;
}

int main() {
	scanf("%d %d %d", &n, &m, &k);
	for (int i = 1; i <= n; i++)
		scanf("%d", &a[i]);
	block = n / sqrt(m * 2 / 3);
	for (int i = 1; i <= m; i++)
		scanf("%d %d", &que[i].l, &que[i].r), que[i].pos = i;
	std::sort(que + 1, que + m + 1);
	int l = 1, r = 0;
	for (int i = 1; i <= m; i++) {
		int ql = que[i].l, qr = que[i].r;
        while (l < ql) del(l++);
        while (l > ql) add(--l);
        while (r < qr) add(++r);
        while (r > qr) del(r--);
        res[que[i].pos] = ans;
	}
	for (int i = 1; i <= m; i++)
		printf("%d\n", res[i]);
}
# P4074 [WC2013] 糖果公园 ## 题目描述 Candyland 有一座糖果公园,公园里不仅有美丽的风景、好玩的游乐项目,还有许多免费糖果的发放点,这引来了许多贪吃的小朋友来糖果公园游玩。 糖果公园的结构十分奇特,它由 $n$ 个游览点构成,每个游览点都有一个糖果发放处,我们可以依次将游览点编号为 $1$ 至 $n$。有 $n - 1$ 条双向道路连接着这些游览点,并且整个糖果公园都是连通的,即从任何一个游览点出发都可以通过这些道路到达公园里的所有其它游览点。 糖果公园所发放的糖果种类非常丰富,总共有 $m$ 种,它们的编号依次为 $1$ 至 $m$。每一个糖果发放处都只发放某种特定的糖果,我们用 $C_i$ 来表示 $i$ 号游览点的糖果。 来到公园里游玩的游客都不喜欢走回头路,他们总是从某个特定的游览点出发前往另一个特定的游览点,并游览途中的景点,这条路线一定是唯一的。他们经过每个游览点,都可以品尝到一颗对应种类的糖果。 大家对不同类型糖果的喜爱程度都不尽相同。 根据游客们的反馈打分,我们得到了糖果的美味指数, 第 $i$ 种糖果的美味指数为 $V_i$。另外,如果一位游客反复地品尝同一种类的糖果,他肯定会觉得有一些腻。根据量化统计,我们得到了游客第 $i$ 次品尝某类糖果的新奇指数 $W_i$。如果一位游客第 $i$ 次品尝第 $j$ 种糖果,那么他的愉悦指数 $H$ 将会增加对应的美味指数与新奇指数的乘积,即 $V_j \times W_i$。这位游客游览公园的愉悦指数最终将是这些乘积的和。 当然,公园中每个糖果发放点所发放的糖果种类不一定是一成不变的。有时,一些糖果点所发放的糖果种类可能会更改(也只会是 $m$ 种中的一种),这样的目的是能够让游客们总是感受到惊喜。 糖果公园的工作人员小 A 接到了一个任务,那就是根据公园最近的数据统计出每位游客游玩公园的愉悦指数。但数学不好的小 A 一看到密密麻麻的数字就觉得头晕,作为小 A 最好的朋友,你决定帮他一把。 ## 输入格式 第一行包含三个正整数 $n, m, q$, 分别表示游览点个数、 糖果种类数和操作次数。 第二行包含 $m$ 个正整数 $V_1, V_2, \ldots, V_m$。 第三行包含 $n$ 个正整数 $W_1, W_2, \ldots, W_n$。 第四行到第 $n + 2$ 行,每行包含两个正整数 $A_i, B_i$,表示这两个游览点之间有路径可以直接到达。 第 $n + 3$ 行包含 $n$ 个正整数 $C_1, C_2, \ldots, C_n$。 接下来 $q$ 行, 每行包含三个整数 $Type, x, y$,表示一次操作: - 若 $Type$ 为 $0$,则 $1 \leq x \leq n$, $1 \leq y \leq m$,表示将编号为 $x$ 的游览点发放的糖果类型改为 $y$; - 若 $Type$ 为 $1$,则 $1 \leq x, y \leq n$,表示对出发点为 $x$,终止点为 $y$ 的路线询问愉悦指数。 ## 输出格式 按照输入的先后顺序,对于每个 $Type$ 为 $1$ 的操作输出一行,用一个正整数表示答案。 ## 输入输出样例 #1 ### 输入 #1 ``` 4 3 5 1 9 2 7 6 5 1 2 3 3 1 3 4 1 2 3 2 1 1 2 1 4 2 0 2 1 1 1 2 1 4 2 ``` ### 输出 #1 ``` 84 131 27 84 ``` ## 说明/提示 【样例解释】 我们分别用 ![](https://cdn.luogu.com.cn/upload/image_hosting/isw3ib3u.png) 代表 $C_i$ 为 $1$、 $2$、 $3$ 的节点,在修改之前: ![](https://cdn.luogu.com.cn/upload/image_hosting/ttkzii1u.png) 在将 $C_2$ 修改为 $1$ 之后: ![](https://cdn.luogu.com.cn/upload/image_hosting/izro364w.png) 【数据规模与约定】 对于所有的数据: $1 \leq V_i, W_i \leq 10^6$,$1 \leq A_i, B_i \leq n$, $1 \leq C_i \leq m$, $W_1, W_2, \ldots, W_n$ 是非递增序列,即对任意 $1 < i \leq n$, 满足 $W_i \le W_{i-1}$。 其它的限制条件如下表所示: ![QQ20180113072014.png](https://cdn.luogu.com.cn/upload/image_hosting/g6884nx1.png) 为什么WA了样例 ```cpp #include <iostream> #include <algorithm> #include <vector> #include <cmath> using namespace std; const int BITS = 20; const int MAX_N = 200050; int n, m, q, v[MAX_N], w[MAX_N], c[MAX_N]; int in[MAX_N], out[MAX_N], dfn[MAX_N], tim; int fa[MAX_N][BITS], dep[MAX_N]; vector<int> e[MAX_N]; void dfs(int u, int father, int depth) { fa[u][0] = father, dep[u] = depth; for (int k = 1; k < BITS; k++) fa[u][k] = fa[fa[u][k - 1]][k - 1]; in[u] = ++tim, dfn[tim] = u; for (int v : e[u]) if (v != father) dfs(v, u, depth + 1); out[u] = ++tim, dfn[tim] = u; } int LCA(int x, int y) { if (dep[x] > dep[y]) swap(x, y); for (int k = BITS - 1; ~k; k--) if (dep[x] <= dep[fa[y][k]]) y = fa[y][k]; if (x == y) return x; for (int k = BITS - 1; ~k; k--) if (fa[x][k] != fa[y][k]) x = fa[x][k], y = fa[y][k]; return fa[x][0]; } int tot1, tot2, pos[MAX_N], cnt[MAX_N]; long long cur, ans[MAX_N]; bool vis[MAX_N]; struct Modify { int x, y; } mod[MAX_N]; struct Query { int l, r, p, t, id; bool operator < (const Query& rhs) const { if (pos[l] != pos[rhs.l]) return pos[l] < pos[rhs.l]; if (pos[r] != pos[rhs.r]) return pos[r] < pos[rhs.r]; return t < rhs.t; } } qry[MAX_N]; inline void add(int idx) { cur += 1ll * v[c[idx]] * w[++cnt[c[idx]]]; } inline void del(int idx) { cur -= 1ll * v[c[idx]] * w[cnt[c[idx]]--]; } inline void move(int idx) { vis[idx] ? del(idx) : add(idx), vis[idx] ^= 1; } inline void update(int i) { if (vis[mod[i].x]) del(mod[i].x); swap(c[mod[i].x], mod[i].y); if (vis[mod[i].x]) add(mod[i].x); } void Mo_s_algorithm_on_trees() { const int size = pow(n * 2, 0.667); for (int i = 1; i <= n * 2; i++) pos[i] = (i - 1) / size + 1; sort(qry + 1, qry + tot2 + 1); int l = 1, r = 0, t = 0; for (int i = 1; i <= tot2; i++) { int ql = qry[i].l, qr = qry[i].r; int qt = qry[i].t, qp = qry[i].p; while (l > ql) move(dfn[--l]); while (r < qr) move(dfn[++r]); while (l < ql) move(dfn[l++]); while (r > qr) move(dfn[r--]); while (t < qt) update(++t); while (t > qt) update(t--); if (qp) move(dfn[qp]); ans[qry[i].id] = cur; if (qp) move(dfn[qp]); } } int main() { cin >> n >> m >> q; for (int i = 1; i <= m; i++) cin >> v[i]; for (int i = 1; i <= n; i++) cin >> w[i]; for (int i = 1, a, b; i < n; i++) { cin >> a >> b; e[a].push_back(b); e[b].push_back(a); } dfs(1, 0, 1); for (int i = 1; i <= n; i++) cin >> c[i]; for (int i = 1, type, x, y; i <= q; i++) { cin >> type >> x >> y; if (type == 0) mod[++tot1] = Modify{x, y}; else { tot2++; if (in[x] > in[y]) swap(x, y); int lca = LCA(x, y); if (lca == x) qry[tot2] = Query{in[x], in[y], 0, tot1, tot2}; else qry[tot2] = Query{out[x], in[y], lca, tot1, tot2}; } } Mo_s_algorithm_on_trees(); for (int i = 1; i <= tot2; i++) cout << ans[i] << '\n'; return 0; } ```
08-10
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值