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
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
1 1
3 2
2 1
HINT
N=100000,M=1000000
Source
题解:
明显莫队搞,然后就可以80s'+过掉了。。。
代码:
View Code
明显莫队搞,然后就可以80s'+过掉了。。。
代码:
1 #include<cstdio> 2 3 #include<cstdlib> 4 5 #include<cmath> 6 7 #include<cstring> 8 9 #include<algorithm> 10 11 #include<iostream> 12 13 #include<vector> 14 15 #include<map> 16 17 #include<set> 18 19 #include<queue> 20 21 #include<string> 22 23 #define inf 1000000000 24 25 #define maxn 1000000+5 26 27 #define maxm 500+100 28 29 #define eps 1e-10 30 31 #define ll long long 32 33 #define pa pair<int,int> 34 35 #define for0(i,n) for(int i=0;i<=(n);i++) 36 37 #define for1(i,n) for(int i=1;i<=(n);i++) 38 39 #define for2(i,x,y) for(int i=(x);i<=(y);i++) 40 41 #define for3(i,x,y) for(int i=(x);i>=(y);i--) 42 43 #define mod 1000000007 44 45 using namespace std; 46 47 inline int read() 48 49 { 50 51 int x=0,f=1;char ch=getchar(); 52 53 while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} 54 55 while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();} 56 57 return x*f; 58 59 } 60 int n,m,a[maxn],b[maxn],d[maxn],s[2][maxn]; 61 struct rec{int x,y,l,r,id;}c[maxn]; 62 struct recc{int x,y;}ans[maxn]; 63 inline bool cmp(rec x,rec y){return b[x.l]==b[y.l]?x.r<y.r:x.l<y.l;} 64 inline void add(int z,int x,int y) 65 { 66 for(;x<=n;x+=x&(-x))s[z][x]+=y; 67 } 68 inline int sum(int z,int x) 69 { 70 int t=0; 71 for(;x;x-=x&(-x))t+=s[z][x]; 72 return t; 73 } 74 inline void update(int x,int y) 75 { 76 if(y>0) 77 { 78 d[x]++; 79 if(d[x]==1)add(0,x,1); 80 add(1,x,1); 81 } 82 else 83 { 84 d[x]--; 85 if(!d[x])add(0,x,-1); 86 add(1,x,-1); 87 } 88 } 89 90 int main() 91 92 { 93 94 freopen("input.txt","r",stdin); 95 96 freopen("output.txt","w",stdout); 97 98 n=read();m=read();int block=sqrt(n); 99 for1(i,n)a[i]=read(),b[i]=(i-1)/block+1; 100 for1(i,m)c[i].l=read(),c[i].r=read(),c[i].x=read(),c[i].y=read(),c[i].id=i; 101 sort(c+1,c+m+1,cmp); 102 int l=1,r=0; 103 for1(i,m) 104 { 105 while(r<c[i].r)update(a[++r],1); 106 while(r>c[i].r)update(a[r--],-1); 107 while(l<c[i].l)update(a[l++],-1); 108 while(l>c[i].l)update(a[--l],1); 109 ans[c[i].id].x=sum(1,c[i].y)-sum(1,c[i].x-1); 110 ans[c[i].id].y=sum(0,c[i].y)-sum(0,c[i].x-1); 111 } 112 for1(i,m)printf("%d %d\n",ans[i].x,ans[i].y); 113 114 return 0; 115 116 }