Submission #59335

#TimeUsernameProblemLanguageResultExecution timeMemory
59335gusfringFortune Telling 2 (JOI14_fortune_telling2)C++14
100 / 100
2392 ms148700 KiB
#include<bits/stdc++.h> using namespace std; const int maxn = 2e5 + 5; int n,m; int a[maxn], b[maxn], c[maxn]; int pos[maxn], res[maxn]; vector<int> p[maxn]; int sz; int limit, tree[maxn*9]; int fen[maxn*3]; map<int,int> mp; int rec[maxn*3]; void cpidx() { for(int i=1; i<=n; i++) mp[a[i]], mp[b[i]]; for(int i=1; i<=m; i++) mp[c[i]]; for(auto &it : mp){ it.second = ++sz; rec[sz] = it.first; } for(int i=1; i<=n; i++) a[i] = mp[a[i]], b[i] = mp[b[i]]; for(int i=1; i<=m; i++) c[i] = mp[c[i]]; } void init(){ limit = 1; while(limit<=sz) limit*=2; } void update(int x, int val){ x += limit; tree[x] = max(tree[x], val); while(x){ x/=2; tree[x] = max(tree[x<<1], tree[x<<1|1]); } } int query(int l, int r){ l += limit; r += limit; int res = 0; while(l<r){ if(l % 2 != 0) res = max(res, tree[l++]); if(r % 2 == 0) res = max(res, tree[r--]); l/=2; r/=2; } if(l == r) res = max(res, tree[l]); return res; } void add(int x, int val){ while(x){ fen[x] += val; x -= x&-x; } } int sum(int x){ int res = 0; while(x<=sz){ res += fen[x]; x += x&-x; } return res; } int main(){ scanf("%d%d",&n,&m); for(int i=1; i<=n; i++) scanf("%d%d",&a[i],&b[i]); for(int i=1; i<=m; i++) scanf("%d",&c[i]); cpidx(); init(); for(int i=1; i<=m; i++) update(c[i], i); for(int i=1; i<=n; i++) { pos[i] = query(min(a[i],b[i]), max(a[i],b[i])-1); p[pos[i]+1].push_back(i); } for(int i=m; i>=1; i--){ add(c[i],1); for(auto x : p[i]) res[x] = sum(max(a[x], b[x])); } long long ans = 0; for(int i=1; i<=n; i++){ if(pos[i]==0){ if(res[i] % 2 == 0) ans += rec[a[i]]; else ans += rec[b[i]]; } else{ if(res[i] % 2 == 1) ans += rec[min(a[i], b[i])]; else ans += rec[max(a[i], b[i])]; } } printf("%lld",ans); }

Compilation message (stderr)

fortune_telling2.cpp: In function 'int main()':
fortune_telling2.cpp:71:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d",&n,&m);
     ~~~~~^~~~~~~~~~~~~~
fortune_telling2.cpp:72:34: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     for(int i=1; i<=n; i++) scanf("%d%d",&a[i],&b[i]);
                             ~~~~~^~~~~~~~~~~~~~~~~~~~
fortune_telling2.cpp:73:34: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     for(int i=1; i<=m; i++) scanf("%d",&c[i]);
                             ~~~~~^~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...