Advertisement
Alex_tz307

3772. JocCuLasere

Apr 25th, 2021
893
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.13 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. using int64 = long long;
  5.  
  6. ifstream fin("lasere.in");
  7. ofstream fout("lasere.out");
  8.  
  9. const int MAXN = 1e5 + 5;
  10.  
  11. struct award {
  12.   int t, h;
  13.  
  14.   bool operator < (const award &A) const {
  15.     return t < A.t;
  16.   }
  17. } e1[MAXN], e2[MAXN];
  18.  
  19. struct laser {
  20.   int t, h1, h2, ind;
  21.  
  22.   bool operator < (const laser &A) const {
  23.     return t < A.t;
  24.   }
  25. } l[MAXN];
  26.  
  27. int sol[MAXN], N, H[MAXN << 1], val, aib[MAXN << 1];
  28. unordered_map<int,int> Hash;
  29.  
  30. void update(int x, int add) {
  31.   for (int i = x; i <= val; i += i & -i)
  32.     aib[i] += add;
  33. }
  34.  
  35. int query(int l, int r) {
  36.   int ans = 0;
  37.   for (int i = r; i > 0; i -= i & -i)
  38.     ans += aib[i];
  39.   for (int i = l - 1; i > 0; i -= i & -i)
  40.     ans -= aib[i];
  41.   return ans;
  42. }
  43.  
  44. void solve() {
  45.   int Q;
  46.   fin >> Q;
  47.   int n = 0, m = 0;
  48.   for (int i = 0; i < Q; ++i) {
  49.     int type;
  50.     fin >> type;
  51.     if (type == 1) {
  52.       int t, d, h;
  53.       fin >> t >> d >> h;
  54.       e1[++n] = {t, h};
  55.       e2[n] = {t + d, h};
  56.       H[++N] = h;
  57.     } else {
  58.       int t, h1, h2;
  59.       fin >> t >> h1 >> h2;
  60.       ++m;
  61.       l[m] = {t, h1, h2, m};
  62.       H[++N] = h1;
  63.       H[++N] = h2;
  64.     }
  65.   }
  66.   sort(H + 1, H + N + 1);
  67.   for (int i = 1; i <= N; ++i) {
  68.     if (H[i] == H[i - 1])
  69.       continue;
  70.     ++val;
  71.     Hash[H[i]] = val;
  72.   }
  73.   for (int i = 1; i <= n; ++i)
  74.     e1[i].h = e2[i].h = Hash[e1[i].h];
  75.   for (int i = 1; i <= m; ++i) {
  76.     l[i].h1 = Hash[l[i].h1];
  77.     l[i].h2 = Hash[l[i].h2];
  78.   }
  79.   Hash.clear();
  80.   sort(e1 + 1, e1 + n + 1);
  81.   sort(e2 + 1, e2 + n + 1);
  82.   sort(l + 1, l + m + 1);
  83.   int p1 = 1, p2 = 1;
  84.   int64 ans = 0;
  85.   for (int i = 1; i <= m; ++i) {
  86.     while (p1 <= n && e1[p1].t <= l[i].t) {
  87.       update(e1[p1].h, 1);
  88.       ++p1;
  89.     }
  90.     while (p2 <= n && e2[p2].t < l[i].t) {
  91.       update(e2[p2].h, -1);
  92.       ++p2;
  93.     }
  94.     sol[l[i].ind] = query(l[i].h1, l[i].h2);
  95.     ans += sol[l[i].ind];
  96.   }
  97.   for (int i = 1; i <= m; ++i)
  98.     fout << sol[i] << '\n';
  99.   fout << ans << '\n';
  100. }
  101.  
  102. void close_files() {
  103.   fin.close();
  104.   fout.close();
  105. }
  106.  
  107. int main() {
  108.   solve();
  109.   close_files();
  110.   return 0;
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement