#include <bits/stdc++.h>
using namespace std;
// macros
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef pair<ll, ll> lll;
typedef tuple<int, int, int> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<iii> viii;
typedef vector<ll> vll;
typedef vector<lll> vlll;
#define REP(a,b,c) for(int a=int(b); a<int(c); a++)
#define RE(a,c) REP(a,0,c)
#define RE1(a,c) REP(a,1,c+1)
#define REI(a,b,c) REP(a,b,c+1)
#define REV(a,b,c) for(int a=int(c-1); a>=int(b); a--)
#define FOR(a,b) for(auto& a : b)
#define all(a) a.begin(), a.end()
#define INF 1e18
#define EPS 1e-9
#define pb push_back
#define popb pop_back
#define fi first
#define se second
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
// input
template<class T> void IN(T& x) {cin >> x;}
template<class H, class... T> void IN(H& h, T&... t) {IN(h); IN(t...); }
// output
template<class T1, class T2> void OUT(const pair<T1,T2>& x);
template<class T> void OUT(const vector<T>& x);
template<class T> void OUT(const T& x) {cout << x;}
template<class H, class... T> void OUT(const H& h, const T&... t) {OUT(h); OUT(t...); }
template<class T1, class T2> void OUT(const pair<T1,T2>& x) {OUT(x.fi,' ',x.se);}
template<class T> void OUT(const vector<T>& x) {RE(i,x.size()) OUT(i==0?"":" ",x[i]);}
template<class... T> void OUTL(const T&... t) {OUT(t..., "\n"); }
template<class H> void OUTLS(const H& h) {OUTL(h); }
template<class H, class... T> void OUTLS(const H& h, const T&... t) {OUT(h,' '); OUTLS(t...); }
// dp
template<class T> bool ckmin(T&a, T&b) { bool bl = a > b; a = min(a,b); return bl;}
template<class T> bool ckmax(T&a, T&b) { bool bl = a < b; a = max(a,b); return bl;}
void program();
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
program();
}
//===================//
// begin program //
//===================//
const int MX = 5e5;
const int N = (1<<20);
const int SQ = 450;
int n, q;
int s[MX], t[MX], c[MX];
int x[MX], y[MX], z[MX];
int a[MX], b[MX];
int sa[MX], sb[MX];
vi difs, dift, difz;
// fenwick tree
int FT[MX], ftTotal=0;
void addFT(int i, int value) {
ftTotal += value;
for(i++; i<MX; i+=i&-i) FT[i] += value;
}
int getFT(int i) {
int ans = 0;
for(i++; i; i-=i&-i) ans += FT[i];
return ans;
}
int cnt[MX], ans[MX];
void program() {
IN(n,q);
RE(i,n) IN(s[i],t[i]);
RE(i,q) IN(x[i],y[i],z[i]);
// coördinate compression
RE(i,n) c[i]=s[i]+t[i];
RE(i,n) difs.pb(s[i]), dift.pb(t[i]), difz.pb(c[i]);
RE(i,q) difs.pb(x[i]), dift.pb(y[i]), difz.pb(z[i]);
sort(all(difs));
sort(all(dift));
sort(all(difz));
RE(i,n) a[i]=lower_bound(all(difs),s[i])-difs.begin(), b[i]=lower_bound(all(dift),t[i])-dift.begin(), c[i]=lower_bound(all(difz),c[i])-difz.begin();
RE(i,q) x[i]=lower_bound(all(difs),x[i])-difs.begin(), y[i]=lower_bound(all(dift),y[i])-dift.begin(), z[i]=lower_bound(all(difz),z[i])-difz.begin();
// sort
RE(i,n) sa[i]=i;
sort(sa,sa+n,[](int i, int j) {
return a[i]<a[j];
});
RE(i,n) sb[i]=i;
sort(sb,sb+n,[](int i, int j) {
return b[i]<b[j];
});
// answering queries
int cx=n, cy=n;
viii pq;
RE(i,q) pq.pb({x[i]/SQ,y[i]/SQ,i});
sort(all(pq));
FOR(p,pq) {
int _, i;
tie(_,_,i) = p;
while(cx && a[sa[cx-1]] >= x[i]) {
cx--;
cnt[sa[cx]]++;
if(cnt[sa[cx]] == 2)
addFT(c[sa[cx]],1);
}
while(cy && b[sb[cy-1]] >= y[i]) {
cy--;
cnt[sb[cy]]++;
if(cnt[sb[cy]] == 2)
addFT(c[sb[cy]],1);
}
while(cx != n && a[sa[cx]] < x[i]) {
if(cnt[sa[cx]] == 2)
addFT(c[sa[cx]],-1);
cnt[sa[cx]]--;
cx++;
}
while(cy != n && b[sb[cy]] < y[i]) {
if(cnt[sb[cy]] == 2)
addFT(c[sb[cy]],-1);
cnt[sb[cy]]--;
cy++;
}
ans[i] = ftTotal - getFT(z[i]-1);
}
RE(i,q) OUTL(ans[i]);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
18 ms |
952 KB |
Output is correct |
8 |
Correct |
18 ms |
856 KB |
Output is correct |
9 |
Correct |
18 ms |
948 KB |
Output is correct |
10 |
Correct |
13 ms |
860 KB |
Output is correct |
11 |
Correct |
12 ms |
844 KB |
Output is correct |
12 |
Correct |
6 ms |
844 KB |
Output is correct |
13 |
Correct |
17 ms |
912 KB |
Output is correct |
14 |
Correct |
17 ms |
920 KB |
Output is correct |
15 |
Correct |
18 ms |
916 KB |
Output is correct |
16 |
Correct |
9 ms |
852 KB |
Output is correct |
17 |
Correct |
12 ms |
896 KB |
Output is correct |
18 |
Correct |
4 ms |
716 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2769 ms |
12848 KB |
Output is correct |
2 |
Correct |
2843 ms |
12976 KB |
Output is correct |
3 |
Correct |
2856 ms |
12844 KB |
Output is correct |
4 |
Correct |
2182 ms |
12076 KB |
Output is correct |
5 |
Correct |
397 ms |
12032 KB |
Output is correct |
6 |
Correct |
176 ms |
11096 KB |
Output is correct |
7 |
Correct |
2387 ms |
12820 KB |
Output is correct |
8 |
Correct |
726 ms |
12844 KB |
Output is correct |
9 |
Correct |
487 ms |
12800 KB |
Output is correct |
10 |
Correct |
302 ms |
11952 KB |
Output is correct |
11 |
Correct |
1922 ms |
11960 KB |
Output is correct |
12 |
Correct |
116 ms |
10664 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2769 ms |
12848 KB |
Output is correct |
2 |
Correct |
2843 ms |
12976 KB |
Output is correct |
3 |
Correct |
2856 ms |
12844 KB |
Output is correct |
4 |
Correct |
2182 ms |
12076 KB |
Output is correct |
5 |
Correct |
397 ms |
12032 KB |
Output is correct |
6 |
Correct |
176 ms |
11096 KB |
Output is correct |
7 |
Correct |
2387 ms |
12820 KB |
Output is correct |
8 |
Correct |
726 ms |
12844 KB |
Output is correct |
9 |
Correct |
487 ms |
12800 KB |
Output is correct |
10 |
Correct |
302 ms |
11952 KB |
Output is correct |
11 |
Correct |
1922 ms |
11960 KB |
Output is correct |
12 |
Correct |
116 ms |
10664 KB |
Output is correct |
13 |
Correct |
2938 ms |
13664 KB |
Output is correct |
14 |
Correct |
2900 ms |
13608 KB |
Output is correct |
15 |
Correct |
2845 ms |
12896 KB |
Output is correct |
16 |
Correct |
2242 ms |
12876 KB |
Output is correct |
17 |
Correct |
441 ms |
12872 KB |
Output is correct |
18 |
Correct |
190 ms |
11192 KB |
Output is correct |
19 |
Correct |
2928 ms |
13652 KB |
Output is correct |
20 |
Correct |
2911 ms |
13664 KB |
Output is correct |
21 |
Correct |
1740 ms |
13740 KB |
Output is correct |
22 |
Correct |
315 ms |
11936 KB |
Output is correct |
23 |
Correct |
1927 ms |
12000 KB |
Output is correct |
24 |
Correct |
112 ms |
10664 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
18 ms |
952 KB |
Output is correct |
8 |
Correct |
18 ms |
856 KB |
Output is correct |
9 |
Correct |
18 ms |
948 KB |
Output is correct |
10 |
Correct |
13 ms |
860 KB |
Output is correct |
11 |
Correct |
12 ms |
844 KB |
Output is correct |
12 |
Correct |
6 ms |
844 KB |
Output is correct |
13 |
Correct |
17 ms |
912 KB |
Output is correct |
14 |
Correct |
17 ms |
920 KB |
Output is correct |
15 |
Correct |
18 ms |
916 KB |
Output is correct |
16 |
Correct |
9 ms |
852 KB |
Output is correct |
17 |
Correct |
12 ms |
896 KB |
Output is correct |
18 |
Correct |
4 ms |
716 KB |
Output is correct |
19 |
Correct |
2769 ms |
12848 KB |
Output is correct |
20 |
Correct |
2843 ms |
12976 KB |
Output is correct |
21 |
Correct |
2856 ms |
12844 KB |
Output is correct |
22 |
Correct |
2182 ms |
12076 KB |
Output is correct |
23 |
Correct |
397 ms |
12032 KB |
Output is correct |
24 |
Correct |
176 ms |
11096 KB |
Output is correct |
25 |
Correct |
2387 ms |
12820 KB |
Output is correct |
26 |
Correct |
726 ms |
12844 KB |
Output is correct |
27 |
Correct |
487 ms |
12800 KB |
Output is correct |
28 |
Correct |
302 ms |
11952 KB |
Output is correct |
29 |
Correct |
1922 ms |
11960 KB |
Output is correct |
30 |
Correct |
116 ms |
10664 KB |
Output is correct |
31 |
Correct |
2938 ms |
13664 KB |
Output is correct |
32 |
Correct |
2900 ms |
13608 KB |
Output is correct |
33 |
Correct |
2845 ms |
12896 KB |
Output is correct |
34 |
Correct |
2242 ms |
12876 KB |
Output is correct |
35 |
Correct |
441 ms |
12872 KB |
Output is correct |
36 |
Correct |
190 ms |
11192 KB |
Output is correct |
37 |
Correct |
2928 ms |
13652 KB |
Output is correct |
38 |
Correct |
2911 ms |
13664 KB |
Output is correct |
39 |
Correct |
1740 ms |
13740 KB |
Output is correct |
40 |
Correct |
315 ms |
11936 KB |
Output is correct |
41 |
Correct |
1927 ms |
12000 KB |
Output is correct |
42 |
Correct |
112 ms |
10664 KB |
Output is correct |
43 |
Correct |
2954 ms |
15616 KB |
Output is correct |
44 |
Correct |
2906 ms |
15588 KB |
Output is correct |
45 |
Correct |
2885 ms |
15524 KB |
Output is correct |
46 |
Correct |
2272 ms |
13988 KB |
Output is correct |
47 |
Correct |
432 ms |
13984 KB |
Output is correct |
48 |
Correct |
191 ms |
10972 KB |
Output is correct |
49 |
Correct |
2391 ms |
15540 KB |
Output is correct |
50 |
Correct |
786 ms |
15528 KB |
Output is correct |
51 |
Correct |
545 ms |
15404 KB |
Output is correct |
52 |
Correct |
348 ms |
13988 KB |
Output is correct |
53 |
Correct |
1946 ms |
12748 KB |
Output is correct |