Dynamic len

【题目链接】Dynamic len

【题目类型】带修莫队

&题解:

莫队可以单点更改,只要再多加一维,代表查询次数,排序的时候3个关键字.

之后循环离线的时候,先暴力时间指针(oi大佬说的),之后l,和r就随便了.还有要会用vis数组.

【时间复杂度】\(O(n^{\frac{5}{3}})\)

&代码:

#include <cstdio>
#include <bitset>
#include <iostream>
#include <set>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <map>
#include <queue>
#include <vector>
using namespace std;
#define INF 0x3f3f3f3f
#define ll long long
const int maxn = 5e4 + 7;
struct nt {
int l, r, id, ts;
} ask[maxn];
struct nd {
int x, pre, v;
} p[maxn];
int n, m, an[maxn], c[maxn], pos[maxn], num[maxn * 20], last[maxn], ans;
bool vis[maxn];
char s[9];
bool cmp(nt a, nt b) {
return pos[a.l] < pos[b.l] ||
pos[a.l] == pos[b.l] && pos[a.r] < pos[b.r] ||
pos[a.l] == pos[b.l] && pos[a.r] == pos[b.r] && a.ts < b.ts;
}
void update(int x) {
if(vis[x]) {
if(!--num[c[x]]) --ans;
}
else if(++num[c[x]] == 1) ans++;
vis[x] ^= 1;
}
void change(int x, int v) {
if(vis[x]) {
update(x); c[x] = v; update(x);
}
else c[x] = v;
}
int main() {
//("E:1.in", "r", stdin);
scanf("%d%d", &n, &m);
int x, y, tot = 0, top = 0;
int bk = sqrt(n);
for(int i = 1; i <= n; i++) {
scanf("%d", &c[i]);
last[i] = c[i];
pos[i] = i / bk;
}
for(int i = 1; i <= m; i++) {
scanf("%s%d%d", s, &x, &y);
x++;
if(s[0] == 'Q') {
//[++tot].id必须要赋为tot. 而且这句话必须在第一位写
ask[++tot].id = tot;
ask[tot].l = x, ask[tot].r = y, ask[tot].ts = top;
}
else {
p[++top].x = x, p[top].v = y, p[top].pre = last[x];
last[x] = y;
}
}
sort(ask + 1, ask + 1 + tot, cmp);
int now = 0, pl = 1, pr = 0;
for(int i = 1; i <= tot; i++) {
if(now < ask[i].ts) {
for(int j = now + 1; j <= ask[i].ts; j++)
change(p[j].x, p[j].v);
}
else {
for(int j = now; j >= ask[i].ts + 1; j--)
change(p[j].x, p[j].pre);
}
if(pr < ask[i].r) {
for(int j = pr + 1; j <= ask[i].r; j++)
update(j);
}
else {
for(int j = pr; j >= ask[i].r + 1; j--)
update(j);
}
if(pl < ask[i].l) {
for(int j = pl; j <= ask[i].l - 1; j++)
update(j);
}
else {
for(int j = pl - 1; j >= ask[i].l; j--)
update(j);
}
pl = ask[i].l, pr = ask[i].r, now = ask[i].ts;
an[ask[i].id] = ans;
}
for(int i = 1; i <= tot; i++) {
printf("%d\n", an[i]);
}
return 0;
}

