BZOJ 3236: [Ahoi2013]作业

题目

3236: [Ahoi2013]作业

Time Limit: 100 Sec  Memory Limit: 512 MB
Submit: 732  Solved: 271

Description

Input

Output

Sample Input

3 4
1 2 2
1 2 1 3
1 2 1 1
1 3 1 3
2 3 2 3

Sample Output

2 2
1 1
3 2
2 1

HINT

N=100000,M=1000000

Source

By wangyisong1996加强数据

题解

一道卡测评的好题!用传说的莫队算法能水过,就是我们把每个询问x0,y0看作平面上的一个点,那么暴力转移的代价就是两个点之间的距离,我们把x轴分块,这样总的转移的代价和就是O(sqrt(n)*n)的了。由于这道题需要树状数组维护一下,所以这题的复杂度是O(nsqrt(n)logn)的。

代码

  1 /*Author:WNJXYK*/
  2 #include<cstdio>
  3 #include<algorithm>
  4 using namespace std;
  5
  6 const int Maxm=1000000;
  7 struct QuestionNode{
  8     int l,r;
  9     int a,b;
 10     int index;
 11 };
 12 QuestionNode quest[Maxm+10];
 13 struct AnsNode{
 14     int Ans1,Ans2;
 15 };
 16 AnsNode ans[Maxm+10];
 17
 18 const int Maxn=100000;
 19 int cnt[Maxn+10];
 20
 21 int siz;
 22 int n,m;
 23 int num[Maxn+10];
 24
 25 int sum1[Maxn+10],sum2[Maxn+10],cot[Maxn+10];
 26
 27 inline int remin(int a,int b){
 28     if (a<b) return a;
 29     return b;
 30 }
 31
 32 inline bool cmp(QuestionNode a,QuestionNode b){
 33     if (cnt[a.l]<cnt[b.l]) return true;
 34     if (cnt[a.l]==cnt[b.l] && a.r<b.r) return true;
 35     return false;
 36 }
 37
 38 inline int lowbit(int x){
 39     return x&-x;
 40 }
 41
 42 inline void add(int c[],int x,int num){
 43     for (int i=x;i<=Maxn;i+=lowbit(i))c[i]+=num;
 44 }
 45
 46 inline int get(int c[],int x){
 47     int res=0;
 48     for (int i=x;i;i-=lowbit(i))res+=c[i];
 49     return res;
 50 }
 51
 52 int main(){
 53     scanf("%d%d",&n,&m);
 54     //GetCnt
 55     while(siz*siz<n) siz++;
 56     for (int i=1;i<=siz;i++){
 57         int st=siz*(i-1)+1,ed=(remin(siz*i,n));
 58         for (int j=st;j<=ed;j++){
 59             cnt[j]=i;
 60         }
 61     }
 62     //GetNum
 63     for (int i=1;i<=n;i++) scanf("%d",&num[i]);
 64     //GetQuest
 65     for (int i=1;i<=m;i++)scanf("%d%d%d%d",&quest[i].l,&quest[i].r,&quest[i].a,&quest[i].b);
 66     for (int i=1;i<=m;i++)quest[i].index=i;
 67     //SortQuest
 68     sort(quest+1,quest+m+1,cmp);
 69     //GetAns
 70     int left=1,right=0;
 71     for (int i=1;i<=m;i++){
 72         int l=quest[i].l,r=quest[i].r;
 73         int a=quest[i].a,b=quest[i].b;
 74         int index=quest[i].index;
 75         while (left<l){
 76             cot[num[left]]--;
 77             add(sum1,num[left],-1);
 78             if (cot[num[left]]==0) add(sum2,num[left],-1);
 79             left++;
 80         }
 81         while(l<left){
 82             left--;
 83             cot[num[left]]++;
 84             add(sum1,num[left],1);
 85             if (cot[num[left]]==1) add(sum2,num[left],1);
 86         }
 87         while(right<r){
 88             right++;
 89             cot[num[right]]++;
 90             add(sum1,num[right],1);
 91             if (cot[num[right]]==1) add(sum2,num[right],1);
 92         }
 93         while(r<right){
 94             cot[num[right]]--;
 95             add(sum1,num[right],-1);
 96             if (cot[num[right]]==0) add(sum2,num[right],-1);
 97             right--;
 98         }
 99         ans[index].Ans1=get(sum1,b)-get(sum1,a-1);
100         ans[index].Ans2=get(sum2,b)-get(sum2,a-1);
101     }
102     //Print
103     for (int i=1;i<=m;i++) printf("%d %d\n",ans[i].Ans1,ans[i].Ans2);
104     return 0;
105 }

