Advertisement
MAGCARI

Magic Ring

May 1st, 2023 (edited)
938
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.69 KB | None | 0 0
  1. /*
  2.     Task        : _example
  3.     Author      : Phumipat C. [MAGCARI]
  4.     Language    : C++
  5. */
  6. #include<bits/stdc++.h>
  7. using namespace std;
  8. struct A{
  9.     int idx,val,type;
  10.     // type 0 = +, type 1 = -, type 2 = *2,type 3 = /2
  11.     bool operator < (const A&o) const{
  12.         if(idx!=o.idx)  return idx < o.idx;
  13.         else            return type > o.type;
  14.     }
  15. };
  16. void solve(){
  17.     int n,m;
  18.     cin >> n >> m;
  19.     vector<A > e;
  20.     for(int i=1;i<=n;i++){
  21.         int t,x,y,z;
  22.         cin >> t >> x >> y >> z;
  23.         if(t == 0){
  24.             if(x+y>m){
  25.                 e.push_back({x,z,0});
  26.                 e.push_back({m,z,1});
  27.                 e.push_back({0,z,0});
  28.                 e.push_back({(x+y)%m,z,1});
  29.             }else{
  30.                 e.push_back({x,z,0});
  31.                 e.push_back({x+y,z,1});
  32.             }
  33.         }else{
  34.             if(x+y>m){
  35.                 e.push_back({x,z,2});
  36.                 e.push_back({m,z,3});
  37.                 e.push_back({0,z,2});
  38.                 e.push_back({(x+y)%m,z,3});
  39.             }else{
  40.                 e.push_back({x,z,2});
  41.                 e.push_back({x+y,z,3});
  42.             }
  43.         }
  44.     }
  45.     e.push_back({0,0,0});
  46.     sort(e.begin(),e.end());
  47.     e.push_back({m,0,0});
  48.     long long now = 0,cnt = 0,ansNum = 0,ansVal = -1e18;
  49.     for(int i=0;i<e.size()-1;i++){
  50.         if(e[i].type == 0){
  51.             now += e[i].val * (1ll << cnt);
  52.         }else if(e[i].type == 1){
  53.             now -= e[i].val * (1ll << cnt);
  54.         }else if(e[i].type == 2){
  55.             now *= 2;
  56.             cnt++;
  57.         }else{
  58.             now /= 2;
  59.             cnt--;
  60.         }
  61.         if(now > ansVal && e[i+1].idx != e[i].idx){
  62.             ansVal = now;
  63.             ansNum = e[i+1].idx - e[i].idx;
  64.         }else if(now == ansVal && e[i+1].idx != e[i].idx){
  65.             ansNum += e[i+1].idx - e[i].idx;
  66.         }
  67.     }
  68.     cout << ansNum << ' ' << ansVal << '\n';
  69. }
  70. int main(){
  71.     int q;
  72.     cin >> q;
  73.     while(q--){
  74.         solve();
  75.     }
  76.     return 0;
  77. }
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement