3809: Gty的二逼妹子序列

Time Limit: 80 Sec  Memory Limit: 28 MB
Submit: 1504  Solved: 436
[Submit][Status][Discuss]

Description

Autumn和Bakser又在研究Gty的妹子序列了！但他们遇到了一个难题。

Sample Input

10 10
4 4 5 1 4 1 5 1 2 1
5 9 1 2
3 4 7 9
4 4 2 5
2 3 4 7
5 10 4 4
3 9 1 1
1 4 5 9
8 9 3 3
2 2 1 6
8 9 1 4

2
0
0
2
1
1
1
0
1
2

5 9 1 2

3 4 7 9

4 4 2 5

2 3 4 7

Source

[Submit][Status][Discuss]

#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define CH c=getchar()
#define For(i,x,y) for(int i=x;i<=y;++i)
using namespace std;
const int N = 1e5+5;
const int Block_Size = 333/2;
char CH;bool f=0;for(;!isdigit(c);CH) if(c=='-') f=1;
int x=0;for(;isdigit(c);CH) x=(x<<1)+(x<<3)+c-48;return f?-x:x;
}
int block[N];
int a[N];
int cnt[N];
int ans[1000005];
struct TreeArray{
#define lowbit(x) (x&-x)
int c[N];
void del(int x){
cnt[x]--;if(cnt[x]!=0)return;
for(int i=x;i<N;i+=lowbit(i)) c[i]--;
}
void ins(int x){
cnt[x]++;if(cnt[x]>1)return;
for(int i=x;i<N;i+=lowbit(i)) c[i]++;
}
int sum(int x){
int res=0;
for(int i=x;i;i-=lowbit(i)) res+=c[i];
return res;
}
}T;
struct Que{
int l,r,rk,u,v;
void get(int i){
}
bool operator < (const Que&b)const{return block[l]<block[b.l]||block[l]==block[b.l]&&r<b.r;}
}q[1000005];
int main(){
int _b = 1;
For(i,1,n){
if(i%Block_Size==0)++_b;
block[i]=_b;
}
For(i,1,m) q[i].get(i);
sort(q+1,q+m+1);
_b=0;int maxr=0,nowl=0;
For(Q,1,m){
int l=q[Q].l,r=q[Q].r,u=q[Q].u,v=q[Q].v;
if(block[l]==_b){
For(i,maxr+1,r) T.ins(a[i]);
if(l<nowl) For(i,l,nowl-1) T.ins(a[i]);
else For(i,nowl,l-1) T.del(a[i]);
ans[q[Q].rk]=T.sum(v)-T.sum(u-1);
maxr=r;nowl=l;
}
else{
_b=block[l];
if(Q>1)For(i,nowl,maxr) T.del(a[i]);
nowl=l,maxr=r;
For(i,l,r) T.ins(a[i]);
ans[q[Q].rk]=T.sum(v)-T.sum(u-1);
}
}
For(i,1,m) printf("%d\n",ans[i]);
}

posted on 2017-01-21 17:37  rwy  阅读(130)  评论(0编辑  收藏  举报