寄寄寄寄寄寄寄寄寄寄寄寄寄寄寄寄寄|

TLE_Automation

园龄:2年9个月粉丝:19关注:23

P2464 [SDOI2008] 郁闷的小 J 题解

简单分块。

用一个 map 来维护每块里的出现次数就行了。

/*
Work by: TLE_Automation
*/
#include<map>
#include<cmath>
#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<tr1/unordered_map>
#define LL long long
// #define int long long
using namespace std;
using namespace std::tr1;
const int N = 1e6 + 10;
const int MAXN = 2e5 + 10;
inline char readchar() {
static char buf[100000], *p1 = buf, *p2 = buf;
return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF : *p1++;
}
inline int read() {
#define readchar getchar
int res = 0, f = 0;char ch = readchar();
for(; !isdigit(ch); ch = readchar()) if(ch == '-') f = 1;
for(; isdigit(ch); ch = readchar()) res = (res << 1) + (res << 3) + (ch ^ '0');
return f ? -res : res;
}
inline void print(int x) {
if (x < 0 ) putchar('-'), x = -x;
if (x > 9 ) print(x / 10);
putchar(x % 10 + '0');
}
int n, Q, a[MAXN], bel[MAXN], l[MAXN], r[MAXN], num;
unordered_map <int, int> Block[1005];
void build() {
int len = n / num;
for(register int i = 1; i <= num; i++) {
l[i] = len * (i - 1) + 1;
r[i] = len * i;
}
r[num] = n;
for(register int i = 1; i <= num; i++) {
for(register int j = l[i]; j <= r[i]; j++) {
bel[j] = i; Block[i][a[j]]++;
}
}
}
int Query(int x, int y, int k) {
int res = 0;
if(bel[x] == bel[y]) {
for(register int i = x; i <= y; i++) res += (a[i] == k);
return res;
}
for(register int i = x; i <= r[bel[x]]; i++) res += (a[i] == k);
for(register int i = l[bel[y]]; i <= y; i++) res += (a[i] == k);
for(register int i = bel[x] + 1; i < bel[y]; i++) res += Block[i][k];
return res;
}
signed main() {
n = read(), Q = read();
num = sqrt(n);
for(int i = 1; i <= n; i++) a[i] = read();
build();
for(int i = 1, l, r, x; i <= Q; i++) {
char opt; cin >> opt;
if(opt == 'Q') l = read(), r = read(), x = read(), printf("%d\n", Query(l, r, x));
else {
l = read(), r = read();
Block[bel[l]][a[l]]--;
a[l] = r;
Block[bel[l]][a[l]]++;
}
}
}

本文作者:TLE_Automation

本文链接:https://www.cnblogs.com/tttttttle/p/16323225.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   TLE_Automation  阅读(31)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起