#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;
}
T pq(int r){
T res = 0;
for(int i = r; i > 0; i -= (i & -i))
res += tree[i];
return res;
}
T 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 = 600;
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 / block == rhs.x / block){
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()){
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 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 |
15 ms |
748 KB |
Output is correct |
8 |
Correct |
18 ms |
748 KB |
Output is correct |
9 |
Correct |
14 ms |
748 KB |
Output is correct |
10 |
Correct |
14 ms |
768 KB |
Output is correct |
11 |
Correct |
12 ms |
620 KB |
Output is correct |
12 |
Correct |
13 ms |
620 KB |
Output is correct |
13 |
Correct |
14 ms |
620 KB |
Output is correct |
14 |
Correct |
14 ms |
620 KB |
Output is correct |
15 |
Correct |
14 ms |
620 KB |
Output is correct |
16 |
Correct |
6 ms |
620 KB |
Output is correct |
17 |
Correct |
12 ms |
620 KB |
Output is correct |
18 |
Correct |
3 ms |
560 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1575 ms |
8172 KB |
Output is correct |
2 |
Correct |
1623 ms |
9788 KB |
Output is correct |
3 |
Correct |
1597 ms |
9680 KB |
Output is correct |
4 |
Correct |
1226 ms |
8956 KB |
Output is correct |
5 |
Correct |
225 ms |
8940 KB |
Output is correct |
6 |
Correct |
161 ms |
8300 KB |
Output is correct |
7 |
Correct |
792 ms |
9708 KB |
Output is correct |
8 |
Correct |
1008 ms |
9708 KB |
Output is correct |
9 |
Correct |
942 ms |
9716 KB |
Output is correct |
10 |
Correct |
161 ms |
8940 KB |
Output is correct |
11 |
Correct |
1190 ms |
8828 KB |
Output is correct |
12 |
Correct |
87 ms |
7788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1575 ms |
8172 KB |
Output is correct |
2 |
Correct |
1623 ms |
9788 KB |
Output is correct |
3 |
Correct |
1597 ms |
9680 KB |
Output is correct |
4 |
Correct |
1226 ms |
8956 KB |
Output is correct |
5 |
Correct |
225 ms |
8940 KB |
Output is correct |
6 |
Correct |
161 ms |
8300 KB |
Output is correct |
7 |
Correct |
792 ms |
9708 KB |
Output is correct |
8 |
Correct |
1008 ms |
9708 KB |
Output is correct |
9 |
Correct |
942 ms |
9716 KB |
Output is correct |
10 |
Correct |
161 ms |
8940 KB |
Output is correct |
11 |
Correct |
1190 ms |
8828 KB |
Output is correct |
12 |
Correct |
87 ms |
7788 KB |
Output is correct |
13 |
Correct |
1615 ms |
10220 KB |
Output is correct |
14 |
Correct |
1623 ms |
10076 KB |
Output is correct |
15 |
Correct |
1591 ms |
9696 KB |
Output is correct |
16 |
Correct |
1226 ms |
9356 KB |
Output is correct |
17 |
Correct |
257 ms |
9324 KB |
Output is correct |
18 |
Correct |
161 ms |
8300 KB |
Output is correct |
19 |
Correct |
1603 ms |
10132 KB |
Output is correct |
20 |
Correct |
1669 ms |
10220 KB |
Output is correct |
21 |
Correct |
1178 ms |
10112 KB |
Output is correct |
22 |
Correct |
159 ms |
8812 KB |
Output is correct |
23 |
Correct |
1183 ms |
8812 KB |
Output is correct |
24 |
Correct |
83 ms |
7788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 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 |
15 ms |
748 KB |
Output is correct |
8 |
Correct |
18 ms |
748 KB |
Output is correct |
9 |
Correct |
14 ms |
748 KB |
Output is correct |
10 |
Correct |
14 ms |
768 KB |
Output is correct |
11 |
Correct |
12 ms |
620 KB |
Output is correct |
12 |
Correct |
13 ms |
620 KB |
Output is correct |
13 |
Correct |
14 ms |
620 KB |
Output is correct |
14 |
Correct |
14 ms |
620 KB |
Output is correct |
15 |
Correct |
14 ms |
620 KB |
Output is correct |
16 |
Correct |
6 ms |
620 KB |
Output is correct |
17 |
Correct |
12 ms |
620 KB |
Output is correct |
18 |
Correct |
3 ms |
560 KB |
Output is correct |
19 |
Correct |
1575 ms |
8172 KB |
Output is correct |
20 |
Correct |
1623 ms |
9788 KB |
Output is correct |
21 |
Correct |
1597 ms |
9680 KB |
Output is correct |
22 |
Correct |
1226 ms |
8956 KB |
Output is correct |
23 |
Correct |
225 ms |
8940 KB |
Output is correct |
24 |
Correct |
161 ms |
8300 KB |
Output is correct |
25 |
Correct |
792 ms |
9708 KB |
Output is correct |
26 |
Correct |
1008 ms |
9708 KB |
Output is correct |
27 |
Correct |
942 ms |
9716 KB |
Output is correct |
28 |
Correct |
161 ms |
8940 KB |
Output is correct |
29 |
Correct |
1190 ms |
8828 KB |
Output is correct |
30 |
Correct |
87 ms |
7788 KB |
Output is correct |
31 |
Correct |
1615 ms |
10220 KB |
Output is correct |
32 |
Correct |
1623 ms |
10076 KB |
Output is correct |
33 |
Correct |
1591 ms |
9696 KB |
Output is correct |
34 |
Correct |
1226 ms |
9356 KB |
Output is correct |
35 |
Correct |
257 ms |
9324 KB |
Output is correct |
36 |
Correct |
161 ms |
8300 KB |
Output is correct |
37 |
Correct |
1603 ms |
10132 KB |
Output is correct |
38 |
Correct |
1669 ms |
10220 KB |
Output is correct |
39 |
Correct |
1178 ms |
10112 KB |
Output is correct |
40 |
Correct |
159 ms |
8812 KB |
Output is correct |
41 |
Correct |
1183 ms |
8812 KB |
Output is correct |
42 |
Correct |
83 ms |
7788 KB |
Output is correct |
43 |
Correct |
1616 ms |
12084 KB |
Output is correct |
44 |
Correct |
1631 ms |
12120 KB |
Output is correct |
45 |
Correct |
1610 ms |
12032 KB |
Output is correct |
46 |
Correct |
1209 ms |
10508 KB |
Output is correct |
47 |
Correct |
282 ms |
10668 KB |
Output is correct |
48 |
Correct |
161 ms |
8044 KB |
Output is correct |
49 |
Correct |
796 ms |
11932 KB |
Output is correct |
50 |
Correct |
1040 ms |
11988 KB |
Output is correct |
51 |
Correct |
982 ms |
11884 KB |
Output is correct |
52 |
Correct |
194 ms |
10348 KB |
Output is correct |
53 |
Correct |
1145 ms |
9580 KB |
Output is correct |