区间查询数值+整体赋值

维护tag代表整个区间被赋成了tag[i]

pushdown操作,而不是修改了再check。

不压缩代码了,调起来心累,长点有啥不好。

//Stay foolish,stay hungry,stay young,stay simple
#include<iostream>
#include<cmath>
#include<cctype>
#include<cstdio>
using namespace std; inline int read_d(){
int ret=0,f=1;char c;
while(c=getchar(),!isdigit(c)) f=c=='-'?-1:1;
while(isdigit(c)){
ret=ret*10+c-'0';
c=getchar();
}
return ret*f;
} const int MAXN=500005; int n;
int a[MAXN],l[MAXN],r[MAXN],tag[MAXN];
int bl[MAXN];
int num,block; void pushdown(int id){
if(tag[id]==-1) return;
for(int i=l[id];i<=r[id];i++) a[i]=tag[id];
tag[id]=-1;
} void build(){
block=sqrt(n);
num=n/block;
if(n%block) num++;
for(int i=1;i<=n;i++){
bl[i]=(i-1)/block+1;
}
for(int i=1;i<=num;i++){
l[i]=(i-1)*block+1;
r[i]=i*block;
tag[i]=-1;
}
r[num]=n;
} int query(int x,int y,int w){
int ret=0;
if(bl[x]==bl[y]){
pushdown(bl[x]);
for(int i=x;i<=y;i++) ret+=(a[i]==w),a[i]=w;
return ret;
}
pushdown(bl[x]);
for(int i=x;i<=r[bl[x]];i++){
ret+=(a[i]==w);a[i]=w;
}
pushdown(bl[y]);
for(int i=l[bl[y]];i<=y;i++){
ret+=(a[i]==w);a[i]=w;
}
for(int i=bl[x]+1;i<=bl[y]-1;i++){
if(tag[i]==-1){
for(int j=l[i];j<=r[i];j++){
ret+=(a[j]==w);
a[j]=w;
}
tag[i]=w;
continue;
}
if(tag[i]==w){
ret+=r[i]-l[i]+1;
}else tag[i]=w;
}
return ret;
} int main(){
n=read_d();
for(int i=1;i<=n;i++) a[i]=read_d();
build();
for(int i=1;i<=n;i++){
int x=read_d(),y=read_d(),z=read_d();
printf("%d\n",query(x,y,z));
}
return 0;
}

[LOJ] 分块九题 8的更多相关文章

  1. [LOJ] 分块九题 6

    单点插入,单点查询. 优化了的链表. 链表老写错,干脆用vector,也不算慢. 注意链表退化的问题,及时(比如操作根号n次)就重新建块,实测速度可以提高一倍,这还是数据随机的情况,若涉及大量同一位置 ...

  2. [LOJ] 分块九题 4

    https://loj.ac/problem/6280 区间修改,区间求和. 本来线段树的活. //Stay foolish,stay hungry,stay young,stay simple #i ...

  3. [LOJ] 分块九题 3

    https://loj.ac/problem/6279 区间修改,区间查询前驱. TLE无数,我觉得这代码最精髓的就是block=1000. 谜一样的1000. 两个启示: 块内可以维护数据结构,比如 ...

  4. [LOJ] 分块九题 2

    https://loj.ac/problem/6278 区间修改,查询区间第k大. 块内有序(另存),块内二分. 还是用vector吧,数组拷贝排序,下标搞不来.. //Stay foolish,st ...

  5. [LOJ] 分块九题 1

    https://loj.ac/problem/6277 区间修改,单点查询. //Stay foolish,stay hungry,stay young,stay simple #include< ...

  6. [LOJ] 分块九题 7

    区间加法,区间乘法,单点查询. 洛谷线段树2 屡清加法乘法的关系,定义答案为 a*mut+add 对于整块: 新的乘w,mut和add都要乘w 新的加w,add加w //Stay foolish,st ...

  7. [LOJ] 分块九题 5

    区间开平方,区间查询. lazy标记改为区间是否全是1或者0,这样的区间是没有更新价值的. //Stay foolish,stay hungry,stay young,stay simple #inc ...

  8. 数列分块总结——题目总版(hzwer分块九题及其他题目)(分块)

    闲话 莫队算法似乎还是需要一点分块思想的......于是我就先来搞分块啦! 膜拜hzwer学长神犇%%%Orz 这九道题,每一道都堪称经典,强力打Call!点这里进入 算法简述 每一次考试被炸得体无完 ...

  9. hzwer分块九题(暂时持续更新)

    hzwer分块9题 分块1:区间加法,单点查询 Code #include<bits/stdc++.h> #define in(i) (i=read()) using namespace ...

随机推荐

  1. 开挂的map

    转自:http://blog.csdn.net/sunshinewave/article/details/8067862 #include<iostream> #include<ma ...

  2. bzoj 3667: Rabin-Miller算法【Miller-Rabin】

    Miller-Rabin模板 #include<iostream> #include<cstdio> #include<algorithm> using names ...

  3. Oracle更新数据为MD5加密数据

    业务场景:在做安全等保问题,需要将原来保存的用户明文密码改成md5加密的密文密码,数据库是Oracle的 首先Oracle要管理员账号登录才可以调md5函数,具体函数是DBMS_OBFUSCATION ...

  4. bug日志-天坑,Spring Security的登陆报错:An internal error occurred while trying to authenticate the user.

    在学习Spring Security的时候,我的编辑器给我报错:An internal error occurred while trying to authenticate the user. 明明 ...

  5. 5.replace的用法,while循环,continue

    1.relace s='dadk12' print(s.replace('生活','dd'))==打印返回dadk12,即使没有也不会报错. 2.while continue while循环输出 1 ...

  6. 合作网络(Corporative Network )并查集+路径压缩

    #include <iostream> #include <algorithm> #include <string> using namespace std; + ...

  7. Github配置SSH连接

    安装git.exe,打开Git Bash 1.检查是否已经有SSH Key. $cd /.ssh 2.生成一个新的SSH. $ ssh-keygen -t rsa -C "email@git ...

  8. 在服务器上执行hbase的jar包

    hadoop命令执行hbase应用jar包时的环境变量加载问题 Apache HBase ™ Reference Guide HBase, MapReduce, and the CLASSPATH

  9. 18.3.1获得Class对象

    package d18_3_1; /** * Java中的java.lang.Class,简单理解就是为每个java对象的类型标识的类, * 虚拟机使用运行时类型信息选择正确的执行方法,用来保存这些运 ...

  10. Suricata里的规则与Snort区别之处

    不多说,直接上干货! 见官网 https://suricata.readthedocs.io/en/latest/rules/differences-from-snort.html