题意:

一个颜色序列,\(a_1, a_2, ...a_i\)表示第i个的颜色,给出每种颜色的美丽度\(w_i\),定义一段颜色的美丽值为该段颜色的美丽值之和(重复的只计算一次),每次都会修改某个位置的颜色或者查询l到r之间的美丽值。

分析:

带修改莫队:在所有询问中多记录一个时间,每次跳转询问前,处理当前时间(上一次操作所在的时间)到目的时间(本次询问所在时间)中的所有修改操作,如果时间是倒退的,就将值改回来,否则就更改值,并且如果修改的位置不在当前的莫队指针之间就直接修改,否则就先删除再添加。

树套树留坑。

code

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<ctime>
#include<vector>
#include<queue>
using namespace std;
namespace IO{
template<typename T>
inline void read(T &x){
T i = 0, f = 1; char ch = getchar();
for(; (ch < '0' || ch > '9') && ch != '-'; ch = getchar());
if(ch == '-') f = -1, ch = getchar();
for(; ch >= '0' && ch <= '9'; ch = getchar()) i = (i << 3) + (i << 1) + (ch - '0');
x = i * f;
}
template<typename T>
inline void wr(T x){
if(x < 0) putchar('-'), x = -x;
if(x > 9) wr(x / 10);
putchar(x % 10 + '0');
}
}using namespace IO; const int N = 1e5 + 50, M = 1e5 + 50;
typedef long long ll;
int n, head, tail, cur, cnt[N], m, val[N], S;
int col[N], cc[N], typ[M], last[M], a[M], b[M];
long long ans[M], now;
struct node{
int l, r, bl, br, id;
inline bool operator < (const node &p) const{
if(bl != p.bl) return bl < p.bl;
if(br != p.br) return br < p.br;
return id < p.id;
}
node(){}
node(int bb, int c, int d, int o, int e):l(bb), r(c), bl(d), br(o), id(e){}
}qry[M];
int q;
bool sta[N]; inline void modify(int k, int v){
if(!sta[k]) col[k] = v;
else {
cnt[col[k]]--;
if(!cnt[col[k]]) now -= val[col[k]];
col[k] = v;
cnt[col[k]]++;
if(!(cnt[col[k]] - 1)) now += val[col[k]];
}
} inline void change(int curT, int tarT){
while(curT < tarT){
curT++;
if(typ[curT] == 1) modify(a[curT], b[curT]);
}
while(curT > tarT){
if(typ[curT] == 1) modify(a[curT], last[curT]);
curT--;
}
} inline void go(int l, int r){
while(tail < r){
tail++;
sta[tail] ^= 1;
cnt[col[tail]]++;
if(!(cnt[col[tail]] - 1)) now += 1ll*val[col[tail]];
}
while(tail > r){
cnt[col[tail]]--;
sta[tail] ^= 1;
if(!cnt[col[tail]]) now -= 1ll*val[col[tail]];
tail--;
}
while(head < l){
cnt[col[head]]--;
sta[head] ^= 1;
if(!cnt[col[head]]) now -= 1ll*val[col[head]];
head++;
}
while(head > l){
head--;
sta[head] ^= 1;
cnt[col[head]]++;
if(!(cnt[col[head]] - 1)) now += 1ll*val[col[head]];
}
} int main(){
freopen("color.in", "r", stdin);
// freopen("color.out", "w", stdout);
// int tt = clock();
read(n), read(m);
S = pow(n, 2.1 / 3);
for(int i = 1; i <= n; i++) read(col[i]), cc[i] = col[i];
for(int i = 1; i <= n; i++) read(val[i]);
for(int i = 1; i <= m; i++){
int x, y;
read(typ[i]), read(x), read(y);
a[i] = x, b[i] = y;
if(typ[i] == 1){
last[i] = cc[x];
cc[x] = y;
}
else {
qry[++q] = node(x, y, x / S + (x % S ? 1 : 0), y / S + (y % S ? 1 : 0), i);
}
}
sort(qry + 1, qry + q + 1);
qry[0].id = 0;
head = tail = now = 0;
for(int i = 1; i <= q; i++){
int l = qry[i].l, r = qry[i].r;
change(qry[i - 1].id, qry[i].id);
go(l, r);
ans[qry[i].id] = now;
}
for(int i = 1; i <= m; i++)
if(typ[i] == 2) wr(ans[i]), putchar('\n');
// cout<<clock() - tt<<endl;
return 0;
}

