Yukino With Subinterval

Yukino has an array a_1, a_2 \cdots a_na1,a2⋯a**n. As a tsundere girl, Yukino is fond of studying subinterval.

Today, she gives you four integers l, r, x, yl,r,x,y, and she is looking for how many different subintervals [L, R][L,R] are in the interval [l, r][l,r] that meet the following restraints:

  1. a_L =a_{L+1} =\cdots=a_Ra**L=a**L+1=⋯=a**R, and for any i \in [L,R], x \le a_i \le yi∈[L,R],xa**iy.
  2. The length of such a subinterval should be maximum under the first restraint.

Note that two subintervals [L_1,R_1] , [L_2,R_2][L1,R1],[L2,R2]are different if and only if at least one of the following formulas is true:

  1. L1 \cancel= L2L1=L2
  2. R1 \cancel= R2R1=R2

Yukino, at the same time, likes making tricks. She will choose two integers pos,vpos,v, and she will change a_{pos}apo**s to vv.

Now, you need to handle the following types of queries:

  • 11 pos \ vpos v: change a_{pos}apo**s to vv
  • 22 l \ r \ x \ yl r x y: print the number of legal subintervals in the interval [l, r][l,r]

Input

The first line of the input contains two integers n, m (1 \le n, m \le 2 \times 10^5)n,m(1≤n,m≤2×105) – the numbers of the array and the numbers of queries respectively.

The second line of the input contains nn integers a_i (1 \le a_i \le n)a**i(1≤a**in).

For the next mm line, each containing a query in one of the following queries:

  • 11 pospos vv (1 \le pos, v \le n)(1≤pos,vn): change a_{pos}apo**s to vv
  • 22 l \ r \ x \ yl r x y (1 \le l \le r \le n) (1 \le x \le y \le n)(1≤lrn)(1≤xyn): print the number of legal subintervals in the interval [l,r][l,r]

Output

For each query of the second type, you should output the number of legal subintervals in the interval [l, r][l,r].

样例输入复制

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

样例输出复制

0
4

样例解释

For the first operations, there are 33 different subintervals ([2, 2],[3, 3],[2,3])([2,2],[3,3],[2,3]) in the interval [2, 3][2,3], but none of them meets all the restraints.

For the third operations, the legal subintervals in interval [1, 6][1,6] are: [1, 2], [3, 3], [4, 4], [6, 6][1,2],[3,3],[4,4],[6,6]

Notes that although subintervals [1,1][1,1] and [2,2][2,2] also meet the first restraint, we can extend them to subinterval [1, 2][1,2]. So the length of them is not long enough, which against the second one.

题目链接:https://nanti.jisuanke.com/t/41356

题意:

给你一个含有n个数的数组,和m个操作

操作1:

将a[pos] 变为val

操作2:

询问在\([l,r]\) 中有多少个子区间满足数值在$[x,y] $ 之间 ,每一个子区间是长度尽可能大的相同数字。

思路:

将数组中 $a[i] $ ,转为在二维坐标平面上的点\((i,a[i])\) ,

那么就转为了一个带修改的二维平面中询问矩阵内点权值和的问题。

这是一个经典的三维偏序问题。

可以用CDQ分治来解决。

不会的话可以先学习一下这题:

https://www.cnblogs.com/qieqiemin/p/11613573.html

本题还需要注意几点:

因为连续相同的数值只贡献一个,所以我们把连续相同的只把第一个点放入平面中(即放入cdq的离线操作中)

那么对于每一个询问,我们就要特判一下\((l,a[l])\) 这个点,如果\(a[l]\) 在 \([x,y]\) 之间,并且 满足

$l >1 $

$a[l]==a[l-1] $

这2个条件,都需要对这个询问的答案加上1。即加上a[l]为开头的子区间的贡献,

以及修改操作,需要判断改变\(a[i]\) 对\(a[i-1],a[i+1]\) 的影响,以及如果更改前的\(a[i]\) 是一个子区间的开头,需要去掉原来的影响(加上相反的值即可。)

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
#define du3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c))
#define du2(a,b) scanf("%d %d",&(a),&(b))
#define du1(a) scanf("%d",&(a));
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) {a %= MOD; if (a == 0ll) {return 0ll;} ll ans = 1; while (b) {if (b & 1) {ans = ans * a % MOD;} a = a * a % MOD; b >>= 1;} return ans;}
void Pv(const vector<int> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%d", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
void Pvl(const vector<ll> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%lld", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}} inline void getInt(int *p);
const int maxn = 1000010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/ ll tree[maxn];
int lowbit(int x)
{
return -x & x;
}
ll ask(int x)
{
// cout<<x<<" ";
ll res = 0ll;
while (x) {
res += tree[x];
x -= lowbit(x);
}
// cout<<res<<endl;
return res;
}
void add(int x, ll val)
{
// cout<<x<<" "<<val<<endl;
while (x < maxn) {
tree[x] += val;
x += lowbit(x);
}
}
int n, m;
struct node {
int time;
int op;
int x, y;
int val;
int ansid;
node() {}
node(int tt, int oo, int xx, int yy, int vv, int aa)
{
time = tt;
op = oo;
x = xx;
y = yy;
val = vv;
ansid = aa;
}
bool operator < (const node &bb) const
{
if (time != bb.time) {
return time < bb.time;
} else if (x != bb.x) {
return x < bb.x;
} else if (y != bb.y) {
return y < bb.y;
} else {
return op < bb.op;
}
}
bool operator<= (const node &bb )const
{
if (x != bb.x) {
return x < bb.x;
} else if (y != bb.y) {
return y < bb.y;
} else {
return op < bb.op;
}
}
} a[maxn], b[maxn];
int ans[maxn];
int tot;
int anstot;
int c[maxn];
int sym[maxn];
void cdq(int l, int r)
{
if (l == r) {
return ;
}
int mid = (l + r) >> 1;
cdq(l, mid);
cdq(mid + 1, r);
int ql = l;
int qr = mid + 1;
repd(i, l, r) {
if (qr > r || (ql <= mid && a[ql] <= a[qr])) {
if (a[ql].op == 1) {
add(a[ql].y, a[ql].val);
sym[i] = 1;
}
b[i] = a[ql++];
} else {
if (a[qr].op == 2) {
ans[a[qr].ansid] += a[qr].val * ask(a[qr].y);
}
b[i] = a[qr++];
}
}
ql = l;
qr = mid + 1;
repd(i, l, r) {if (qr > r || (ql <= mid && a[ql] <= a[qr])) {if (a[ql].op == 1) {add(a[ql].y, -a[ql].val);} ql++;} else {qr++;}}
repd(i, l, r) {a[i] = b[i];}
} int main()
{
//freopen("D:\\code\\text\\input.txt","r",stdin);
//freopen("D:\\code\\text\\output.txt","w",stdout);
du2(n, m);
repd(i, 1, n) {
du1(c[i]);
if (c[i] != c[i - 1]) {
a[++tot] = node(-1, 1, i, c[i], 1, 0);
}
}
// node(int tt, int oo, int xx, int yy, int vv, int aa)
repd(i, 1, m) {
int op;
du1(op);
if (op == 1) {
int pos, v;
du2(pos, v);
if (pos != n && c[pos] == c[pos + 1]) {
a[++tot] = node(i, 1, pos + 1, c[pos + 1], 1, 0);
}
if (pos == 1 || c[pos] != c[pos - 1]) {
a[++tot] = node(i, 1, pos, c[pos], -1, 0);
}
c[pos] = v;
if (pos != n && c[pos] == c[pos + 1]) {
a[++tot] = node(i, 1, pos + 1, c[pos + 1], -1, 0);
}
if (pos == 1 || c[pos] != c[pos - 1]) {
a[++tot] = node(i, 1, pos, c[pos], 1, 0);
}
} else {
int l, r, x, y;
du2(l, r);
du2(x, y);
if (l != 1 && c[l] == c[l - 1] && c[l] <= y && c[l] >= x) {
ans[anstot]++;
}
a[++tot] = node(i, 2, r, y, 1, anstot);
a[++tot] = node(i, 2, l - 1, x - 1, 1, anstot);
a[++tot] = node(i, 2, r, x - 1, -1, anstot);
a[++tot] = node(i, 2, l - 1, y, -1, anstot++);
}
}
sort(a + 1, a + 1 + tot);
cdq(1, tot);
repd(i, 0, anstot - 1) {
printf("%d\n", max(ans[i], 0));
}
return 0;
} inline void getInt(int *p)
{
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '0');
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 - ch + '0';
}
} else {
*p = ch - '0';
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 + ch - '0';
}
}
}

