Hướng dẫn giải của Hai nhà máy điện nguyên tử
Chỉ dùng lời giải này khi không có ý tưởng, và đừng copy-paste code từ lời giải này. Hãy tôn trọng người ra đề và người viết lời giải.
Nộp một lời giải chính thức trước khi tự giải là một hành động có thể bị ban.
Nộp một lời giải chính thức trước khi tự giải là một hành động có thể bị ban.
Lưu ý: Các code mẫu dưới đây chỉ mang tính tham khảo và có thể không AC được bài tập này
Code mẫu của flashmt
#include <iostream> #include <algorithm> #include <cstdio> #include <vector> #define sqr(x) (1LL*(x)*(x)) using namespace std; struct point { int z; long long x, y; point() {} point(long long _x, long long _y, int _z) { x = _x; y = _y; z = _z; } bool operator < (point u) const { if (x != u.x) return x < u.x; if (y != u.y) return y < u.y; return z < u.z; } } a[400400]; int n, q, x[200200], y[200200], ans[200200], tree[400400]; long long d1[200200], d2[200200]; vector <long long> Y; int calc(long long d[], long long r) { return upper_bound(d, d + n, r) - d; } int main() { int x1, y1, x2, y2; long long r1, r2; cin >> n; for (int i = 0; i < n; i++) scanf("%d%d", x + i, y + i); cin >> x1 >> y1 >> x2 >> y2 >> q; for (int i = 0; i < n; i++) { d1[i] = sqr(x[i] - x1) + sqr(y[i] - y1); d2[i] = sqr(x[i] - x2) + sqr(y[i] - y2); a[i] = point(d1[i], d2[i], -1); Y.push_back(d2[i]); } sort(d1, d1 + n); sort(d2, d2 + n); for (int i = 0; i < q; i++) { scanf("%lld%lld", &r1, &r2); r1 *= r1; r2 *= r2; a[n + i] = point(r1, r2, i); Y.push_back(r2); ans[i] += calc(d1, r1) + calc(d2, r2); } sort(Y.begin(), Y.end()); Y.resize(unique(Y.begin(), Y.end()) - Y.begin()); sort(a, a + n + q); for (int i = 0; i < n + q; i++) { int y = lower_bound(Y.begin(), Y.end(), a[i].y) - Y.begin(); y++; if (a[i].z >= 0) while (y) ans[a[i].z] -= tree[y], y -= y & -y; else while (y <= int(Y.size())) tree[y]++, y += y & -y; } for (int i = 0; i < q; i++) printf("%d\n", ans[i]); }
Code mẫu của happyboy99x
#include<cstdio> #include<cmath> #include<algorithm> #include<vector> using namespace std; #define sqr(x) 1LL*(x)*(x) #define REP(i,n) for(int i = 0, _n = (n); i < _n; ++i) #define TR(v,it) for(__typeof((v).begin()) it = (v).begin(), _e = (v).end(); it != _e; ++it) typedef long long LL; const static int MAX = (int)(2e5) + 1; class FenwickTree { int t[MAX+1]; public: FenwickTree() { fill(t,t+MAX,0); } void add(int idx, int v){ for(; idx<=MAX; idx+=(idx+1)&-(idx+1)) t[idx]+=v; } int sum(int idx){ int r=0; for(;idx>=0; idx-=(idx+1)&-(idx+1)) r+=t[idx]; return r; } }; class CPoint { public: int x, y, id; //x: k/c toi nha may 1, y: k/c toi nha may 2 CPoint(int _x, int _y, int _id): x(_x), y(_y), id(_id) {} inline bool operator < (const CPoint & o) const { return y < o.y || y == o.y && x < o.x || y == o.y && x == o.x && id < o.id; } }; int distance( int x, int y, int a, int b ) { LL d = sqr(x-a) + sqr(y-b); int res = (int) floor(sqrt(d)); return sqr(res) < d ? res+1 : res; } int countX[MAX+1], countY[MAX+1], n, q, ax, bx, ay, by, answer[MAX]; vector<CPoint> a; FenwickTree bit; void enter() { //Enter and Pre-process scanf("%d",&n); REP(i,n) { int x, y; scanf("%d%d",&x,&y); a.push_back(CPoint(x,y,0)); } scanf("%d%d%d%d%d",&ax,&ay,&bx,&by,&q); TR(a,it) { int tmpx = distance(it->x, it->y, ax, ay), tmpy = distance(it->x, it->y, bx, by); if((it->x = tmpx) <= MAX) ++countX[tmpx]; if((it->y = tmpy) <= MAX) ++countY[tmpy]; } REP(i,q) { int x,y; scanf("%d%d",&x,&y); a.push_back(CPoint(x,y,i+1)); } REP(i,MAX) { countX[i+1] += countX[i]; countY[i+1] += countY[i]; } } void solve() { sort(a.begin(), a.end()); TR(a,it) if(it->id) answer[it->id - 1] = countX[it->x] + countY[it->y] - bit.sum(it->x); else bit.add(it->x, 1); } void print() { REP(i,q) printf("%d\n", answer[i]); } int main() { #ifndef ONLINE_JUDGE freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); #endif enter(); solve(); print(); return 0; }
Code mẫu của ladpro98
#include <iostream> #include <cstdio> #include <algorithm> #include <vector> #include <cmath> #define ii pair<int, int> #define iv pair<ii, ii> #define X first #define Y second const int ASK = 1; const int UPD = 0; const int N = 300005; const int M = N + N; using namespace std; int n, q, m, maxR; ii a, b, house[N]; iv Q[N + N]; int bit[M], res[M], Fa[M], Fb[M]; int dist(ii A, ii B) {return ceil(sqrt(pow(A.X - B.X, 2) + pow(A.Y - B.Y, 2)));} void Upd(int i) {for(i++; i <= maxR + 1; i += i & -i) bit[i]++;} int Get(int i) {int s = 0; for(i++; i; i -= i & -i) s += bit[i]; return s;} int main() { scanf("%d", &n); for(int i = 1; i <= n; i++) scanf("%d %d\n", &house[i].X, &house[i].Y); scanf("%d %d %d %d %d", &a.X, &a.Y, &b.X, &b.Y, &q); for(int i = 1; i <= n; i++) { house[i] = ii(dist(house[i], a), dist(house[i], b)); Fa[house[i].X]++; Fb[house[i].Y]++; maxR = max(maxR, max(house[i].X, house[i].Y)); } for(int i = 1; i <= q; i++) { scanf("%d %d", &Q[i].X.X, &Q[i].X.Y); Q[i].Y.X = ASK; Q[i].Y.Y = i; maxR = max(maxR, max(Q[i].X.X, Q[i].X.Y)); } for(int i = 1; i <= maxR; i++) Fa[i] += Fa[i - 1], Fb[i] += Fb[i - 1]; for(int i = 1; i <= n; i++) Q[q + i] = iv(house[i], ii(UPD, 0)); int m = q + n; sort(Q + 1, Q + 1 + m); for(int i = 1; i <= m; i++) { int x = Q[i].X.X, y = Q[i].X.Y; int t = Q[i].Y.X, p = Q[i].Y.Y; if (t == UPD) Upd(y); else res[p] = Fa[x] + Fb[y] - Get(y); } for(int i = 1; i <= q; i++) printf("%d\n", res[i]); return 0; }
Code mẫu của RR
#include <sstream> #include <iomanip> #include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <algorithm> #include <vector> #include <set> #include <map> #include <stack> #include <queue> #include <string> #include <deque> #include <complex> #define FOR(i,a,b) for(int i=(a),_b=(b); i<=_b; i++) #define FORD(i,a,b) for(int i=(a),_b=(b); i>=_b; i--) #define REP(i,a) for(int i=0,_a=(a); i<_a; i++) #define FORN(i,a,b) for(int i=(a),_b=(b);i<_b;i++) #define DOWN(i,a,b) for(int i=a,_b=(b);i>=_b;i--) #define SET(a,v) memset(a,v,sizeof(a)) #define sqr(x) ((x)*(x)) #define ll long long #define F first #define S second #define PB push_back #define MP make_pair #define DEBUG(x) cout << #x << " = "; cout << x << endl; #define PR(a,n) cout << #a << " = "; FOR(_,1,n) cout << a[_] << ' '; cout << endl; #define PR0(a,n) cout << #a << " = "; REP(_,n) cout << a[_] << ' '; cout << endl; using namespace std; //Buffer reading int INP,AM,REACHEOF; #define BUFSIZE (1<<12) char BUF[BUFSIZE+1], *inp=BUF; #define GETCHAR(INP) { \ if(!*inp) { \ if (REACHEOF) return 0;\ memset(BUF,0,sizeof BUF);\ int inpzzz = fread(BUF,1,BUFSIZE,stdin);\ if (inpzzz != BUFSIZE) REACHEOF = true;\ inp=BUF; \ } \ INP=*inp++; \ } #define DIG(a) (((a)>='0')&&((a)<='9')) #define GN(j) { \ AM=0;\ GETCHAR(INP); while(!DIG(INP) && INP!='-') GETCHAR(INP);\ if (INP=='-') {AM=1;GETCHAR(INP);} \ j=INP-'0'; GETCHAR(INP); \ while(DIG(INP)){j=10*j+(INP-'0');GETCHAR(INP);} \ if (AM) j=-j;\ } //End of buffer reading const long double PI = acos((long double) -1.0); const int MN = 1000111; struct Point { long long x, y; Point(long long x = 0, long long y = 0) : x(x), y(y) {} Point operator - (Point a) { return Point(x-a.x, y-a.y); } long long sqlen() { return x*x + y*y; } long long len() { return (long long) ceil(sqrt(sqlen()) - 1e-6); } } a[MN], P, Q; int d1[MN], d2[MN], n, q, res[MN], bit[MN]; #define _(x) ((x) & (-(x))) void update(int u) { ++bit[u]; u += _(u); if (u <= 1000000) update(u); } int get(int u) { if (u <= 0) return 0; return bit[u] + get(u - _(u)); } struct Query { int r1, r2; int id; } queries[MN]; bool operator < (const Query &a, const Query &b) { if (a.r1 != b.r1) return a.r1 < b.r1; if (a.r2 != b.r2) return a.r2 < b.r2; return a.id < b.id; } void init() { FOR(i,1,n) { d1[i] = (P - a[i]).len(); d2[i] = (Q - a[i]).len(); } } int main() { scanf("%d", &n); int x, y; FOR(i,1,n) { scanf("%d%d", &x, &y); a[i] = Point(x, y); } scanf("%d%d", &x, &y); P = Point(x, y); scanf("%d%d", &x, &y); Q = Point(x, y); init(); scanf("%d", &q); FOR(i,1,q) { scanf("%d%d", &queries[i].r1, &queries[i].r2); queries[i].id = i; } int saveq = q; FOR(i,1,n) { ++q; queries[q].r1 = d1[i]; queries[q].r2 = d2[i]; queries[q].id = 0; } sort(d1+1, d1+n+1); sort(d2+1, d2+n+1); d1[n+1] = d2[n+1] = 1000111000; sort(queries + 1, queries + q + 1); FOR(i,1,q) { if (queries[i].id == 0) { update(queries[i].r2); } else { res[queries[i].id] = upper_bound(d1+1, d1+n+2, queries[i].r1) - d1 - 1 + upper_bound(d2+1, d2+n+2, queries[i].r2) - d2 - 1 - get(queries[i].r2); } } FOR(i,1,saveq) printf("%d\n", res[i]); return 0; }
Code mẫu của hieult
#include <set> #include <map> #include <list> #include <cmath> #include <queue> #include <stack> #include <cstdio> #include <string> #include <vector> #include <cstdlib> #include <cstring> #include <sstream> #include <iomanip> #include <iostream> #include <algorithm> #include <ctime> #include <deque> #include <bitset> #include <cctype> #include <utility> #include <cassert> using namespace std; typedef long long ll; typedef long double ld; typedef unsigned int ui; typedef unsigned long long ull; #define Rep(i,n) for(int i = 0; i < (n); ++i) #define Repd(i,n) for(int i = (n)-1; i >= 0; --i) #define For(i,a,b) for(int i = (a); i <= (b); ++i) #define Ford(i,a,b) for(int i = (a); i >= (b); --i) //#define Fit(i,v) for(__typeof((v).begin()) i = (v).begin(); i != (v).end(); ++i) //#define Fitd(i,v) for(__typeof((v).rbegin()) i = (v).rbegin(); i != (v).rend(); ++i) #define mp make_pair #define pb push_back #define fi first #define se second #define sz(a) ((int)(a).size()) #define all(a) (a).begin(), (a).end() #define ms(a,x) memset(a, x, sizeof(a)) template<class F, class T> T convert(F a, int p = -1) { stringstream ss; if (p >= 0) ss << fixed << setprecision(p); ss << a; T r; ss >> r; return r; } template<class T> T gcd(T a, T b) { T r; while (b != 0) { r = a % b; a = b; b = r; } return a; } template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; } template<class T> T sqr(T x) { return x * x; } template<class T> T cube(T x) { return x * x * x; } template<class T> int getbit(T s, int i) { return (s >> i) & 1; } template<class T> T onbit(T s, int i) { return s | (T(1) << i); } template<class T> T offbit(T s, int i) { return s & (~(T(1) << i)); } template<class T> int cntbit(T s) { return s == 0 ? 0 : cntbit(s >> 1) + (s & 1); } const int bfsz = 1 << 16; char bf[bfsz + 5]; int rsz = 0; int ptr = 0; char gc() { if (rsz <= 0) { ptr = 0; rsz = (int) fread(bf, 1, bfsz, stdin); if (rsz <= 0) return EOF; } --rsz; return bf[ptr++]; } void ga(char &c) { c = EOF; while (!isalpha(c)) c = gc(); } int gs(char s[]) { int l = 0; char c = gc(); while (isspace(c)) c = gc(); while (c != EOF && !isspace(c)) { s[l++] = c; c = gc(); } s[l] = '\0'; return l; } template<class T> bool gi(T &v) { v = 0; char c = gc(); while (c != EOF && c != '-' && !isdigit(c)) c = gc(); if (c == EOF) return false; bool neg = c == '-'; if (neg) c = gc(); while (isdigit(c)) { v = v * 10 + c - '0'; c = gc(); } if (neg) v = -v; return true; } typedef pair<int, int> II; const ld PI = acos(-1.0); const ld eps = 1e-13; const int dr[] = {0, +1, 0, -1}; const int dc[] = {+1, 0, -1, 0}; const int inf = (int)1e9 + 5; const ll linf = (ll)1e16 + 5; const ll mod = (ll)1e9 + 7; #define maxn 200005 struct Point{ ll x, y; Point(){}; Point(ll _x, ll _y){ x = _x; y = _y; } }; struct Query{ ll r1, r2; int id; Query(){}; Query(ll _r1, ll _r2, int _id){ r1 = _r1; r2 = _r2; id = _id; } bool operator <(const Query &Q)const{ return r1 > Q.r1; } }q[maxn]; struct Dist{ ll d1, d2; Dist(){}; Dist(ll _d1, ll _d2){ d1 = _d1; d2 = _d2; } bool operator <(const Dist &D)const{ return d1 > D.d1; } }D[maxn]; int n, m, f[maxn], a[maxn * 2]; Point P[maxn], A, B; vector<ll> V; ll sqrDist(Point P0, Point P1){ return sqr(P0.x - P1.x) + sqr(P0.y - P1.y); } void update(int u){ for(int i = u; i > 0; i -= i & -i) a[i]++; } int get(int u){ int res = 0; for(int i = u; i < maxn * 2; i += i & -i){ res += a[i]; } return res; } int main(){ // freopen("in.txt", "r", stdin); cin >> n; Rep(i, n){ scanf("%lld %lld", &P[i].x, &P[i].y); } scanf("%lld %lld %lld %lld %d", &A.x, &A.y, &B.x, &B.y, &m); Rep(i, m) { scanf("%lld %lld", &q[i].r1, &q[i].r2); q[i].r1 = sqr(q[i].r1); q[i].r2 = sqr(q[i].r2); q[i].id = i; } Rep(i, n){ D[i] = Dist(sqrDist(P[i], A), sqrDist(P[i], B)); } Rep(i, n) V.pb(D[i].d2); Rep(i, m) V.pb(q[i].r2); sort(all(V)); Rep(i, n) D[i].d2 = lower_bound(all(V), D[i].d2) - V.begin() + 1; Rep(i, m) q[i].r2 = lower_bound(all(V), q[i].r2) - V.begin() + 1; sort(D, D + n); sort(q, q + m); ms(a, 0); int run = -1; Rep(i, m){ while(run + 1 < n && D[run + 1].d1 > q[i].r1) { run++; update(D[run].d2); } f[q[i].id] = n - get(q[i].r2 + 1); } Rep(i, m) printf("%d\n", f[i]); return 0; }
Code mẫu của skyvn97
#include<bits/stdc++.h> #define MAX 200200 #define FOR(i,a,b) for (int i=(a),_b=(b);i<=_b;i=i+1) #define REP(i,n) for (int i=0,_n=(n);i<_n;i=i+1) #define SQR(x) (1LL*(x)*(x)) #define fi first #define se second using namespace std; typedef long long ll; class FenwickTree { private: int n; vector<int> v; public: FenwickTree() { n=0; } FenwickTree(int n) { this->n=n; v.assign(n+7,0); } void update(int x,int d) { if (x<1) return; for (;x<=n;x+=x&-x) v[x]+=d; } int get(int x) const { int res=0; if (x>n) return (0); for (;x>=1;x&=x-1) res+=v[x]; return (res); } }; struct Query { ll r1,r2,id; Query() { r1=r2=id=0; } void input(int id) { this->id=id; int _r1,_r2; scanf("%d%d",&_r1,&_r2); r1=-SQR(_r1);r2=-SQR(_r2); } bool operator < (const Query &x) const { return (r1==x.r1?r2<x.r2:r1<x.r1); } }; pair<int,int> a[MAX]; pair<ll,ll> dis[MAX]; pair<int,int> cen1,cen2; vector<ll> allDis; Query query[MAX]; int n,q; int answer[MAX]; void init(void) { scanf("%d",&n); FOR(i,1,n) scanf("%d%d",&a[i].fi,&a[i].se); scanf("%d%d%d%d%d",&cen1.fi,&cen1.se,&cen2.fi,&cen2.se,&q); FOR(i,1,q) query[i].input(i); } ll distan(const pair<int,int> &a,const pair<int,int> &b) { return (SQR(a.fi-b.fi)+SQR(a.se-b.se)); } void precount(void) { FOR(i,1,n) dis[i]=make_pair(-distan(a[i],cen1),-distan(a[i],cen2)); sort(dis+1,dis+n+1); FOR(i,1,n) allDis.push_back(dis[i].se); sort(allDis.begin(),allDis.end()); allDis.resize(unique(allDis.begin(),allDis.end())-allDis.begin()); FOR(i,1,n) dis[i].se=lower_bound(allDis.begin(),allDis.end(),dis[i].se)-allDis.begin()+1; sort(query+1,query+q+1); } void process(void) { FenwickTree tree(n); int j=1; FOR(i,1,q) { while (j<=n && dis[j].fi<query[i].r1) { tree.update(dis[j].se,1); j++; } int t=lower_bound(allDis.begin(),allDis.end(),query[i].r2)-allDis.begin(); if (t>n) t=n; answer[query[i].id]=n-tree.get(t); } FOR(i,1,q) printf("%d\n",answer[i]); } int main(void) { #ifndef ONLINE_JUDGE freopen("tmp.txt","r",stdin); #endif // ONLINE_JUDGE init(); precount(); process(); return 0; }
Bình luận