NOI模拟 颜色 - 带修莫队/树套树的更多相关文章

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

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

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

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

  3. bzoj2120 数颜色——带修莫队

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2120 带修改的莫队: 用结构体存下修改和询问,排好序保证时间后就全局移动修改即可: 参考了T ...

  4. 【bzoj2120】数颜色 带修莫队

    数颜色 题目描述 墨墨购买了一套N支彩色画笔(其中有些颜色可能相同),摆成一排,你需要回答墨墨的提问.墨墨会像你发布如下指令: 1. Q L R代表询问你从第L支画笔到第R支画笔中共有几种不同颜色的画 ...

  5. bzoj2120: 数颜色 带修莫队

    墨墨购买了一套N支彩色画笔(其中有些颜色可能相同),摆成一排,你需要回答墨墨的提问.墨墨会像你发布如下指令: 1. Q L R代表询问你从第L支画笔到第R支画笔中共有几种不同颜色的画笔. 2. R P ...

  6. luogu4074 [WC2013]糖果公园(树上带修莫队)

    link 题目大意:给一个树,树上每个点都有一种颜色,每个颜色都有一个收益 每次修改一个点上的颜色 或询问一条链上所有颜色第i次遇到颜色j可以获得w[i]*v[j]的价值,求链上价值和 题解:树上带修 ...

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

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

  8. 【BZOJ2120】数颜色(带修莫队)

    点此看题面 大致题意:告诉你\(n\)只蜡笔的颜色,有两种操作:第一种操作将第\(x\)只蜡笔颜色改成\(y\),第二种操作询问区间\([l,r]\)内有多少种颜色的蜡笔. 考虑普通莫队 这题目第一眼 ...

  9. P1903 [国家集训队]数颜色 / 维护队列(带修莫队)

    题目描述: 墨墨购买了一套N支彩色画笔(其中有些颜色可能相同),摆成一排,你需要回答墨墨的提问.墨墨会向你发布如下指令: 1. Q L R代表询问你从第L支画笔到第R支画笔中共有几种不同颜色的画笔. ...

随机推荐

  1. 囧 appspot.com/

    囧 appspot.com/ 我负责公司人事,最近车间招了一批外来打工妹,让她们填写个人资料表格,早上在看表格登记,发现其中一张政治面貌一栏赫然写着"瓜子脸",当时笑得眼泪直流,没 ...

  2. elasticsearch节点间通信的基础transport

    在前一篇中我们分析了cluster的一些元素.接下来的章节会对cluster的运作机制做详细分析.本节先分析一些transport,它是cluster间通信的基础.它有两种实现,一种是基于netty实 ...

  3. CSS外边距合并(塌陷/margin越界)

    原文 简书原文:https://www.jianshu.com/p/5f18f12cd162 大纲 1.什么是外边距合并?(折叠外边距) 2.外边距带来的影响 3.折叠的结果 4.产生折叠的原因 5. ...

  4. app 自动化测试 Appium+Java可以运行的代码

    地址:http://www.cnblogs.com/sunny-sl/p/6520465.html

  5. 十分钟上手-搭建vue开发环境(新手教程)

    想写一些关于vue的文章已经很久了,因为这个框架已经火了很久,在公司里用的框架都比较老旧,但怎么也得跟上前端发展变化的潮流,这不,开始使用vue开发项目了,一遍开发一边踩坑中,今天要记录的是五分钟搭建 ...

  6. C# 文件转byte数组,byte数组再转换文件

    using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...

  7. Project Euler 501 Eight Divisors (数论)

    题目链接: https://projecteuler.net/problem=501 题意: \(f(n)\) be the count of numbers not exceeding \(n\) ...

  8. Loading half a billion rows into MySQL---转载

    Background We have a legacy system in our production environment that keeps track of when a user tak ...

  9. JS实现放大镜效果(放大图片)

    注意:里边的两张图片(一大一小)可以自己添加,JQ采用jquery-1.11.3.js版,也可自行调换. HTML代码: <!DOCTYPE html> <html> < ...

  10. QWaitCondition 的正确使用方法(通过 mutex 把有严格时序要求的代码保护起来,同时把 wakeAll() 也用同一个 mutex 保护起来)

    简单用法 QWaitCondition 用于多线程的同步,一个线程调用QWaitCondition::wait() 阻塞等待,直到另一个线程调用QWaitCondition::wake() 唤醒才继续 ...