#include <bits/stdc++.h>
#define ll int
#define MAXN 100010
using namespace std;
pair<ll,ll> a[MAXN],b[MAXN];
vector<ll> sums;
unordered_map<ll,ll> comp;
ll n,q,x[MAXN],y[MAXN],z[MAXN],len,cntind[MAXN],ans[MAXN],sumab[MAXN];
bool Cmp(pair<ll,ll> p1,pair<ll,ll> p2)
{
return p1.first>p2.first;
}
ll Find_Pos(ll x,pair<ll,ll> *p)
{
ll l=1,r=n,mid,ans=0;
while (l<=r)
{
mid=(l+r)/2;
if (p[mid].first>=x)
{
ans=mid;
l=mid+1;
}
else
r=mid-1;
}
return ans;
}
struct bit
{
vector<ll> sum;
void Init()
{
sum.resize(2*MAXN);
}
void Add(ll pos,ll val)
{
for (ll i=pos;i<2*MAXN;i+=i&(-i))
sum[i]+=val;
}
ll Calc(ll pos)
{
ll ans=0;
for (ll i=pos;i>0;i-=i&(-i))
ans+=sum[i];
return ans;
}
ll Query(ll l,ll r)
{
return Calc(r)-Calc(l-1);
}
};
bit B;
struct query
{
ll l,r,ind;
};
query qq[MAXN];
bool Cmp_Query(query q1,query q2)
{
if (q1.l/len!=q2.l/len)
return q1.l/len<q2.l/len;
return q1.r<q2.r;
}
void Add(ll pos,pair<ll,ll> *p)
{
ll i=p[pos].second;
cntind[i]++;
if (cntind[i]==2)
B.Add(sumab[i],1);
}
void Remove(ll pos,pair<ll,ll> *p)
{
ll i=p[pos].second;
cntind[i]--;
if (cntind[i]==1)
B.Add(sumab[i],-1);
}
int main()
{
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n >> q;
len=sqrt(n);
for (ll i=1;i<=n;i++)
{
cin >> a[i].first >> b[i].first;
a[i].second=b[i].second=i;
sumab[i]=a[i].first+b[i].first;
sums.push_back(sumab[i]);
}
sort(a+1,a+1+n,Cmp);
sort(b+1,b+1+n,Cmp);
for (ll i=1;i<=q;i++)
{
cin >> x[i] >> y[i] >> z[i];
sums.push_back(z[i]);
}
sort(sums.begin(),sums.end());
ll cnt=1;
for (auto i : sums)
comp[i]=cnt++;
for (ll i=1;i<=q;i++)
{
qq[i].l=Find_Pos(x[i],a);
qq[i].r=Find_Pos(y[i],b);
qq[i].ind=i;
z[i]=comp[z[i]];
}
for (ll i=1;i<=n;i++)
sumab[i]=comp[sumab[i]];
sort(qq+1,qq+1+q,Cmp_Query);
ll curl=0,curr=0;
B.Init();
for (ll i=1;i<=q;i++)
{
while (curl<qq[i].l)
{
curl++;
Add(curl,a);
}
while (curr<qq[i].r)
{
curr++;
Add(curr,b);
}
while (curl>qq[i].l)
{
Remove(curl,a);
curl--;
}
while (curr>qq[i].r)
{
Remove(curr,b);
curr--;
}
ans[qq[i].ind]=B.Query(z[qq[i].ind],2*MAXN-1);
}
for (ll i=1;i<=q;i++)
cout << ans[i] << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1108 KB |
Output is correct |
2 |
Correct |
1 ms |
1108 KB |
Output is correct |
3 |
Correct |
1 ms |
1112 KB |
Output is correct |
4 |
Correct |
1 ms |
1108 KB |
Output is correct |
5 |
Correct |
1 ms |
1108 KB |
Output is correct |
6 |
Correct |
1 ms |
1108 KB |
Output is correct |
7 |
Correct |
10 ms |
1748 KB |
Output is correct |
8 |
Correct |
10 ms |
1752 KB |
Output is correct |
9 |
Correct |
10 ms |
1760 KB |
Output is correct |
10 |
Correct |
10 ms |
1620 KB |
Output is correct |
11 |
Correct |
5 ms |
1620 KB |
Output is correct |
12 |
Correct |
4 ms |
1380 KB |
Output is correct |
13 |
Correct |
9 ms |
1492 KB |
Output is correct |
14 |
Correct |
10 ms |
1648 KB |
Output is correct |
15 |
Correct |
9 ms |
1508 KB |
Output is correct |
16 |
Correct |
5 ms |
1492 KB |
Output is correct |
17 |
Correct |
9 ms |
1636 KB |
Output is correct |
18 |
Correct |
3 ms |
1380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1348 ms |
10928 KB |
Output is correct |
2 |
Correct |
1323 ms |
10692 KB |
Output is correct |
3 |
Correct |
1345 ms |
10596 KB |
Output is correct |
4 |
Correct |
1107 ms |
10260 KB |
Output is correct |
5 |
Correct |
135 ms |
10276 KB |
Output is correct |
6 |
Correct |
100 ms |
7804 KB |
Output is correct |
7 |
Correct |
371 ms |
10592 KB |
Output is correct |
8 |
Correct |
366 ms |
10588 KB |
Output is correct |
9 |
Correct |
266 ms |
10600 KB |
Output is correct |
10 |
Correct |
104 ms |
10176 KB |
Output is correct |
11 |
Correct |
1014 ms |
10148 KB |
Output is correct |
12 |
Correct |
58 ms |
7464 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1348 ms |
10928 KB |
Output is correct |
2 |
Correct |
1323 ms |
10692 KB |
Output is correct |
3 |
Correct |
1345 ms |
10596 KB |
Output is correct |
4 |
Correct |
1107 ms |
10260 KB |
Output is correct |
5 |
Correct |
135 ms |
10276 KB |
Output is correct |
6 |
Correct |
100 ms |
7804 KB |
Output is correct |
7 |
Correct |
371 ms |
10592 KB |
Output is correct |
8 |
Correct |
366 ms |
10588 KB |
Output is correct |
9 |
Correct |
266 ms |
10600 KB |
Output is correct |
10 |
Correct |
104 ms |
10176 KB |
Output is correct |
11 |
Correct |
1014 ms |
10148 KB |
Output is correct |
12 |
Correct |
58 ms |
7464 KB |
Output is correct |
13 |
Correct |
1389 ms |
12964 KB |
Output is correct |
14 |
Correct |
1358 ms |
12528 KB |
Output is correct |
15 |
Correct |
1311 ms |
10652 KB |
Output is correct |
16 |
Correct |
1124 ms |
11688 KB |
Output is correct |
17 |
Correct |
150 ms |
11620 KB |
Output is correct |
18 |
Correct |
99 ms |
7808 KB |
Output is correct |
19 |
Correct |
1390 ms |
12892 KB |
Output is correct |
20 |
Correct |
1400 ms |
12652 KB |
Output is correct |
21 |
Correct |
669 ms |
12968 KB |
Output is correct |
22 |
Correct |
102 ms |
10184 KB |
Output is correct |
23 |
Correct |
1020 ms |
10140 KB |
Output is correct |
24 |
Correct |
61 ms |
7496 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1108 KB |
Output is correct |
2 |
Correct |
1 ms |
1108 KB |
Output is correct |
3 |
Correct |
1 ms |
1112 KB |
Output is correct |
4 |
Correct |
1 ms |
1108 KB |
Output is correct |
5 |
Correct |
1 ms |
1108 KB |
Output is correct |
6 |
Correct |
1 ms |
1108 KB |
Output is correct |
7 |
Correct |
10 ms |
1748 KB |
Output is correct |
8 |
Correct |
10 ms |
1752 KB |
Output is correct |
9 |
Correct |
10 ms |
1760 KB |
Output is correct |
10 |
Correct |
10 ms |
1620 KB |
Output is correct |
11 |
Correct |
5 ms |
1620 KB |
Output is correct |
12 |
Correct |
4 ms |
1380 KB |
Output is correct |
13 |
Correct |
9 ms |
1492 KB |
Output is correct |
14 |
Correct |
10 ms |
1648 KB |
Output is correct |
15 |
Correct |
9 ms |
1508 KB |
Output is correct |
16 |
Correct |
5 ms |
1492 KB |
Output is correct |
17 |
Correct |
9 ms |
1636 KB |
Output is correct |
18 |
Correct |
3 ms |
1380 KB |
Output is correct |
19 |
Correct |
1348 ms |
10928 KB |
Output is correct |
20 |
Correct |
1323 ms |
10692 KB |
Output is correct |
21 |
Correct |
1345 ms |
10596 KB |
Output is correct |
22 |
Correct |
1107 ms |
10260 KB |
Output is correct |
23 |
Correct |
135 ms |
10276 KB |
Output is correct |
24 |
Correct |
100 ms |
7804 KB |
Output is correct |
25 |
Correct |
371 ms |
10592 KB |
Output is correct |
26 |
Correct |
366 ms |
10588 KB |
Output is correct |
27 |
Correct |
266 ms |
10600 KB |
Output is correct |
28 |
Correct |
104 ms |
10176 KB |
Output is correct |
29 |
Correct |
1014 ms |
10148 KB |
Output is correct |
30 |
Correct |
58 ms |
7464 KB |
Output is correct |
31 |
Correct |
1389 ms |
12964 KB |
Output is correct |
32 |
Correct |
1358 ms |
12528 KB |
Output is correct |
33 |
Correct |
1311 ms |
10652 KB |
Output is correct |
34 |
Correct |
1124 ms |
11688 KB |
Output is correct |
35 |
Correct |
150 ms |
11620 KB |
Output is correct |
36 |
Correct |
99 ms |
7808 KB |
Output is correct |
37 |
Correct |
1390 ms |
12892 KB |
Output is correct |
38 |
Correct |
1400 ms |
12652 KB |
Output is correct |
39 |
Correct |
669 ms |
12968 KB |
Output is correct |
40 |
Correct |
102 ms |
10184 KB |
Output is correct |
41 |
Correct |
1020 ms |
10140 KB |
Output is correct |
42 |
Correct |
61 ms |
7496 KB |
Output is correct |
43 |
Correct |
1404 ms |
16884 KB |
Output is correct |
44 |
Correct |
1426 ms |
21380 KB |
Output is correct |
45 |
Correct |
1371 ms |
21344 KB |
Output is correct |
46 |
Correct |
1179 ms |
19928 KB |
Output is correct |
47 |
Correct |
179 ms |
19808 KB |
Output is correct |
48 |
Correct |
101 ms |
8536 KB |
Output is correct |
49 |
Correct |
410 ms |
21324 KB |
Output is correct |
50 |
Correct |
420 ms |
21300 KB |
Output is correct |
51 |
Correct |
331 ms |
21140 KB |
Output is correct |
52 |
Correct |
148 ms |
19644 KB |
Output is correct |
53 |
Correct |
1047 ms |
14360 KB |
Output is correct |