/*
SOLUTION : For each item we calculate separately. Separate for each number the queries into 3 regions. Anything
in middle region basically keeps higher->higher and lower->higher. so find last region-type-2. count #region-typ
e-3 after that. (region-type-1 has no effect). Also, take acnt of the initial swap.
*/
#include <iostream>
#include <vector>
#include <set>
#include <iomanip>
#include <algorithm>
#include <functional>
#include <stdio.h>
#include <cmath>
#include <queue>
#include <string>
#include <map>
#include <fstream>
#include <complex>
#include <stack>
#include <set>
#define FOR(i,n) for(int i=0;i<n;i++)
#define FORE(i,a,b) for(int i=a;i<=b;i++)
#define ll long long int
#define vi vector<int>
#define ii pair<int,int>
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define pll pair<ll,ll>
#define cd complex<double>
#define ld long double
#define pld pair<ld,ld>
#define iii pair<ii,int>
const ld INF = 1e9+10;
using namespace std;
const int MAXN = 1000*100*2+10;
const int MAXVAL = 1e9+10;
struct Node{
Node* left;
Node* right;
int val;
Node(int v){
left = right = NULL;
val = v;
}
};
struct MaxSegtree{
Node* head;
inline void expand(Node* node){
if(node->left == NULL)node->left = new Node(-1);
if(node->right == NULL)node->right = new Node(-1);
}
void update(Node* node,int ss,int se,int i,int val){
if(i < ss or i > se)return;
expand(node);
if(ss == se){
node->val = val;
return;
}
int mid = (ss+se)/2;
update(node->left,ss,mid,i,val);
update(node->right,mid+1,se,i,val);
node->val = max(node->left->val,node->right->val);
}
int get(Node* node,int ss,int se,int l,int r){
if(node == NULL)return -1;
if(l > se or ss > r)return -1;
if(l <= ss and se <= r)return node->val;
int mid = (ss+se)/2;
return max(get(node->left,ss,mid,l,r),get(node->right,mid+1,se,l,r));
}
MaxSegtree(){
head = new Node(-1);
}
};
struct SumSegtree{
Node* head;
inline void expand(Node* node){
if(node->left == NULL)node->left = new Node(0);
if(node->right == NULL)node->right = new Node(0);
}
void update(Node* node,int ss,int se,int i,int val){
if(i < ss or i > se)return;
expand(node);
if(ss == se){
node->val += val;
return;
}
int mid = (ss+se)/2;
update(node->left,ss,mid,i,val);
update(node->right,mid+1,se,i,val);
node->val = (node->left->val + node->right->val);
}
int get(Node* node,int ss,int se,int l,int r){
if(node == NULL)return 0;
if(l > se or ss > r)return 0;
if(l <= ss and se <= r)return node->val;
int mid = (ss+se)/2;
return (get(node->left,ss,mid,l,r) + get(node->right,mid+1,se,l,r));
}
SumSegtree(){
head = new Node(0);
}
};
int ans[MAXN];
void brute(int* a,int* b,int* t,int n,int k){
ll sum = 0;
FOR(i,n){
FOR(j,k){
if(t[j] >= a[i]){
swap(a[i],b[i]);
}
}
sum += a[i];
cout << a[i] << " ";
}
cout << sum << endl;
}
int main(){
int n,k;
cin >> n >> k;
vector<iii> all;
int aa[n];
int bb[n];
FOR(i,n){
int a,b;
cin >> a >> b;
aa[i] = a;
bb[i] = b;
if(b>a){
swap(a,b);
ans[i] = 1;
}else{
ans[i] = 0;
}
all.pb({{a,b},i});
}
MaxSegtree mst;
SumSegtree sst;
vector<ii> t;
int t2[k];
//mst.update(mst.head,0,MAXVAL,1e9+1,0);
//t.pb({1e9+1,0});
FOR(i,k){
int x;
cin >> x;
t2[i] = x;
t.pb({x,i+1});
mst.update(mst.head,0,MAXVAL,x,i+1);
}
//brute(aa,bb,t2,n,k);
//cout << endl;
//return 0;
sort(all.begin(), all.end());
sort(t.begin(), t.end());
reverse(all.begin(), all.end());
reverse(t.begin(), t.end());
int ptr = 0;
for(auto e : all){
while(ptr < (int)t.size() and t[ptr].ff >= e.ff.ff){
sst.update(sst.head,0,MAXVAL,t[ptr].ss,1);
ptr++;
}
int lst = 1 + mst.get(mst.head,0,MAXVAL,e.ff.ss,e.ff.ff-1);
//ns[e.ss] += lst > 0;
if(ans[e.ss] == 1 and lst > 0)ans[e.ss] = 0;
//cout << e.ss+1 << " " << e.ff.ff << " " << lst << " " << ptr << " ";
int x = sst.get(sst.head,0,MAXVAL,lst,MAXVAL-2);
//cout << x << " " << ans[e.ss] << endl;;
ans[e.ss] += x;
}
ll sum = 0;
ll tos[n];
for(auto e:all){
if(ans[e.ss]%2 == 0){
sum += e.ff.ff;
tos[e.ss] = e.ff.ff;
}else{
sum += e.ff.ss;
tos[e.ss] = e.ff.ss;
}
}
//FOR(i,n)cout << tos[i] << " ";
cout << sum << endl;
return 0;
}
Compilation message
fortune_telling2.cpp: In function 'int main()':
fortune_telling2.cpp:133:6: warning: variable 'aa' set but not used [-Wunused-but-set-variable]
int aa[n];
^~
fortune_telling2.cpp:134:6: warning: variable 'bb' set but not used [-Wunused-but-set-variable]
int bb[n];
^~
fortune_telling2.cpp:152:6: warning: variable 't2' set but not used [-Wunused-but-set-variable]
int t2[k];
^~
fortune_telling2.cpp:195:5: warning: variable 'tos' set but not used [-Wunused-but-set-variable]
ll tos[n];
^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
1792 KB |
Output is correct |
2 |
Correct |
6 ms |
1792 KB |
Output is correct |
3 |
Correct |
7 ms |
1792 KB |
Output is correct |
4 |
Correct |
7 ms |
1764 KB |
Output is correct |
5 |
Correct |
9 ms |
1792 KB |
Output is correct |
6 |
Correct |
6 ms |
1764 KB |
Output is correct |
7 |
Correct |
7 ms |
1792 KB |
Output is correct |
8 |
Correct |
7 ms |
1664 KB |
Output is correct |
9 |
Correct |
5 ms |
1692 KB |
Output is correct |
10 |
Correct |
4 ms |
640 KB |
Output is correct |
11 |
Correct |
7 ms |
1792 KB |
Output is correct |
12 |
Correct |
6 ms |
1792 KB |
Output is correct |
13 |
Correct |
6 ms |
1792 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
1792 KB |
Output is correct |
2 |
Correct |
6 ms |
1792 KB |
Output is correct |
3 |
Correct |
7 ms |
1792 KB |
Output is correct |
4 |
Correct |
7 ms |
1764 KB |
Output is correct |
5 |
Correct |
9 ms |
1792 KB |
Output is correct |
6 |
Correct |
6 ms |
1764 KB |
Output is correct |
7 |
Correct |
7 ms |
1792 KB |
Output is correct |
8 |
Correct |
7 ms |
1664 KB |
Output is correct |
9 |
Correct |
5 ms |
1692 KB |
Output is correct |
10 |
Correct |
4 ms |
640 KB |
Output is correct |
11 |
Correct |
7 ms |
1792 KB |
Output is correct |
12 |
Correct |
6 ms |
1792 KB |
Output is correct |
13 |
Correct |
6 ms |
1792 KB |
Output is correct |
14 |
Correct |
52 ms |
13284 KB |
Output is correct |
15 |
Correct |
105 ms |
24760 KB |
Output is correct |
16 |
Correct |
157 ms |
35700 KB |
Output is correct |
17 |
Correct |
225 ms |
47680 KB |
Output is correct |
18 |
Correct |
225 ms |
47696 KB |
Output is correct |
19 |
Correct |
216 ms |
46876 KB |
Output is correct |
20 |
Correct |
210 ms |
47608 KB |
Output is correct |
21 |
Correct |
178 ms |
44644 KB |
Output is correct |
22 |
Correct |
106 ms |
12052 KB |
Output is correct |
23 |
Correct |
107 ms |
11856 KB |
Output is correct |
24 |
Correct |
109 ms |
11732 KB |
Output is correct |
25 |
Correct |
102 ms |
12656 KB |
Output is correct |
26 |
Correct |
175 ms |
46956 KB |
Output is correct |
27 |
Correct |
184 ms |
47852 KB |
Output is correct |
28 |
Correct |
183 ms |
46228 KB |
Output is correct |
29 |
Correct |
199 ms |
47764 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
1792 KB |
Output is correct |
2 |
Correct |
6 ms |
1792 KB |
Output is correct |
3 |
Correct |
7 ms |
1792 KB |
Output is correct |
4 |
Correct |
7 ms |
1764 KB |
Output is correct |
5 |
Correct |
9 ms |
1792 KB |
Output is correct |
6 |
Correct |
6 ms |
1764 KB |
Output is correct |
7 |
Correct |
7 ms |
1792 KB |
Output is correct |
8 |
Correct |
7 ms |
1664 KB |
Output is correct |
9 |
Correct |
5 ms |
1692 KB |
Output is correct |
10 |
Correct |
4 ms |
640 KB |
Output is correct |
11 |
Correct |
7 ms |
1792 KB |
Output is correct |
12 |
Correct |
6 ms |
1792 KB |
Output is correct |
13 |
Correct |
6 ms |
1792 KB |
Output is correct |
14 |
Correct |
52 ms |
13284 KB |
Output is correct |
15 |
Correct |
105 ms |
24760 KB |
Output is correct |
16 |
Correct |
157 ms |
35700 KB |
Output is correct |
17 |
Correct |
225 ms |
47680 KB |
Output is correct |
18 |
Correct |
225 ms |
47696 KB |
Output is correct |
19 |
Correct |
216 ms |
46876 KB |
Output is correct |
20 |
Correct |
210 ms |
47608 KB |
Output is correct |
21 |
Correct |
178 ms |
44644 KB |
Output is correct |
22 |
Correct |
106 ms |
12052 KB |
Output is correct |
23 |
Correct |
107 ms |
11856 KB |
Output is correct |
24 |
Correct |
109 ms |
11732 KB |
Output is correct |
25 |
Correct |
102 ms |
12656 KB |
Output is correct |
26 |
Correct |
175 ms |
46956 KB |
Output is correct |
27 |
Correct |
184 ms |
47852 KB |
Output is correct |
28 |
Correct |
183 ms |
46228 KB |
Output is correct |
29 |
Correct |
199 ms |
47764 KB |
Output is correct |
30 |
Correct |
815 ms |
198304 KB |
Output is correct |
31 |
Correct |
966 ms |
200552 KB |
Output is correct |
32 |
Correct |
1167 ms |
202892 KB |
Output is correct |
33 |
Correct |
1324 ms |
207916 KB |
Output is correct |
34 |
Correct |
595 ms |
182472 KB |
Output is correct |
35 |
Correct |
1345 ms |
208016 KB |
Output is correct |
36 |
Correct |
1385 ms |
208040 KB |
Output is correct |
37 |
Correct |
1326 ms |
208096 KB |
Output is correct |
38 |
Correct |
1203 ms |
203876 KB |
Output is correct |
39 |
Correct |
1248 ms |
208100 KB |
Output is correct |
40 |
Correct |
993 ms |
188764 KB |
Output is correct |
41 |
Correct |
1223 ms |
206816 KB |
Output is correct |
42 |
Correct |
1235 ms |
207836 KB |
Output is correct |
43 |
Correct |
790 ms |
178244 KB |
Output is correct |
44 |
Correct |
748 ms |
169824 KB |
Output is correct |
45 |
Correct |
763 ms |
153416 KB |
Output is correct |
46 |
Correct |
662 ms |
58324 KB |
Output is correct |
47 |
Correct |
713 ms |
57444 KB |
Output is correct |
48 |
Correct |
1018 ms |
206308 KB |
Output is correct |
49 |
Correct |
1000 ms |
200748 KB |
Output is correct |