题意:询问区间有多少个连续的段,而且这段的颜色在[L,R]才算贡献,每段贡献是1。 有单点修改和区间查询。

思路:46min交了第一发树套树,T了。 稍加优化多交几次就过了。

不难想到,除了L这个点,其他的点都可以只统计这一段的段首。把位置看成x,颜色看成y,就成了二维平面就矩形内点的个数,这就是裸的树套树或者CDQ了。

树套树:34**ms。

/*
2019南昌网络赛I。
询问区间有多少个连续的段,而且这段的颜色在[L,R]才算贡献,每段贡献是1。 有单点修改和区间查询。
也可以CDQ来做,常数小很多。
*/
#pragma GCC optimize(2)
#include<bits/stdc++.h>
#define ll long long
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int maxn=;
struct in{
int l,r,sum;
}s[maxn*];
int a[maxn],rt[maxn],N,M,cnt;
void add(int &Now,int L,int R,int pos,int v)
{
if(!Now) Now=++cnt; s[Now].sum+=v;
if(L==R) return ;int Mid=(L+R)>>;
if(pos<=Mid) add(s[Now].l,L,Mid,pos,v);
else add(s[Now].r,Mid+,R,pos,v);
}
void Add(int x,int pos,int v)
{
while(x<=N){
add(rt[x],,N,pos,v);
x+=(-x)&x;
}
}
int query(int Now,int L,int R,int l,int r)
{
if(!Now||s[Now].sum==) return ;
if(l<=L&&r>=R) return s[Now].sum;
int Mid=(L+R)>>,res=;
if(l<=Mid) res+=query(s[Now].l,L,Mid,l,r);
if(r>Mid) res+=query(s[Now].r,Mid+,R,l,r);
return res;
}
int Query(int x,int L,int R)
{
if(L>R||x==) return ;
int res=; while(x){
res+=query(rt[x],,N,L,R);
x-=(-x)&x;
} return res;
}
void read(int &x){
x=; char c=getchar();
while(c>''||c<'') c=getchar();
while(c>=''&&c<='') x=x*+c-'',c=getchar();
}
int main()
{
scanf("%d%d",&N,&M);
rep(i,,N) {
read(a[i]);
if(a[i]!=a[i-]) Add(i,a[i],);
}
int opt,pos,L,R,x,y;
while(M--){
read(opt);
if(opt==){
read(L); read(R); read(x); read(y);
int ans=Query(R,x,y)-Query(L-,x,y);
if(a[L]==a[L-]&&a[L]<=y&&a[L]>=x) ans++;
printf("%d\n",ans);
}
else {
read(pos); read(x);
if(a[pos]==x) continue;
if(a[pos]!=a[pos-]) Add(pos,a[pos],-);
if(pos+!=N&&a[pos+]!=a[pos]) Add(pos+,a[pos+],-);
a[pos]=x;
if(a[pos]!=a[pos-]) Add(pos,a[pos],);
if(pos+!=N&&a[pos+]!=a[pos]) Add(pos+,a[pos+],);
}
}
return ;
}

CDQ:700ms。

/*
2019南昌网络赛I:
*/
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int maxn=;
struct in{
int opt,x,y,z,id;
//opt=0是修改,否则是查询。
//opt=1表示时间为x,查询[1,x],[y,z]的矩形面积
//时间为第一维,x为第二维,y到z为第三维
}s[maxn<<],q[maxn<<];
int c[maxn],ans[maxn],tot,Q;
int sum[maxn],N,M;
void add(int x,int val)
{
for(int i=x;i<=N;i+=(-i)&i) sum[i]+=val;
}
int query(int x){
int res=;
for(int i=x;i;i-=(-i)&i) res+=sum[i];
return res;
}
void CDQ(int y,int z,int L,int R) //x在[y,z]区间,操作在[L,R]。 不停地对x进行分治,并把[L,R]进行相应的划分
{
if(L>=R) return ;
if(y>z) return ;
if(y==z){ //特殊的一行
rep(i,L,R) {
if(!s[i].opt) add(s[i].y,s[i].z);
if(s[i].opt!=) ans[s[i].id]+=s[i].opt*(query(s[i].z)-query(s[i].y-));
}
rep(i,L,R) if(!s[i].opt) add(s[i].y,-s[i].z);
return ;
}
int Mid=(y+z)>>,F=L,C=L;
rep(i,L,R) {
if(s[i].x<=Mid) C++;
if(!s[i].opt&&s[i].x<=Mid) add(s[i].y,s[i].z);
if(s[i].opt!=&&s[i].x>Mid) ans[s[i].id]+=s[i].opt*(query(s[i].z)-query(s[i].y-));
}
rep(i,L,R) if(!s[i].opt&&s[i].x<=Mid) add(s[i].y,-s[i].z);
rep(i,L,R) if(s[i].x<=Mid) q[F++]=s[i]; else q[C++]=s[i];
rep(i,L,R) s[i]=q[i];
CDQ(y,Mid,L,F-); CDQ(Mid+,z,F,R);
}
int main()
{
scanf("%d%d",&N,&M);
rep(i,,N) {
scanf("%d",&c[i]);
if(c[i]!=c[i-]) s[++tot]=in{,i,c[i],,};
}
int opt,L,R,x,y;
rep(i,,M){
scanf("%d",&opt);
if(opt&){
scanf("%d%d",&x,&y);
if(y==c[x]) continue;
if(c[x]!=c[x-]) s[++tot]=in{,x,c[x],-,};
if(x<N&&c[x]!=c[x+]) s[++tot]=in{,x+,c[x+],-,};
c[x]=y;
if(c[x]!=c[x-]) s[++tot]=in{,x,c[x],,};
if(x<N&&c[x]!=c[x+]) s[++tot]=in{,x+,c[x+],,};
}
else {
scanf("%d%d%d%d",&L,&R,&x,&y); Q++;
s[++tot]=in{,R,x,y,Q};
s[++tot]=in{-,L,x,y,Q};
if(c[L]>=x&&c[L]<=y) ans[Q]++;
}
}
CDQ(,N,,tot);
rep(i,,Q) printf("%d\n",ans[i]);
return ;
}

