NC26006. Twinkle
描述
输入描述
The first line contains three integers n, q, c — the number of the stars, the number of the views and the maximum brightness of the stars.
The next n lines contain the stars description. The i-th from these lines contains three integers — the coordinates of i-th star and its initial brightness.
The next q lines contain the views description. The i-th from these lines contains five integers — the moment of the i-th view and the coordinates of the viewed rectangle.
For all coordinates,
输出描述
For each view print the total brightness of the viewed stars.
示例1
输入:
3 3 5 1 1 2 2 3 0 3 3 1 0 1 1 3 3 1 2 2 3 3 2 2 1 3 3
输出:
3 3 5
C++11(clang++ 3.9) 解法, 执行用时: 697ms, 内存消耗: 6320K, 提交时间: 2019-09-04 01:33:06
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int N=1e6+9; int n,q,c,na,nx; int f[N],x[N],tim[N]; void add(int x,int y){for(;x<N;x+=(x&-x))f[x]+=y;} int query(int x){int ans=0;while(x)ans+=f[x],x-=(x&-x);return ans;} ll ans[N],gs[N],les[N]; struct T{int x,y,s,z,id;}a[N]; bool cmp1(T t1,T t2){if(t1.x!=t2.x)return t1.x<t2.x;return t1.id<t2.id;}/***很重要的一步 插入优先于查询***/ bool cmp2(T t1,T t2){if(t1.y!=t2.y)return t1.y<t2.y;} inline int getidx(int y){return lower_bound(x+1,x+nx+1,y)-x;} void cdq(int l,int r){ if(l==r)return; int mid=(l+r)>>1; cdq(l,mid),cdq(mid+1,r); sort(a+l,a+mid+1,cmp2),sort(a+mid+1,a+r+1,cmp2); int p=l,js=0; ll he=0; for(int i=mid+1;i<=r;i++){ while(p<=mid&&a[p].y<=a[i].y){if(a[p].id==0)add(a[p].s,1),js++,he+=x[a[p].s];p++;} if(a[i].id)gs[a[i].id]+=js*a[i].z,ans[a[i].id]+=he*a[i].z,les[a[i].id]+=query(a[i].s)*a[i].z; } for(int i=l;i<p;i++)if(a[i].id==0)add(a[i].s,-1); } int main(){ ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); cin>>n>>q>>c; for(int i=1,xx,y,s;i<=n;i++)cin>>xx>>y>>s,a[++na]=(T){xx,y,s,1,0},x[++nx]=s; for(int i=1,t,x1,y1,x2,y2;i<=q;i++){ cin>>t>>x1>>y1>>x2>>y2;tim[i]=t; a[++na]=(T){x1-1,y1-1,c-t,1,i}; a[++na]=(T){x1-1,y2,c-t,-1,i}; a[++na]=(T){x2,y1-1,c-t,-1,i}; a[++na]=(T){x2,y2,c-t,1,i}; x[++nx]=c-t; } sort(x+1,x+nx+1);nx=unique(x+1,x+nx+1)-x-1; for(int i=1;i<=na;i++)a[i].s=getidx(a[i].s); sort(a+1,a+na+1,cmp1); cdq(1,na); for(int i=1;i<=q;i++)cout<<ans[i]+1ll*gs[i]*tim[i]-1ll*(gs[i]-les[i])*(c+1)<<endl; return 0; }