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& ...
随机推荐
- Python之利用reduce函数求序列的最值及排序
在一般将Python的reduce函数的例子中,通常都是拿列表求和来作为例子.那么,是否还有其他例子呢? 本次分享将讲述如何利用Python中的reduce函数对序列求最值以及排序. 我们用r ...
- IFROG线上赛做过的题目
#6 1068: 找规律 int main(){ int t,n; cin>>t; while(t--){ cin>>n; ==)printf(); else printf(& ...
- 【NOIP模拟赛】超级树 DP
这个题我在考试的时候把所有的转移都想全了就是新加一个点时有I.不作为II.自己呆着III.连一个IV.连接两个子树中的两个V连接一个子树中的两个,然而V我并不会转移........ 这个题的正解体现了 ...
- centos安装net-speeder
以前介绍过VPS上安装锐速对VPS的加速效果,但是这货对 Linux 内核有要求,一般就只能在XEN或者KVM的机子上安装.不过还好锐速有一个免费的代替品:net-speeder,所以这里介绍一下 D ...
- 对Office文档进行授权
Microsoft.Office.Interop.Word.ApplicationClass app = new Microsoft.Office.Interop.Word.ApplicationCl ...
- RPC-Thrift(四)
Client Thrift客户端有两种:同步客户端和异步客户端. 同步客户端 同步客户端比较简单,以RPC-Thrift(一)中的的例子为基础进行研究源码,先看一下类图. TServiceClient ...
- php连接mysql报错——Fatal error: Call to undefined function mysql_connect() in
练习php连接mysql数据库 代码:mysql_connect("127.0.0.1:3306","root", ..... 浏览器报错:Fatal erro ...
- 【STSRM13】木之本樱
[题意]抽象模型后转化为:给定n个直线,ans+=C(x,4)*8,x为每个经过直线数>=4的点的直线数,不存在平行直线. [算法]数学 [题解] 运用了一个很简单的道理:经过同一个点的线段互相 ...
- [转载]C#用正则表达式 获取网页源代码标签的属性或值
最近调试程序需要用到获取网页指定标签的属性和值,找到了一个比较好的正则匹配方法,特此备份. [原]C#用正则表达式 获取网页源代码标签的属性或值 整理两个 在C#中,用正则表达式 获取网页源代码标签的 ...
- DP+贪心水题合集_C++
本文含有原创题,涉及版权利益问题,严禁转载,违者追究法律责任 本次是最后一篇免费的考试题解,以后的考试题目以及题解将会以付费的方式阅读,题目质量可以拿本次作为参考 本来半个月前就已经搞得差不多了,然后 ...