#include <bits/stdc++.h>
#define ld long double
#define endl "\n"
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define pb push_back
#define mp(a,b) make_pair(a,b)
#define ms(v,x) memset(v,x,sizeof(v))
#define all(v) v.begin(),v.end()
#define ff first
#define ss second
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define per(i, a, b) for(int i = b-1; i>=a ; i--)
#define trav(a, x) for(auto& a : x)
#define allin(a , x) for(auto a : x)
#define Unique(v) sort(all(v));v.erase(unique(all(v)),v.end());
#define sz(v) ((int)v.size())
using namespace std;
typedef vector<int> vi;
#define y1 abacaba
//#define left oooooopss
#define db(x) cerr << #x <<" == "<<x << endl;
#define db2(x,y) cerr<<#x <<" == "<<x<<", "<<#y<<" == "<<y<<endl;
#define db3(x,y,z) cerr << #x<<" == "<<x<<", "<<#y<<" == "<<y<<", "<<#z<<" == "<<z<<endl;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<ll> vl;
std::mt19937 rng((int) std::chrono::steady_clock::now().time_since_epoch().count());
ll cdiv(ll a, ll b) { return a/b+((a^b)>0&&a%b); } // divide a by b rounded up
ll fdiv(ll a, ll b) { return a/b-((a^b)<0&&a%b); } // divide a by b rounded down
inline ll mod(ll n, ll m ){ ll ret = n%m; if(ret < 0) ret += m; return ret; }
ll gcd(ll a, ll b){return (b == 0LL ? a : gcd(b, a%b));}
ll exp(ll b,ll e,ll m){
b%=m;
ll ans = 1;
for (; e; b = b * b % m, e /= 2)
if (e & 1) ans = ans * b % m;
return ans;
}
// debug:
void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T){
cerr << ' ' << H;
dbg_out(T...);
}
#ifdef LOCAL
#define dbg(...) cerr<<"(" << #__VA_ARGS__<<"):" , dbg_out(__VA_ARGS__) , cerr << endl
#else
#define dbg(...) 42
#endif
//
const int N = 400100;
int A[N],B[N],T[N];
int mx[N],mn[N];
int n,k;
struct node1{
int mx;
node1(int id=N){
mx = id;
}
node1 operator+(const node1& o) const{
node1 r;
r.mx = max(mx,o.mx);
return r;
}
};
struct node2{
int cnt;
node2(int v=0){
cnt=v;
}
node2 operator+(const node2& o) const{
node2 r;
r.cnt = cnt + o.cnt;
return r;
}
};
template <class T>
struct ST{
vector<T> st; int n;
ST(int n) : st(2*n) , n(n){}
void upd(int pos, T val){ // pos 0index
for(st[pos += n] = val ; pos /= 2;)
st[pos] = st[2*pos] + st[2*pos + 1];
}
T query(int x , int y){ // [x,y] , x, y -0index
T ra , rb = T();
bool okl = false, okr = false;
for(x += n, y += n+1 ; x < y ; x/=2 , y/=2){
if(x&1) ra = (okl ? ra + st[x] : st[x]) , x++ , okl = true;
if(y&1) --y , rb = (okr ? st[y] + rb : st[y]) , okr = true;
}
return (okl ? (okr ? ra + rb : ra): rb);
}
};
int jmx[N];
struct query{
int l,r,mx,id;
query(int l,int r,int mx,int id):l(l),r(r),mx(mx),id(id){}
};
int32_t main(){
fastio;
cin >> n >> k;
vi comp;
rep(i,1,n+1){
cin >> A[i]>>B[i];
mn[i] = min(A[i],B[i]);
mx[i] = A[i] + B[i] - mn[i];
}
rep(i,1,k+1){
cin >> T[i];
comp.pb(T[i]);
}
Unique(comp);
int tam = sz(comp);
ST<node1> tree(tam);
rep(i,1,k+1){
int p = lower_bound(all(comp),T[i]) - comp.begin();
tree.upd(p,node1(i));
}
auto get = [&](int x) -> int{
return int(lower_bound(all(comp),x) - comp.begin());
};
vector<query> Q;
rep(i,1,n+1){
int L = get(mn[i]);
int R = get(mx[i])-1;
int ok=0;
if(L<=R){
jmx[i] = tree.query(L,R).mx;
if(jmx[i] <= k){
Q.pb(query(jmx[i]+1,k,mx[i],i));
ok=1;
}
}else jmx[i] = k+1;
if(!ok){
Q.pb(query(1,k,mx[i],i));
}
}
sort(all(Q),[&](query a,query b){
return a.mx > b.mx;
});
vi ids;
rep(i,1,k+1)ids.pb(i);
sort(all(ids),[&](int a,int b){
return T[a] > T[b];
});
int pt=0;
ST<node2> seg(k);
ll res=0;
for(auto it : Q){
while(pt < sz(ids) && T[ids[pt]] >= it.mx){
seg.upd(ids[pt]-1,node2(1));
pt++;
}
int cnt = seg.query(it.l-1,it.r-1).cnt;
cnt%=2;
int i = it.id;
if(jmx[i]<=k){
res+=(cnt == 0 ? mx[i] : mn[i]);
}else{
res+=(cnt==1 ? B[i] : A[i]);
}
}
cout << res << endl;
// math -> gcd it all
// Did u check N=1? Did you switch N,M?
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
388 KB |
Output is correct |
3 |
Correct |
2 ms |
344 KB |
Output is correct |
4 |
Correct |
2 ms |
400 KB |
Output is correct |
5 |
Correct |
1 ms |
460 KB |
Output is correct |
6 |
Correct |
2 ms |
504 KB |
Output is correct |
7 |
Correct |
1 ms |
420 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
388 KB |
Output is correct |
3 |
Correct |
2 ms |
344 KB |
Output is correct |
4 |
Correct |
2 ms |
400 KB |
Output is correct |
5 |
Correct |
1 ms |
460 KB |
Output is correct |
6 |
Correct |
2 ms |
504 KB |
Output is correct |
7 |
Correct |
1 ms |
420 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
460 KB |
Output is correct |
14 |
Correct |
10 ms |
1356 KB |
Output is correct |
15 |
Correct |
21 ms |
2248 KB |
Output is correct |
16 |
Correct |
33 ms |
3152 KB |
Output is correct |
17 |
Correct |
44 ms |
4192 KB |
Output is correct |
18 |
Correct |
45 ms |
4220 KB |
Output is correct |
19 |
Correct |
41 ms |
4188 KB |
Output is correct |
20 |
Correct |
42 ms |
4360 KB |
Output is correct |
21 |
Correct |
41 ms |
4244 KB |
Output is correct |
22 |
Correct |
31 ms |
3668 KB |
Output is correct |
23 |
Correct |
30 ms |
3684 KB |
Output is correct |
24 |
Correct |
30 ms |
3644 KB |
Output is correct |
25 |
Correct |
30 ms |
3768 KB |
Output is correct |
26 |
Correct |
36 ms |
3864 KB |
Output is correct |
27 |
Correct |
40 ms |
4244 KB |
Output is correct |
28 |
Correct |
37 ms |
4264 KB |
Output is correct |
29 |
Correct |
39 ms |
4276 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
388 KB |
Output is correct |
3 |
Correct |
2 ms |
344 KB |
Output is correct |
4 |
Correct |
2 ms |
400 KB |
Output is correct |
5 |
Correct |
1 ms |
460 KB |
Output is correct |
6 |
Correct |
2 ms |
504 KB |
Output is correct |
7 |
Correct |
1 ms |
420 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
460 KB |
Output is correct |
14 |
Correct |
10 ms |
1356 KB |
Output is correct |
15 |
Correct |
21 ms |
2248 KB |
Output is correct |
16 |
Correct |
33 ms |
3152 KB |
Output is correct |
17 |
Correct |
44 ms |
4192 KB |
Output is correct |
18 |
Correct |
45 ms |
4220 KB |
Output is correct |
19 |
Correct |
41 ms |
4188 KB |
Output is correct |
20 |
Correct |
42 ms |
4360 KB |
Output is correct |
21 |
Correct |
41 ms |
4244 KB |
Output is correct |
22 |
Correct |
31 ms |
3668 KB |
Output is correct |
23 |
Correct |
30 ms |
3684 KB |
Output is correct |
24 |
Correct |
30 ms |
3644 KB |
Output is correct |
25 |
Correct |
30 ms |
3768 KB |
Output is correct |
26 |
Correct |
36 ms |
3864 KB |
Output is correct |
27 |
Correct |
40 ms |
4244 KB |
Output is correct |
28 |
Correct |
37 ms |
4264 KB |
Output is correct |
29 |
Correct |
39 ms |
4276 KB |
Output is correct |
30 |
Correct |
121 ms |
9332 KB |
Output is correct |
31 |
Correct |
134 ms |
10564 KB |
Output is correct |
32 |
Correct |
186 ms |
14152 KB |
Output is correct |
33 |
Correct |
255 ms |
19832 KB |
Output is correct |
34 |
Correct |
97 ms |
7864 KB |
Output is correct |
35 |
Correct |
262 ms |
19692 KB |
Output is correct |
36 |
Correct |
239 ms |
19640 KB |
Output is correct |
37 |
Correct |
251 ms |
19728 KB |
Output is correct |
38 |
Correct |
254 ms |
19672 KB |
Output is correct |
39 |
Correct |
247 ms |
19712 KB |
Output is correct |
40 |
Correct |
221 ms |
19468 KB |
Output is correct |
41 |
Correct |
242 ms |
19640 KB |
Output is correct |
42 |
Correct |
242 ms |
19628 KB |
Output is correct |
43 |
Correct |
158 ms |
19000 KB |
Output is correct |
44 |
Correct |
146 ms |
18984 KB |
Output is correct |
45 |
Correct |
145 ms |
18888 KB |
Output is correct |
46 |
Correct |
179 ms |
17460 KB |
Output is correct |
47 |
Correct |
164 ms |
17024 KB |
Output is correct |
48 |
Correct |
223 ms |
19664 KB |
Output is correct |
49 |
Correct |
227 ms |
19788 KB |
Output is correct |