Radio stations CodeForces - 762E (cdq分治)
生活随笔
收集整理的這篇文章主要介紹了
Radio stations CodeForces - 762E (cdq分治)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
大意: 給定$n$個三元組$(x,r,f)$, 求所有對$(i,j)$, 滿足$i<j, |f_i-f_j|\le k, min(r_i,r_j)\ge |x_i-x_j|$
?
按$r$降序排, 去掉$min$, 然后就是個裸的三維數點問題, 可以用$CDQ$分治解決.
#include <iostream> #include <sstream> #include <algorithm> #include <cstdio> #include <math.h> #include <set> #include <map> #include <queue> #include <string> #include <string.h> #include <bitset> #define REP(i,a,n) for(int i=a;i<=n;++i) #define PER(i,a,n) for(int i=n;i>=a;--i) #define hr putchar(10) #define pb push_back #define lc (o<<1) #define rc (lc|1) #define mid ((l+r)>>1) #define ls lc,l,mid #define rs rc,mid+1,r #define x first #define y second #define io std::ios::sync_with_stdio(false) #define endl '\n' #define DB(a) ({REP(__i,1,n) cout<<a[__i]<<' ';hr;}) using namespace std; typedef long long ll; typedef pair<int,int> pii; const int P = 1e9+7, INF = 0x3f3f3f3f; ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;} ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;} ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;} inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;} //head#ifdef ONLINE_JUDGE const int N = 1e6+10; #else const int N = 111; #endifint n, k, tot; struct _ {int x,r,f;} a[N]; struct __ {int type,x,y;bool operator < (const __ & rhs) const {if (x!=rhs.x) return x<rhs.x;if (y!=rhs.y) return y<rhs.y;return type<rhs.type;} } e[N]; ll ans; int b[N], c[N], L, R; void add(int x, int v) {for (; x<=*b; x+=x&-x) c[x]+=v; } int qry(int x) {int r = 0;for (; x; x^=x&-x) r+=c[x];return r; } void qry(int x1, int y1, int x2, int y2) {e[++tot] = {1,x2,y2};e[++tot] = {1,x1-1,y1-1};e[++tot] = {2,x1-1,y2};e[++tot] = {2,x2,y1-1};b[++*b]=y2,b[++*b]=y1-1; } void ins(int x, int y) {e[++tot] = {0,x,y}; } void merge(int l, int r) {if (l==r) return;merge(l,mid),merge(mid+1,r);int now = l;REP(i,mid+1,r) {while (now<=mid&&e[now].x<=e[i].x) {if (e[now].type==0) add(e[now].y,1);++now;}if (e[i].type==1) ans+=qry(e[i].y);else if (e[i].type==2) ans-=qry(e[i].y);}while (now!=l) {if (e[--now].type==0) add(e[now].y,-1);}inplace_merge(e+l,e+mid+1,e+r+1); }int main() {scanf("%d%d", &n, &k);REP(i,1,n) scanf("%d%d%d",&a[i].x,&a[i].r,&a[i].f);sort(a+1,a+1+n,[](_ a,_ b){return a.r>b.r;});REP(i,1,n) {qry(a[i].f-k,a[i].x-a[i].r,a[i].f+k,a[i].x+a[i].r);ins(a[i].f,a[i].x);}sort(b+1,b+1+*b),*b=unique(b+1,b+1+*b)-b-1;REP(i,1,tot) e[i].y=lower_bound(b+1,b+1+*b,e[i].y)-b;merge(1,tot);printf("%lld\n", ans); }?
轉載于:https://www.cnblogs.com/uid001/p/10827935.html
總結
以上是生活随笔為你收集整理的Radio stations CodeForces - 762E (cdq分治)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数学图形(1.35)Kappa curv
- 下一篇: 记GMIC 2014