Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 80 Accepted Submission(s): 42
//1008 #include<stdio.h> #include<iostream> #include<algorithm> #include<map> #include<vector> #include<string.h> #include<stdlib.h> #include<math.h> using namespace std; const int MAXN=100010; int c[MAXN]; int n; int lowbit(int x) { return x&(-x); } void add(int i,int val) { while(i<=n) { c[i]+=val; i+=lowbit(i); } } int sum(int i) { int s=0; while(i>0) { s+=c[i]; i-=lowbit(i); } return s; } struct Node { int s,t; int h; int index; }node[MAXN]; int answer[MAXN]; struct SS { int index; int v; }nn[MAXN]; bool cmp(SS a,SS b) { return a.v<b.v; } bool cmp2(Node a,Node b) { return a.h<b.h; } int input() { int ret=0; char ch; ch=getchar(); while(ch<'0'||ch>'9')ch=getchar(); while(ch>='0'&&ch<='9') { ret*=10; ret+=ch-'0'; ch=getchar(); } return ret; } int main() { // freopen("H.in","r",stdin); // freopen("H.out","w",stdout); int T; int m; int iCase=0; //scanf("%d",&T); T=input(); while(T--) { iCase++; scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) { //scanf("%d",&nn[i].v); nn[i].v=input(); nn[i].index=i; } for(int i=0;i<m;i++) { // scanf("%d%d%d",&node[i].s,&node[i].t,&node[i].h); node[i].s=input(); node[i].t=input(); node[i].h=input(); node[i].s++; node[i].t++; node[i].index=i; } memset(c,0,sizeof(c)); sort(node,node+m,cmp2); /* for(int i=0;i<m;i++) printf("%d %d %d %d\n",node[i].s,node[i].t,node[i].index,node[i].h); */ sort(nn+1,nn+1+n,cmp); /* for(int i=1;i<=n;i++) printf("%d %d\n",nn[i].index,nn[i].v); */ int i,j; i=1; j=0; while(j<m) { while(i<=n) { if(nn[i].v>node[j].h)break; // printf("i:%d\n",i); add(nn[i].index,1); i++; } while(j<m) { //printf("%d %d\n",node[j].h,nn[i].v); if(i<=n&&node[j].h>=nn[i].v)break; // printf("j:%d\n",j); answer[node[j].index]=sum(node[j].t)-sum(node[j].s-1); //printf("%d %d\n",node[i].t,node[i].s-1); // printf("answer:%d %d\n",node[j].index,answer[node[j].index]); j++; } } printf("Case %d:\n",iCase); for(int i=0;i<m;i++) printf("%d\n",answer[i]); } return 0; }
//1008 #include<stdio.h> #include<iostream> #include<algorithm> #include<map> #include<vector> #include<string.h> #include<stdlib.h> #include<math.h> using namespace std; const int MAXN=100010; struct NN { int l,r; int sum; int lazy; int t; }segTree[MAXN*3]; int s[MAXN]; void Build(int i,int l,int r) { segTree[i].l=l; segTree[i].r=r; segTree[i].sum=0; segTree[i].lazy=0; if(l==r) { s[l]=i; return; } int mid=(l+r)>>1; Build(i<<1,l,mid); Build((i<<1)|1,mid+1,r); } void update(int t) { int m=s[t]; while(m) { segTree[m].sum+=1; m>>=1; } } int query(int i,int l,int r) { if(segTree[i].l==l&&segTree[i].r==r)return segTree[i].sum; int mid=(segTree[i].l+segTree[i].r)>>1; if(r<=mid)return query(i<<1,l,r); else if(l>mid) return query((i<<1)|1,l,r); else return query(i<<1,l,mid)+query((i<<1)|1,mid+1,r); } struct Node { int s,t; int h; int index; }node[MAXN]; int answer[MAXN]; struct SS { int index; int v; }nn[MAXN]; bool cmp(SS a,SS b) { return a.v<b.v; } bool cmp2(Node a,Node b) { return a.h<b.h; } int input() { int ret=0; char ch; ch=getchar(); while(ch<'0'||ch>'9')ch=getchar(); while(ch>='0'&&ch<='9') { ret*=10; ret+=ch-'0'; ch=getchar(); } return ret; } int main() { //freopen("H.in","r",stdin); //freopen("H.out","w",stdout); int T; int m; int n; int iCase=0; //scanf("%d",&T); T=input(); while(T--) { iCase++; scanf("%d%d",&n,&m); Build(1,1,n); for(int i=1;i<=n;i++) { nn[i].v=input(); nn[i].index=i; } for(int i=0;i<m;i++) { node[i].s=input(); node[i].t=input(); node[i].h=input(); node[i].s++; node[i].t++; node[i].index=i; } sort(node,node+m,cmp2); sort(nn+1,nn+1+n,cmp); int i,j; i=1; j=0; while(j<m) { while(i<=n) { if(nn[i].v>node[j].h)break; update(nn[i].index); i++; } while(j<m) { if(i<=n&&node[j].h>=nn[i].v)break; answer[node[j].index]=query(1,node[j].s,node[j].t); j++; } } printf("Case %d:\n",iCase); for(int i=0;i<m;i++) printf("%d\n",answer[i]); } return 0; }