Problem:
Array Update 2
Problem
項数
N
、初項
a
、公差
d
の等差数列
A
がある。
  以下の形式で、数列を書き換える
M
個の命令文が与えられるので、与えられた順序で
M
回
  数列
A
を書き換えたときの数列
A
の
K
項目の値を求めなさい。
i
番目の命令文は3つの整数
x
i
, y
i
, z
i
で与えられる。(1 ≤
i
≤
M
)
x
i
が0だった場合、
y
i
項目から
z
i
項目までの区間において、値の順序を反転する。
x
i
が1だった場合、
y
i
項目から
z
i
項目までの区間において、それぞれの値を1増加させる。
x
i
が2だった場合、
y
i
項目から
z
i
項目までの区間において、それぞれの値を半分にする（小数点以下は切り捨てる）。
Input
N
a
d
M
x
1
y
1
z
1
x
2
y
2
z
2
...
x
M
y
M
z
M
K
1行目に、1つの整数
N
が与えられる。
  2行目に、2つの整数
a
と
d
が空白区切りで与えられる。
  3行目に、1つの整数
M
が与えられる。

  4行目からの
M
行のうち
i
行目には
i
番目の命令文を表す 3 つの整数
x
i
, y
i
, z
i
が空白区切りで与えられる。

  最後の行に、1つの整数
K
が与えられる。
Constraints
2 ≤
N
≤ 200000
1 ≤
a
≤ 5
1 ≤
d
≤ 5
1 ≤
M
≤ 200000
0 ≤
x
i
≤ 2 (1 ≤
i
≤
M
)
1 ≤
y
i
≤
N
(1 ≤
i
≤
M
)
1 ≤
z
i
≤
N
(1 ≤
i
≤
M
)
y
i
<
z
i
(1 ≤
i
≤
M
)
1 ≤
K
≤
N
Output
入力で与えられた順番で数列
A
を
M
回更新したときの
K
項目を出力せよ。
Sample Input 1
4
2 1
3
0 1 2
1 1 4
2 2 4
3
Sample Output 1
2
{ 2 , 3 , 4 , 5 }
↓  0 1 2 ... 1項目から2項目までの区間の値の順序を反転する
{ 3 , 2 , 4 , 5 }
↓  1 1 4 ... 1項目から4項目までの区間の値をそれぞれ1増やす
{ 4 , 3 , 5 , 6 }
↓ 2 2 4 ... 2項目から4項目までの区間の値をそれぞれ半分にする（小数点以下切り捨てる）
{ 3 , 1 , 2 , 3 }
よって3項目の値は2である。
Sample Input 2
5
1 2
3
1 2 3
2 3 5
0 1 5
1
Sample Output 2
4
{ 1 , 3 , 5 , 7 , 9 }
↓  1 2 3 ... 2項目から3項目までの区間の値をそれぞれ1増やす
{ 1 , 4 , 6 , 7 , 9 }
↓ 2 3 5 ... 3項目から5項目までの区間の値をそれぞれ半分にする（小数点以下切り捨てる）
{ 1 , 4 , 3 , 3 , 4 }
↓  0 1 5 ... 1項目から5項目までの区間の値の順序を反転する
{ 4 , 3 , 3 , 4 , 1 }
よって1項目の値は4である。

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define int long long
typedef long long ll;
using namespace std;
#define inf 0x3f3f3f3f
const int N = 2E5+1000;
int n;
int a,d;
int m;
int k;
int x[N],y[N],z[N];
//一切从简单的地方出发
int sp[N],p = 0;

signed main(void){
    cin>>n;
    cin>>a>>d;
    cin>>m;
    for(int i = 0;i<m;i++){
            cin>>x[i]>>y[i]>>z[i];
            if(!x[i]){
                    sp[p++] = i;
            }
    }
    cin>>k;
    for(int i = p-1;i>=0;i--){
            int od = sp[i];
            int l = y[od], r = z[od];
            if(k>=l && k<=r){
                        k = l+r-k;
            }
    }
    int val = a+(k-1)*d;
    for(int i = 0;i<m;i++){
            if( k>=y[i] && k<=z[i]){
                    if(x[i] == 0){
                            k = y[i]+z[i]-k;
                    }else if(x[i] == 1){
                            ++val;
                    }else{
                            val/=2;
                    }
            }
    }
    cout<<val<<endl;


}
accuracy: 1
time_ms: 70
memory_kb: 8344
final_rank: 11

code_snippet: #include <bits/stdc++.h>
//#include <atcoder/all>
//using namespace atcoder;
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
using namespace std;
 
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<int, int> pii;
typedef pair<long long, long long> PLL;
typedef pair<int, PII> TIII;
 
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
 
 
#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
 
#define MOD 1000000007
 
template<class T1, class T2> inline bool chmax(T1 &a, T2 b) {if (a < b) {a = b; return true;} return false;}
template<class T1, class T2> inline bool chmin(T1 &a, T2 b) {if (a > b) {a = b; return true;} return false;}
const double EPS = 1e-8, PI = acos(-1);
const double pi = 3.141592653589793238462643383279;
//ここから編集    
typedef string::const_iterator State;
ll GCD(ll a, ll b){
  return (b==0)?a:GCD(b, a%b);
}
ll LCM(ll a, ll b){
  return a/GCD(a, b) * b;
}
template< int mod >
struct ModInt {
  int x;
 
  ModInt() : x(0) {}
 
  ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
 
  ModInt &operator+=(const ModInt &p) {
    if((x += p.x) >= mod) x -= mod;
    return *this;
  }
 
  ModInt &operator-=(const ModInt &p) {
    if((x += mod - p.x) >= mod) x -= mod;
    return *this;
  }
 
  ModInt &operator*=(const ModInt &p) {
    x = (int) (1LL * x * p.x % mod);
    return *this;
  }
 
  ModInt &operator/=(const ModInt &p) {
    *this *= p.inverse();
    return *this;
  }
 
  ModInt operator-() const { return ModInt(-x); }
 
  ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
 
  ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
 
  ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
 
  ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }
 
  bool operator==(const ModInt &p) const { return x == p.x; }
 
  bool operator!=(const ModInt &p) const { return x != p.x; }
 
  ModInt inverse() const {
    int a = x, b = mod, u = 1, v = 0, t;
    while(b > 0) {
      t = a / b;
      swap(a -= t * b, b);
      swap(u -= t * v, v);
    }
    return ModInt(u);
  }
 
  ModInt pow(int64_t n) const {
    ModInt ret(1), mul(x);
    while(n > 0) {
      if(n & 1) ret *= mul;
      mul *= mul;
      n >>= 1;
    }
    return ret;
  }
 
  friend ostream &operator<<(ostream &os, const ModInt &p) {
    return os << p.x;
  }
 
  friend istream &operator>>(istream &is, ModInt &a) {
    int64_t t;
    is >> t;
    a = ModInt< mod >(t);
    return (is);
  }
 
  static int get_mod() { return mod; }
};
 
using modint = ModInt< 1000000007 >;
template< typename T >
struct Combination {
  vector< T > _fact, _rfact, _inv;
 
  Combination(int sz) : _fact(sz + 1), _rfact(sz + 1), _inv(sz + 1) {
    _fact[0] = _rfact[sz] = _inv[0] = 1;
    for(int i = 1; i <= sz; i++) _fact[i] = _fact[i - 1] * i;
    _rfact[sz] /= _fact[sz];
    for(int i = sz - 1; i >= 0; i--) _rfact[i] = _rfact[i + 1] * (i + 1);
    for(int i = 1; i <= sz; i++) _inv[i] = _rfact[i] * _fact[i - 1];
  }
 
  inline T fact(int k) const { return _fact[k]; }
 
  inline T rfact(int k) const { return _rfact[k]; }
 
  inline T inv(int k) const { return _inv[k]; }
 
  T P(int n, int r) const {
    if(r < 0 || n < r) return 0;
    return fact(n) * rfact(n - r);
  }
 
  T C(int p, int q) const {
    if(q < 0 || p < q) return 0;
    return fact(p) * rfact(q) * rfact(p - q);
  }
 
  T H(int n, int r) const {
    if(n < 0 || r < 0) return (0);
    return r == 0 ? 1 : C(n + r - 1, r);
  }
};
 
ll modpow(ll x, ll n, ll mod) {
  ll res = 1;
  while(n) {
    if(n&1) res = (res * x) % mod;
    x = (x * x) % mod;
    n >>= 1;
  }
  return res;
} 
inline long long mod(long long a, long long m) {
    return (a % m + m) % m;
}
template<typename T> 
struct BIT{
  int N;
  std::vector<T> node;

  BIT(int n){
    N = n;
    node.resize(N+10);
  }

  /* a: 1-indexed */
  void add(int a, T x){
    for(int i=a; i<(int)node.size(); i += i & -i) node[i] += x;
  }

  /* [1, a] */
  T sum(int a){
    T res = 0;
    for(int x=a; x>0; x -= x & -x) res += node[x];
    return res;
  }

  /* [l, r] */
  T rangesum(int l, int r){
    return sum(r) - sum(l-1);
  }

  /* 
    a1+a2+...+aw >= valとなるような最小のwを返す
    @verify https://codeforces.com/contest/992/problem/E
  */
  int lower_bound(T val) {
    if(val < 0) return 0;

    int res = 0;
    int n = 1; 
    while (n < N) n *= 2;

    T tv=0;
    for (int k = n; k > 0; k /= 2) {
      if(res + k < N && node[res + k] < val){
        val -= node[res+k];
        res += k;
      }
    }
    return res+1; 
  }
};
struct UnionFind{
  std::vector<int> par;
  std::vector<int> siz;

  UnionFind(int sz_): par(sz_), siz(sz_) {
    for(int i=0; i<sz_; ++i) par[i] = i, siz[i] = 1;
  }

  void init(int sz_){
    par.resize(sz_);
    siz.resize(sz_);
    for(int i=0; i<sz_; ++i) par[i] = i, siz[i] = 1;
  }

  int root(int x){
    if(x == par[x]) return x;
    return par[x] = root(par[x]);
  }

  bool merge(int x, int y){
    x = root(x), y = root(y);
    if(x == y) return false;
    if(siz[x] < siz[y]) std::swap(x, y);
    siz[x] += siz[y];
    par[y] = x;
    return true;
  }

  bool issame(int x, int y){
    return root(x) == root(y);
  }

  int size(int x){
    return siz[root(x)];
  }
};
struct RollingHash {
    static const int base1 = 1007, base2 = 2009;
    static const int mod1 = 1000000007, mod2 = 1000000009;
    vector<long long> hash1, hash2, power1, power2;
    int n;
    // construct
    RollingHash(const string &S) {
        n = (int)S.size();
        hash1.assign(n+1, 0);
        hash2.assign(n+1, 0);
        power1.assign(n+1, 1);
        power2.assign(n+1, 1);
        for (int i = 0; i < n; ++i) {
            hash1[i+1] = (hash1[i] * base1 + S[i]) % mod1;
            hash2[i+1] = (hash2[i] * base2 + S[i]) % mod2;
            power1[i+1] = (power1[i] * base1) % mod1;
            power2[i+1] = (power2[i] * base2) % mod2;
        }
    }
    
    // get hash of S[left:right]
    inline pair<long long, long long> get(int l, int r) const {
        long long res1 = hash1[r] - hash1[l] * power1[r-l] % mod1;
        if (res1 < 0) res1 += mod1;
        long long res2 = hash2[r] - hash2[l] * power2[r-l] % mod2;
        if (res2 < 0) res2 += mod2;
        return {res1, res2};
    }
 
