Submission #548235

# Submission time Handle Problem Language Result Execution time Memory
548235 2022-04-12T18:24:59 Z Bungmint Sjeckanje (COCI21_sjeckanje) C++17
0 / 110
16 ms 37904 KB
//Copyright © 2022 Youngmin Park. All rights reserved.
//#pragma GCC optimize("O3")
//#pragma GCC target("avx2")
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using vi = vector<int>;
using pii = pair<int, int>;
using vpi = vector<pii>;
using pll = pair<ll, ll>;
using vl = vector<ll>;
using vpl = vector<pll>;
using ld = long double;
template <typename T, size_t SZ>
using ar = array<T, SZ>;

#define all(v) (v).begin(), (v).end()
#define pb push_back
#define sz(x) (int)(x).size()
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define F0R(i, a) FOR(i, 0, a)
#define ROF(i, a, b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i, a) ROF(i, 0, a)
#define REP(a) F0R(_, a)

const int INF = 1e9;
const ll LINF = 1e18;
const int MOD = 1e9 + 7; //998244353;
const ld PI = acos((ld)-1.0);
const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template <typename T>
using pqg = priority_queue<T, vector<T>, greater<T>>;
template <typename T>
bool ckmin(T &a, const T &b) { return b < a ? a = b, 1 : 0; }
template <typename T>
bool ckmax(T &a, const T &b) { return b > a ? a = b, 1 : 0; }

template <typename A, typename B>
ostream &operator<<(ostream &os, const pair<A, B> &p)
{
    return os << '(' << p.first << ", " << p.second << ')';
}
template <typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type>
ostream &operator<<(ostream &os, const T_container &v)
{
    os << '{';
    string sep;
    for (const T &x : v)
        os << sep << x, sep = ", ";
    return os << '}';
}
void dbg_out()
{
    cerr << endl;
}
template <typename Head, typename... Tail>
void dbg_out(Head H, Tail... T)
{
    cerr << ' ' << H;
    dbg_out(T...);
}
#ifdef LOCAL
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...) 42
#endif

inline namespace RecursiveLambda{
	template <typename Fun>
	struct y_combinator_result{
		Fun fun_;
		template <typename T> 
		explicit y_combinator_result(T &&fun): fun_(forward<T>(fun)){}
		template <typename...Args>
		decltype(auto) operator()(Args &&...args){
			return fun_(ref(*this), forward<Args>(args)...);
		}
	};
	template <typename Fun>
	decltype(auto) y_combinator(Fun &&fun){
		return y_combinator_result<decay_t<Fun>>(forward<Fun>(fun));
	}
};

void setIO(string s) // USACO
{
	#ifndef LOCAL
	    freopen((s + ".in").c_str(), "r", stdin);
	    freopen((s + ".out").c_str(), "w", stdout);
	#endif
}

bool overlap(const pll& a, const pll& b) {
	return !(a.se < b.fi || b.se < a.fi);
}