2019南昌网络赛I:Yukino With Subinterval(CDQ) (树状数组套主席树)的更多相关文章

  1. 2019 ICPC 南昌网络赛I:Yukino With Subinterval(CDQ分治)

    Yukino With Subinterval Yukino has an array a_1, a_2 \cdots a_na1,a2⋯*a**n*. As a tsundere girl, Yuk ...

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

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

  3. 2019icpc徐州现场赛 H Yuuki and a problem (树状数组套主席树)

    题意 2e5的数组,q个操作 1.将\(a[x]\)改为y 2.求下标l到r内所有的\(a[i]\)通过加法不能构成的最小的值 思路 通过二操作可以知道需要提取l到r内的值及其数量,而提取下标为l到r ...

  4. 2019南昌网络赛  I. Yukino With Subinterval 树状数组套线段树

    I. Yukino With Subinterval 题目链接: Problem Descripe Yukino has an array \(a_1, a_2 \cdots a_n\). As a ...

  5. 13年山东省赛 Boring Counting(离线树状数组or主席树+二分or划分树+二分)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud 2224: Boring Counting Time Limit: 3 Sec   ...

  6. ACM-ICPC 2019南昌网络赛I题 Yukino With Subinterval

    ACM-ICPC 2019南昌网络赛I题 Yukino With Subinterval 题目大意:给一个长度为n,值域为[1, n]的序列{a},要求支持m次操作: 单点修改 1 pos val 询 ...

  7. ACM-ICPC 2019南昌网络赛F题 Megumi With String

    ACM-ICPC 南昌网络赛F题 Megumi With String 题目描述 给一个长度为\(l\)的字符串\(S\),和关于\(x\)的\(k\)次多项式\(G[x]\).当一个字符串\(str ...

  8. 2019南昌网络赛 hello 2019

    这道题和一道2017,2016的类似. A string t is called nice if a string “2017” occurs in t as a subsequence but a ...

  9. 2019南昌网络赛G. tsy's number

    题意:\(\sum_{i=1}^n\sum_{j=1}^n\sum_{k=1}^n\frac{\phi(i)*\phi(j^2)*\phi(k^3)}{\phi(i)*\phi(j)*\phi(k)} ...

随机推荐

  1. [LeetCode] 660. Remove 9 移除9

    Start from integer 1, remove any integer that contains 9 such as 9, 19, 29... So now, you will have ...

  2. 2 datax mysql 和 mysql之间相互导入

    插件文档: https://github.com/alibaba/DataX/blob/master/hdfswriter/doc/hdfswriter.md   1,参照第1篇日记,安装好datax ...

  3. 利用Jenkins打包并远程部署NodeJS应用

    本文Jenkins版本2.190.2,为19年11月最新 1.安装Jenkins.Git和NodeJS Jenkins安装参考:https://www.cnblogs.com/zhi-leaf/p/1 ...

  4. C++ 函数重载和参数的缺省值

    一.函数重载 1.1 重载的起源 自然语言中,一个词可以有许多不同的含义,即该词被重载了.人们可以通过上下文来判断该词到底是哪种含义."词的重载"可以使语言更加简练.例如" ...

  5. pyqt 调用QT设计师创建的对话框

    一.实验环境 1.Windows7x64_SP1 2.anaconda2.5.0 + python2.7(anaconda集成,不需单独安装) 3.pyinstaller3.0 二.实验步骤 2.1 ...

  6. go-gin-api 路由中间件 - 签名验证(七)

    概览 首先同步下项目概况: 上篇文章分享了,路由中间件 - Jaeger 链路追踪(实战篇),文章反响真是出乎意料, 「Go中国」 公众号也转发了,有很多朋友加我好友交流,直呼我大神,其实我哪是什么大 ...

  7. Zookeeper在linux上的安装

    1:进入 cd  /usr/local目录下 2:创建zookeeper目录  midir zookeeper 3:将压缩包复制到zookeeper目录下  cp /root/zookeeper/zo ...

  8. Vagrant 安装Oracle19c RAC测试环境的简单学习

    1. 学习自网站: https://xiaoyu.blog.csdn.net/article/details/103135158 简单学习了下 能够将oracle RAC开起来了 但是 对后期的维护和 ...

  9. Redis-2-五种基本类型及相关命令

    目录 1.字符串类型:string 1.1 命令 1.2 实践 2.散列类型:hash 2.1命令 2.2 实践 3.列表类型:list 3.1 命令 3.2 实践 4.集合类型:set 4.1 命令 ...

  10. 'while' statement cannot complete without throwing an exception

    You are probably using Android Studio or IntelliJ. If so, you can add this above your method contain ...