2019 ICPC 南昌网络赛I:Yukino With Subinterval(CDQ分治)的更多相关文章

  1. 2019南昌网络赛I:Yukino With Subinterval(CDQ) (树状数组套主席树)

    题意:询问区间有多少个连续的段,而且这段的颜色在[L,R]才算贡献,每段贡献是1. 有单点修改和区间查询. 思路:46min交了第一发树套树,T了. 稍加优化多交几次就过了. 不难想到,除了L这个点, ...

  2. 2019 ICPC 南昌网络赛

    2019 ICPC 南昌网络赛 比赛时间:2019.9.8 比赛链接:The 2019 Asia Nanchang First Round Online Programming Contest 总结 ...

  3. 2019南昌网络赛-I. Yukino With Subinterval 线段树套树状数组,CDQ分治

    TMD...这题卡内存卡的真优秀... 所以以后还是别用主席树的写法...不然怎么死的都不知道... 树套树中,主席树方法开权值线段树...会造成空间的浪费...这道题内存卡的很紧... 由于树套树已 ...

  4. 2019 ICPC南昌网络赛 B题

    英雄灭火问题忽略了一点丫 一个超级源点的事情,需要考虑周全丫 2 #include<cstdio> #include<cstring> #include<queue> ...

  5. Magic Master(2019年南昌网络赛E题+约瑟夫环)

    目录 题目链接 题意 思路 代码 题目链接 传送门 题意 初始时你有\(n\)张牌(按顺序摆放),每一次操作你将顶端的牌拿出,然后按顺序将上面的\(m\)张牌放到底部. 思路 首先我们发下拿走\(1\ ...

  6. 2019年ICPC南昌网络赛 J. Distance on the tree 树链剖分+主席树

    边权转点权,每次遍历到下一个点,把走个这条边的权值加入主席树中即可. #include<iostream> #include<algorithm> #include<st ...

  7. 2019 ICPC上海网络赛 A 题 Lightning Routing I (动态维护树的直径)

    题目: 给定一棵树, 带边权. 现在有2种操作: 1.修改第i条边的权值. 2.询问u到其他一个任意点的最大距离是多少. 题解: 树的直径可以通过两次 dfs() 的方法求得.换句话说,到任意点最远的 ...

  8. 2019 ICPC 沈阳网络赛 J. Ghh Matin

    Problem Similar to the strange ability of Martin (the hero of Martin Martin), Ghh will random occurr ...

  9. 2019 ICPC 徐州网络赛 B.so easy (并查集)

    计蒜客链接:https://nanti.jisuanke.com/t/41384 题目大意:给定n个数,从1到n排列,其中有q次操作,操作(1) 删除一个数字 // 操作(2)求这个数字之后第一个没有 ...

随机推荐

  1. $.ajax 中的contentType类型

    参考链接:https://www.jianshu.com/p/f4d92b3d387d

  2. oracle 的安装 及环境的配置...

    前言 最近这段时间和香港同事一起做项目  负责给日本客户做一个产品  使用的是Oracle 数据库   在一开始项目启动时一直报和oracle数据库有关    本机在这之前就已经安装好了    由于使 ...

  3. Redis 根据Key模糊批量查询数据

    前言 经常会有这样一种业务逻辑,就是需要根据Redis中Key的规则,模糊查询对应的数据,当数据量少时,利用常规的命令也能满足需求,但是数据量大时,就会导致堵塞,就算是采用不堵塞的函数,如果数据需要显 ...

  4. Java总复习内容

    StringBuffer定义时需要用正确的方式 例如: StringBuffer xxx = new StringBuffer("雯雯是猪"); 使用StringBuffer的连接 ...

  5. [LuoguP2164][SHOI2007]交通网络_拓扑排序_概率期望

    交通网络 题目链接:https://www.luogu.org/problemnew/solution/P2164 数据范围:略. 题解: 直接算不好算,我们考虑建反图然后$Toposort$. 这样 ...

  6. 【深入浅出-JVM】(7):栈上分配

    概念 对那些作用于不会逃逸出方法的对象,在分配内存时,不在将对象分配在堆内存中,而是将对象属性打散后分配在线程私有栈内存上,这样随着方法调用结束,栈上分配打散的对象也被回收掉,不在增加 GC 额外压力 ...

  7. 剑指offer19:按照从外向里以顺时针的顺序依次打印出每一个数字,4 X 4矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出数字1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10.

    1 题目描述 输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下4 X 4矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印 ...

  8. linux下shell 脚本 中windows换行符换成linux换行符

    sed -i 's/\r//' filename window下默认是 \r\n linux下是\n unix下是\r

  9. 使用Harbor搭建Docker私有仓库

    ip:192.168.0.145 环境设置 防火墙,selinux等,可以使用本章开头的那个shell脚本 其他主机的hosts文件也都添加上 ip hub.aaa.com windows系统的hos ...

  10. 在生产环境中使用Compose 【翻译】

    在生产环境中使用Compose 在开发环境中使用Compose定义你的应用,可以使用此定义在不同的环境,(如 CI.暂存和生产)中运行应用程序. 部署应用程序的最简单方法是在单个服务器上运行该应用程序 ...