    inline pair<long long, long long> c_shift(int l) {
      auto h1 = get(0, l);
      auto h2 = get(l, n);
      return {(h1.first + h2.first * power1[l]) % mod1, (h1.second + h2.second * power2[l]) % mod2};
    }
 
    // get lcp of S[a:] and T[b:]
    inline int getLCP(int a, int b) const {
        int len = min((int)hash1.size()-a, (int)hash1.size()-b);
        int low = 0, high = len;
        while (high - low > 1) {
            int mid = (low + high) >> 1;
            if (get(a, a+mid) != get(b, b+mid)) high = mid;
            else low = mid;
        }
        return low;
    }
};
int main() {  
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(12);

  int N; cin >> N;
  int a, d; cin >> a >> d;

  int M; cin >> M;
  vector<tuple<int, int, int>> query(M);
  REP(i,M) {
    int x, y, z; cin >> x >> y >> z;
    y--; z--;
    query[i] = {x, y, z}; 
  }
  int K; cin >> K;

  K--;
  for(int i=M-1; i>=0; i--) {
    auto[x, y, z] = query[i];
    if(x == 0) {
      if(y <= K && K <= z) {
        int len = z-y;
        K = y + (z-K);
      }
    }
  }
  int num = a + K*d;
  for(int i=0; i<M; i++) {
    auto[x, y, z] = query[i];
    if(x == 0) {
      if(y <= K && K <= z) {
        int len = z-y;
        K = y + (z - K);
      }
    }else if(x == 1) {
      if(y <= K && K <= z) num++;
    }else{
      if(y <= K && K <= z) num >>= 1;
    }
  }
  cout << num << endl;
  return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 5164
final_rank: 1

code_snippet: #include "bits/stdc++.h"

#define REP(i,num) for(ll i=0;i<(num);++i)
#define FOR(i,c,num) for(ll (i)=(c);(i)<(num);++(i))
#define LOOP(i) while(i--)
#define ALL(c) c.begin(),c.end()
#define PRINTALL(c) for(auto pitr=c.begin();pitr!=c.end();++pitr){cout<<*pitr;if(next(pitr,1)!=c.end())cout<<' ';}cout<<endl;
#define PAIRCOMP(c,comp) [](const pair<ll,ll>& lhs,const pair<ll,ll>& rhs){return lhs.c comp rhs.c;}

using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;

constexpr ll atcoder_mod = 1e9+7;

template<typename T=ll>
T in(){ T x; cin >> x; return (x); }
template<typename T=ll,typename C=vector<T>>
C vecin(int N){ C x(N);REP(i,N){ x[i]=in<T>(); }return x; }

void vout(){ cout << endl; }
template<typename Head,typename... Tail>
void vout(Head&& h,Tail&&... t){ cout << ' ' << h;vout(forward<Tail>(t)...); }
void out(){ cout << endl; }
template<typename Head,typename... Tail>
void out(Head&& h,Tail&&... t){ cout << h;vout(forward<Tail>(t)...); }

template<typename T>
bool chmax(T& a,T b){ if(a<b){ a=b;return true; }return false; }
template<typename T>
bool chmin(T& a,T b){ if(a>b){ a=b;return true; }return false; }

class Query{
public:
	ll x,y,z;
};

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	cout<<fixed<<setprecision(10);

	auto N=in(),A=in(),D=in(),M=in();
	vector<Query> Q(M);
	REP(i,M){
		Q[i].x=in();
		Q[i].y=in();
		Q[i].z=in();
	}
	ll K=in()-1;

	REP(i,M){
		ll index = M-1-i;
		if(Q[index].x==0 && Q[index].y-1<=K && K<=Q[index].z-1){
			ll l=Q[index].y-1,r=Q[index].z-1;
			ll x=K-l;
			K = r-x;
		}
	}

	ll V=A+K*D;
	REP(i,M){
		if(Q[i].x==0){
			if(Q[i].y-1<=K && K<=Q[i].z-1){
				ll l=Q[i].y-1,r=Q[i].z-1;
				ll x=K-l;
				K = r-x;
			}
		}
		else if(Q[i].x==1){
			if(Q[i].y-1<=K && K<=Q[i].z-1){
				V++;
			}
		}
		else if(Q[i].x==2){
			if(Q[i].y-1<=K && K<=Q[i].z-1){
				V/=2;
			}
		}
	}
	out(V);
	return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 7408
final_rank: 6

code_snippet: // verification-helper: PROBLEM http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1579

#include<bits/stdc++.h>
using namespace std;

#define call_from_test
#ifndef call_from_test
#include <bits/stdc++.h>
using namespace std;
#endif
//BEGIN CUT HERE
template<typename Impl, typename Data, typename Node, size_t LIM>
struct RBST{
  using u32 = uint32_t;
  u32 xor128(){
    static u32 x = 123456789;
    static u32 y = 362436069;
    static u32 z = 521288629;
    static u32 w = 88675123;

    u32 t = x ^ (x << 11);
    x = y; y = z; z = w;
    return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));
  }

  alignas(Node) static inline char pool[sizeof(Node)*LIM];
  static inline Node* ptr = (Node*)pool;
  static inline size_t size;

  template<typename... Args>
  inline Node* create(Args&&... args){
    return new (ptr+size++) Node(std::forward<Args>(args)...);
  }

  inline size_t count(const Node *t){return Data::count(t);}

  inline Node* touch(Node *t){
    return static_cast<Impl*>(this)->touch(t);
  }

  inline void toggle(Node *t){
    return static_cast<Impl*>(this)->toggle(t);
  }

  inline Node* pushup(Node *t){
    return static_cast<Impl*>(this)->pushup(t);
  }

  Node* toggle(Node *a,size_t l,size_t r){
    auto s=split(a,l);
    auto t=split(s.second,r-l);
    auto u=touch(t.first);
    toggle(u);
    return merge(s.first,merge(u,t.second));
  }

  Node* merge(Node* a,Node* b){
    if(a==nullptr) return b;
    if(b==nullptr) return a;
    if(xor128()%(count(a)+count(b))<count(a)){
      a=touch(a);
      a->r=merge(a->r,b);
      a->r->p=a;
      return pushup(a);
    }
    b=touch(b);
    b->l=merge(a,b->l);
    b->l->p=b;
    return pushup(b);
  }

  pair<Node*, Node*> split(Node* a,size_t k){
    if(a==nullptr) return make_pair(a,a);
    a=touch(a);
    if(k<=count(a->l)){
      if(a->l) a->l->p=nullptr;
      auto s=split(a->l,k);
      a->l=s.second;
      if(a->l) a->l->p=a;
      return make_pair(s.first,pushup(a));
    }
    if(a->r) a->r->p=nullptr;
    auto s=split(a->r,k-(count(a->l)+1));
    a->r=s.first;
    if(a->r) a->r->p=a;
    return make_pair(pushup(a),s.second);
  }

  Node* insert(Node *a,size_t k,Node v){
    Node* b=create(v);
    auto s=split(a,k);
    return merge(merge(s.first,b),s.second);
  }

  Node* erase(Node *a,size_t k){
    assert(k<count(a));
    auto s=split(a,k);
    auto t=split(s.second,1);
    return merge(s.first,t.second);
  }

  Node* find_by_order(Node *a,size_t k){
    assert(k<count(a));
    a=touch(a);
    size_t num=count(a->l);
    if(k<num) return find_by_order(a->l,k);
    if(k>num) return find_by_order(a->r,k-(num+1));
    return a;
  }

  inline bool is_right(Node* a){
    return a->p and a->p->r==a;
  }

  size_t order_of_key(Node* a){
    size_t res=count(a->l);
    while(a){
      if(is_right(a)) res+=count(a->p->l)+1;
      a=a->p;
    }
    return res;
  }

  Node* build(size_t l,size_t r,const vector<Node> &vs){
    if(l+1==r) return create(vs[l]);
    size_t m=(l+r)>>1;
    return merge(build(l,m,vs),build(m,r,vs));
  }

  Node* build(const vector<Node> &vs){
    return build(0,vs.size(),vs);
  }

  template<typename T>
  Node* set_val(Node *a,size_t k,T val){
    assert(k<count(a));
    a=touch(a);
    size_t num=count(a->l);
    if(k<num) a->l=set_val(a->l,k,val);
    if(k>num) a->r=set_val(a->r,k-(num+1),val);
    if(k==num) a->val=val;
    return pushup(a);
  }

  Node* get_val(Node *a,size_t k){
    assert(k<count(a));
    a=touch(a);
    size_t num=count(a->l);
    if(k<num) return get_val(a->l,k);
    if(k>num) return get_val(a->r,k-(num+1));
    return a;
  }

  template<typename E>
  Node* update(Node *a,size_t l,size_t r,E v){
    auto s=split(a,l);
    auto t=split(s.second,r-l);
    auto u=touch(t.first);
    static_cast<Impl*>(this)->propagate(u,v);
    return merge(s.first,merge(u,t.second));
  }

  decltype(auto) query(Node *&a,size_t l,size_t r){
    auto s=split(a,l);
    auto t=split(s.second,r-l);
    auto u=t.first;
    auto res=static_cast<Impl*>(this)->query(u);
    a=merge(s.first,merge(u,t.second));
    return res;
  }
};
//END CUT HERE
#ifndef call_from_test
//INSERT ABOVE HERE
signed main(){
  return 0;
}
#endif

#ifndef call_from_test
#include <bits/stdc++.h>
using namespace std;
#endif
//BEGIN CUT HERE
template<typename E, typename H>
struct Dual{
  struct Node{
    Node *l,*r,*p;
    size_t cnt;
    bool rev;
    E val,laz;
    Node(E val):
      cnt(1),rev(0),val(val),laz(val){l=r=p=nullptr;}
  };

  H h;
  E ei;
  Dual(H h,E ei):h(h),ei(ei){}

  static inline size_t count(const Node *t){
    return t?t->cnt:0;
  }

  inline void toggle(Node *t){
    swap(t->l,t->r);
    t->rev^=1;
  }

  inline void propagate(Node *t,E x){
    t->val=h(t->val,x);
    t->laz=h(t->laz,x);
  }

  inline bool dirty(Node *t){
    return t->rev or t->laz!=ei;
  }

  inline Node* eval(Node* t){
    if(t->rev){
      if(t->l) toggle(t->l);
      if(t->r) toggle(t->r);
      t->rev=false;
    }
    if(t->laz!=ei){
      if(t->l) propagate(t->l,t->laz);
      if(t->r) propagate(t->r,t->laz);
      t->laz=ei;
    }
    return t;
  }

  inline Node* pushup(Node *t){
    t->cnt=count(t->l)+1+count(t->r);
    return t;
  }

  inline E reflect(Node *t){
    assert(t);
    return h(t->val,t->laz);
  }
};
//END CUT HERE
#ifndef call_from_test
//INSERT ABOVE HERE
signed main(){
  return 0;
}
#endif

#ifndef call_from_test
#include <bits/stdc++.h>
using namespace std;

#define call_from_test
#include "../rbst.cpp"
#undef call_from_test

#endif
//BEGIN CUT HERE
template<typename Data, size_t LIM>
struct Basic : RBST<Basic<Data, LIM>, Data, typename Data::Node, LIM>{
  using super = RBST<Basic, Data, typename Data::Node, LIM>;
  using Node = typename Data::Node;

  Data data;

