Jeremiah_

Banco do Faraó - Neps

Mar 15th, 2020
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.44 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. #define SYNC ios::sync_with_stdio(0);
  4. #define F first
  5. #define S second
  6. #define endl '\n'
  7.  
  8.  
  9. using namespace std;
  10.  
  11. using ll = long long int;
  12. using ii = pair<int, int>;
  13. using vii = vector<ii>;
  14. using vi = vector<int>;
  15. using graph = vector<vi>;
  16. const int INF = 0x3f3f3f3f;
  17. const int MAXN = 199999;
  18. const ll mod = 1000000007;
  19. const double eps = 0.000000001;
  20.  
  21.  
  22. int acc[MAXN];
  23.  
  24. struct data {
  25.   int sum, pref, suff, ans, num_elem_sum, num_elem_pref, num_elem_suff, num_elem_ans;
  26. };
  27.  
  28. data segtree[4*MAXN];
  29.  
  30. data make_data(int val) {
  31.   data temp;
  32.   temp.sum = temp.pref = temp.suff = temp.ans = val;
  33.   temp.num_elem_sum = temp.num_elem_pref = temp.num_elem_suff = temp.num_elem_ans = 1;
  34.   return temp;
  35. }
  36.  
  37. data combine(data l, data r) {
  38.   data temp;
  39.   temp.sum = l.sum + r.sum;
  40.   temp.num_elem_sum = l.num_elem_sum + r.num_elem_sum;
  41.   if (l.pref > l.sum + r.pref) {
  42.     temp.pref = l.pref;
  43.     temp.num_elem_pref = l.num_elem_pref;
  44.   } else {
  45.     temp.pref = l.sum + r.pref;
  46.     temp.num_elem_pref = l.num_elem_sum + r.num_elem_pref;
  47.   }
  48.  
  49.   if (r.suff > l.suff + r.sum) {
  50.     temp.suff = r.suff;
  51.     temp.num_elem_suff = r.num_elem_suff;
  52.   } else {
  53.     temp.suff = l.suff + r.sum;
  54.     temp.num_elem_suff = l.num_elem_suff + r.num_elem_sum;
  55.   }
  56.  
  57.   if (max(l.ans, r.ans) > l.suff + r.pref) {
  58.     if (l.ans > r.ans) {
  59.       temp.ans = l.ans;
  60.       temp.num_elem_ans = l.num_elem_ans;
  61.     } else if (l.ans < r.ans) {
  62.       temp.ans = r.ans;
  63.       temp.num_elem_ans = r.num_elem_ans;
  64.     } else {
  65.       temp.ans = r.ans;
  66.       temp.num_elem_ans = max(r.num_elem_ans, l.num_elem_ans);
  67.     }
  68.   } else if (max(l.ans, r.ans) < l.suff + r.pref) {
  69.     temp.ans = l.suff + r.pref;
  70.     temp.num_elem_ans = l.num_elem_suff + r.num_elem_pref;
  71.   } else {
  72.     temp.ans = l.suff + r.pref;
  73.     temp.num_elem_ans = max(max(l.num_elem_ans, r.num_elem_ans), l.num_elem_suff + r.num_elem_pref);
  74.   }
  75.  
  76.   return temp;
  77.  
  78. }
  79.  
  80. data combin(data l, data r) {
  81.   data res;
  82.   res.sum = l.sum + r.sum;
  83.   res.pref = max(l.pref, l.sum + r.pref);
  84.   res.suff = max(r.suff, r.sum + l.suff);
  85.   res.ans = max(max(l.ans, r.ans), l.suff + r.pref);
  86.   return res;
  87.  
  88.  
  89. }
  90.  
  91. void build(int node, int cl, int cr) {
  92.   if (cl == cr) {
  93.     segtree[node] = make_data(acc[cl]);
  94.   } else {
  95.     int mid = cl + (cr - cl)/2;
  96.     build(node*2, cl, mid);
  97.     build(node*2 + 1, mid+1, cr);
  98.     segtree[node] = combine(segtree[node*2], segtree[node*2 + 1]);
  99.   }
  100. }
  101.  
  102. data max_sum(int node, int cl, int cr, int l, int r) {
  103.   if (l > r) return make_data(-INF);
  104.  
  105.   if (cl == l && cr == r) {
  106.     return segtree[node];
  107.   }
  108.  
  109.   int mid = cl + (cr - cl)/2;
  110.  
  111.   data d1 = max_sum(node*2, cl, mid, l, min(r, mid));
  112.   data d2 = max_sum(node*2 +1, mid+1, cr, max(l, mid+1), r);
  113.   if (d1.ans == -INF) return d2;
  114.   if (d2.ans == -INF) return d1;
  115.   return combine(d1, d2);
  116. }
  117.  
  118.  
  119.  
  120. int main() {
  121.   SYNC
  122.   int num_inst, num_acc, num_q;
  123.   scanf("%d", &num_inst);
  124.   while(num_inst--) {
  125.     //memset(segtree, 0, sizeof(segtree));
  126.     //memset(acc, 0, sizeof(acc));
  127.     scanf("%d", &num_acc);
  128.     for (int i = 0; i < num_acc; ++i) {
  129.       scanf("%d", &acc[i]);
  130.     }
  131.  
  132.     build(1, 0, num_acc-1);
  133.     scanf("%d", &num_q);  
  134.     int l, r;
  135.     while(num_q--) {
  136.       scanf("%d %d", &l, &r);
  137.       data ans = max_sum(1, 0, num_acc-1, l-1, r-1); //find the subsegment with the greater sum
  138.       printf("%d %d\n", ans.ans, ans.num_elem_ans);
  139.     }
  140.   }
  141.   return 0;
  142. }
Advertisement
Add Comment
Please, Sign In to add comment