#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T, typename U> using ordered_map = tree<T, U, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
#define FOR(i,j,k,in) for(int i=(j); i < (k);i+=in)
#define FORD(i,j,k,in) for(int i=(j); i >=(k);i-=in)
#define REP(i,b) FOR(i,0,b,1)
#define REPD(i,b) FORD(i,b,0,1)
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define all(x) begin(x), end(x)
#define MANY_TESTS int tcase; cin >> tcase; while(tcase--)
const double EPS = 1e-9;
const int MOD = 1e9+7;
const ll INFF = 1e18;
const int INF = 1e9;
const ld PI = acos((ld)-1);
const vi dy = {1, 0, -1, 0, -1, 1, 1, -1};
const vi dx = {0, 1, 0, -1, -1, 1, -1, 1};
void DBG(){cout << "]" << endl;}
template<typename T, typename ...U> void DBG(const T& head, const U... args){ cout << head << "; "; DBG(args...); }
#define dbg(...) cout << "Line(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__);
#define chk() cout << "Check at line(" << __LINE__ << ") hit." << endl;
template<class T, unsigned int U>
ostream& operator<<(ostream& out, const array<T, U> &v){out << "["; REP(i, U) out << v[i] << ", "; out << "]"; return out;}
template <class T, class U>
ostream& operator<<(ostream& out, const pair<T, U> &par) {out << "[" << par.first << ";" << par.second << "]"; return out;}
template <class T>
ostream& operator<<(ostream& out, const set<T> &cont) { out << "{"; for( const auto &x:cont) out << x << ", "; out << "}"; return out; }
template <class T, class U>
ostream& operator<<(ostream& out, const map<T, U> &cont) {out << "{"; for( const auto &x:cont) out << x << ", "; out << "}"; return out; }
template<class T>
ostream& operator<<(ostream& out, const vector<T> &v){ out << "["; REP(i, v.size()) out << v[i] << ", "; out << "]"; return out;}
template<class T>
istream& operator>>(istream& in, vector<T> &v){ for(auto &x : v) in >> x; return in; }
template<typename T>
struct fenwick{
vector<T> tree;
int n;
fenwick(int _n) : n(_n){
tree.resize(n + 5, 0);
}
void upd(int pos, T val){
pos++;
for(int i = pos; i <= n; i += (i & -i))
tree[i] += val;
}
ll pq(int r){
ll res = 0;
for(int i = r; i > 0; i -= (i & -i))
res += tree[i];
return res;
}
ll query(int l, int r){
return pq(r) - pq(l);
}
};
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
int n, q;
cin >> n >> q;
vector<array<int, 2>> points(n);
vector<array<int, 2>> points2(n);
vector<int> sum_comp(n);
vector<int> sum(n);
REP(i, n){
cin >> points[i][0] >> points2[i][0];
points[i][1] = points2[i][1] = i;
sum_comp[i] = points[i][0] + points2[i][0];
sum[i] = sum_comp[i];
}
const int block = 512;
sort(all(points));
sort(all(points2));
vector<int> compressed_sum(n);
vector<int> x_comp(n), y_comp(n);
REP(i, n){
x_comp[i] = points[i][0]; y_comp[i] = points2[i][0];
}
sort(all(sum_comp));
x_comp.erase(unique(all(x_comp)), x_comp.end());
y_comp.erase(unique(all(y_comp)), y_comp.end());
sum_comp.erase(unique(all(sum_comp)), sum_comp.end());
REP(i, n){
compressed_sum[i] = lower_bound(all(sum_comp), sum[i]) - sum_comp.begin();
points[i][0] = lower_bound(all(x_comp), points[i][0]) - x_comp.begin();
points2[i][0] = lower_bound(all(y_comp), points2[i][0]) - y_comp.begin();
}
struct query{
int x, y, z, id;
};
vector<query> queries(q);
REP(i, q){
cin >> queries[i].x >> queries[i].y >> queries[i].z;
queries[i].id = i;
queries[i].z = lower_bound(all(sum_comp), queries[i].z) - sum_comp.begin();
queries[i].x = lower_bound(all(x_comp), queries[i].x) - x_comp.begin();
array<int, 2> to_search = {queries[i].x, -INF};
queries[i].x = lower_bound(all(points), to_search) - points.begin();
queries[i].y = lower_bound(all(y_comp), queries[i].y) - y_comp.begin();
to_search[0] = queries[i].y;
queries[i].y = lower_bound(all(points2), to_search) - points2.begin();
}
auto comp = [&](query &lhs, query &rhs){
if((lhs.x >> 9) == (rhs.x >> 9)){
return lhs.y < rhs.y;
}
else{
return lhs.x < rhs.x;
}
};
sort(all(queries), comp);
fenwick<int> ft(n + 10);
vector<int> cnt(n, 0);
int xpointer = n; int ypointer = n;
auto move_x_left = [&](){
xpointer--;
int id = points[xpointer][1];
cnt[id]++;
if(cnt[id] == 2){
ft.upd(compressed_sum[id], 1);
}
};
auto move_x_right = [&](){
int id = points[xpointer][1];
cnt[id]--;
if(cnt[id] == 1){
ft.upd(compressed_sum[id], -1);
}
xpointer++;
};
auto move_y_left = [&](){
ypointer--;
int id = points2[ypointer][1];
cnt[id]++;
if(cnt[id] == 2){
ft.upd(compressed_sum[id], 1);
}
};
auto move_y_right = [&](){
int id = points2[ypointer][1];
cnt[id]--;
if(cnt[id] == 1){
ft.upd(compressed_sum[id], -1);
}
ypointer++;
};
vector<int> ans(q, 0);
REP(i, queries.size()){
auto que = queries[i];
while(xpointer > que.x)
move_x_left();
while(xpointer < que.x)
move_x_right();
while(ypointer > que.y)
move_y_left();
while(ypointer < que.y)
move_y_right();
ans[que.id] = ft.query(que.z, n + 5);
}
REP(i, q){
cout << ans[i] << "\n";
}
return 0;
}
Compilation message
examination.cpp: In function 'int main()':
examination.cpp:19:40: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<main()::query>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
19 | #define FOR(i,j,k,in) for(int i=(j); i < (k);i+=in)
| ^
examination.cpp:21:18: note: in expansion of macro 'FOR'
21 | #define REP(i,b) FOR(i,0,b,1)
| ^~~
examination.cpp:173:5: note: in expansion of macro 'REP'
173 | REP(i, queries.size()){
| ^~~
examination.cpp:96:15: warning: unused variable 'block' [-Wunused-variable]
96 | const int block = 512;
| ^~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
13 ms |
688 KB |
Output is correct |
8 |
Correct |
13 ms |
748 KB |
Output is correct |
9 |
Correct |
13 ms |
688 KB |
Output is correct |
10 |
Correct |
12 ms |
620 KB |
Output is correct |
11 |
Correct |
10 ms |
620 KB |
Output is correct |
12 |
Correct |
10 ms |
620 KB |
Output is correct |
13 |
Correct |
13 ms |
640 KB |
Output is correct |
14 |
Correct |
13 ms |
620 KB |
Output is correct |
15 |
Correct |
13 ms |
620 KB |
Output is correct |
16 |
Correct |
4 ms |
620 KB |
Output is correct |
17 |
Correct |
10 ms |
620 KB |
Output is correct |
18 |
Correct |
3 ms |
560 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1665 ms |
7404 KB |
Output is correct |
2 |
Correct |
1654 ms |
7916 KB |
Output is correct |
3 |
Correct |
1660 ms |
7568 KB |
Output is correct |
4 |
Correct |
1234 ms |
7612 KB |
Output is correct |
5 |
Correct |
221 ms |
7660 KB |
Output is correct |
6 |
Correct |
158 ms |
7532 KB |
Output is correct |
7 |
Correct |
715 ms |
7660 KB |
Output is correct |
8 |
Correct |
896 ms |
7668 KB |
Output is correct |
9 |
Correct |
803 ms |
7568 KB |
Output is correct |
10 |
Correct |
160 ms |
7404 KB |
Output is correct |
11 |
Correct |
1209 ms |
7532 KB |
Output is correct |
12 |
Correct |
83 ms |
7276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1665 ms |
7404 KB |
Output is correct |
2 |
Correct |
1654 ms |
7916 KB |
Output is correct |
3 |
Correct |
1660 ms |
7568 KB |
Output is correct |
4 |
Correct |
1234 ms |
7612 KB |
Output is correct |
5 |
Correct |
221 ms |
7660 KB |
Output is correct |
6 |
Correct |
158 ms |
7532 KB |
Output is correct |
7 |
Correct |
715 ms |
7660 KB |
Output is correct |
8 |
Correct |
896 ms |
7668 KB |
Output is correct |
9 |
Correct |
803 ms |
7568 KB |
Output is correct |
10 |
Correct |
160 ms |
7404 KB |
Output is correct |
11 |
Correct |
1209 ms |
7532 KB |
Output is correct |
12 |
Correct |
83 ms |
7276 KB |
Output is correct |
13 |
Correct |
1677 ms |
7660 KB |
Output is correct |
14 |
Correct |
1677 ms |
7560 KB |
Output is correct |
15 |
Correct |
1670 ms |
7640 KB |
Output is correct |
16 |
Correct |
1260 ms |
7660 KB |
Output is correct |
17 |
Correct |
245 ms |
7788 KB |
Output is correct |
18 |
Correct |
160 ms |
7660 KB |
Output is correct |
19 |
Correct |
1697 ms |
7532 KB |
Output is correct |
20 |
Correct |
1702 ms |
7532 KB |
Output is correct |
21 |
Correct |
1122 ms |
7532 KB |
Output is correct |
22 |
Correct |
162 ms |
7532 KB |
Output is correct |
23 |
Correct |
1212 ms |
7404 KB |
Output is correct |
24 |
Correct |
82 ms |
7276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
13 ms |
688 KB |
Output is correct |
8 |
Correct |
13 ms |
748 KB |
Output is correct |
9 |
Correct |
13 ms |
688 KB |
Output is correct |
10 |
Correct |
12 ms |
620 KB |
Output is correct |
11 |
Correct |
10 ms |
620 KB |
Output is correct |
12 |
Correct |
10 ms |
620 KB |
Output is correct |
13 |
Correct |
13 ms |
640 KB |
Output is correct |
14 |
Correct |
13 ms |
620 KB |
Output is correct |
15 |
Correct |
13 ms |
620 KB |
Output is correct |
16 |
Correct |
4 ms |
620 KB |
Output is correct |
17 |
Correct |
10 ms |
620 KB |
Output is correct |
18 |
Correct |
3 ms |
560 KB |
Output is correct |
19 |
Correct |
1665 ms |
7404 KB |
Output is correct |
20 |
Correct |
1654 ms |
7916 KB |
Output is correct |
21 |
Correct |
1660 ms |
7568 KB |
Output is correct |
22 |
Correct |
1234 ms |
7612 KB |
Output is correct |
23 |
Correct |
221 ms |
7660 KB |
Output is correct |
24 |
Correct |
158 ms |
7532 KB |
Output is correct |
25 |
Correct |
715 ms |
7660 KB |
Output is correct |
26 |
Correct |
896 ms |
7668 KB |
Output is correct |
27 |
Correct |
803 ms |
7568 KB |
Output is correct |
28 |
Correct |
160 ms |
7404 KB |
Output is correct |
29 |
Correct |
1209 ms |
7532 KB |
Output is correct |
30 |
Correct |
83 ms |
7276 KB |
Output is correct |
31 |
Correct |
1677 ms |
7660 KB |
Output is correct |
32 |
Correct |
1677 ms |
7560 KB |
Output is correct |
33 |
Correct |
1670 ms |
7640 KB |
Output is correct |
34 |
Correct |
1260 ms |
7660 KB |
Output is correct |
35 |
Correct |
245 ms |
7788 KB |
Output is correct |
36 |
Correct |
160 ms |
7660 KB |
Output is correct |
37 |
Correct |
1697 ms |
7532 KB |
Output is correct |
38 |
Correct |
1702 ms |
7532 KB |
Output is correct |
39 |
Correct |
1122 ms |
7532 KB |
Output is correct |
40 |
Correct |
162 ms |
7532 KB |
Output is correct |
41 |
Correct |
1212 ms |
7404 KB |
Output is correct |
42 |
Correct |
82 ms |
7276 KB |
Output is correct |
43 |
Correct |
1725 ms |
7668 KB |
Output is correct |
44 |
Correct |
1688 ms |
7540 KB |
Output is correct |
45 |
Correct |
1709 ms |
7660 KB |
Output is correct |
46 |
Correct |
1256 ms |
7540 KB |
Output is correct |
47 |
Correct |
249 ms |
7660 KB |
Output is correct |
48 |
Correct |
157 ms |
7404 KB |
Output is correct |
49 |
Correct |
754 ms |
7660 KB |
Output is correct |
50 |
Correct |
939 ms |
7660 KB |
Output is correct |
51 |
Correct |
836 ms |
7640 KB |
Output is correct |
52 |
Correct |
185 ms |
7404 KB |
Output is correct |
53 |
Correct |
1195 ms |
7532 KB |
Output is correct |