inline namespace Seg {
	const int N = 2e5;
	ll lazy[N << 2];
	struct Node {
		pll pref, suf;
		ll sum;
		bool same;
		Node(ll x = LINF) {
			pref = {x, x};
			suf = {x, x};
			same = 1;
			sum = 0;
		}
		friend Node operator+(const Node& a, const Node& b) {
			if (a.pref.fi == LINF) return b;
			if (b.pref.fi == LINF) return a;
			Node res{};
			res.same = 0;
			if (a.same && b.same) {
				if (overlap(a.suf, b.pref)) {
					res.pref = a.pref;
					res.suf = b.suf;
					res.sum = a.sum + b.sum;
				}else {
					res.same = 1;
					res.pref = res.suf = {min(a.suf.fi, b.pref.fi), max(a.suf.se, b.pref.se)};
					res.sum = res.pref.se - res.pref.fi;
				}
			}else if (a.same) {
				res.suf = b.suf;
				if (overlap(a.suf, b.pref)) {
					res.pref = a.pref;
					res.sum = a.sum + b.sum;
				}else {
					res.pref = {min(a.suf.fi, b.pref.fi), max(a.suf.se, b.pref.se)};
					res.sum = a.sum + b.sum + res.suf.se - res.suf.fi - (a.suf.se - a.suf.fi + b.pref.se - b.pref.fi);
				}
			}else if (b.same) {
				res.pref = a.pref;
				if (overlap(a.suf, b.pref)) {
					res.suf = b.suf;
					res.sum = a.sum + b.sum;
				}else {
					res.suf = {min(a.suf.fi, b.pref.fi), max(a.suf.se, b.pref.se)};
					res.sum = a.sum + b.sum + res.suf.se - res.suf.fi - (a.suf.se - a.suf.fi + b.pref.se - b.pref.fi);
				}
			}else {
				res.pref = a.pref;
				res.suf = b.suf;
				res.sum = a.sum + b.sum;
				if (!overlap(a.suf, b.pref)) {
					res.sum += max(a.suf.se, b.pref.se) - min(a.suf.fi, b.pref.fi) - (a.suf.se - a.suf.fi + b.pref.se - b.pref.fi);
				}
			}
			return res;
		}
	}t[N << 2];
	int n, q;
	int SZ;
	
	void init() {
		SZ = 1;
		while (SZ < n) SZ *= 2;
	}
	void push(int x) {
		if (lazy[x] == 0) return;
		t[x].pref.fi += lazy[x];
		t[x].pref.se += lazy[x];
		t[x].suf.fi += lazy[x];
		t[x].suf.se += lazy[x];
		if (2 * x + 1 < 2 * SZ) {
			lazy[2 * x + 1] += lazy[x];
			lazy[2 * x + 2] += lazy[x];
		}
		lazy[x] = 0;
	}
	void build(vector<Node>& a, int x, int l, int r) {
		if (r - l == 1) {
			if (l < n) t[x] = a[l];
			return;
		}
		int m = (l + r) >> 1;
		build(a, 2 * x + 1, l, m);
		build(a, 2 * x + 2, m, r);
		t[x] = t[2 * x + 1] + t[2 * x + 2];
	}
	void upd(int l, int r, ll v, int x, int lx, int rx) {
		push(x);
		if (lx >= r || l >= rx) return;
		if (l <= lx && rx <= r) {
			lazy[x] += v;
			push(x);
			return;
		}
		int m = (lx + rx) >> 1;
		upd(l, r, v, 2 * x + 1, lx, m);
		upd(l, r, v, 2 * x + 2, m, rx);
		t[x] = t[2 * x + 1] + t[2 * x + 2];
	}
	Node query(int l, int r, int x, int lx, int rx) {
		push(x);
		if (lx >= r || l >= rx) return Node();
		if (l <= lx && rx <= r) return t[x];
		int m = (lx + rx) >> 1;
		Node a = query(l, r, 2 * x + 1, lx, m);
		Node b = query(l, r, 2 * x + 2, m, rx);
		return a + b;
	}
};



void solve()
{
	cin >> n >> q;
	vector<Node> a(n);
	F0R(i, n) {
		ll x;
		cin >> x;
		a[i] = Node(x);
	}
	init();
	build(a, 0, 0, SZ);
	REP(q) {
		int l, r;
		ll x;
		cin >> l >> r >> x;
		l--;
		upd(l, r, x, 0, 0, SZ);
		cout << query(0, n, 0, 0, SZ).sum << '\n';
	}
}

int main()
{
    cin.tie(0)->sync_with_stdio(0);
    cin.exceptions(cin.failbit);
    int testcase=1;
    // cin >> testcase;
    while (testcase--)
    {
        solve();
    }
}

Compilation message

Main.cpp: In function 'void setIO(std::string)':
Main.cpp:94:13: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   94 |      freopen((s + ".in").c_str(), "r", stdin);
      |      ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:95:13: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   95 |      freopen((s + ".out").c_str(), "w", stdout);
      |      ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 16 ms 37904 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 16 ms 37904 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 16 ms 37904 KB Output isn't correct
2 Halted 0 ms 0 KB -