  template<class... Args>
  Basic(Args... args):data(forward<Args>(args)...){}

  inline Node* touch(Node *t){return data.eval(t);}

  using super::toggle;
  inline void toggle(Node *t){return data.toggle(t);}
  template<typename E>
  inline void propagate(Node *t,E x){return data.propagate(t,x);}
  inline Node* pushup(Node *t){return data.pushup(t);}

  inline decltype(auto) get_val(Node *a,size_t k){
    return data.reflect(super::get_val(a,k));
  }

  using super::query;
  inline decltype(auto) query(Node *a){
    return data.reflect(a);
  }
};
//END CUT HERE
#ifndef call_from_test
//INSERT ABOVE HERE
signed main(){
  return 0;
}
#endif

#undef call_from_test

signed main(){
  cin.tie(0);
  ios::sync_with_stdio(0);

  int n,a,d;
  cin>>n>>a>>d;

  using ll = long long;
  struct E{
    ll a,b,c;
    E(){}
    E(ll a,ll b,ll c):a(a),b(b),c(c){}
    bool operator!=(const E &o) const{
      return make_tuple(a,b,c)!=make_tuple(o.a,o.b,o.c);
    }
  };

  const ll MAX = 1e9;
  auto h=[](E a,E b){
    E c(a.a+a.b*(a.c+b.a),a.b*b.b,0);
    c.c=c.a/c.b+b.c;
    c.a%=c.b;
    if(c.b>MAX){
      c.a=max(0LL,MAX-(c.b-c.a));
      c.b=MAX;
    }
    return c;
  };
  E ei(0,1,0);

  using Data = Dual<E, decltype(h)>;
  using Node = Data::Node;
  constexpr size_t LIM = 1e6;
  Basic<Data, LIM> G(h,ei);

  auto r=G.build(vector<Node>(n,ei));
  for(int i=0;i<n;i++){
    int v=a+d*i;
    r=G.update(r,i,i+1,E(0,1,v));
  }

  int m;
  cin>>m;
  for(int i=0;i<m;i++){
    int x,y,z;
    cin>>x>>y>>z;
    y--;
    if(x==0) r=G.toggle(r,y,z);
    if(x==1) r=G.update(r,y,z,E(0,1,1));
    if(x==2) r=G.update(r,y,z,E(0,2,0));
  }

  int k;
  cin>>k;
  k--;
  cout<<G.get_val(r,k).c<<endl;
  return 0;
}
accuracy: 1
time_ms: 650
memory_kb: 37932
final_rank: 17

code_snippet: #include<iostream>
#include<vector>
using namespace std;
#define inRange(x,a,b) (a <= x && x <= b)

int main(){
    int n, a, d, m, k;
    cin >> n >> a >> d;
    cin >> m;
    vector<int> x(m), y(m), z(m);
    for(int i = 0; i < m; i++){
        cin >> x[i] >> y[i] >> z[i];
        y[i]--, z[i]--;
    }
    cin >> k;
    k--;
    for(int i = m-1; i >= 0; i--){
        if(x[i]==0&&inRange(k,y[i],z[i])){
            k = y[i] + z[i] - k;
        }
    }
    int ans = a+k*d;
    for(int i = 0; i < m; i++){
        if(!inRange(k, y[i], z[i])) continue;
        if(x[i] == 0){
            k = y[i] + z[i] - k;
        }else if(x[i] == 1){
            ans++;
        }else if(x[i] == 2){
            ans /= 2;
        }
    }
    cout << ans << endl;
    return 0;
}
accuracy: 1
time_ms: 100
memory_kb: 4944
final_rank: 9

code_snippet: #define PROBLEM "http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1579"

#include<bits/stdc++.h>
using namespace std;

#define call_from_test
#ifndef call_from_test
#include<bits/stdc++.h>
using namespace std;
using Int = long long;
#endif
//BEGIN CUT HERE
struct FastIO{
  FastIO(){
    cin.tie(0);
    ios::sync_with_stdio(0);
  }
}fastio_beet;
//END CUT HERE
#ifndef call_from_test
signed main(){
  return 0;
}
#endif

#ifndef call_from_test
#include<bits/stdc++.h>
using namespace std;
#endif
//BEGIN CUT HERE
template<typename Node, size_t LIM>
struct BBSTBase{
  using u32 = uint32_t;
  u32 xor128(){
    static u32 x = 123456789;
    static u32 y = 362436069;
    static u32 z = 521288629;
    static u32 w = 88675123;

    u32 t = x ^ (x << 11);
    x = y; y = z; z = w;
    return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));
  }

  static array<Node, LIM> pool;
  size_t ptr;
  BBSTBase():ptr(0){}

  size_t count(const Node *a){
    return a?a->cnt:0;
  }

  inline Node* create(){
    return &pool[ptr++];
  }

  template<typename T>
  inline Node* create(T v){
    return &(pool[ptr++]=Node(v));
  }

  virtual void toggle(Node *a)=0;
  virtual Node* eval(Node* a)=0;
  virtual Node* recalc(Node* a)=0;

  Node* toggle(Node *a,size_t l,size_t r){
    auto s=split(a,l);
    auto t=split(s.second,r-l);
    auto u=eval(t.first);
    toggle(u);
    return merge(s.first,merge(u,t.second));
  }

  Node* merge(Node* a,Node* b){
    if(a==nullptr) return b;
    if(b==nullptr) return a;
    if(xor128()%(count(a)+count(b))<count(a)){
      a=eval(a);
      a->r=merge(a->r,b);
      a->r->p=a;
      return recalc(a);
    }
    b=eval(b);
    b->l=merge(a,b->l);
    b->l->p=b;
    return recalc(b);
  }

  pair<Node*, Node*> split(Node* a,size_t k){
    if(a==nullptr) return make_pair(a,a);
    a=eval(a);
    if(k<=count(a->l)){
      if(a->l) a->l->p=nullptr;
      auto s=split(a->l,k);
      a->l=s.second;
      if(a->l) a->l->p=a;
      return make_pair(s.first,recalc(a));
    }
    if(a->r) a->r->p=nullptr;
    auto s=split(a->r,k-(count(a->l)+1));
    a->r=s.first;
    if(a->r) a->r->p=a;
    return make_pair(recalc(a),s.second);
  }

  template<typename T>
  Node* insert(Node *a,size_t pos,T v){
    Node* b=create(v);
    auto s=split(a,pos);
    return a=merge(merge(s.first,b),s.second);
  }

  Node* erase(Node *a,size_t pos){
    auto s=split(a,pos);
    auto t=split(s.second,1);
    return merge(s.first,t.second);
  }

  Node* find_by_order(Node *a,size_t k){
    assert(k<count(a));
    a=eval(a);
    size_t num=count(a->l);
    if(k<num) return find_by_order(a->l,k);
    if(k>num) return find_by_order(a->r,k-(num+1));
    return a;
  }

  inline bool is_right(Node* a){
    return a->p&&a->p->r==a;
  }

  size_t order_of_key(Node* a){
    size_t res=count(a->l);
    while(a){
      if(is_right(a)) res+=count(a->p->l)+1;
      a=a->p;
    }
    return res;
  }

  template<typename T>
  Node* build(size_t l,size_t r,const vector<T> &vs){
    if(l+1==r) return create(vs[l]);
    size_t m=(l+r)>>1;
    return merge(build(l,m,vs),build(m,r,vs));
  }

  template<typename T>
  Node* build(const vector<T> &vs){
    return build(0,vs.size(),vs);
  }
};
template<typename Node, size_t LIM>
array<Node, LIM> BBSTBase<Node, LIM>::pool;
//END CUT HERE
#ifndef call_from_test
//INSERT ABOVE HERE
signed main(){
  return 0;
}
#endif

#ifndef call_from_test
#include<bits/stdc++.h>
using namespace std;
template<typename Node, size_t LIM>
struct BBSTBase{
  using u32 = uint32_t;
  u32 xor128(){
    static u32 x = 123456789;
    static u32 y = 362436069;
    static u32 z = 521288629;
    static u32 w = 88675123;

    u32 t = x ^ (x << 11);
    x = y; y = z; z = w;
    return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));
  }

  static array<Node, LIM> pool;
  size_t ptr;
  BBSTBase():ptr(0){}

  size_t count(const Node *a){
    return a?a->cnt:0;
  }

  inline Node* create(){
    return &pool[ptr++];
  }

  template<typename T>
  inline Node* create(T v){
    return &(pool[ptr++]=Node(v));
  }

  virtual void toggle(Node *a)=0;
  virtual Node* eval(Node* a)=0;
  virtual Node* recalc(Node* a)=0;

  Node* toggle(Node *a,size_t l,size_t r){
    auto s=split(a,l);
    auto t=split(s.second,r-l);
    auto u=eval(t.first);
    toggle(u);
    return merge(s.first,merge(u,t.second));
  }

  Node* merge(Node* a,Node* b){
    if(a==nullptr) return b;
    if(b==nullptr) return a;
    if(xor128()%(count(a)+count(b))<count(a)){
      a=eval(a);
      a->r=merge(a->r,b);
      a->r->p=a;
      return recalc(a);
    }
    b=eval(b);
    b->l=merge(a,b->l);
    b->l->p=b;
    return recalc(b);
  }

  pair<Node*, Node*> split(Node* a,size_t k){
    if(a==nullptr) return make_pair(a,a);
    a=eval(a);
    if(k<=count(a->l)){
      if(a->l) a->l->p=nullptr;
      auto s=split(a->l,k);
      a->l=s.second;
      if(a->l) a->l->p=a;
      return make_pair(s.first,recalc(a));
    }
    if(a->r) a->r->p=nullptr;
    auto s=split(a->r,k-(count(a->l)+1));
    a->r=s.first;
    if(a->r) a->r->p=a;
    return make_pair(recalc(a),s.second);
  }

  template<typename T>
  Node* insert(Node *a,size_t pos,T v){
    Node* b=create(v);
    auto s=split(a,pos);
    return a=merge(merge(s.first,b),s.second);
  }

  Node* erase(Node *a,size_t pos){
    auto s=split(a,pos);
    auto t=split(s.second,1);
    return merge(s.first,t.second);
  }

  Node* find_by_order(Node *a,size_t k){
    assert(k<count(a));
    a=eval(a);
    size_t num=count(a->l);
    if(k<num) return find_by_order(a->l,k);
    if(k>num) return find_by_order(a->r,k-(num+1));
    return a;
  }

  template<typename T>
  Node* build(size_t l,size_t r,const vector<T> &vs){
    if(l+1==r) return create(vs[l]);
    size_t m=(l+r)>>1;
    return merge(build(l,m,vs),build(m,r,vs));
  }

  template<typename T>
  Node* build(const vector<T> &vs){
    return build(0,vs.size(),vs);
  }
};
template<typename Node, size_t LIM>
array<Node, LIM> BBSTBase<Node, LIM>::pool;
#endif
//INSERT ABOVE HERE
//BEGIN CUT HERE

template<typename Ep>
struct NodeBase{
  using E = Ep;
  NodeBase *l,*r,*p;
  size_t cnt;
  bool rev;
  E dat,laz;
  NodeBase(){}
  NodeBase(E dat,E laz):
    cnt(1),rev(0),dat(dat),laz(laz){l=r=p=nullptr;}
};

template<typename Node, size_t LIM>
struct Dual : BBSTBase<Node, LIM>{
  using E = typename Node::E;
  using super = BBSTBase<Node, LIM>;
  using H = function<E(E, E)>;

  H h;
  E ei;

  Dual(H h,E ei):super(),h(h),ei(ei){}

  using super::create;
  using super::merge;
  using super::split;

  Node* build(size_t l,size_t r){
    if(l+1==r) return create(Node(ei,ei));
    size_t m=(l+r)>>1;
    return merge(build(l,m),build(m,r));
  }

  Node* init(int n){
    return build(0,n);
  }

  using super::count;
  Node* recalc(Node *a){
    a->cnt=count(a->l)+1+count(a->r);
    return a;
  }

  void propagate(Node *a,E x){
    a->dat=h(a->dat,x);
    a->laz=h(a->laz,x);
  }

  using super::toggle;
  void toggle(Node *a){
    swap(a->l,a->r);
    a->rev^=1;
  }

  // remove "virtual" for optimization
  virtual Node* eval(Node* a){
    if(a->laz!=ei){
      if(a->l) propagate(a->l,a->laz);
      if(a->r) propagate(a->r,a->laz);
      a->laz=ei;
    }
    if(a->rev){
      if(a->l) toggle(a->l);
      if(a->r) toggle(a->r);
      a->rev=false;
    }
    return recalc(a);
  }

  Node* update(Node *a,size_t l,size_t r,E x){
    auto s=split(a,l);
    auto t=split(s.second,r-l);
    auto u=eval(t.first);
    propagate(u,x);
    return merge(s.first,merge(u,t.second));
  }

  Node* set_val(Node *a,size_t k,E x){
    assert(k<count(a));
    a=eval(a);
    size_t num=count(a->l);
    if(k<num) a->l=set_val(a->l,k,x);
    if(k>num) a->r=set_val(a->r,k-(num+1),x);
    if(k==num) a->dat=x;
    return recalc(a);
  }

  E get_val(Node *a,size_t k){
    assert(k<count(a));
    a=eval(a);
    size_t num=count(a->l);
    if(k<num) return get_val(a->l,k);
    if(k>num) return get_val(a->r,k-(num+1));
    return a->dat;
  }

  void dump(Node* a,typename vector<E>::iterator it){
    if(!count(a)) return;
    a=eval(a);
    dump(a->l,it);
    *(it+count(a->l))=a->dat;
    dump(a->r,it+count(a->l)+1);
  }

  vector<E> dump(Node* a){
    vector<E> vs(count(a));
    dump(a,vs.begin());
    return vs;
  }
};
//END CUT HERE
//INSERT ABOVE HERE
#ifndef call_from_test
signed main(){
  return 0;
}
#endif

#undef call_from_test

signed main(){
  int n,a,d;
  cin>>n>>a>>d;

  using ll = long long;
  struct E{
    ll a,b,c;
    E(){}
    E(ll a,ll b,ll c):a(a),b(b),c(c){}
    bool operator!=(const E &o) const{
      return make_tuple(a,b,c)!=make_tuple(o.a,o.b,o.c);
    }
  };

  const ll MAX = 1e9;
  auto h=
    [](E a,E b){
      E c(a.a+a.b*(a.c+b.a),a.b*b.b,0);
      c.c=c.a/c.b+b.c;
      c.a%=c.b;
      if(c.b>MAX){
        c.a=max(0LL,MAX-(c.b-c.a));
        c.b=MAX;
      }
      return c;
    };
  E ei(0,1,0);

  using Node = NodeBase<E>;
  constexpr size_t LIM = 2e5+100;
  Dual<Node, LIM> G(h,ei);

  auto r=G.init(n);
  for(int i=0;i<n;i++){
    int v=a+d*i;
    r=G.update(r,i,i+1,E(0,1,v));
  }

  int m;
  cin>>m;
  for(int i=0;i<m;i++){
    int x,y,z;
    cin>>x>>y>>z;
    y--;
    if(x==0) r=G.toggle(r,y,z);
    if(x==1) r=G.update(r,y,z,E(0,1,1));
    if(x==2) r=G.update(r,y,z,E(0,2,0));
  }

  int k;
  cin>>k;
  k--;
  cout<<G.get_val(r,k).c<<endl;
  return 0;
}
accuracy: 1
time_ms: 1000
memory_kb: 19692
final_rank: 15

code_snippet: #define PROBLEM "http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1579"

#include<bits/stdc++.h>
using namespace std;

#define call_from_test
#ifndef call_from_test
#include<bits/stdc++.h>
using namespace std;
using Int = long long;
#endif
//BEGIN CUT HERE
struct FastIO{
  FastIO(){
    cin.tie(0);
    ios::sync_with_stdio(0);
  }
}fastio_beet;
//END CUT HERE
#ifndef call_from_test
signed main(){
  return 0;
}
#endif

#ifndef call_from_test
#include<bits/stdc++.h>
using namespace std;
#endif
//BEGIN CUT HERE
template<typename Node, size_t LIM>
struct BBSTBase{
  using u32 = uint32_t;
  u32 xor128(){
    static u32 x = 123456789;
    static u32 y = 362436069;
    static u32 z = 521288629;
    static u32 w = 88675123;

    u32 t = x ^ (x << 11);
    x = y; y = z; z = w;
    return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));
  }

  static array<Node, LIM> pool;
  size_t ptr;
  BBSTBase():ptr(0){}

  size_t count(const Node *a){
    return a?a->cnt:0;
  }

  inline Node* create(){
    return &pool[ptr++];
  }

  template<typename T>
  inline Node* create(T v){
    return &(pool[ptr++]=Node(v));
  }

  virtual void toggle(Node *a)=0;
  virtual Node* eval(Node* a)=0;
  virtual Node* recalc(Node* a)=0;

  Node* toggle(Node *a,size_t l,size_t r){
    auto s=split(a,l);
    auto t=split(s.second,r-l);
    auto u=eval(t.first);
    toggle(u);
    return merge(s.first,merge(u,t.second));
  }

  Node* merge(Node* a,Node* b){
    if(a==nullptr) return b;
    if(b==nullptr) return a;
    if(xor128()%(count(a)+count(b))<count(a)){
      a=eval(a);
      a->r=merge(a->r,b);
      a->r->p=a;
      return recalc(a);
    }
    b=eval(b);
    b->l=merge(a,b->l);
    b->l->p=b;
    return recalc(b);
  }

  pair<Node*, Node*> split(Node* a,size_t k){
    if(a==nullptr) return make_pair(a,a);
    a=eval(a);
    if(k<=count(a->l)){
      if(a->l) a->l->p=nullptr;
      auto s=split(a->l,k);
      a->l=s.second;
      if(a->l) a->l->p=a;
      return make_pair(s.first,recalc(a));
    }
    if(a->r) a->r->p=nullptr;
    auto s=split(a->r,k-(count(a->l)+1));
    a->r=s.first;
    if(a->r) a->r->p=a;
    return make_pair(recalc(a),s.second);
  }

  template<typename T>
  Node* insert(Node *a,size_t pos,T v){
    Node* b=create(v);
    auto s=split(a,pos);
    return a=merge(merge(s.first,b),s.second);
  }

  Node* erase(Node *a,size_t pos){
    auto s=split(a,pos);
    auto t=split(s.second,1);
    return merge(s.first,t.second);
  }

  Node* find_by_order(Node *a,size_t k){
    assert(k<count(a));
    a=eval(a);
    size_t num=count(a->l);
    if(k<num) return find_by_order(a->l,k);
    if(k>num) return find_by_order(a->r,k-(num+1));
    return a;
  }

  inline bool is_right(Node* a){
    return a->p&&a->p->r==a;
  }

  size_t order_of_key(Node* a){
    size_t res=count(a->l);
    while(a){
      if(is_right(a)) res+=count(a->p->l)+1;
      a=a->p;
    }
    return res;
  }

  template<typename T>
  Node* build(size_t l,size_t r,const vector<T> &vs){
    if(l+1==r) return create(vs[l]);
    size_t m=(l+r)>>1;
    return merge(build(l,m,vs),build(m,r,vs));
  }

  template<typename T>
  Node* build(const vector<T> &vs){
    return build(0,vs.size(),vs);
  }
};
template<typename Node, size_t LIM>
array<Node, LIM> BBSTBase<Node, LIM>::pool;
//END CUT HERE
#ifndef call_from_test
//INSERT ABOVE HERE
signed main(){
  return 0;
}
#endif

#ifndef call_from_test
#include<bits/stdc++.h>
using namespace std;
template<typename Node, size_t LIM>
struct BBSTBase{
  using u32 = uint32_t;
  u32 xor128(){
    static u32 x = 123456789;
    static u32 y = 362436069;
    static u32 z = 521288629;
    static u32 w = 88675123;

    u32 t = x ^ (x << 11);
    x = y; y = z; z = w;
    return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));
  }

  static array<Node, LIM> pool;
  size_t ptr;
  BBSTBase():ptr(0){}

  size_t count(const Node *a){
    return a?a->cnt:0;
  }

  inline Node* create(){
    return &pool[ptr++];
  }

  template<typename T>
  inline Node* create(T v){
    return &(pool[ptr++]=Node(v));
  }

  virtual void toggle(Node *a)=0;
  virtual Node* eval(Node* a)=0;
  virtual Node* recalc(Node* a)=0;

  Node* toggle(Node *a,size_t l,size_t r){
    auto s=split(a,l);
    auto t=split(s.second,r-l);
    auto u=eval(t.first);
    toggle(u);
    return merge(s.first,merge(u,t.second));
  }

  Node* merge(Node* a,Node* b){
    if(a==nullptr) return b;
    if(b==nullptr) return a;
    if(xor128()%(count(a)+count(b))<count(a)){
      a=eval(a);
      a->r=merge(a->r,b);
      a->r->p=a;
      return recalc(a);
    }
    b=eval(b);
    b->l=merge(a,b->l);
    b->l->p=b;
    return recalc(b);
  }

  pair<Node*, Node*> split(Node* a,size_t k){
    if(a==nullptr) return make_pair(a,a);
    a=eval(a);
    if(k<=count(a->l)){
      if(a->l) a->l->p=nullptr;
      auto s=split(a->l,k);
      a->l=s.second;
      if(a->l) a->l->p=a;
      return make_pair(s.first,recalc(a));
    }
    if(a->r) a->r->p=nullptr;
    auto s=split(a->r,k-(count(a->l)+1));
    a->r=s.first;
    if(a->r) a->r->p=a;
    return make_pair(recalc(a),s.second);
  }

  template<typename T>
  Node* insert(Node *a,size_t pos,T v){
    Node* b=create(v);
    auto s=split(a,pos);
    return a=merge(merge(s.first,b),s.second);
  }

  Node* erase(Node *a,size_t pos){
    auto s=split(a,pos);
    auto t=split(s.second,1);
    return merge(s.first,t.second);
  }

  Node* find_by_order(Node *a,size_t k){
    assert(k<count(a));
    a=eval(a);
    size_t num=count(a->l);
    if(k<num) return find_by_order(a->l,k);
    if(k>num) return find_by_order(a->r,k-(num+1));
    return a;
  }

  template<typename T>
  Node* build(size_t l,size_t r,const vector<T> &vs){
    if(l+1==r) return create(vs[l]);
    size_t m=(l+r)>>1;
    return merge(build(l,m,vs),build(m,r,vs));
  }

  template<typename T>
  Node* build(const vector<T> &vs){
    return build(0,vs.size(),vs);
  }
};
template<typename Node, size_t LIM>
array<Node, LIM> BBSTBase<Node, LIM>::pool;
#endif
//INSERT ABOVE HERE
//BEGIN CUT HERE

