记录编号 |
191976 |
评测结果 |
AAAAAAAAAA |
题目名称 |
[AHOI 2013] 作业 |
最终得分 |
100 |
用户昵称 |
forever |
是否通过 |
通过 |
代码语言 |
C++ |
运行时间 |
15.347 s |
提交时间 |
2015-10-09 14:13:22 |
内存使用 |
99.47 MiB |
显示代码纯文本
- #include<cstdio>
- #include<cmath>
- #include<algorithm>
- using namespace std;
- struct dd{
- int l,r,id,A,B,belong;
- }jie[2000010];
- int belong[2000010],a[2000010],n,m,c[2000010],d[2000010],as[2000010],bs[2000010];
- int s[2000010];
- int lowbit(int x){
- return x&(-x);
- }
- bool comp(const dd a,const dd b){
- if(a.belong==b.belong) return a.r<b.r;
- return a.l<b.l;
- }
- int Que(int x,int y){
- int sum=0; x--;
- for(;y;y-=lowbit(y)) sum+=c[y];
- for(;x;x-=lowbit(x)) sum-=c[x];
- return sum;
- }
- int Quue(int x,int y){
- int sum=0;x--;
- for(;y;y-=lowbit(y)) sum+=d[y];
- for(;x;x-=lowbit(x)) sum-=d[x];
- return sum;
- }
- void update(int x,int po){
- for(int i=x;i<=n;i+=lowbit(i)) c[i]+=po;
- s[x]+=po;
- if(s[x]==0||(s[x]==1&&po>0)){
- for(int i=x;i<=n;i+=lowbit(i)) d[i]+=po;
- }
- }
- int main(){
- freopen("ahoi2013_homework.in","r",stdin);
- freopen("ahoi2013_homework.out","w",stdout);
- scanf("%d%d",&n,&m);
- a[0]=0;int bol=(int)sqrt(n+0.5);
- for(int i=1;i<=n;++i) {
- scanf("%d",&a[i]);
- belong[i]=(i+1)/bol+1;
- }
- for(int i=1;i<=m;++i){
- jie[i].id=i; scanf("%d%d%d%d",&jie[i].l,&jie[i].r,&jie[i].A,&jie[i].B);
- jie[i].belong=belong[jie[i].l];
- }
- sort(jie+1,jie+m+1,comp);
- int le=jie[1].l,re=jie[1].r;
- for(int i=le;i<=re;++i) update(a[i],1);
- as[jie[1].id]=Que(jie[1].A,jie[1].B);
- bs[jie[1].id]=Quue(jie[1].A,jie[1].B);
- for(int i=2;i<=m;++i){
- while(le>jie[i].l) update(a[--le],1);
- while(re<jie[i].r) update(a[++re],1);
- while(re>jie[i].r) update(a[re--],-1);
- while(le<jie[i].l) update(a[le++],-1);
- as[jie[i].id]=Que(jie[i].A,jie[i].B);
- bs[jie[i].id]=Quue(jie[i].A,jie[i].B);
- }
- for(int i=1;i<=m;++i) printf("%d %d\n",as[i],bs[i]);
- return 0;
- }