AC日记——[Ahoi2013]作业 bzoj 3236
思路:
莫队+树状数组维护;
代码:
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; #define maxn 100005 struct QueryType {
int l,r,a,b,id;
};
struct QueryType qu[maxn*]; int n,sum[maxn],sum_[maxn],ti[maxn],ans[maxn*][];
int ai[maxn],size,bel[maxn],m; inline void in(int &now)
{
char Cget=getchar();now=;
while(Cget>''||Cget<'') Cget=getchar();
while(Cget>=''&&Cget<='')
{
now=now*+Cget-'';
Cget=getchar();
}
} inline int lowbit(int x)
{
return x&(-x);
} inline void sadd(int x,int k)
{
while(x<=n) sum[x]+=k,x+=lowbit(x);
} inline int ssum(int p,int x)
{
int res=;p--;
while(x) res+=sum[x],x-=lowbit(x);
while(p) res-=sum[p],p-=lowbit(p);
return res;
} inline void tadd(int x,int k)
{
while(x<=n) sum_[x]+=k,x+=lowbit(x);
} inline int tsum(int p,int x)
{
int res=;p--;
while(x) res+=sum_[x],x-=lowbit(x);
while(p) res-=sum_[p],p-=lowbit(p);
return res;
} inline bool cmp(QueryType aaa,QueryType bbb)
{
if(bel[aaa.l]==bel[bbb.l])
{
if(bel[aaa.r]==bel[bbb.r])
{
if(aaa.l==bbb.l) return aaa.r<bbb.r;
else return aaa.l<bbb.l;
}
return bel[aaa.r]<bel[bbb.r];
}
else return bel[aaa.l]<bel[bbb.l];
} inline void updata(int now,int x)
{
int pre=ti[now];
ti[now]+=x,sadd(now,x);
if(pre==&&ti[now]==) tadd(now,);
if(pre==&&ti[now]==) tadd(now,-);
} int main()
{
in(n),in(m),size=sqrt(n);for(int i=;i<=n;i++) in(ai[i]),bel[i]=(i-)/size;
for(int i=;i<=m;i++) in(qu[i].l),in(qu[i].r),in(qu[i].a),in(qu[i].b),qu[i].id=i;
sort(qu+,qu+m+,cmp);int l=,r=,pos;
for(int no=;no<=m;no++)
{
if(l<qu[no].l) for(int i=l;i<qu[no].l;i++) updata(ai[i],-);
else for(int i=l-;i>=qu[no].l;i--) updata(ai[i],);
if(r>qu[no].r) for(int i=r;i>qu[no].r;i--) updata(ai[i],-);
else for(int i=r+;i<=qu[no].r;i++) updata(ai[i],);
l=qu[no].l,r=qu[no].r,ans[qu[no].id][]=ssum(qu[no].a,qu[no].b),ans[qu[no].id][]=tsum(qu[no].a,qu[no].b);
}
for(int i=;i<=m;i++) printf("%d %d\n",ans[i][],ans[i][]);
return ;
}
AC日记——[Ahoi2013]作业 bzoj 3236的更多相关文章
- [BZOJ 3236] [Ahoi2013] 作业 && [BZOJ 3809] 【莫队(+分块)】
题目链接: BZOJ - 3236 BZOJ - 3809 算法一:莫队 首先,单纯的莫队算法是很好想的,就是用普通的第一关键字为 l 所在块,第二关键字为 r 的莫队. 这样每次端点移动添加或删 ...
- 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 ...
- AC日记——NOI2016区间 bzoj 4653
4653 思路: 线段树,指针滑动: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 1000005 #def ...
- AC日记——Rmq Problem bzoj 3339
3339 思路: 恶心: 代码: #include <cstdio> #include <cstring> #include <iostream> #include ...
- AC日记——[HNOI2008]越狱 bzoj 1008
1008 思路: 越狱情况=总情况-不越狱情况: 代码: #include <cstdio> #include <cstring> #include <iostream& ...
随机推荐
- LeetCode--Reverse Linked List(Java)
相似题目: Palindrome Number Valid PalinDrome Reverse Linked List Palindrome Linked List 翻转单链表(要注意的是是否含有头 ...
- Ural 1297 Palindrome(Manacher或者后缀数组+RMQ-ST)
1297. Palindrome Time limit: 1.0 second Memory limit: 64 MB The “U.S. Robots” HQ has just received a ...
- 【BZOJ 4007】[JLOI2015]战争调度 DP+搜索+状压
又是一道思路清新的小清晰. 观察题目,如果我们确定了平民或者贵族的任意一方,我们便可以贪心的求出另一方,至此20分:我们发现层数十分小,那么我们就也是状压层数,用lca转移,线性dp,至此50分(好像 ...
- 【BZOJ 1724】[Usaco2006 Nov]Fence Repair 切割木板 堆+贪心
堆对于stl priority_queue ,我们自己定义的类自己重载<,对于非自定义类我们默认大根堆,如若改成小根堆则写成std::priority<int,vector<int& ...
- HDU 1203 01背包
I NEED A OFFER! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- linux下解压,压缩命令大全---方便新手查看
本文最后讲述如何在打包时排除某些文件 .tar 解包:tar xvf FileName.tar 打包:tar cvf FileName.tar DirName (注:tar是打包,不是压缩!) --- ...
- HDU1878 欧拉回路---(并查集+图论性质)
http://acm.hdu.edu.cn/showproblem.php?pid=1878 欧拉回路 Time Limit: 2000/1000 MS (Java/Others) Memory ...
- [bzoj2049][Sdoi2008]Cave 洞穴勘测——lct
Brief Description 给定一个森林,您需要支持两种操作: 链接两个节点. 断开两个节点之间的链接. Algorithm Design 对于树上的操作,我们现在已经有了树链剖分可以处理这些 ...
- 硬币问题 tarjan缩点+DP 莫涛
2013-09-15 20:04 题目描述 有这样一个游戏,桌面上摆了N枚硬币,分别标号1-N,每枚硬币有一个分数C[i]与一个后继硬币T[i].作为游戏参与者的你,可以购买一个名为mlj的小机器人, ...
- Python学习笔记 - day6 - 函数
函数 函数在编程语言中就是完成特定功能的一个词句组(代码块),这组语句可以作为一个单位使用,并且给它取一个名字.可以通过函数名在程序的不同地方多次执行(这叫函数的调用).函数在编程语言中有基本分为:预 ...