Submission #123640

#TimeUsernameProblemLanguageResultExecution timeMemory
123640Osama_AlkhodairySails (IOI07_sails)C++17
100 / 100
622 ms4344 KiB
#include <bits/stdc++.h> using namespace std; #define finish(x) return cout << x << endl, 0 #define ll long long const int N = 100001; int n, tree[4 * N], lazy[4 * N]; int arr[N]; vector <pair <int, int> > a; void shift(int l, int r, int node){ if(r < l) return; if(lazy[node] == 0) return; tree[node] += (r - l + 1) * lazy[node]; if(l != r){ lazy[2 * node] += lazy[node]; lazy[2 * node + 1] += lazy[node]; } lazy[node] = 0; } void update(int l, int r, int node, int s, int e){ if(r < l || r < s || l > e) return; shift(l, r, node); if(s <= l && r <= e){ tree[node] += r - l + 1; if(l != r){ lazy[2 * node]++; lazy[2 * node + 1]++; } return; } int mid = (l + r) / 2; update(l, mid, 2 * node, s, e); update(mid + 1, r, 2 * node + 1, s, e); tree[node] = tree[2 * node] + tree[2 * node + 1]; } int query(int l, int r, int node, int idx){ assert(l <= idx && idx <= r); shift(l, r, node); if(l == r) return tree[node]; int mid = (l + r) / 2; if(idx <= mid) return query(l, mid, 2 * node, idx); return query(mid + 1, r, 2 * node + 1, idx); } int query(int idx){ if(idx <= 0 || idx >= N) return -1; return query(1, N - 1, 1, idx); } pair <int, int> bounds(int idx){ pair <int, int> ret; int cur = query(idx); { int l = 1, r = idx; while(l <= r){ int mid = (l + r) / 2; if(query(mid) == cur) r = mid - 1; else l = mid + 1; } ret.first = l; } { int l = idx, r = N; while(l <= r){ int mid = (l + r) / 2; if(query(mid) == cur) l = mid + 1; else r = mid - 1; } ret.second = r; } return ret; } void upd(int l, int r){ if(r < l) return; update(1, N - 1, 1, l, r); } void update(int l, int r){ int k = r - l + 1; auto b = bounds(l); b.second = min(b.second, r); upd(b.second + 1, r); k -= r - b.second; upd(b.first, b.first + k - 1); } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; a.resize(n); for(auto &i : a) cin >> i.first >> i.second; sort(a.begin(), a.end()); for(auto &i : a){ update(i.first - i.second + 1, i.first); } ll ans = 0; for(int i = 1 ; i < N ; i++){ int cur = query(i); ans += 1LL * cur * (cur - 1) / 2; } cout << ans << endl; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...