template<typename Ep>
struct NodeBase{
  using E = Ep;
  NodeBase *l,*r,*p;
  size_t cnt;
  bool rev;
  E dat,laz;
  NodeBase(){}
  NodeBase(E dat,E laz):
    cnt(1),rev(0),dat(dat),laz(laz){l=r=p=nullptr;}
};

template<typename Node, size_t LIM>
struct Dual : BBSTBase<Node, LIM>{
  using E = typename Node::E;
  using super = BBSTBase<Node, LIM>;
  using H = function<E(E, E)>;

  H h;
  E ei;

  Dual(H h,E ei):super(),h(h),ei(ei){}

  using super::create;
  using super::merge;
  using super::split;

  Node* build(size_t l,size_t r){
    if(l+1==r) return create(Node(ei,ei));
    size_t m=(l+r)>>1;
    return merge(build(l,m),build(m,r));
  }

  Node* init(int n){
    return build(0,n);
  }

  using super::count;
  Node* recalc(Node *a){
    a->cnt=count(a->l)+1+count(a->r);
    return a;
  }

  void propagate(Node *a,E x){
    a->dat=h(a->dat,x);
    a->laz=h(a->laz,x);
  }

  using super::toggle;
  void toggle(Node *a){
    swap(a->l,a->r);
    a->rev^=1;
  }

  // remove "virtual" for optimization
  virtual Node* eval(Node* a){
    if(a->laz!=ei){
      if(a->l) propagate(a->l,a->laz);
      if(a->r) propagate(a->r,a->laz);
      a->laz=ei;
    }
    if(a->rev){
      if(a->l) toggle(a->l);
      if(a->r) toggle(a->r);
      a->rev=false;
    }
    return recalc(a);
  }

  Node* update(Node *a,size_t l,size_t r,E x){
    auto s=split(a,l);
    auto t=split(s.second,r-l);
    auto u=eval(t.first);
    propagate(u,x);
    return merge(s.first,merge(u,t.second));
  }

  Node* set_val(Node *a,size_t k,E x){
    assert(k<count(a));
    a=eval(a);
    size_t num=count(a->l);
    if(k<num) a->l=set_val(a->l,k,x);
    if(k>num) a->r=set_val(a->r,k-(num+1),x);
    if(k==num) a->dat=x;
    return recalc(a);
  }

  E get_val(Node *a,size_t k){
    assert(k<count(a));
    a=eval(a);
    size_t num=count(a->l);
    if(k<num) return get_val(a->l,k);
    if(k>num) return get_val(a->r,k-(num+1));
    return a->dat;
  }

  void dump(Node* a,typename vector<E>::iterator it){
    if(!count(a)) return;
    a=eval(a);
    dump(a->l,it);
    *(it+count(a->l))=a->dat;
    dump(a->r,it+count(a->l)+1);
  }

  vector<E> dump(Node* a){
    vector<E> vs(count(a));
    dump(a,vs.begin());
    return vs;
  }
};
//END CUT HERE
//INSERT ABOVE HERE
#ifndef call_from_test
signed main(){
  return 0;
}
#endif

#undef call_from_test

signed main(){
  int n,a,d;
  cin>>n>>a>>d;

  using ll = long long;
  struct E{
    ll a,b,c;
    E(){}
    E(ll a,ll b,ll c):a(a),b(b),c(c){}
    bool operator!=(const E &o) const{
      return make_tuple(a,b,c)!=make_tuple(o.a,o.b,o.c);
    }
  };

  const ll MAX = 1e9;
  auto h=
    [](E a,E b){
      E c(a.a+a.b*(a.c+b.a),a.b*b.b,0);
      c.c=c.a/c.b+b.c;
      c.a%=c.b;
      if(c.b>MAX){
        c.a=max(0LL,MAX-(c.b-c.a));
        c.b=MAX;
      }
      return c;
    };
  E ei(0,1,0);

  using Node = NodeBase<E>;
  constexpr size_t LIM = 1e5+100;
  Dual<Node, LIM> G(h,ei);

  auto r=G.init(n);
  for(int i=0;i<n;i++){
    int v=a+d*i;
    r=G.update(r,i,i+1,E(0,1,v));
  }

  int m;
  cin>>m;
  for(int i=0;i<m;i++){
    int x,y,z;
    cin>>x>>y>>z;
    y--;
    if(x==0) r=G.toggle(r,y,z);
    if(x==1) r=G.update(r,y,z,E(0,1,1));
    if(x==2) r=G.update(r,y,z,E(0,2,0));
  }

  int k;
  cin>>k;
  k--;
  cout<<G.get_val(r,k).c<<endl;
  return 0;
}
accuracy: 0.29411764705882354
time_ms: 270
memory_kb: 7592
final_rank: 18

code_snippet: #define PROBLEM "http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1579"

#include<bits/stdc++.h>
using namespace std;

#define call_from_test
#ifndef call_from_test
#include<bits/stdc++.h>
using namespace std;
using Int = long long;
#endif
//BEGIN CUT HERE
struct FastIO{
  FastIO(){
    cin.tie(0);
    ios::sync_with_stdio(0);
  }
}fastio_beet;
//END CUT HERE
#ifndef call_from_test
signed main(){
  return 0;
}
#endif

#ifndef call_from_test
#include<bits/stdc++.h>
using namespace std;
#endif
//BEGIN CUT HERE
template<typename Node, size_t LIM>
struct BBSTBase{
  using u32 = uint32_t;
  u32 xor128(){
    static u32 x = 123456789;
    static u32 y = 362436069;
    static u32 z = 521288629;
    static u32 w = 88675123;

    u32 t = x ^ (x << 11);
    x = y; y = z; z = w;
    return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));
  }

  static array<Node, LIM> pool;
  size_t ptr;
  BBSTBase():ptr(0){}

  size_t count(const Node *a){
    return a?a->cnt:0;
  }

  inline Node* create(){
    return &pool[ptr++];
  }

  template<typename T>
  inline Node* create(T v){
    return &(pool[ptr++]=Node(v));
  }

  virtual void toggle(Node *a)=0;
  virtual Node* eval(Node* a)=0;
  virtual Node* recalc(Node* a)=0;

  Node* toggle(Node *a,size_t l,size_t r){
    auto s=split(a,l);
    auto t=split(s.second,r-l);
    auto u=eval(t.first);
    toggle(u);
    return merge(s.first,merge(u,t.second));
  }

  Node* merge(Node* a,Node* b){
    if(a==nullptr) return b;
    if(b==nullptr) return a;
    if(xor128()%(count(a)+count(b))<count(a)){
      a=eval(a);
      a->r=merge(a->r,b);
      a->r->p=a;
      return recalc(a);
    }
    b=eval(b);
    b->l=merge(a,b->l);
    b->l->p=b;
    return recalc(b);
  }

  pair<Node*, Node*> split(Node* a,size_t k){
    if(a==nullptr) return make_pair(a,a);
    a=eval(a);
    if(k<=count(a->l)){
      if(a->l) a->l->p=nullptr;
      auto s=split(a->l,k);
      a->l=s.second;
      if(a->l) a->l->p=a;
      return make_pair(s.first,recalc(a));
    }
    if(a->r) a->r->p=nullptr;
    auto s=split(a->r,k-(count(a->l)+1));
    a->r=s.first;
    if(a->r) a->r->p=a;
    return make_pair(recalc(a),s.second);
  }

  template<typename T>
  Node* insert(Node *a,size_t pos,T v){
    Node* b=create(v);
    auto s=split(a,pos);
    return a=merge(merge(s.first,b),s.second);
  }

  Node* erase(Node *a,size_t pos){
    auto s=split(a,pos);
    auto t=split(s.second,1);
    return merge(s.first,t.second);
  }

  Node* find_by_order(Node *a,size_t k){
    assert(k<count(a));
    a=eval(a);
    size_t num=count(a->l);
    if(k<num) return find_by_order(a->l,k);
    if(k>num) return find_by_order(a->r,k-(num+1));
    return a;
  }

  inline bool is_right(Node* a){
    return a->p&&a->p->r==a;
  }

  size_t order_of_key(Node* a){
    size_t res=count(a->l);
    while(a){
      if(is_right(a)) res+=count(a->p->l)+1;
      a=a->p;
    }
    return res;
  }

  template<typename T>
  Node* build(size_t l,size_t r,const vector<T> &vs){
    if(l+1==r) return create(vs[l]);
    size_t m=(l+r)>>1;
    return merge(build(l,m,vs),build(m,r,vs));
  }

  template<typename T>
  Node* build(const vector<T> &vs){
    return build(0,vs.size(),vs);
  }
};
template<typename Node, size_t LIM>
array<Node, LIM> BBSTBase<Node, LIM>::pool;
//END CUT HERE
#ifndef call_from_test
//INSERT ABOVE HERE
signed main(){
  return 0;
}
#endif

#ifndef call_from_test
#include<bits/stdc++.h>
using namespace std;
template<typename Node, size_t LIM>
struct BBSTBase{
  using u32 = uint32_t;
  u32 xor128(){
    static u32 x = 123456789;
    static u32 y = 362436069;
    static u32 z = 521288629;
    static u32 w = 88675123;

    u32 t = x ^ (x << 11);
    x = y; y = z; z = w;
    return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));
  }

  static array<Node, LIM> pool;
  size_t ptr;
  BBSTBase():ptr(0){}

  size_t count(const Node *a){
    return a?a->cnt:0;
  }

  inline Node* create(){
    return &pool[ptr++];
  }

  template<typename T>
  inline Node* create(T v){
    return &(pool[ptr++]=Node(v));
  }

  virtual void toggle(Node *a)=0;
  virtual Node* eval(Node* a)=0;
  virtual Node* recalc(Node* a)=0;

  Node* toggle(Node *a,size_t l,size_t r){
    auto s=split(a,l);
    auto t=split(s.second,r-l);
    auto u=eval(t.first);
    toggle(u);
    return merge(s.first,merge(u,t.second));
  }

  Node* merge(Node* a,Node* b){
    if(a==nullptr) return b;
    if(b==nullptr) return a;
    if(xor128()%(count(a)+count(b))<count(a)){
      a=eval(a);
      a->r=merge(a->r,b);
      a->r->p=a;
      return recalc(a);
    }
    b=eval(b);
    b->l=merge(a,b->l);
    b->l->p=b;
    return recalc(b);
  }

  pair<Node*, Node*> split(Node* a,size_t k){
    if(a==nullptr) return make_pair(a,a);
    a=eval(a);
    if(k<=count(a->l)){
      if(a->l) a->l->p=nullptr;
      auto s=split(a->l,k);
      a->l=s.second;
      if(a->l) a->l->p=a;
      return make_pair(s.first,recalc(a));
    }
    if(a->r) a->r->p=nullptr;
    auto s=split(a->r,k-(count(a->l)+1));
    a->r=s.first;
    if(a->r) a->r->p=a;
    return make_pair(recalc(a),s.second);
  }

  template<typename T>
  Node* insert(Node *a,size_t pos,T v){
    Node* b=create(v);
    auto s=split(a,pos);
    return a=merge(merge(s.first,b),s.second);
  }

  Node* erase(Node *a,size_t pos){
    auto s=split(a,pos);
    auto t=split(s.second,1);
    return merge(s.first,t.second);
  }

  Node* find_by_order(Node *a,size_t k){
    assert(k<count(a));
    a=eval(a);
    size_t num=count(a->l);
    if(k<num) return find_by_order(a->l,k);
    if(k>num) return find_by_order(a->r,k-(num+1));
    return a;
  }

  template<typename T>
  Node* build(size_t l,size_t r,const vector<T> &vs){
    if(l+1==r) return create(vs[l]);
    size_t m=(l+r)>>1;
    return merge(build(l,m,vs),build(m,r,vs));
  }

  template<typename T>
  Node* build(const vector<T> &vs){
    return build(0,vs.size(),vs);
  }
};
template<typename Node, size_t LIM>
array<Node, LIM> BBSTBase<Node, LIM>::pool;
#endif
//INSERT ABOVE HERE
//BEGIN CUT HERE

