Advertisement
sk__j

Battle_Of_Bests_Soln

Apr 20th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.68 KB | None | 0 0
  1.     #include<bits/stdc++.h>
  2.     #include <ext/pb_ds/assoc_container.hpp> // Common file
  3.     #include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
  4.     #define ff first
  5.     #define ss second
  6.     #define pb push_back
  7.     #define pf push_front
  8.     #define eb emplace_back
  9.     #define emp emplace
  10.     #define ins insert
  11.     #define mp make_pair
  12.     #define mt make_tuple
  13.     #define sz(s) (int)s.size()
  14.     #define forp(i,a,b) for( int i=a;i<=b;i++)
  15.     #define rep(i,n)    for( int i=0;i<n;i++)
  16.     #define ren(i,n)    for( int i=n-1;i>=0;i--)
  17.     #define forn(i,a,b) for( int i=a;i>=b;i--)
  18.     #define w(t) while(t)
  19.     #define on cout<<"\n"
  20.     #define os cout <<" "
  21.     #define o2(a,b) cout<<a<<" "<<b
  22.     #define o(a) cout << a
  23.     #define bitcount __builtin_popcount
  24.     #define gcd __gcd
  25.     #define all(v) v.begin(),v.end()
  26.     #define mem(n,m) memset(n,m,sizeof(n))
  27.     #define pii pair<int,int>
  28.     #define tiii tuple<int, int, int>
  29.     #define pll pair<long long,long long>
  30.     #define sii set<int>
  31.     #define sll set<long long>
  32.     #define vii vector<int>
  33.     #define vll vector<long long>
  34.     #define mii map<int,int>
  35.     #define mll map<long long, long long>
  36.     #define lob lower_bound
  37.     #define upb upper_bound
  38.     #define ret return
  39.     #define present(s,x) (s.find(x) != s.end())
  40.     #define cpresent(s,x) (find(all(s),x) != s.end())
  41.     #define ford(container, it) for(auto it = container.begin(); it != container.end(); it++)
  42.     #define fors(container, it, a, b) for(auto it = a; it != b; it++)
  43.     using namespace __gnu_pbds;
  44.     #define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  45.     #define MOD 1000000007
  46.     #define EPSILON 1e-9
  47.     #define PI acos(-1)
  48.     #define inf 1e18
  49.     #define siz 100005
  50.     #define SIZ 1000005
  51.     #define SIZE 200005
  52.     #define int long long
  53.     #define debug(a) cerr<<#a<<": ";for(auto i:a)cerr<<i<<" ";cerr<<'\n';
  54.     #define trace(a) cerr<<#a<<": "<<a<<"\n"
  55.     #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
  56.      
  57.      
  58.     typedef long long  ll;
  59.     typedef long double  ldo;
  60.     typedef double  db ;
  61.     using namespace std;
  62.     auto start = std::chrono::system_clock::now();
  63.     inline void skj()
  64.     {
  65.       std::chrono::time_point<std::chrono::system_clock> end;
  66.       end = std::chrono::system_clock::now();
  67.       std::chrono::duration<double> elapsed_seconds = end - start;
  68.       std::time_t end_time = std::chrono::system_clock::to_time_t(end);
  69.       cerr<<"Time taken " << elapsed_seconds.count()*1000<<"\n";
  70.     }
  71.      
  72.     mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  73.     //uniform_int_distribution<int> ud(1, 100); use this for random number generator
  74.      
  75.         //custom hash for unordered map
  76.     struct custom_hash {
  77.         static uint64_t splitmix64(uint64_t x) {
  78.             // http://xorshift.di.unimi.it/splitmix64.c
  79.             x += 0x9e3779b97f4a7c15;
  80.             x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
  81.             x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
  82.             return x ^ (x >> 31);
  83.         }
  84.          
  85.         size_t operator()(uint64_t x) const {
  86.             static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
  87.             return splitmix64(x + FIXED_RANDOM);
  88.         }
  89.     };
  90.     inline int binexp(int a,int b,int m)
  91.     {
  92.         int res=1;
  93.         a%=m;
  94.         while(b)
  95.         {
  96.             if(b&1)
  97.                 res=(res*1ll*a)%m;
  98.             a=(a*1ll*a)%m;
  99.             b>>=1;
  100.         }
  101.         return res;
  102.     }
  103.     const ll is_query = -(1LL<<62);
  104.     struct Line {
  105.         ll m, b;
  106.         mutable function<const Line*()> succ;
  107.         bool operator<(const Line& rhs) const {
  108.             if (rhs.b != is_query) return m < rhs.m;
  109.             const Line* s = succ();
  110.             if (!s) return 0;
  111.             ll x = rhs.m;
  112.             return b - s->b < (s->m - m + 0.0) * x;
  113.         }
  114.     };
  115.     struct HullDynamic : public multiset<Line> { // will maintain upper hull for maximum
  116.         bool bad(iterator y) {
  117.             auto z = next(y);
  118.             if (y == begin()) {
  119.                 if (z == end()) return 0;
  120.                 return y->m == z->m && y->b <= z->b;
  121.             }
  122.             auto x = prev(y);
  123.             if (z == end()) return y->m == x->m && y->b <= x->b;
  124.             return (x->b - y->b)*1.0*(z->m - y->m) >= (y->b - z->b)*1.0*(y->m - x->m);
  125.         }
  126.         void insert_line(ll m, ll b) {
  127.             auto y = insert({ m, b });
  128.             y->succ = [=] { return next(y) == end() ? 0 : &*next(y); };
  129.             if (bad(y)) { erase(y); return; }
  130.             while (next(y) != end() && bad(next(y))) erase(next(y));
  131.             while (y != begin() && bad(prev(y))) erase(prev(y));
  132.         }
  133.         ll eval(ll x) {
  134.             auto l = *lower_bound((Line) { x, is_query });
  135.             return l.m * x + l.b;
  136.         }
  137.     };
  138.     const int N = 1e4+5, M = 15;
  139.     HullDynamic seg[4 * N][M];
  140.     int expr[N][M], rel[N][M];
  141.     inline void build(int i, int pos, int s, int e) {
  142.         if(s == e) {
  143.             seg[i][pos].insert_line(rel[s][pos], expr[s][pos]);
  144.         }
  145.         else {
  146.             int mid = (s + e) >> 1, p = i << 1, q = p | 1;
  147.             build(p, pos, s, mid);
  148.             build(q, pos, mid + 1, e);
  149.             forp(j, s, e) {
  150.                 seg[i][pos].insert_line(rel[j][pos], expr[j][pos]);
  151.             }
  152.         }
  153.     }
  154.     inline void update(int i, int pos, int s, int e, int ind) {
  155.         if(s == e) {
  156.             seg[i][pos].insert_line(rel[s][pos], expr[s][pos]);
  157.         }
  158.         else {
  159.             int mid = (s + e) >> 1, p = i << 1, q = p | 1;
  160.             if(ind <= mid) {
  161.                 update(p, pos, s, mid, ind);
  162.             }
  163.             else {
  164.                 update(q, pos, mid + 1, e, ind);
  165.             }
  166.             seg[i][pos].insert_line(rel[ind][pos], expr[ind][pos]);
  167.         }
  168.     }
  169.     inline int query(int i, int pos, int s, int e, int l, int r, int x) {
  170.         if(s > r || e < l) {
  171.             ret 0;
  172.         }
  173.         if(s >= l && e <= r) {
  174.             ret seg[i][pos].eval(x);
  175.         }
  176.         int mid = (s + e) >> 1, p = i << 1, q = p | 1;
  177.         ret max(query(p, pos, s, mid, l, r, x), query(q, pos, mid + 1, e, l, r, x));
  178.     }
  179.     inline void solve_me_senpai() {
  180.         int n, m, q;
  181.         cin >> n >> m >> q;
  182.         assert(1 <= n && n <= 1e4);
  183.         assert(1 <= m && m <= 10);
  184.         assert(1 <= q && q <= 1e5);
  185.         forp(i, 1, n) {
  186.             forp(j, 1, m) {
  187.                 cin >> rel[i][j];
  188.                 assert(1 <= rel[i][j] && rel[i][j] <= 1e6);
  189.             }
  190.         }
  191.         forp(i, 1, n) {
  192.             forp(j, 1, m) {
  193.                 cin >> expr[i][j];     
  194.                 assert(1 <= expr[i][j] && expr[i][j] <= 1e6);
  195.             }
  196.         }
  197.         forp(i, 1, m) {
  198.             build(1, i, 1, n);
  199.         }
  200.         int c = 0;
  201.         w(q--) {
  202.             int t;
  203.             cin >> t;
  204.             if(t == 1) {
  205.                 int i, p, x;
  206.                 cin >> i >> p >> x;
  207.                 expr[i][p] += x;
  208.                 assert(1 <= i && i <= n);
  209.                 assert(1 <= p && p <= m);
  210.                 assert(1 <= x && x <= 1e6);
  211.                 update(1, p, 1, n, i);
  212.             }
  213.             else {
  214.                 int l, r, p, x;
  215.                 cin >> l >> r >> p >> x;
  216.                 assert(l <= r);
  217.                 assert(1 <= l);
  218.                 assert(r <= n);
  219.                 assert(1 <= p &&  p <= m);
  220.                 assert(1 <= x && x <= 1e6);
  221.                 o2(query(1, p, 1, n, l, r, x), "\n");
  222.                 c++;
  223.             }
  224.         }
  225.         assert(c > 0);
  226.     }
  227.      
  228.     signed main()
  229.     {
  230.         boost
  231.         #ifndef ONLINE_JUDGE
  232.         freopen("input.txt", "r",stdin);
  233.         freopen("ot.txt", "w", stdout);
  234.         #endif
  235.         int t = 1;
  236.         //cin >> t;
  237.         //int a = 1;
  238.         while(t--) {
  239.           //  cout << "Case #" << a  << ": ";
  240.             solve_me_senpai();
  241.             //a++;
  242.         }
  243.         skj();
  244.         return 0;
  245.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement