Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define re return
- #define pb push_back
- #define po pop_back
- #define all(x) (x).begin(), (x).end()
- #define fi first
- #define se second
- #define sqrt(x) sqrt(abs(x))
- #define mp make_pair
- #define pi 3.14159265359
- #define fo(i, n) for(int i = 0; i < n; ++i)
- #define ro(i, n) for(int i = n - 1; i >= 0; --i)
- #define fill(x, y) memset(x, y, sizeof(x))
- typedef vector<int> vi;
- typedef vector<vi> vvi;
- typedef pair<int, int> ii;
- typedef vector<ii> vii;
- typedef vector<string> vs;
- typedef double D;
- typedef long double ld;
- typedef long long ll;
- typedef pair<ll, ll> pll;
- typedef vector<ll> vll;
- class node{
- public:
- ll value;
- ll plus;
- node *l;
- node *r;
- node() {
- plus = 0;
- value = (ll) 1e14;
- l = r = nullptr;
- }
- };
- #define tree node *
- #define left (0)
- #define right (300000)
- node root[1000000];
- int now = 1;
- void add(tree t, int l, int r, int pos, int val) {
- if (t -> value > (ll) val) t -> value = (ll)val;
- if (r - l == 1) re;
- int c = (l + r) >> 1;
- if (pos < c) {
- if (!t -> l) t -> l = &root[now++];
- add(t -> l, l, c, pos, val);
- }
- else {
- if (!t -> r) t -> r = &root[now++];
- add(t -> r, c, r, pos, val);
- }
- }
- void update(tree t, int l, int r, int L, int R, int val) {
- if (l == L && r == R) {
- t -> plus += val;
- re;
- }
- int c = (l + r) >> 1;
- if (L < c && t -> l) update(t -> l, l, c, L, min(c, R), val);
- if (R >= c && t -> r) update(t -> r, c, r, max(c, L), R, val);
- if (t -> l && t -> r) t -> value = min(t -> l -> value + t -> l -> plus, t -> r -> value + t -> r -> plus);
- else {
- if (t -> l) t -> value = t -> l -> value + t -> l -> plus;
- else {
- if (t -> r) t -> value = t -> r -> value + t -> r -> plus;
- else t -> value = (ll) 1e14;
- }
- }
- }
- ll get_min(tree t, int l, int r, int L, int R) {
- if (l == L && r == R) re t -> value + t -> plus;
- int c = (l + r) >> 1;
- ll cur1, cur2;
- cur1 = cur2 = (ll) 1e14;
- if (L < c && t -> l) cur1 = get_min(t -> l, l, c, L, min(c, R));
- if (R >= c && t -> r) cur2 = get_min(t -> r, c, r, max(c, L), R);
- if (cur1 < cur2) re cur1 + t -> plus;
- re cur2 + t -> plus;
- }
- int main() {
- int n;
- scanf("%d", &n);
- fo(i, n) {
- int x;
- scanf("%d", &x);
- add(root, left, right, i, x);
- }
- int m;
- scanf("%d\n", &m);
- char c[150];
- fo(i, m) {
- gets(c);
- int q = 0;
- int l = 0, r = 0, p = 0;
- for (int i = 0; c[i] != '\n' && c[i] != '\0'; ++i) {
- if (!isdigit(c[i]) && c[i] != '-') continue;
- string str;
- while(isdigit(c[i]) || c[i] == '-') str += c[i++];
- switch (q) {
- case 0:
- l = atoi(str.c_str());
- break;
- case 1:
- r = atoi(str.c_str());
- break;
- case 2:
- p = atoi(str.c_str());
- break;
- }
- --i;
- ++q;
- }
- if (q == 2) {
- ll ans = (ll) 1e18;
- --ans;
- if (l > r) {
- ans = min (ans, get_min(root, left, right, 0, r + 1));
- ans = min (ans, get_min(root, left, right, l, n));
- }
- else ans = get_min(root, left, right, l, r + 1);
- printf ("%I64d\n", ans);
- //cout << '!' << ans << endl;
- }
- else {
- if (l > r) {
- update(root, left, right, 0, r + 1, p);
- update(root, left, right, l, n, p);
- }
- else update(root, left, right, l, r + 1, p);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement