标签:
Description
Input
Output
Sample Input
1 10 10 0 5 2 7 5 4 3 8 7 7 2 8 6 3 5 0 1 3 1 1 9 4 0 1 0 3 5 5 5 5 1 4 6 3 1 5 7 5 7 3
Sample Output
Case 1: 4 0 0 3 1 2 0 1 5 1
分析:离线离散化后,按时间插入主席树,最后求下区间个数和即可;
代码:
#include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <algorithm> #include <climits> #include <cstring> #include <string> #include <set> #include <map> #include <queue> #include <stack> #include <vector> #include <list> #define rep(i,m,n) for(i=m;i<=n;i++) #define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++) #define mod 1000000007 #define inf 0x3f3f3f3f #define vi vector<int> #define pb push_back #define mp make_pair #define fi first #define se second #define ll long long #define pi acos(-1.0) #define pii pair<int,int> #define Lson L, mid, rt<<1 #define Rson mid+1, R, rt<<1|1 const int maxn=1e5+10; using namespace std; ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);} ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;} int n,m,k,t,a[maxn],b[maxn*2],s[maxn*20],ls[maxn*20],rs[maxn*20],root[maxn*20],sz; inline ll read() { ll x=0;int f=1;char ch=getchar(); while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();} while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();} return x*f; } struct node { int x,y,z; node(){} node(int _x,int _y,int _z):x(_x),y(_y),z(_z){} }op[maxn]; void insert(int l,int r,int x,int &y,int v) { y=++sz; s[y]=s[x]+1; if(l==r)return; ls[y]=ls[x],rs[y]=rs[x]; int mid=l+r>>1; if(v<=mid)insert(l,mid,ls[x],ls[y],v); else insert(mid+1,r,rs[x],rs[y],v); } int query(int l,int r,int L,int R,int x,int y) { if(l==L&&r==R)return s[y]-s[x]; int mid=L+R>>1; if(r<=mid)return query(l,r,L,mid,ls[x],ls[y]); else if(l>mid)return query(l,r,mid+1,R,rs[x],rs[y]); else return query(l,mid,L,mid,ls[x],ls[y])+query(mid+1,r,mid+1,R,rs[x],rs[y]); } int main() { int i,j; scanf("%d",&t); while(t--) { sz=0; scanf("%d%d",&n,&m); rep(i,1,n)a[i]=read(),b[i]=a[i]; rep(i,1,m) { int c,d,e; c=read(),d=read(),e=read(); c++,d++; b[i+n]=e; op[i]=node(c,d,e); } printf("Case %d:\n",++k); sort(b+1,b+n+m+1); int num=unique(b+1,b+n+m+1)-b-1; rep(i,1,n) { a[i]=lower_bound(b+1,b+num+1,a[i])-b; insert(1,num,root[i-1],root[i],a[i]); } rep(i,1,m) { int x=op[i].x,y=op[i].y,z=op[i].z; z=lower_bound(b+1,b+num+1,z)-b; printf("%d\n",query(1,z,1,num,root[x-1],root[y])); } } //system("Pause"); return 0; }
标签:
原文地址:http://www.cnblogs.com/dyzll/p/5924853.html