[AHOI2013]作业 莫队 树状数组
#include<cmath>
#include<cstdio>
#include<algorithm>
#include<string>
#include<cstring>
#include<iostream>
#define REP(i,a,n)for(int i=a;i<=n;++i)
#define CLR(d,a)memset(d,a,sizeof(d)); using namespace std; void SetIO(string a){
string in=a+".in", out=a+".out";
freopen(in.c_str(),"r",stdin);
freopen(out.c_str(),"w",stdout);
} void End(){
fclose(stdin);
fclose(stdout);
} const int maxn=300000+4; int val[maxn]; int n,m; struct Queries{
int l,r,a,b;
Queries(int l=0,int r=0,int a=0,int b=0):l(l),r(r),a(a),b(b){}
}ask[maxn]; void Read(){
scanf("%d%d",&n,&m);
REP(i,1,n)scanf("%d",&val[i]);
REP(i,1,m){
int a,b,c,d;
scanf("%d%d%d%d",&a,&b,&c,&d);
ask[i]=Queries(a,b,c,d);
}
} int block, belong[maxn], A[maxn]; int get_belong(int i){ return (i-1)/block+1; } bool cmp(int i,int j){
if(belong[ask[i].l]==belong[ask[j].l]) return ask[i].r<ask[j].r;
return belong[ask[i].l]<belong[ask[j].l];
} void Build(){
block=sqrt(n);
REP(i,1,n) belong[i]=get_belong(i);
REP(i,1,m) A[i]=i;
sort(A+1,A+1+m,cmp);
} int count1[maxn]; struct BIT{
int C[2][maxn]; int lowbit(int t){return t&(-t);} void update(int pos,int o,int delta){
while(pos<=n){
C[o][pos]+=delta;
pos+=lowbit(pos);
}
} int query(int pos,int o){
int sum=0;
while(pos>0){
sum+=C[o][pos];
pos-=lowbit(pos);
}
return sum;
}
void Modify(int i,int delta){
if(i==0) return ;
update(i,0,delta);
count1[i]+=delta;
if(delta<0 && count1[i]==0) update(i,1,delta);
if(delta>0 && count1[i]==1) update(i,1,delta);
} }Tree; int ans1[maxn], ans2[maxn]; void Work(){
int l=1,r=1;
Tree.Modify(val[1],1);
REP(i,1,m){
int cur=A[i];
int l2=ask[cur].l;
int r2=ask[cur].r; if(r<r2) {
++r;
while(r<=r2){ Tree.Modify(val[r],1), ++r; }
--r;
}
else
while(r>r2) { Tree.Modify(val[r],-1), --r; } if(l<l2) {
while(l<l2){ Tree.Modify(val[l],-1); ++l; }
}
else {
--l;
while(l>=l2) { Tree.Modify(val[l],1), --l; }
++l;
} ans1[cur]=Tree.query(ask[cur].b,0)-Tree.query(ask[cur].a-1,0);
ans2[cur]=Tree.query(ask[cur].b,1)-Tree.query(ask[cur].a-1,1);
}
} void Print(){
REP(i,1,m)
printf("%d %d\n",ans1[i],ans2[i]);
} int main(){
SetIO("input");
Read();
Build();
Work();
Print();
End();
return 0;
}
[AHOI2013]作业 莫队 树状数组的更多相关文章
- BZOJ3236[Ahoi2013]作业——莫队+树状数组/莫队+分块
题目描述 输入 输出 样例输入 3 4 1 2 2 1 2 1 3 1 2 1 1 1 3 1 3 2 3 2 3 样例输出 2 2 1 1 3 2 2 1 提示 N=100000,M=1000000 ...
- COGS.1822.[AHOI2013]作业(莫队 树状数组/分块)
题目链接: COGS.BZOJ3236 Upd: 树状数组实现的是单点加 区间求和,采用值域分块可以\(O(1)\)修改\(O(sqrt(n))\)查询.同BZOJ3809. 莫队为\(O(n^{1. ...
- BZOJ 3236: [Ahoi2013]作业(莫队+树状数组)
传送门 解题思路 莫队+树状数组.把求\([a,b]\)搞成前缀和形式,剩下的比较裸吧,用\(cnt\)记一下数字出现次数.时间复杂度\(O(msqrt(n)log(n)\),莫名其妙过了. 代码 # ...
- bzoj3236 作业 莫队+树状数组
莫队+树状数组 #include<cstdio> #include<cstring> #include<iostream> #include<algorith ...
- BZOJ 3236 AHOI 2013 作业 莫队+树状数组
BZOJ 3236 AHOI 2013 作业 内存限制:512 MiB 时间限制:10000 ms 标准输入输出 题目类型:传统 评测方式:文本比较 题目大意: 此时己是凌晨两点,刚刚做了Co ...
- BZOJ_3289_Mato的文件管理_莫队+树状数组
BZOJ_3289_Mato的文件管理_莫队+树状数组 Description Mato同学从各路神犇以各种方式(你们懂的)收集了许多资料,这些资料一共有n份,每份有一个大小和一个编号 .为了防止他人 ...
- bzoj 3289: Mato的文件管理 莫队+树状数组
3289: Mato的文件管理 Time Limit: 40 Sec Memory Limit: 128 MB[Submit][Status][Discuss] Description Mato同学 ...
- 51nod 1290 Counting Diff Pairs | 莫队 树状数组
51nod 1290 Counting Diff Pairs | 莫队 树状数组 题面 一个长度为N的正整数数组A,给出一个数K以及Q个查询,每个查询包含2个数l和r,对于每个查询输出从A[i]到A[ ...
- 【BZOJ3460】Jc的宿舍(树上莫队+树状数组)
点此看题面 大致题意: 一棵树,每个节点有一个人,他打水需要\(T_i\)的时间,每次询问两点之间所有人去打水的最小等待时间. 伪·强制在线 这题看似强制在线,但实际上,\(pre\ mod\ 2\) ...
随机推荐
- pgpool中定义的数据库节点及pgpool支持的复制模式
/* * The first DB node id appears in pgpool.conf or the first "live" DB * node otherwise. ...
- My97 DatePicker获取自定义日期的前一天
1.控件,获取第一个input中的时间,再将这个时间的前一天赋值给第二个input <input type="text" class="form-control i ...
- 8、Situation-Dependent Combination of Long-Term and Session-Based Preferences in Group Recommendations: An Experimental Analysis ----组推荐中基于长期和会话偏好的情景依赖组合
一.摘要: 背景:会话组推荐系统的一个主要挑战是如何适当地利用群组成员之间的交互引起用户偏好,这可能会偏离用户的长期偏好.长期偏好和群组诱导的偏好之间的相对重要性应该根据具体的群组设置而变化. 本文: ...
- centos7安装anaconda之后报错:rpm: /home/wyl/anaconda3/lib/liblzma.so.5: version `XZ_5.1.2alpha' not found (required by /lib64/librpmio.so.3)
1.报错 参考:https://stackoverflow.com/questions/47633870/rpm-lib64-liblzma-so-5-version-xz-5-1-2alpha-no ...
- C语言基本语法——数组
一.一维数组 1.什么是数组 2.数组语法 3.下标 4.初始化 5.数组名和数组首地址 二.一维数组的应用 1.数组的赋值与拷贝 2.数组的正反遍历 3.随机数 4.数组乱序 5.数组的重复 三.二 ...
- vue自定义tap指令
1.Vue指令 Vue提供自定义实现指令的功能, 和组件类似,可以是全局指令和局部指令,详细可以参见vue官网自定义指令一节(https://cn.vuejs.org/v2/guide/custom- ...
- [USACO18JAN] Cow at Large G (dfs)
题目大意:有一只狐狸从给定的S点开始逃跑(出发),向叶节点移动以逃离这棵树,叶节点可能出现农民去抓捕狐狸,当农民和狐狸出现在同一个节点的时候,狐狸会被抓住,农民和狐狸移动速度相同,求抓捕狐狸所需要的最 ...
- NOIP2018提高组省一冲奖班模测训练(四)
NOIP2018提高组省一冲奖班模测训练(四) 这次比赛只AC了第一题,而且花了40多分钟,貌似是A掉第一题里面最晚的 而且还有一个半小时我就放弃了…… 下次即使想不出也要坚持到最后 第二题没思路 第 ...
- libvips
libvips : an image processing library libvips is a 2D image processing library. Compared tosimilar l ...
- C#-MSMQ安装及配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...