AC日记——数颜色 bzoj 2120
思路:
带修改的莫队;
对于离线排序询问的算法,如何修改呢?
每个询问添加一个修改标记;
表示当前询问在第几个修改之后;
然后把修改标记作为第三关键字来排序;
每次更新端点,先更新时间;
块的大小为n的2/3次方;
来,上代码:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; #define maxn 100050 struct QueryType {
int l,r,t,id;
};
struct QueryType qu[maxn]; struct ChangeType {
int to,x,h;
};
struct ChangeType cha[maxn]; int n,m,size=,tot,num,ai[maxn],bel[maxn],ti[maxn*],ans[maxn];
int now; bool if_[maxn]; inline void in(int &now)
{
char Cget=getchar();now=;
while(Cget>''||Cget<'') Cget=getchar();
while(Cget>=''&&Cget<='')
{
now=now*+Cget-'';
Cget=getchar();
}
} bool cmp(QueryType aa,QueryType bb)
{
if(bel[aa.l]==bel[bb.l])
{
if(bel[aa.r]==bel[bb.r]) return aa.t<bb.t;
else return aa.r<bb.r;
}
else return aa.l<bb.l;
} inline void change(int x)
{
if(if_[cha[x].to])
{
ti[ai[cha[x].to]]--;
if(!ti[ai[cha[x].to]]) now--;
}
cha[x].h=ai[cha[x].to];
ai[cha[x].to]=cha[x].x;
if(if_[cha[x].to])
{
if(!ti[ai[cha[x].to]]) now++;
ti[ai[cha[x].to]]++;
}
} inline void unchange(int x)
{
if(if_[cha[x].to])
{
ti[ai[cha[x].to]]--;
if(!ti[ai[cha[x].to]]) now--;
}
ai[cha[x].to]=cha[x].h;
if(if_[cha[x].to])
{
if(!ti[ai[cha[x].to]]) now++;
ti[ai[cha[x].to]]++;
}
} inline void updata(int to,int x)
{
int pos=ti[ai[to]];
ti[ai[to]]+=x;
if(ti[ai[to]]==&&pos==) now--;
if(ti[ai[to]]==&&pos==) now++;
if(x==) if_[to]=true;
else if_[to]=false;
} int main()
{
in(n),in(m);char ch[];int l,r,t;
for(int i=;i<=n;i++) in(ai[i]),bel[i]=(i-)/size;
for(int i=;i<=m;i++)
{
scanf("%s",ch);in(l),in(r);
if(ch[]=='Q') qu[++tot].l=l,qu[tot].r=r,qu[tot].t=num,qu[tot].id=tot;
else cha[++num].to=l,cha[num].x=r;
}
sort(qu+,qu+tot+,cmp),l=,r=,t=,now=;
for(int no=;no<=tot;no++)
{
while(t<qu[no].t) change(++t);
while(t>qu[no].t) unchange(t--);
while(r<qu[no].r) updata(++r,);
while(r>qu[no].r) updata(r--,-);
while(l<qu[no].l) updata(l++,-);
while(l>qu[no].l) updata(--l,);
ans[qu[no].id]=now;
}
for(int i=;i<=tot;i++) printf("%d\n",ans[i]);
fclose(stdin),fclose(stdout);
return ;
}
AC日记——数颜色 bzoj 2120的更多相关文章
- AC日记——数1的个数 openjudge 1.5 40
40:数1的个数 总时间限制: 1000ms 内存限制: 65536kB 描述 给定一个十进制正整数n,写下从1到n的所有整数,然后数一下其中出现的数字“1”的个数. 例如当n=2时,写下1,2. ...
- AC日记——[SCOI2010]游戏 bzoj 1854
1854: [Scoi2010]游戏 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 4938 Solved: 1948[Submit][Status] ...
- AC日记——[Sdoi2013]森林 bzoj 3123
3123: [Sdoi2013]森林 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 3216 Solved: 944[Submit][Status] ...
- AC日记——王室联邦 bzoj 1086
Description “余”人国的国王想重新编制他的国家.他想把他的国家划分成若干个省,每个省都由他们王室联邦的一个成员来管理.他的国家有n个城市,编号为1..n.一些城市之间有道路相连,任意两个不 ...
- AC日记——[Hnoi2017]影魔 bzoj 4826
4826 思路: 主席树矩阵加减+单调栈预处理: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 200005 ...
- AC日记——[LNOI2014]LCA bzoj 3626
3626 思路: 离线操作+树剖: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 100005 #defin ...
- AC日记——[ZJOI2012]网络 bzoj 2816
2816 思路: 多个LCT: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 10005 #define l ...
- AC日记——[SCOI2009]游戏 bzoj 1025
[SCOI2009]游戏 思路: 和为n的几个数最小公倍数有多少种. dp即可: 代码: #include <bits/stdc++.h> using namespace std; #de ...
- AC日记——[HNOI2014]世界树 bzoj 3572
3572 思路: 虚树+乱搞: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 300005 #define ...
随机推荐
- 2018"百度之星"程序设计大赛 - 资格赛 - 题集
1001 $ 1 \leq m \leq 10 $ 像是状压的复杂度. 于是我们(用二进制)枚举留下的问题集合 然后把这个集合和问卷们的答案集合 $ & $ 一下 就可以只留下被选中的问题的答 ...
- 《Cracking the Coding Interview》——第5章:位操作——题目5
2014-03-19 06:22 题目:将整数A变成整数B,每次只能变一个二进制位,要变多少次呢. 解法:异或,然后求‘1’的个数. 代码: // 5.5 Determine the number o ...
- HDU 4288 Coder ( 离散化 + 离线 + 线段树 )
这题跟ZOJ 3606的解题思路很相似. 题意:有3中操作:1.向集合中增加一个数x(1≤x≤1e9):2.从集合中删去一个数x(保证这个数存在):3.查询集合中所有位置满足i%5==3的数a[i]的 ...
- c#中onclick事件请求的两种区别
在C#中如果是asp控件的button有两个click的调用,一个是OnClick,一个是OnClientClick.那么这两者有什么区别呢,下面就来说说区别. <asp:Button ID=& ...
- jQuery中Ajax的属性设置
1.全局设置为同步 $.ajaxSetup({ async: false });
- MyBatis 基本演示
主配置文件 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration P ...
- 【bzoj2097】[Usaco2010 Dec]Exercise 奶牛健美操 二分+贪心
题目描述 Farmer John为了保持奶牛们的健康,让可怜的奶牛们不停在牧场之间 的小路上奔跑.这些奶牛的路径集合可以被表示成一个点集和一些连接 两个顶点的双向路,使得每对点之间恰好有一条简单路径. ...
- Codeforces Round #306 (Div. 2) 550A Two Substrings
链接:http://codeforces.com/contest/550/problem/A 这是我第一次玩cf这种比赛,前面做了几场练习,觉得div2的前面几个还是比较水的. 所以看到这道题我果断觉 ...
- 枪战(maf)
枪战(maf) settle the dispute. Negotiations were very tense, and at one point the trigger-happy partici ...
- 安装PL/SQL Developer,链接本地64位Oracle
请参考: http://www.cnblogs.com/ymj126/p/3712727.html 或者 http://blog.csdn.net/cselmu9/article/details/80 ...