UVA 12345 Dynamic len(带修莫队)的更多相关文章

  1. LG4074【WC2013】糖果公园 【树上莫队,带修莫队】

    题目描述:给出一棵 \(n\) 个点的树,点有颜色 \(C_i\),长度为 \(m\) 的数组 \(V\) 和长度为 \(n\) 的数组 \(W\).有两种操作: 将 \(C_x\) 修改为 \(y\ ...

  2. P5168 xtq玩魔塔 [克鲁斯卡尔重构树+带修莫队]

    P5168 xtq玩魔塔 又是码农题- 利用克鲁斯卡尔重构树的性质 我们就可以得出 \(dep\) 值小的,肯定比 \(dep\) 大的值要优. 于是第二问就可以直接 LCA 求出来了- 至于第三问, ...

  3. 【BZOJ-3052】糖果公园 树上带修莫队算法

    3052: [wc2013]糖果公园 Time Limit: 200 Sec  Memory Limit: 512 MBSubmit: 883  Solved: 419[Submit][Status] ...

  4. 「洛谷1903」「BZOJ2120」「国家集训队」数颜色【带修莫队,树套树】

    题目链接 [BZOJ传送门] [洛谷传送门] 题目大意 单点修改,区间查询有多少种数字. 解法1--树套树 可以直接暴力树套树,我比较懒,不想写. 稍微口胡一下,可以直接来一个树状数组套主席树,也就是 ...

  5. BZOJ2120 数颜色 莫队 带修莫队

    原文链接https://www.cnblogs.com/zhouzhendong/p/BZOJ2120.html 题目传送门 - BZOJ2120 题意 给定一个长度为 $n$ 的序列 $a$ ,有 ...

  6. BZOJ3052/UOJ#58 [wc2013]糖果公园 莫队 带修莫队 树上莫队

    原文链接https://www.cnblogs.com/zhouzhendong/p/BZOJ3052.html 题目传送门 - BZOJ3052 题目传送门 - UOJ#58 题意 给定一棵树,有 ...

  7. bzoj 2120 数颜色 (带修莫队)

    题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=2120 题意:两种操作:Q 询问区间  l - r  内颜色的种类 ,R 单点修改 思路 ...

  8. BZOJ 4129 Haruna’s Breakfast (分块 + 带修莫队)

    4129: Haruna’s Breakfast Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 835  Solved: 409[Submit][St ...

  9. BZOJ 2120 数颜色 (带修莫队)

    2120: 数颜色 Time Limit: 6 Sec  Memory Limit: 259 MBSubmit: 6367  Solved: 2537[Submit][Status][Discuss] ...

随机推荐

  1. iOS 库操作

    目录 库操作 主工程和子工程的引用关系 库之间的引用关系 ar命令 nm命令 库操作 主工程和子工程的引用关系 子工程引用主工程中的文件需要在子工程的search path中加入头文件的目录 子工程引 ...

  2. git log --pretty=format:" "

    控制显示的记录格式,常用的格式占位符写法及其代表的意义如下: 选项 说明%H   提交对象(commit)的完整哈希字串%h    提交对象的简短哈希字串%T    树对象(tree)的完整哈希字串% ...

  3. HTML自动跳转

    本文首发于 http://youngzy.com/ 关于页面的自动跳转,以前知道有JavaScript的方法,直接设置window.location 或者在setTimeout 函数里进行设置. 现在 ...

  4. vue filter方法-时间格式化

    plugins/filter.js import Vue from 'vue' // 时间格式化 // 用法:<div>{{data | dataFormat('yyyy-MM-dd hh ...

  5. M - 非诚勿扰 优先队列

    来源 作为2013年699万应届毕业生中的一员,由于宏观经济的不景气,小明在毕业当天就华丽丽地失业了! 经历了千难万苦的求职过程,小明特别能理解毕业生的就业之难,所以,他现在准备创建一家专门针对IT人 ...

  6. D - Bridge

    n people wish to cross a bridge at night. A group of at most two people may cross at any time, and e ...

  7. Q - Girls and Boys

    来源poj1068 In the second year of the university somebody started a study on the romantic relations be ...

  8. Java课程寒假之开发记账本软件(网页版)之五

    一.实现基本功能之后 可以添加其他功能,比如说添加账户,删除账户,以及查询页面的分页.(我都没写,滑稽) 二.基本功能部分截图

  9. markdown 基本语法(转载)

    最近感觉一直使用富文本编辑器写东西,感觉有点烦,所以就试着学习了一下简单的markdown编辑器的使用 原文地址:http://www.jianshu.com/p/815788f4b01d markd ...

  10. Spring framewrok 源码概览

    关于Spring 框架阅读记录 关于SpringMvc 框架阅读记录 1.首先看一个 spring-web 项目 项目结构 http 包 主要存储 http 相关信息 HttpRequst,HttpS ...