1834F - Typewriter - CodeForces Solution


brute force math *2500

Please click on ads to support us..

C++ Code:

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
template<class T, class S>
ostream& operator << (ostream &o, const pair<T, S> &p) {
    return o << '(' << p.first << ", " << p.second << ')';
}
template<template<class, class...> class T, class... A>
typename enable_if<!is_same<T<A...>, string>(), ostream&>::type
operator << (ostream &o, T<A...> V) {
	o << '[';
	for(auto a : V) o << a << ", ";
	return o << ']';
}

typedef long long int ll;
typedef long double ld;
typedef pair<ll, ll> pl;
typedef vector<ll> vl;

#define G(x) ll x; cin >> x;
#define GD(x) ld x; cin >> x;
#define GS(s) string s; cin >> s;
#define F(i, l, r) for(ll i = l; i < (r); ++i)
#define FD(i, r, l) for(ll i = r; i > (l); --i)
#define P(a, n) { cout << "{ "; F(_, 0, n) cout << a[_] << " "; cout << "}\n"; }
#define EX(x) { cout << x << '\n'; exit(0); }
#define A(a) (a).begin(), (a).end()
#define K first
#define V second
#define M 1000000007 //998244353
#define N 400010

ll ct[2][2 * N], p[N];

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    G(n)
    F(i, 1, n + 1) cin >> p[i];
    F(k, 0, 2) {
        F(i, 1, n + 1) if(p[i] < n) {
            ll l = (p[i] + 1 - i + n) % n;
            ll r = n - i;
            if(r < l) r += n;
            ct[k][l]++;
            ct[k][r + 1]--;
            ct[k][l + n]++;
            if(r + 1 + n < 2 * n) ct[k][r + 1 + n]--;
        }
        F(i, 1, 2 * n) ct[k][i] += ct[k][i - 1];
        reverse(p + 1, p + n + 1);
    }
    ll r0 = 0, r1 = 0; bool rev = false;
    cout << ct[0][n] << '\n';
    G(q) while(q--) {
        G(t)
        if(t < 3) {
            if((t == 1) == !rev) { G(x) r0 = (r0 - x + n) % n; r1 = (r1 + x) % n; }
            else { G(x) r0 = (r0 + x) % n; r1 = (r1 - x + n) % n; }
        } else rev = !rev;
        cout << ct[rev][n + (rev ? r1 : r0)] << '\n';
    }
}


Comments

Submit
0 Comments
More Questions

1364A - XXXXX
1499B - Binary Removals
1569C - Jury Meeting
108A - Palindromic Times
46A - Ball Game
114A - Cifera
776A - A Serial Killer
25B - Phone numbers
1633C - Kill the Monster
1611A - Make Even
1030B - Vasya and Cornfield
1631A - Min Max Swap
1296B - Food Buying
133A - HQ9+
1650D - Twist the Permutation
1209A - Paint the Numbers
1234A - Equalize Prices Again
1613A - Long Comparison
1624B - Make AP
660B - Seating On Bus
405A - Gravity Flip
499B - Lecture
709A - Juicer
1358C - Celex Update
1466B - Last minute enhancements
450B - Jzzhu and Sequences
1582C - Grandma Capa Knits a Scarf
492A - Vanya and Cubes
217A - Ice Skating
270A - Fancy Fence