template<typename Ep>
struct NodeBase{
  using E = Ep;
  NodeBase *l,*r,*p;
  size_t cnt;
  bool rev;
  E dat,laz;
  NodeBase(){}
  NodeBase(E dat,E laz):
    cnt(1),rev(0),dat(dat),laz(laz){l=r=p=nullptr;}
};

template<typename Node, size_t LIM>
struct Dual : BBSTBase<Node, LIM>{
  using E = typename Node::E;
  using super = BBSTBase<Node, LIM>;
  using H = function<E(E, E)>;

  H h;
  E ei;

  Dual(H h,E ei):super(),h(h),ei(ei){}

  using super::create;
  using super::merge;
  using super::split;

  Node* build(size_t l,size_t r){
    if(l+1==r) return create(Node(ei,ei));
    size_t m=(l+r)>>1;
    return merge(build(l,m),build(m,r));
  }

  Node* init(int n){
    return build(0,n);
  }

  using super::count;
  Node* recalc(Node *a){
    a->cnt=count(a->l)+1+count(a->r);
    return a;
  }

  void propagate(Node *a,E x){
    a->dat=h(a->dat,x);
    a->laz=h(a->laz,x);
  }

  using super::toggle;
  void toggle(Node *a){
    swap(a->l,a->r);
    a->rev^=1;
  }

  // remove "virtual" for optimization
  virtual Node* eval(Node* a){
    if(a->laz!=ei){
      if(a->l) propagate(a->l,a->laz);
      if(a->r) propagate(a->r,a->laz);
      a->laz=ei;
    }
    if(a->rev){
      if(a->l) toggle(a->l);
      if(a->r) toggle(a->r);
      a->rev=false;
    }
    return recalc(a);
  }

  Node* update(Node *a,size_t l,size_t r,E x){
    auto s=split(a,l);
    auto t=split(s.second,r-l);
    auto u=eval(t.first);
    propagate(u,x);
    return merge(s.first,merge(u,t.second));
  }

  Node* set_val(Node *a,size_t k,E x){
    assert(k<count(a));
    a=eval(a);
    size_t num=count(a->l);
    if(k<num) a->l=set_val(a->l,k,x);
    if(k>num) a->r=set_val(a->r,k-(num+1),x);
    if(k==num) a->dat=x;
    return recalc(a);
  }

  E get_val(Node *a,size_t k){
    assert(k<count(a));
    a=eval(a);
    size_t num=count(a->l);
    if(k<num) return get_val(a->l,k);
    if(k>num) return get_val(a->r,k-(num+1));
    return a->dat;
  }

  void dump(Node* a,typename vector<E>::iterator it){
    if(!count(a)) return;
    a=eval(a);
    dump(a->l,it);
    *(it+count(a->l))=a->dat;
    dump(a->r,it+count(a->l)+1);
  }

  vector<E> dump(Node* a){
    vector<E> vs(count(a));
    dump(a,vs.begin());
    return vs;
  }
};
//END CUT HERE
//INSERT ABOVE HERE
#ifndef call_from_test
signed main(){
  return 0;
}
#endif

#undef call_from_test

signed main(){
  int n,a,d;
  cin>>n>>a>>d;

  using ll = long long;
  struct E{
    ll a,b,c;
    E(){}
    E(ll a,ll b,ll c):a(a),b(b),c(c){}
    bool operator!=(const E &o) const{
      return make_tuple(a,b,c)!=make_tuple(o.a,o.b,o.c);
    }
  };

  const ll MAX = 1e9;
  auto h=
    [](E a,E b){
      E c(a.a+a.b*(a.c+b.a),a.b*b.b,0);
      c.c=c.a/c.b+b.c;
      c.a%=c.b;
      if(c.b>MAX){
        c.a=max(0LL,MAX-(c.b-c.a));
        c.b=MAX;
      }
      return c;
    };
  E ei(0,1,0);

  using Node = NodeBase<E>;
  constexpr size_t LIM = 1e6;
  Dual<Node, LIM> G(h,ei);

  auto r=G.init(n);
  for(int i=0;i<n;i++){
    int v=a+d*i;
    r=G.update(r,i,i+1,E(0,1,v));
  }

  int m;
  cin>>m;
  for(int i=0;i<m;i++){
    int x,y,z;
    cin>>x>>y>>z;
    y--;
    if(x==0) r=G.toggle(r,y,z);
    if(x==1) r=G.update(r,y,z,E(0,1,1));
    if(x==2) r=G.update(r,y,z,E(0,2,0));
  }

  int k;
  cin>>k;
  k--;
  cout<<G.get_val(r,k).c<<endl;
  return 0;
}
accuracy: 1
time_ms: 1000
memory_kb: 19692
final_rank: 15

code_snippet: #include<bits/stdc++.h>
using namespace std;
using Int = long long;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}


template<typename E>
struct RBST{
  using u32 = uint32_t;
  u32 xor128(){
    static u32 x = 123456789;
    static u32 y = 362436069;
    static u32 z = 521288629;
    static u32 w = 88675123;

    u32 t = x ^ (x << 11);
    x = y; y = z; z = w;
    return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));
  }

  struct Node{
    Node *l,*r;
    size_t cnt;
    bool rev;
    E dat,laz;
    Node(){}
    Node(E dat,E laz):
      cnt(1),rev(0),dat(dat),laz(laz){l=r=nullptr;}
  };

  using H = function<E(E,E)>;

  H h;
  E ei;

  const size_t LIM = 1e6;
  vector<Node> pool;
  size_t ptr;

  RBST(H h,E ei):h(h),ei(ei),pool(LIM),ptr(0){}

  inline Node* create(){
    return &(pool[ptr++]=Node(ei,ei));
  }

  Node* build(size_t l,size_t r){
    if(l+1==r) return create();
    size_t m=(l+r)>>1;
    return merge(build(l,m),build(m,r));
  }

  Node* init(int n){
    return build(0,n);
  }

  size_t count(const Node *a){
    return a?a->cnt:0;
  }

  Node* recalc(Node *a){
    a->cnt=count(a->l)+1+count(a->r);
    return a;
  }

  void propagate(Node *a,E x){
    a->dat=h(a->dat,x);
    a->laz=h(a->laz,x);
  }

  void toggle(Node *a){
    swap(a->l,a->r);
    a->rev^=1;
  }

  // remove "virtual" for optimization
  Node* eval(Node* a){
    if(a->laz!=ei){
      if(a->l) propagate(a->l,a->laz);
      if(a->r) propagate(a->r,a->laz);
      a->laz=ei;
    }
    if(a->rev){
      if(a->l) toggle(a->l);
      if(a->r) toggle(a->r);
      a->rev=false;
    }
    return recalc(a);
  }

  pair<Node*, Node*> split(Node* a,size_t k){
    if(a==nullptr) return make_pair(a,a);
    a=eval(a);
    if(k<=count(a->l)){
      auto s=split(a->l,k);
      a->l=s.second;
      return make_pair(s.first,recalc(a));
    }
    auto s=split(a->r,k-(count(a->l)+1));
    a->r=s.first;
    return make_pair(recalc(a),s.second);
  }

  Node* merge(Node* a,Node* b){
    if(a==nullptr) return b;
    if(b==nullptr) return a;
    if(xor128()%(count(a)+count(b))<count(a)){
      a=eval(a);
      a->r=merge(a->r,b);
      return recalc(a);
    }
    b=eval(b);
    b->l=merge(a,b->l);
    return recalc(b);
  }

  Node* insert(Node *a,size_t pos){
    Node* b=create();
    auto s=split(a,pos);
    return a=merge(merge(s.first,b),s.second);
  }

  Node* erase(Node *a,size_t pos){
    auto s=split(a,pos);
    auto t=split(s.second,1);
    return merge(s.first,t.second);
  }

  Node* toggle(Node *a,size_t l,size_t r){
    auto s=split(a,l);
    auto t=split(s.second,r-l);
    auto u=eval(t.first);
    toggle(u);
    return merge(s.first,merge(u,t.second));
  }

  Node* update(Node *a,size_t l,size_t r,E x){
    auto s=split(a,l);
    auto t=split(s.second,r-l);
    auto u=eval(t.first);
    propagate(u,x);
    return merge(s.first,merge(u,t.second));
  }

  Node* set_val(Node *a,size_t k,E x){
    assert(k<count(a));
    a=eval(a);
    size_t num=count(a->l);
    if(k<num) a->l=set_val(a->l,k,x);
    if(k>num) a->r=set_val(a->r,k-(num+1),x);
    if(k==num) a->dat=x;
    return recalc(a);
  }

  E get_val(Node *a,size_t k){
    assert(k<count(a));
    a=eval(a);
    size_t num=count(a->l);
    if(k<num) return get_val(a->l,k);
    if(k>num) return get_val(a->r,k-(num+1));
    return a->dat;
  }

  void dump(Node* a,typename vector<E>::iterator it){
    if(!count(a)) return;
    a=eval(a);
    dump(a->l,it);
    *(it+count(a->l))=a->dat;
    dump(a->r,it+count(a->l)+1);
  }

  vector<E> dump(Node* a){
    vector<E> v(count(a));
    dump(a,v.begin());
    return v;
  }
};


//INSERT ABOVE HERE
signed main(){
  cin.tie(0);
  ios::sync_with_stdio(0);

  int n,a,d;
  cin>>n>>a>>d;

  using ll = long long;
  struct E{
    ll a,b,c;
    E(){}
    E(ll a,ll b,ll c):a(a),b(b),c(c){}
    bool operator!=(const E &o) const{
      return make_tuple(a,b,c)!=make_tuple(o.a,o.b,o.c);
    }
  };

  const ll LIM = 1e9;
  auto h=
    [](E a,E b){
      E c(a.a+a.b*(a.c+b.a),a.b*b.b,0);
      c.c=c.a/c.b+b.c;
      c.a%=c.b;
      if(c.b>LIM){
        c.a=max(0LL,LIM-(c.b-c.a));
        c.b=LIM;
      }
      return c;
    };
  E ei(0,1,0);
  RBST<E> G(h,ei);

  auto r=G.init(n);
  for(int i=0;i<n;i++){
    int v=a+d*i;
    r=G.update(r,i,i+1,E(0,1,v));
  }

  int m;
  cin>>m;
  for(int i=0;i<m;i++){
    int x,y,z;
    cin>>x>>y>>z;
    y--;
    if(x==0) r=G.toggle(r,y,z);
    if(x==1) r=G.update(r,y,z,E(0,1,1));
    if(x==2) r=G.update(r,y,z,E(0,2,0));
  }

  int k;
  cin>>k;
  k--;
  cout<<G.get_val(r,k).c<<endl;
  return 0;
}
accuracy: 1
time_ms: 970
memory_kb: 17808
final_rank: 13

code_snippet: #include<bits/stdc++.h>
using namespace std;
using Int = long long;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}


template<typename E>
struct RBST{
  using u32 = uint32_t;
  u32 xor128(){
    static u32 x = 123456789;
    static u32 y = 362436069;
    static u32 z = 521288629;
    static u32 w = 88675123;

    u32 t = x ^ (x << 11);
    x = y; y = z; z = w;
    return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));
  }

  struct Node{
    Node *l,*r;
    size_t cnt;
    bool rev;
    E dat,laz;
    Node(){}
    Node(E dat,E laz):
      cnt(1),rev(0),dat(dat),laz(laz){l=r=nullptr;}
  };

  using H = function<E(E,E)>;

  H h;
  E ei;

  const size_t LIM = 1e6;
  vector<Node> pool;
  size_t ptr;

  RBST(H h,E ei):h(h),ei(ei),pool(LIM),ptr(0){}

  inline Node* create(){
    return &(pool[ptr++]=Node(ei,ei));
  }

  Node* build(size_t l,size_t r){
    if(l+1==r) return create();
    size_t m=(l+r)>>1;
    return merge(build(l,m),build(m,r));
  }

  Node* init(int n){
    return build(0,n);
  }

  size_t count(const Node *a){
    return a?a->cnt:0;
  }

  Node* recalc(Node *a){
    a->cnt=count(a->l)+1+count(a->r);
    return a;
  }

  void propagate(Node *a,E x){
    a->dat=h(a->dat,x);
    a->laz=h(a->laz,x);
  }

  void toggle(Node *a){
    swap(a->l,a->r);
    a->rev^=1;
  }

  // remove "virtual" for optimization
  virtual Node* eval(Node* a){
    if(a->laz!=ei){
      if(a->l) propagate(a->l,a->laz);
      if(a->r) propagate(a->r,a->laz);
      a->laz=ei;
    }
    if(a->rev){
      if(a->l) toggle(a->l);
      if(a->r) toggle(a->r);
      a->rev=false;
    }
    return recalc(a);
  }

  pair<Node*, Node*> split(Node* a,size_t k){
    if(a==nullptr) return make_pair(a,a);
    a=eval(a);
    if(k<=count(a->l)){
      auto s=split(a->l,k);
      a->l=s.second;
      return make_pair(s.first,recalc(a));
    }
    auto s=split(a->r,k-(count(a->l)+1));
    a->r=s.first;
    return make_pair(recalc(a),s.second);
  }

  Node* merge(Node* a,Node* b){
    if(a==nullptr) return b;
    if(b==nullptr) return a;
    if(xor128()%(count(a)+count(b))<count(a)){
      a=eval(a);
      a->r=merge(a->r,b);
      return recalc(a);
    }
    b=eval(b);
    b->l=merge(a,b->l);
    return recalc(b);
  }

  Node* insert(Node *a,size_t pos){
    Node* b=create();
    auto s=split(a,pos);
    return a=merge(merge(s.first,b),s.second);
  }

  Node* erase(Node *a,size_t pos){
    auto s=split(a,pos);
    auto t=split(s.second,1);
    return merge(s.first,t.second);
  }

  Node* toggle(Node *a,size_t l,size_t r){
    auto s=split(a,l);
    auto t=split(s.second,r-l);
    auto u=eval(t.first);
    toggle(u);
    return merge(s.first,merge(u,t.second));
  }

  Node* update(Node *a,size_t l,size_t r,E x){
    auto s=split(a,l);
    auto t=split(s.second,r-l);
    auto u=eval(t.first);
    propagate(u,x);
    return merge(s.first,merge(u,t.second));
  }

  Node* set_val(Node *a,size_t k,E x){
    assert(k<count(a));
    a=eval(a);
    size_t num=count(a->l);
    if(k<num) a->l=set_val(a->l,k,x);
    if(k>num) a->r=set_val(a->r,k-(num+1),x);
    if(k==num) a->dat=x;
    return recalc(a);
  }

  E get_val(Node *a,size_t k){
    assert(k<count(a));
    a=eval(a);
    size_t num=count(a->l);
    if(k<num) return get_val(a->l,k);
    if(k>num) return get_val(a->r,k-(num+1));
    return a->dat;
  }

  void dump(Node* a,typename vector<E>::iterator it){
    if(!count(a)) return;
    a=eval(a);
    dump(a->l,it);
    *(it+count(a->l))=a->dat;
    dump(a->r,it+count(a->l)+1);
  }

  vector<E> dump(Node* a){
    vector<E> v(count(a));
    dump(a,v.begin());
    return v;
  }
};


//INSERT ABOVE HERE
signed main(){
  cin.tie(0);
  ios::sync_with_stdio(0);

  int n,a,d;
  cin>>n>>a>>d;

  using ll = long long;
  struct E{
    ll a,b,c;
    E(){}
    E(ll a,ll b,ll c):a(a),b(b),c(c){}
    bool operator!=(const E &o) const{
      return make_tuple(a,b,c)!=make_tuple(o.a,o.b,o.c);
    }
  };

  const ll LIM = 1e9;
  auto h=
    [](E a,E b){
      E c(a.a+a.b*(a.c+b.a),a.b*b.b,0);
      c.c=c.a/c.b+b.c;
      c.a%=c.b;
      if(c.b>LIM){
        c.a=max(0LL,LIM-(c.b-c.a));
        c.b=LIM;
      }
      return c;
    };
  E ei(0,1,0);
  RBST<E> G(h,ei);

  auto r=G.init(n);
  for(int i=0;i<n;i++){
    int v=a+d*i;
    r=G.update(r,i,i+1,E(0,1,v));
  }

  int m;
  cin>>m;
  for(int i=0;i<m;i++){
    int x,y,z;
    cin>>x>>y>>z;
    y--;
    if(x==0) r=G.toggle(r,y,z);
    if(x==1) r=G.update(r,y,z,E(0,1,1));
    if(x==2) r=G.update(r,y,z,E(0,2,0));
  }

  int k;
  cin>>k;
  k--;
  cout<<G.get_val(r,k).c<<endl;
  return 0;
}
accuracy: 1
time_ms: 960
memory_kb: 17768
final_rank: 12

code_snippet: #include<bits/stdc++.h>
using namespace std;
using Int = long long;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}


template<typename E>
struct RBST{
  using u32 = uint32_t;
  u32 xor128(){
    static u32 x = 123456789;
    static u32 y = 362436069;
    static u32 z = 521288629;
    static u32 w = 88675123;

    u32 t = x ^ (x << 11);
    x = y; y = z; z = w;
    return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));
  }

  struct Node{
    Node *l,*r;
    size_t cnt;
    bool rev;
    E dat,laz;
    Node(){}
    Node(E dat,E laz):
      cnt(1),rev(0),dat(dat),laz(laz){l=r=nullptr;}
  };

  using H = function<E(E,E)>;

  H h;
  E ei;

  const size_t LIM = 1e6;
  vector<Node> pool;
  size_t ptr;

  RBST(H h,E ei):h(h),ei(ei),pool(LIM),ptr(0){}

  inline Node* create(){
    return &(pool[ptr++]=Node(ei,ei));
  }

  Node* build(size_t l,size_t r){
    if(l+1==r) return create();
    size_t m=(l+r)>>1;
    return merge(build(l,m),build(m,r));
  }

  Node* init(int n){
    return build(0,n);
  }

  size_t count(const Node *a){
    return a?a->cnt:0;
  }

  Node* recalc(Node *a){
    a->cnt=count(a->l)+1+count(a->r);
    return a;
  }

  void propagate(Node *a,E x){
    a->dat=h(a->dat,x);
    a->laz=h(a->laz,x);
  }

  void toggle(Node *a){
    swap(a->l,a->r);
    a->rev^=1;
  }

  // remove "virtual" for optimization
  virtual Node* eval(Node* a){
    if(a->laz!=ei){
      if(a->l) propagate(a->l,a->laz);
      if(a->r) propagate(a->r,a->laz);
      a->laz=ei;
    }
    if(a->rev){
      if(a->l) toggle(a->l);
      if(a->r) toggle(a->r);
      a->rev=false;
    }
    return recalc(a);
  }

  pair<Node*, Node*> split(Node* a,size_t k){
    if(a==nullptr) return make_pair(a,a);
    a=eval(a);
    if(k<=count(a->l)){
      auto s=split(a->l,k);
      a->l=s.second;
      return make_pair(s.first,recalc(a));
    }
    auto s=split(a->r,k-(count(a->l)+1));
    a->r=s.first;
    return make_pair(recalc(a),s.second);
  }

  Node* merge(Node* a,Node* b){
    if(a==nullptr) return b;
    if(b==nullptr) return a;
    if(xor128()%(count(a)+count(b))<count(a)){
      a=eval(a);
      a->r=merge(a->r,b);
      return recalc(a);
    }
    b=eval(b);
    b->l=merge(a,b->l);
    return recalc(b);
  }

  Node* insert(Node *a,size_t pos){
    Node* b=create();
    auto s=split(a,pos);
    return a=merge(merge(s.first,b),s.second);
  }

  Node* erase(Node *a,size_t pos){
    auto s=split(a,pos);
    auto t=split(s.second,1);
    return merge(s.first,t.second);
  }

  Node* toggle(Node *a,size_t l,size_t r){
    auto s=split(a,l);
    auto t=split(s.second,r-l);
    auto u=eval(t.first);
    toggle(u);
    return merge(s.first,merge(u,t.second));
  }

  Node* update(Node *a,size_t l,size_t r,E x){
    auto s=split(a,l);
    auto t=split(s.second,r-l);
    auto u=eval(t.first);
    propagate(u,x);
    return merge(s.first,merge(u,t.second));
  }

  Node* set_val(Node *a,size_t k,E x){
    assert(k<count(a));
    a=eval(a);
    size_t num=count(a->l);
    if(k<num) a->l=set_val(a->l,k,x);
    if(k>num) a->r=set_val(a->r,k-(num+1),x);
    if(k==num) a->dat=x;
    return recalc(a);
  }

  E get_val(Node *a,size_t k){
    assert(k<count(a));
    a=eval(a);
    size_t num=count(a->l);
    if(k<num) return get_val(a->l,k);
    if(k>num) return get_val(a->r,k-(num+1));
    return a->dat;
  }

  void dump(Node* a,typename vector<E>::iterator it){
    if(!count(a)) return;
    a=eval(a);
    dump(a->l,it);
    *(it+count(a->l))=a->dat;
    dump(a->r,it+count(a->l)+1);
  }

  vector<E> dump(Node* a){
    vector<E> v(count(a));
    dump(a,v.begin());
    return v;
  }
};


//INSERT ABOVE HERE
signed main(){
  cin.tie(0);
  ios::sync_with_stdio(0);

  int n,a,d;
  cin>>n>>a>>d;

  using ll = long long;
  struct E{
    ll a,b,c;
    E(){}
    E(ll a,ll b,ll c):a(a),b(b),c(c){}
    bool operator==(const E &o) const{
      return a==o.a&&b==o.b&&c==o.c;
    }
    bool operator!=(const E &o) const{
      return !(*this==o);
    }
  };

  const ll LIM = 1e9;
  auto h=
    [](E a,E b){
      E c(a.a+a.b*(a.c+b.a),a.b*b.b,0);
      c.c=c.a/c.b+b.c;
      c.a%=c.b;
      if(c.b>LIM){
        c.a=max(0LL,LIM-(c.b-c.a));
        c.b=LIM;
      }
      return c;
    };
  E ei(0,1,0);
  RBST<E> G(h,ei);

  auto r=G.init(n);
  for(int i=0;i<n;i++){
    int v=a+d*i;
    r=G.update(r,i,i+1,E(0,1,v));
  }

  int m;
  cin>>m;
  for(int i=0;i<m;i++){
    int x,y,z;
    cin>>x>>y>>z;
    y--;
    if(x==0) r=G.toggle(r,y,z);
    if(x==1) r=G.update(r,y,z,E(0,1,1));
    if(x==2) r=G.update(r,y,z,E(0,2,0));
  }

  int k;
  cin>>k;
  k--;
  cout<<G.get_val(r,k).c<<endl;
  return 0;
}
accuracy: 1
time_ms: 970
memory_kb: 17864
final_rank: 14

