#include<bits/stdc++.h>
using namespace std;
const long long inf = (long long) 1e18 + 10;
const int inf1 = (int) 1e9 + 10;
#define int long long
#define dbl long double
#define endl '\n'
#define sc second
#define fr first
#define mp make_pair
#define pb push_back
#define all(x) x.begin(), x.end()
#define maxn 220000
int n, q, tr[4*maxn], trs[4*maxn], a[maxn], b[maxn], t[maxn], ccval[5*maxn];
void att(int no, int l, int r, int pos, int val) {
if(l > pos || r < pos) return;
if(l == r) {
tr[no] = max(tr[no],val);
return;
}
int f1 = 2*no;
int f2 = 2*no+1;
int mid = (l+r)/2;
att(f1,l,mid,pos,val);
att(f2,mid+1,r,pos,val);
tr[no] = max(tr[f1],tr[f2]);
}
int qrr(int no, int l, int r, int L, int R) {
if(L > R) return 0;
if(l > R || r < L) return 0;
if(l >= L && r <= R) return tr[no];
int f1 = 2*no;
int f2 = 2*no+1;
int mid = (l+r)/2;
return max(qrr(f1,l,mid,L,R),qrr(f2,mid+1,r,L,R));
}
void atts(int no, int l, int r, int pos, int val) {
if(l > pos || r < pos) return;
if(l == r) {
trs[no]+= val;
return;
}
int f1 = 2*no;
int f2 = 2*no+1;
int mid = (l+r)/2;
atts(f1,l,mid,pos,val);
atts(f2,mid+1,r,pos,val);
trs[no] = trs[f1]+trs[f2];
}
int qrrs(int no, int l, int r, int L, int R) {
if(L > R) return 0;
if(l > R || r < L) return 0;
if(l >= L && r <= R) return trs[no];
int f1 = 2*no;
int f2 = 2*no+1;
int mid = (l+r)/2;
return qrrs(f1,l,mid,L,R)+qrrs(f2,mid+1,r,L,R);
}
void solve() {
cin >> n >> q;
vector<int> cc;
for(int i = 1; i <= n; i++) {
cin >> a[i] >> b[i];
cc.pb(a[i]);
cc.pb(b[i]);
}
for(int i = 1; i <= q; i++) {
cin >> t[i];
cc.pb(t[i]);
}
sort(all(cc));
cc.erase(unique(all(cc)),cc.end());
int m = cc.size();
vector<pair<pair<int,int>,pair<int,int>>> qr;
for(int i = 1; i <= n; i++) {
int aux;
aux = a[i];
a[i] = upper_bound(all(cc),a[i]) - cc.begin();
ccval[a[i]] = aux;
aux = b[i];
b[i] = upper_bound(all(cc),b[i]) - cc.begin();
ccval[b[i]] = aux;
qr.pb(mp(mp(max(a[i],b[i]),0),mp(a[i],b[i])));
// cout << a[i] << " " << b[i] << endl;
}
for(int i = 1; i <= q; i++) {
t[i] = upper_bound(all(cc),t[i]) - cc.begin();
att(1,1,m,t[i],i);
qr.pb(mp(mp(t[i],1),mp(i,0)));
// cout << " att " << t[i] << " " << i << endl;
}
sort(all(qr),greater<pair<pair<int,int>,pair<int,int>>>());
int ans = 0;
for(auto X : qr) {
if(X.fr.sc == 0) {
//query
//primeiro pega a pos que vai ter que ver ate o final
int a1 = X.sc.fr;
int b1 = X.sc.sc;
// cout << ccval[a1] << " " << ccval[b1] << endl;
int pos = qrr(1,1,m,min(a1,b1),max(a1,b1)-1);
// cout << pos << endl;
int qtd = qrrs(1,1,q,pos+1,q);
if(pos != 0 && b1 > a1) qtd++;
if(qtd%2 == 0) ans+= ccval[a1];
else ans+= ccval[b1];
}
else {
//att
int pos = X.sc.fr;
//adicionar 1 em pos
atts(1,1,q,pos,1);
}
}
cout << ans << endl;
}
int32_t main() {
ios::sync_with_stdio(false); cin.tie(0);
// freopen("in.in", "r", stdin);
//freopen("out.out", "w", stdout);
int tt = 1;
// cin >> tt;
while(tt--) solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
460 KB |
Output is correct |
2 |
Correct |
2 ms |
584 KB |
Output is correct |
3 |
Correct |
2 ms |
588 KB |
Output is correct |
4 |
Correct |
2 ms |
588 KB |
Output is correct |
5 |
Correct |
2 ms |
588 KB |
Output is correct |
6 |
Correct |
2 ms |
588 KB |
Output is correct |
7 |
Correct |
2 ms |
588 KB |
Output is correct |
8 |
Correct |
2 ms |
652 KB |
Output is correct |
9 |
Correct |
1 ms |
588 KB |
Output is correct |
10 |
Correct |
2 ms |
580 KB |
Output is correct |
11 |
Correct |
2 ms |
588 KB |
Output is correct |
12 |
Correct |
2 ms |
588 KB |
Output is correct |
13 |
Correct |
2 ms |
588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
460 KB |
Output is correct |
2 |
Correct |
2 ms |
584 KB |
Output is correct |
3 |
Correct |
2 ms |
588 KB |
Output is correct |
4 |
Correct |
2 ms |
588 KB |
Output is correct |
5 |
Correct |
2 ms |
588 KB |
Output is correct |
6 |
Correct |
2 ms |
588 KB |
Output is correct |
7 |
Correct |
2 ms |
588 KB |
Output is correct |
8 |
Correct |
2 ms |
652 KB |
Output is correct |
9 |
Correct |
1 ms |
588 KB |
Output is correct |
10 |
Correct |
2 ms |
580 KB |
Output is correct |
11 |
Correct |
2 ms |
588 KB |
Output is correct |
12 |
Correct |
2 ms |
588 KB |
Output is correct |
13 |
Correct |
2 ms |
588 KB |
Output is correct |
14 |
Correct |
17 ms |
3008 KB |
Output is correct |
15 |
Correct |
34 ms |
5440 KB |
Output is correct |
16 |
Correct |
56 ms |
7792 KB |
Output is correct |
17 |
Correct |
76 ms |
10552 KB |
Output is correct |
18 |
Correct |
76 ms |
10568 KB |
Output is correct |
19 |
Correct |
73 ms |
10568 KB |
Output is correct |
20 |
Correct |
74 ms |
10560 KB |
Output is correct |
21 |
Correct |
73 ms |
10424 KB |
Output is correct |
22 |
Correct |
64 ms |
9760 KB |
Output is correct |
23 |
Correct |
57 ms |
8632 KB |
Output is correct |
24 |
Correct |
60 ms |
8512 KB |
Output is correct |
25 |
Correct |
59 ms |
9864 KB |
Output is correct |
26 |
Correct |
57 ms |
9792 KB |
Output is correct |
27 |
Correct |
82 ms |
10204 KB |
Output is correct |
28 |
Correct |
67 ms |
10176 KB |
Output is correct |
29 |
Correct |
68 ms |
10412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
460 KB |
Output is correct |
2 |
Correct |
2 ms |
584 KB |
Output is correct |
3 |
Correct |
2 ms |
588 KB |
Output is correct |
4 |
Correct |
2 ms |
588 KB |
Output is correct |
5 |
Correct |
2 ms |
588 KB |
Output is correct |
6 |
Correct |
2 ms |
588 KB |
Output is correct |
7 |
Correct |
2 ms |
588 KB |
Output is correct |
8 |
Correct |
2 ms |
652 KB |
Output is correct |
9 |
Correct |
1 ms |
588 KB |
Output is correct |
10 |
Correct |
2 ms |
580 KB |
Output is correct |
11 |
Correct |
2 ms |
588 KB |
Output is correct |
12 |
Correct |
2 ms |
588 KB |
Output is correct |
13 |
Correct |
2 ms |
588 KB |
Output is correct |
14 |
Correct |
17 ms |
3008 KB |
Output is correct |
15 |
Correct |
34 ms |
5440 KB |
Output is correct |
16 |
Correct |
56 ms |
7792 KB |
Output is correct |
17 |
Correct |
76 ms |
10552 KB |
Output is correct |
18 |
Correct |
76 ms |
10568 KB |
Output is correct |
19 |
Correct |
73 ms |
10568 KB |
Output is correct |
20 |
Correct |
74 ms |
10560 KB |
Output is correct |
21 |
Correct |
73 ms |
10424 KB |
Output is correct |
22 |
Correct |
64 ms |
9760 KB |
Output is correct |
23 |
Correct |
57 ms |
8632 KB |
Output is correct |
24 |
Correct |
60 ms |
8512 KB |
Output is correct |
25 |
Correct |
59 ms |
9864 KB |
Output is correct |
26 |
Correct |
57 ms |
9792 KB |
Output is correct |
27 |
Correct |
82 ms |
10204 KB |
Output is correct |
28 |
Correct |
67 ms |
10176 KB |
Output is correct |
29 |
Correct |
68 ms |
10412 KB |
Output is correct |
30 |
Correct |
191 ms |
22464 KB |
Output is correct |
31 |
Runtime error |
81 ms |
25516 KB |
Execution killed with signal 11 |
32 |
Halted |
0 ms |
0 KB |
- |