首页 > 其他分享 >C2. Adjust The Presentation (Hard Version)

C2. Adjust The Presentation (Hard Version)

时间:2024-10-06 21:00:23浏览次数:1  
标签:le slide Adjust members each test C2 line Presentation

C2. Adjust The Presentation (Hard Version)

This is the hard version of the problem. In the two versions, the constraints on $q$ and the time limit are different. In this version, $0 \leq q \leq 2 \cdot 10^5$. You can make hacks only if all the versions of the problem are solved.

A team consisting of $n$ members, numbered from $1$ to $n$, is set to present a slide show at a large meeting. The slide show contains $m$ slides.

There is an array $a$ of length $n$. Initially, the members are standing in a line in the order of $a_1, a_2, \ldots, a_n$ from front to back. The slide show will be presented in order from slide $1$ to slide $m$. Each section will be presented by the member at the front of the line. After each slide is presented, you can move the member at the front of the line to any position in the lineup (without changing the order of the rest of the members). For example, suppose the line of members is $[\color{red}{3},1,2,4]$. After member $3$ presents the current slide, you can change the line of members into either $[\color{red}{3},1,2,4]$, $[1,\color{red}{3},2,4]$, $[1,2,\color{red}{3},4]$ or $[1,2,4,\color{red}{3}]$.

There is also an array $b$ of length $m$. The slide show is considered good if it is possible to make member $b_i$ present slide $i$ for all $i$ from $1$ to $m$ under these constraints.

However, your annoying boss wants to make $q$ updates to the array $b$. In the $i$-th update, he will choose a slide $s_i$ and a member $t_i$ and set $b_{s_i} := t_i$. Note that these updates are persistent, that is changes made to the array $b$ will apply when processing future updates.

For each of the $q+1$ states of array $b$, the initial state and after each of the $q$ updates, determine if the slideshow is good.

Input

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

The first line of each test case contains three integers $n$, $m$ and $q$ ($1 \le n, m \le 2 \cdot 10^5$; $0 \leq q \leq 2 \cdot 10^5$) — the number of members and the number of sections.

The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1 \le a_i \le n$) — the initial order of the members from front to back. It is guaranteed that each integer from $1$ to $n$ appears exactly once in $a$.

The third line of each test case contains $m$ integers $b_1, b_2, \ldots, b_m$ ($1 \le b_i \le n$) — the members who should present each section.

Each of the next $q$ lines contains two integers $s_i$ and $t_i$ ($1 \le s_i \le m$, $1 \le t_i \le n$) — parameters of an update.

It is guaranteed that the sum of $n$, the sum of $m$ and the sum of $q$ over all test cases do not exceed $2 \cdot 10^5$ respectively.

Output

For each test case, output $q+1$ lines corresponding to the $q+1$ states of the array $b$. Output "YA" if the slide show is good, and "TIDAK" otherwise.

You can output the answer in any case (upper or lower). For example, the strings "yA", "Ya", "ya", and "YA" will be recognized as positive responses.

Example

Input

3
4 2 2
1 2 3 4
1 1
1 2
1 1
3 6 2
1 2 3
1 1 2 3 3 2
3 3
2 2
4 6 2
3 1 4 2
3 1 1 2 3 4
3 4
4 2

Output

YA
TIDAK
YA
YA
TIDAK
YA
TIDAK
YA
YA

Note

For the first test case, you do not need to move the members as both slides are presented by member $1$, who is already at the front of the line. After that, set $b_1 := 2$, now slide $1$ must be presented by member $2$ which is impossible as member $1$ will present slide $1$ first. Then, set $b_1 = 1$, the $b$ is the same as the initial $b$, making a good presentation possible.

 

解题思路

  当 $b$ 固定后,要判断其是否是好的,只需从 $b$ 中依次把第一次出现的数选出来,并判断所构成的序列 $c$ 是否等于 $a$ 的前缀即可。例如有 $a = [2, 4, 1, 3]$,$b = [2, 2, 4, 2, 4, 1, 4]$,依次选出第一次出现的数就是 $c = [2, 4, 1]$,等于 $a$ 的前缀,说明是好的。如果 $c$ 不是 $a$ 的前缀,说明必定存在最小的位置 $i$ 使得 $a_i \ne c_i$,说明在 $a$ 中 $a_i$ 比 $c_i$ 先出现。又因为 $c_1 \sim c_{i-1}$ 中不存在 $a_i$,因此 $a_i$ 必定会与 $b$ 中某个位置发生失配。否则 $c$ 是 $a$ 的前缀,容易知道存在构造方案使得 $b$ 是好的。

  由于涉及到修改,容易想到维护 $c$,每次修改相当于令 $c$ 中某一段循环左移或右移一个单位,非常难维护而且还要判断是否为 $a$ 的前缀,一开始死磕这种方法做不出来。参考了一下别人的代码,发现实际上维护的是每个值第一次出现的位置。定义 $f(i)$ 表示在 $b$ 中值 $a_i$ 第一次出现的位置(不存在则设为 $+\infty$),如果 $c$ 是 $a$ 的前缀,说明在 $b$ 中一定是先出现 $a_1$,再出现 $a_2$,再出现 $a_3$ 以此类推,这等价于 $f(1) < f(2) < \cdots < f(n)$。所以可以用一个变量 $\text{cnt}$ 统计不满足 $f(i) < f(i+1)$ 的数目,如果 $\text{cnt} = 0$ 说明 $b$ 是好的,否则不好。

  给 $1 \sim n$ 每个值开一个 std::set 存 $b$ 中出现的位置。当将 $b_x$ 修改成 $t$ 时,因为 $f(p_{b_x})$ 和 $f(p_{t})$ 会改变,其中 $p_x$ 表示 $a$ 中值 $x$ 的下标,因此受影响的只有 $f(p_{b_x} - 1)$,$f(p_{b_x})$,$f(p_{b_x} + 1)$ 之间的关系,以及 $f(p_{t} - 1)$,$f(p_{t})$,$f(p_{t} + 1)$ 之间的关系。在将 $b_x$ 改成 $t$ 的过程中相应地更新 $\text{cnt}$ 即可,同时还要维护 $b_x$ 和 $t$ 对应的 std::set

  AC 代码如下,时间复杂度为 $O(q\log{n})$:

#include <bits/stdc++.h>
using namespace std;

typedef long long LL;

const int N = 2e5 + 5;

int n, m, k;
int a[N], b[N], p[N];
set<int> st[N];
int f[N], cnt;

void upd(int x, int op) {
    if (p[b[x]] - 1 > 0) cnt -= f[p[b[x]] - 1] > f[p[b[x]]];
    if (p[b[x]] + 1 <= n) cnt -= f[p[b[x]]] > f[p[b[x]] + 1];
    if (op) st[b[x]].insert(x);
    else st[b[x]].erase(x);
    f[p[b[x]]] = *st[b[x]].begin();
    if (p[b[x]] - 1 > 0) cnt += f[p[b[x]] - 1] > f[p[b[x]]];
    if (p[b[x]] + 1 <= n) cnt += f[p[b[x]]] > f[p[b[x]] + 1];
}

void solve() {
    cin >> n >> m >> k;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        p[a[i]] = i;
    }
    for (int i = 1; i <= n; i++) {
        st[i].clear();
        st[i].insert(m + 1);
    }
    for (int i = 1; i <= m; i++) {
        cin >> b[i];
        st[b[i]].insert(i);
    }
    for (int i = 1; i <= n; i++) {
        f[i] = *st[a[i]].begin();
    }
    cnt = 0;
    for (int i = 1; i < n; i++) {
        cnt += f[i] > f[i + 1];
    }
    cout << (cnt ? "TIDAK" : "YA") << '\n';
    while (k--) {
        int x, y;
        cin >> x >> y;
        upd(x, 0);
        b[x] = y;
        upd(x, 1);
        cout << (cnt ? "TIDAK" : "YA") << '\n';
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    cin >> t;
    while (t--) {
        solve();
    }
    
    return 0;
}

标签:le,slide,Adjust,members,each,test,C2,line,Presentation
From: https://www.cnblogs.com/onlyblues/p/18449401

相关文章

  • ICPC2023沈阳K
    https://codeforces.com/gym/104869/problem/KDS题尽量进一步思考,简化维护过程权值线段树上二分首先得出一个显然的转化:对于每次操作,求出此次下所有正数从小到大的前缀和的第一次大于所有负数和的绝对值的位置即为答案。赛时做法既然要求每次都求a升序下的前缀和,很显然的想......
  • ABC221G Jumping Sequences 题解
    JumpingSequences把移动的上下左右改成左上、左下、右上、右下(坐标轴旋转\(45\)°)。则最终目的地是\((A+B,A-B)\)。(以前移动的方式是\((\pmd_i,0),(0,\pmd_i)\)。现在每次移动的方式是\((\pmd_i,\pmd_i)\))则\(x,y\)两维可以分开考虑。目标:从\(d_1\simd_n\)中选......
  • win11,vc22源码编译opencv410
    1.安装cmake 2.配代理,否则无法下载依赖包3.自行编译OpenCV源码步骤4.注意配置系统变量,重启机器https://blog.csdn.net/weixin_50648158/article/details/139742826亲测可用OpenCV4.10.0在Windows10,64位,vs2022下的编译及配置方法https://blog.csdn.net/yxfamyself/article......
  • ABC225F String Cards
    题意给你\(n\)个串\(s_i\),你需要选出\(k\)个串并按照某个顺序拼接起来形成的字符串字典序最小。\(n,k,|s|\le50\)。分析由于顺序不固定,所以我们无法直接DP。而状压的复杂度也太高了,怎么办呢?考虑钦定一个顺序,使得按照这个顺序排列字符串一定最优。一个经典的错误想法......
  • 题解:P6902 [ICPC2014 WF] Surveillance
    可以在cnblog中阅读。考虑弱化版链怎么做,每次选取左端点在当前位置前面的线段,跳到其中最大的右端点,可以维护一个数组表示起点为\(i\)的目标位置,可以做到\(O(n+k)\)。考虑多次询问一段区间\([l,r]\)的答案,这时如果暴力从\(l-1\)开始跳是\(O(n^2)\)的,只需要一个倍增数......
  • 洛谷题单指南-分治与倍增-P6648 [CCC2019] Triangle: The Data Structure
    原题链接:https://www.luogu.com.cn/problem/P6648题意解读:在一个n行的数字三角形中,求所有边长为k的正三角形最大值之和。解题思路:1、枚举法枚举每一个边长为k的三角形,在其中求max,然后累加,n最多3000,时间复杂度是n^4,显然超时。2、倍增和ST思想此题非常类似于RMQ问题,也就是求区......
  • NEERC2014题解
    A结论题,行着取intn,m;signedmain(void){#ifdefONLINE_JUDGEfreopen("alter.in","r",stdin);freopen("alter.out","w",stdout);#endif read(n),read(m); writeln(n/2+m/2); for(inti=2;i<=n;i......
  • Learning Continuous Image Representation with Local Implicit Image Function
    LearningContinuousImageRepresentationwithLocalImplicitImageFunction(阅读笔记)11.03局部隐式图像函数(LIIF)表示连续中的图像,可以以任意高分辨率表示。摘要:如何表示图像?当视觉世界以连续的方式呈现时,机器用二维像素数组以离散的方式存储和观看图像。本文中,试图学习......
  • CC2541一款低能耗和私有片载系统 应用范围在低能耗系统等消费类
    CC2541一款低能耗和私有片载系统应用范围在低能耗系统等消费类CC2541外设功能强大的5通道直接内存访问(DMA)通用定时器(1个16位,2个8位)含8通道和可配置分辨率的12位模数转换器(ADC)2个具有LED驱动功能的I/O引脚I2C接口高级加密标准(AES)安全协处理器集成的高性能比较......
  • 代码随想录算法训练营Day03-链表 | LC203移除链表元素、LC707设计链表、LC206反转链表
    目录前言LeetCode203.移除链表元素思路完整代码LeetCode707.设计链表思路完整代码LeetCode206.反转链表思路完整代码今日总结前言拖延症犯了,哈哈,拖了一天LeetCode203.移除链表元素给你一个链表的头节点head和一个整数val,请你删除链表中所有满足Node.val......