code_snippet: /*  -*- coding: utf-8 -*-
 *
 * 1579.cc: Array Update 2
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_M = 200000;

/* typedef */

struct Op {
  int x, y, z;
  Op() {}
  Op(int _x, int _y, int _z): x(_x), y(_y), z(_z) {}
};

/* global variables */

Op ops[MAX_M];
int xs[MAX_M];

/* subroutines */

/* main */

int main() {
  int n, a, d, m;
  scanf("%d%d%d%d", &n, &a, &d, &m);

  for (int i = 0; i < m; i++)
    scanf("%d%d%d", &ops[i].x, &ops[i].y, &ops[i].z);

  int k;
  scanf("%d", &k);

  int l = 0;
  for (int i = m - 1; i >= 0; i--) {
    Op &op = ops[i];
    if (op.y <= k && k <= op.z) {
      if (op.x != 0)
	xs[l++] = op.x;
      else
	k = op.z - (k - op.y);
    }
  }
  //printf("l=%d, k=%d\n", l, k);

  int v = a + d * (k - 1);
  for (int i = l - 1; i >= 0; i--) {
    if (xs[i] == 1) v++;
    else v >>= 1;
  }

  printf("%d\n", v);
  return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 5412
final_rank: 3

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fi first
#define se second
#define dbg(x) cout<<#x" = "<<((x))<<endl
template<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}

int rev(int x, int l, int r){
    int n = x-l;
    return r-n;
}

int main(){
    int n,a,d,m;
    cin >>n >>a >>d >>m;

    vector<int> x(m),y(m),z(m);
    rep(i,m){
        cin >>x[i] >>y[i] >>z[i];
        --y[i];
        --z[i];
    }

    int k;
    cin >>k;
    --k;

    int idx = k;
    for(int i=m-1; i>=0; --i){
        if(x[i]==0){
            if(y[i]<=idx && idx<=z[i]) idx = rev(idx, y[i], z[i]);
        }
    }

    ll val = a+idx*d;
    rep(i,m){
        if(y[i]<=idx && idx<=z[i]){
            if(x[i]==0) idx = rev(idx, y[i], z[i]);
            if(x[i]==1) ++val;
            if(x[i]==2) val /= 2;
        }
    }
    cout << val << endl;
    return 0;
}
accuracy: 1
time_ms: 100
memory_kb: 4936
final_rank: 8

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define _MACRO(_1, _2, _3, NAME, ...) NAME
#define _repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define _rep(i,n) _repl(i,0,n)
#define rep(...) _MACRO(__VA_ARGS__, _repl, _rep)(__VA_ARGS__)
#define mp make_pair
#define pb push_back
#define all(x) begin(x),end(x)
#define uniq(x) sort(all(x)),(x).erase(unique(all(x)),end(x))
#define fi first
#define se second
#define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
void _dbg(string){cerr<<endl;}
template<class H,class... T> void _dbg(string s,H h,T... t){int l=s.find(',');cerr<<s.substr(0,l)<<" = "<<h<<", ";_dbg(s.substr(l+1),t...);}
template<class T,class U> ostream& operator<<(ostream &o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream &o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}

#define long long long // for codeforces

int main(){
  ios::sync_with_stdio(false);
  cin.tie(0); cout.tie(0);

  int n;
  long a,d;
  int m;
  cin>>n>>a>>d>>m;

  vector<int> x(m), y(m), z(m);
  rep(i,m) cin>>x[i]>>y[i]>>z[i];
  rep(i,m) y[i]--,z[i]--;
  int k;
  cin>>k;
  k--;

  for(int i=m-1; i>=0; i--){
    if(x[i]==0 && y[i]<=k && k<=z[i]){
      k = y[i] + z[i] - k;
    }
  }

  long ans = a + d*k;
  rep(i,m) if(y[i]<=k && k<=z[i]){
    if(x[i]==0){
      k = y[i] + z[i] - k;
    }
    else if(x[i]==1){
      ans++;
    }
    else if(x[i]==2){
      ans /= 2;
    }
    else assert(false);
  }

  cout << ans << endl;

  return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 4856
final_rank: 2

code_snippet: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <set>
#include <map>
#include <time.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


enum ORDER{
	PLUS,
	HALF,
};

int x[200000],y[200000],z[200000];

int main(){

	int N;
	scanf("%d",&N);

	int a,d;
	scanf("%d %d",&a,&d);

	int M;
	scanf("%d",&M);

	for(int i = 0; i < M; i++){
		scanf("%d %d %d",&x[i],&y[i],&z[i]);
		y[i]--;
		z[i]--;
	}

	int loc;
	scanf("%d",&loc);
	loc--;

	stack<ORDER> S;

	for(int i = M-1; i >= 0; i--){

		switch(x[i]){
		case 0:
			if(loc >= y[i] && loc <= z[i]){
				loc = z[i]-(loc-y[i]);
			}
			break;
		case 1:
			if(loc >= y[i] && loc <= z[i]){
				S.push(PLUS);
			}
			break;
		case 2:
			if(loc >= y[i] && loc <= z[i]){
				S.push(HALF);
			}
			break;
		}
	}

	int ans = a+loc*d;

	while(!S.empty()){

		if(S.top() == PLUS){
			ans++;
		}else{
			ans /= 2;
		}

		S.pop();
	}

	printf("%d\n",ans);

	return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 5520
final_rank: 5

code_snippet: #include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

int main(){

  int n,a,d,m,k;
  cin >> n;
  cin >> a >> d;
  cin >> m;
  vector<int> x(m),y(m),z(m);
  for(int i=0; i<m; i++){
    cin >> x[i] >> y[i] >> z[i];
    y[i]--;
    z[i]--;
  }
  cin >> k;
  k--;
  vector<int> com;
  for(int i=m-1; i>=0; i--){
    if(x[i]==0 && (y[i]<=k && k<=z[i])){
      k = y[i]+z[i]-k;
      //cout << "1 " << i << endl;;
    }else{
      if(y[i]<=k && k<=z[i]){
        com.push_back(x[i]);
      }
    }
  }
  //cout << k << endl;
  int num = a+d*k;
  //cout << com.size() << endl;
  for(int i=com.size()-1;i>=0;i--){
      if(com[i]==1){
          num++;
      }else if(com[i]==2){
          num/=2;
      }
  }
  
  cout << num << endl;

  return 0;
}
accuracy: 1
time_ms: 100
memory_kb: 5236
final_rank: 10

code_snippet: #include<iostream>
using namespace std;
int main(){
  int n;
  cin>>n;
  int a,b;
  cin>>a>>b;
  int vec[n];
  for(int i=0;i<n;++i,a+=b) vec[i]=a;
  int m;
  cin>>m;
  int x[m], y[m], z[m];
  for(int i=0;i<m;++i) cin>>x[i]>>y[i]>>z[i];
  int k;
  cin>>k;
  --k;
  for(int i=m-1;i>=0;--i){
    if(x[i]==0){
      if(y[i]-1<=k && k<z[i]) k=z[i]-(k-y[i]+1)-1;
    }
  }
  for(int i=0;i<m;++i){
    if(y[i]-1<=k && k<z[i]){
      if(x[i]==1) vec[k]++;
      else if(x[i]==2) vec[k]/=2;
    }
  }
  cout<<vec[k]<<endl;
}
accuracy: 0.17647058823529413
time_ms: 50
memory_kb: 4636
final_rank: 19

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define MAX 200005

int N,M,K,a,d;
int x[MAX],y[MAX],z[MAX];

int main(){
  scanf("%d %d %d %d",&N,&a,&d,&M);
  for(int i=0;i<M;i++)scanf("%d %d %d",&x[i],&y[i],&z[i]);
  scanf("%d",&K);
  stack<int> st;
  for(int i=M-1;i>=0;i--){
    if(K<y[i]||z[i]<K)continue;
    if(x[i]==0)K=z[i]+y[i]-K; 
    else st.push(x[i]);
  }
  int ans=a+d*(K-1);
  while(!st.empty()){
    if(st.top()==1)ans++;
    else ans/=2;
    st.pop();
  }
  printf("%d\n",ans);
  return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 5476
final_rank: 4

code_snippet: #include <iostream>
#include <vector>

constexpr int REVERSE   = 0;
constexpr int INCREMENT = 1;
constexpr int HALF      = 2;

bool cover(int l, int r, int x)
{
    return l <= x && x <= r;
}

int reverse(int l, int r, int x)
{
    return r + l - x;
}

int main()
{
    int N, a, d, M;
    std::cin >> N >> a >> d >> M;

    std::vector<int> x(M), y(M), z(M);
    for (int i = 0; i < M; i++) {
        std::cin >> x[i] >> y[i] >> z[i];
    }

    int K;
    std::cin >> K;

    for (int i = M - 1; i >= 0; i--) {
        if (x[i] == REVERSE && cover(y[i], z[i], K)) {
            K = reverse(y[i], z[i], K);
        }
    }

    int val = a + (K - 1) * d;
    for (int i = 0; i < M; i++) {
        if (!cover(y[i], z[i], K)) {
            continue;
        }
        
        switch (x[i]) {
            case REVERSE:
                K = reverse(y[i], z[i], K);
                break;
                
            case INCREMENT:
                val++;
                break;

            case HALF:
                val /= 2;
                break;
        }
    }

    std::cout << val << std::endl;
    
    return 0;
}
accuracy: 1
time_ms: 100
memory_kb: 4928
final_rank: 7

code_snippet: #include <iostream>
#include <vector>

constexpr int REVERSE   = 0;
constexpr int INCREMENT = 1;
constexpr int HALF      = 2;

bool cover(int l, int r, int x)
{
    return l <= x && x <= r;
}

int reverse(int l, int r, int x)
{
    return r + l - x;
}

int main()
{
    int N, a, d, M;
    std::cin >> N >> a >> d >> M;

    std::vector<int> x(M), y(M), z(M);
    for (int i = 0; i < M; i++) {
        std::cin >> x[i] >> y[i] >> z[i];
    }

    int K;
    std::cin >> K;

    for (int i = M - 1; i >= 0; i--) {
        if (x[i] == REVERSE && cover(y[i], z[i], K)) {
            K = reverse(y[i], z[i], K);
        }
    }

    int val = a + (K - 1) * d;
    for (int i = 0; i < M; i++) {
        if (!cover(y[i], z[i], K)) {
            continue;
        }
        
        switch (x[i]) {
            case INCREMENT:
                val++;
                break;

            case HALF:
                val /= 2;
                break;
        }
    }

    std::cout << val << std::endl;
    
    return 0;
}
accuracy: 0.17647058823529413
time_ms: 60
memory_kb: 4340
final_rank: 20