时间: 2024-08-25 06:01:21

BZOJ 3236: [Ahoi2013]作业的相关文章

bzoj 3236: [Ahoi2013]作业(缺线段树)

3236: [Ahoi2013]作业 Time Limit: 100 Sec  Memory Limit: 512 MBSubmit: 1744  Solved: 702[Submit][Status][Discuss] Description Input Output Sample Input 3 4 1 2 2 1 2 1 3 1 2 1 1 1 3 1 3 2 3 2 3 Sample Output 2 2 1 1 3 2 2 1 HINT N=100000,M=1000000 Sourc

BZOJ 3236: [Ahoi2013]作业( 莫队 + BIT )

莫队..用两个树状数组计算.时间复杂度应该是O(N1.5logN). 估计我是写残了...跑得很慢... ------------------------------------------------------------------------- #include<bits/stdc++.h> using namespace std; #define lowbit(x) ((x) & -(x)) const int maxn = 100009; const int maxm =

[BZOJ 3236] [Ahoi2013] 作业 &amp;&amp; [BZOJ 3809] 【莫队 | 分块】

题目链接: BZOJ - 3236   BZOJ - 3809 算法一:莫队 首先,单纯的莫队算法是很好想的,就是用普通的第一关键字为 l 所在块,第二关键字为 r 的莫队. 这样每次端点移动添加或删除一个数字,用树状数组维护所求的信息就是很容易的.由于这里有 logn复杂度,所以复杂度还是挺高的. 于是 BZOJ-3236 的时限 100s,我的代码跑了 98s,险过...... However..BZOJ-3809 的出题人(SLYZ的神犇)就没有这么善良了!直接内存限制 28MB 就直接把

树套树专题——bzoj 3110: [Zjoi2013] K大数查询 &amp; 3236 [Ahoi2013] 作业 题解

[原题1] 3110: [Zjoi2013]K大数查询 Time Limit: 20 Sec  Memory Limit: 512 MB Submit: 978  Solved: 476 Description 有N个位置,M个操作.操作有两种,每次操作如果是1 a b c的形式表示在第a个位置到第b个位置,每个位置加入一个数c 如果是2 a b c形式,表示询问从第a个位置到第b个位置,第C大的数是多少. Input 第一行N,M 接下来M行,每行形如1 a b c或2 a b c Outpu

【BZOJ 3236】 [Ahoi2013]作业

3236: [Ahoi2013]作业 Time Limit: 100 Sec Memory Limit: 512 MB Submit: 819 Solved: 307 [Submit][Status][Discuss] Description Input Output Sample Input 3 4 1 2 2 1 2 1 3 1 2 1 1 1 3 1 3 2 3 2 3 Sample Output 2 2 1 1 3 2 2 1 HINT N=100000,M=1000000 Source

BZOJ3236: [Ahoi2013]作业

3236: [Ahoi2013]作业 Time Limit: 100 Sec  Memory Limit: 512 MBSubmit: 702  Solved: 262[Submit][Status] Description Input Output Sample Input 3 4 1 2 2 1 2 1 3 1 2 1 1 1 3 1 3 2 3 2 3 Sample Output 2 2 1 1 3 2 2 1 HINT N=100000,M=1000000 Source By wangy

AC日记——[Ahoi2013]作业 bzoj 3236

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*10

BZOJ 3236 AHOI 2013 作业 莫队算法

题目大意:给出一些数,问在一个区间中不同的数值有多少种,和在一个区间中不同的数值有多少个. 思路:由于没有修改,所以就想到了莫队算法.然后我写了5K+的曼哈顿距离最小生成树,然后果断T了.(100s的时限啊,刷status都要刷疯了..,结果最后加了手写读入也没能A).后来果断放弃,写了分块版的莫队算法.84sAC...这题卡的..貌似莫队并不是正解. 其实用分块来写莫队就很简单了.只需要将所有询问的区间排序,左端点所在块作为第一键值,右端点作为第二季键值排序,之后就可以转移了.理论上来时还是曼

BZOJ 3233: [Ahoi2013]找硬币( dp )

dp(x)表示最大面值为x时需要的最少硬币数. 枚举x的质因数p,  dp(x) = min( dp(x/p) - (p-1) * sigma[a[i]/x] ). ---------------------------------------------------------------------------------- #include<cstdio> #include<cstring> #include<algorithm> using namespace