Advertisement
Dang_Quan_10_Tin

SSEQ (VOI 2022)

Mar 5th, 2022
1,050
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.37 KB | None | 0 0
  1. #define task "SSEQ"
  2. #include <iostream>
  3. #include <cstdio>
  4. #include <vector>
  5. #include <algorithm>
  6. #include <cstring>
  7.  
  8. using namespace std;
  9. using ll = long long;
  10.  
  11. template<class T>
  12. void read(T &x) {
  13.     x = 0;
  14.     register int c;
  15.  
  16.     while((c = getchar()) && (c > '9' || c < '0'));
  17.  
  18.     for(; c >= '0' && c <= '9'; c = getchar())
  19.         x = x * 10 + c - '0';
  20. }
  21.  
  22. template<class T>
  23. void readneg(T &x) {
  24.     x = 0;
  25.     register int c;
  26.     bool neg = 0;
  27.  
  28.     while((c = getchar()) && (c > '9' || c < '0'))
  29.         if(c == '-')
  30.             neg = 1;
  31.  
  32.     for(; c >= '0' && c <= '9'; c = getchar())
  33.         x = x * 10 + c - '0';
  34.  
  35.     if(neg) x = -x;
  36. }
  37.  
  38. constexpr int N = 1e6 + 5;
  39. int n;
  40. struct Segment{
  41.     int l, r;
  42.     ll w;
  43. } a[N];
  44.  
  45. vector<int> s, in[N];
  46.  
  47. void Read() {
  48.     //cin >> n;
  49.     read(n);
  50.  
  51.     for(int i = 1; i <= n; ++i)
  52.         //cin >> a[i].l >> a[i].r >> a[i].w;
  53.         read(a[i].l), read(a[i].r), readneg(a[i].w);
  54. }
  55.  
  56. void Sub_1() {
  57.     for(int i = 1; i <= n; ++i) {
  58.         s.emplace_back(a[i].l);
  59.         s.emplace_back(a[i].r);
  60.     }
  61.  
  62.     sort(s.begin(), s.end());
  63.     s.resize(unique(s.begin(), s.end()) - s.begin());
  64.  
  65.     ll ans(0);
  66.  
  67.     for(int i = 0; i < (int)s.size(); ++i)
  68.         for(int j = i; j < (int)s.size(); ++j) {
  69.             ll res(0);
  70.  
  71.             for(int t = 1; t <= n; ++t)
  72.                 if(s[i] <= a[t].l && a[t].r <= s[j])
  73.                     res += a[t].w;
  74.  
  75.             ans = max(ans, res);
  76.         }
  77.  
  78.     cout << ans;
  79. }
  80.  
  81. struct SegmentTree {
  82.     int n;
  83.     ll st[N * 4], lazy[N * 4];
  84.  
  85.     SegmentTree() {
  86.         n = 1e6;
  87.         memset(lazy, 0, sizeof lazy);
  88.         memset(st, 0, sizeof st);
  89.     }
  90.  
  91.     void Push(int id) {
  92.         if(lazy[id]) {
  93.             st[id << 1] += lazy[id];
  94.             lazy[id << 1] += lazy[id];
  95.             st[id << 1 | 1] += lazy[id];
  96.             lazy[id << 1 | 1] += lazy[id];
  97.             lazy[id] = 0;
  98.         }
  99.     }
  100.  
  101.     void Update(int id, int l, int r, const int &a, const int &b, const ll &v) {
  102.         if(l > b || r < a) return;
  103.         if(l >= a && r <= b) {
  104.             st[id] += v;
  105.             lazy[id] += v;
  106.             return;
  107.         }
  108.  
  109.         Push(id);
  110.  
  111.         Update(id << 1, l, (l + r) / 2, a, b, v);
  112.         Update(id << 1 | 1, (l + r) / 2 + 1, r, a, b, v);
  113.  
  114.         st[id] = max(st[id << 1], st[id << 1 | 1]);
  115.     }
  116.  
  117.     void Update(int l, int r, ll v) {
  118.         Update(1, 1, n, l, r, v);
  119.     }
  120.  
  121.     ll Get(int id, int l, int r, const int &a, const int &b) {
  122.         if(l > b || r < a) return 0;
  123.  
  124.         if(l >= a && r <= b) return st[id];
  125.  
  126.         Push(id);
  127.  
  128.         return max(Get(id << 1, l, (l + r) / 2, a, b), Get(id << 1 | 1, (l + r) / 2 + 1, r, a, b));
  129.     }
  130.  
  131.     ll Get(int l, int r) {
  132.         return Get(1, 1, n, l, r);
  133.     }
  134. } f;
  135.  
  136. void Sub_4() {
  137.     for(int i = 1; i <= n; ++i)
  138.         in[a[i].r].emplace_back(i);
  139.  
  140.     ll ans(0);
  141.  
  142.  
  143.     for(int i = 1; i <= 1e6; ++i) {
  144.         for(auto j : in[i])
  145.             f.Update(1, a[j].l, a[j].w);
  146.         ans = max(ans, f.Get(1, i));
  147.     }
  148.  
  149.     cout << ans;
  150. }
  151.  
  152. int32_t main() {
  153.     ios_base::sync_with_stdio(0);
  154.     cin.tie(0);
  155.     cout.tie(0);
  156.  
  157.     if(fopen(task".INP", "r")) {
  158.         freopen(task".INP", "r", stdin);
  159.         freopen(task".OUT", "w", stdout);
  160.     }
  161.  
  162.     Read();
  163.  
  164.     if(n <= 200)
  165.         Sub_1();
  166.     else
  167.         Sub_4();
  168. }
  169.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement