problem_id
stringlengths 6
6
| language
stringclasses 2
values | original_status
stringclasses 3
values | original_src
stringlengths 19
243k
| changed_src
stringlengths 19
243k
| change
stringclasses 3
values | i1
int64 0
8.44k
| i2
int64 0
8.44k
| j1
int64 0
8.44k
| j2
int64 0
8.44k
| error
stringclasses 270
values | stderr
stringlengths 0
226k
|
---|---|---|---|---|---|---|---|---|---|---|---|
p02345
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
#define DUMP(x) cerr << #x << "=" << x << endl
#define DUMP2(x, y) \
cerr << "(" << #x << ", " << #y << ") = (" << x << ", " << y << ")" << endl
#define BINARY(x) static_cast<bitset<16>>(x)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, m, n) for (int i = m; i < (int)(n); i++)
#define in_range(x, y, w, h) \
(0 <= (int)(x) && (int)(x) < (int)(w) && 0 <= (int)(y) && (int)(y) < (int)(h))
#define ALL(a) (a).begin(), (a).end()
typedef long long ll;
const int INF = 1e9;
typedef pair<int, int> PII;
int dx[4] = {0, -1, 1, 0}, dy[4] = {-1, 0, 0, 1};
class RMQ {
int n;
vector<int> dat;
public:
RMQ(int _n) { init(_n); }
RMQ() {}
void init(int _n) {
n = 1;
while (n < _n)
n *= 2;
dat.resize(2 * n - 1, INT_MAX);
}
void update(int k, int a) {
k += n - 1;
dat[k] = a;
while (k > 0) {
k = (k - 1) / 2;
dat[k] = min(dat[k * 2 + 1], dat[k * 2 + 2]);
}
}
int size() { return n; }
int query(int a, int b, int k, int l, int r) {
if (r <= a || b <= l)
return INT_MAX;
if (a <= l && r <= b)
return dat[k];
else {
int vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
int vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
return min(vl, vr);
}
}
int query(int l, int r) { return query(l, r, 0, 0, n); }
};
// RMQ R;
int main() {
int N, Q;
cin >> N >> Q;
// R.init(N);
RMQ R(N);
while (Q--) {
int C, X, Y;
cin >> C >> X >> Y;
if (C == 0) {
R.update(X, Y);
} else {
cerr << R.query(X, Y + 1) << endl;
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
#define DUMP(x) cerr << #x << "=" << x << endl
#define DUMP2(x, y) \
cerr << "(" << #x << ", " << #y << ") = (" << x << ", " << y << ")" << endl
#define BINARY(x) static_cast<bitset<16>>(x)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, m, n) for (int i = m; i < (int)(n); i++)
#define in_range(x, y, w, h) \
(0 <= (int)(x) && (int)(x) < (int)(w) && 0 <= (int)(y) && (int)(y) < (int)(h))
#define ALL(a) (a).begin(), (a).end()
typedef long long ll;
const int INF = 1e9;
typedef pair<int, int> PII;
int dx[4] = {0, -1, 1, 0}, dy[4] = {-1, 0, 0, 1};
class RMQ {
int n;
vector<int> dat;
public:
RMQ(int _n) { init(_n); }
RMQ() {}
void init(int _n) {
n = 1;
while (n < _n)
n *= 2;
dat.resize(2 * n - 1, INT_MAX);
}
void update(int k, int a) {
k += n - 1;
dat[k] = a;
while (k > 0) {
k = (k - 1) / 2;
dat[k] = min(dat[k * 2 + 1], dat[k * 2 + 2]);
}
}
int size() { return n; }
int query(int a, int b, int k, int l, int r) {
if (r <= a || b <= l)
return INT_MAX;
if (a <= l && r <= b)
return dat[k];
else {
int vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
int vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
return min(vl, vr);
}
}
int query(int l, int r) { return query(l, r, 0, 0, n); }
};
// RMQ R;
int main() {
int N, Q;
cin >> N >> Q;
// R.init(N);
RMQ R(N);
while (Q--) {
int C, X, Y;
cin >> C >> X >> Y;
if (C == 0) {
R.update(X, Y);
} else {
cout << R.query(X, Y + 1) << endl;
}
}
}
|
replace
| 74 | 75 | 74 | 75 |
0
|
1
2
|
p02345
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
#define MAX_N 1 << 17
int n, dat[2 * MAX_N - 1];
// INT_MAXで初期化
void init(int n_) {
n = 1;
while (n < n_)
n *= 2;
for (int i = 0; i < 2 * n - 1; i++)
dat[i] = INT_MAX;
}
void update(int k, int a) {
k += n - 1;
dat[k] = a;
while (k > 0) {
k = (k - 1) / 2;
dat[k] = min(dat[k * 2 + 1], dat[k * 2 + 2]);
}
}
int query(int a, int b, int k = 0, int l = 0, int r = n) {
if (r <= a || b <= l)
return INT_MAX;
if (a <= l && r <= b)
return dat[k];
else {
int vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
int vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
return min(vl, vr);
}
}
int main() {
int q;
cin >> n >> q;
init(n);
for (int i = 0; i < q; i++) {
int com, x, y;
cin >> com >> x >> y;
if (com)
cout << query(x, y + 1) << endl;
else
update(x, y);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define MAX_N 1 << 17
int n, dat[2 * (MAX_N)-1];
// INT_MAXで初期化
void init(int n_) {
n = 1;
while (n < n_)
n *= 2;
for (int i = 0; i < 2 * n - 1; i++)
dat[i] = INT_MAX;
}
void update(int k, int a) {
k += n - 1;
dat[k] = a;
while (k > 0) {
k = (k - 1) / 2;
dat[k] = min(dat[k * 2 + 1], dat[k * 2 + 2]);
}
}
int query(int a, int b, int k = 0, int l = 0, int r = n) {
if (r <= a || b <= l)
return INT_MAX;
if (a <= l && r <= b)
return dat[k];
else {
int vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
int vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
return min(vl, vr);
}
}
int main() {
int q;
cin >> n >> q;
init(n);
for (int i = 0; i < q; i++) {
int com, x, y;
cin >> com >> x >> y;
if (com)
cout << query(x, y + 1) << endl;
else
update(x, y);
}
return 0;
}
|
replace
| 3 | 4 | 3 | 4 |
0
| |
p02345
|
C++
|
Runtime Error
|
#include <algorithm>
#include <cstdio>
using namespace std;
typedef long long ll;
static const int INF = 2147483647;
static const int MAX_N = 100000;
int n, dat[2 * MAX_N - 1];
void init(int n_) {
n = 1;
while (n < n_)
n *= 2;
for (int i = 0; i < 2 * n - 1; i++)
dat[i] = INF;
}
void update(int k, int a) {
k += n - 1;
dat[k] = a;
while (k > 0) {
k = (k - 1) / 2;
dat[k] = min(dat[k * 2 + 1], dat[k * 2 + 2]);
}
}
int query(int a, int b, int k, int l, int r) {
if (r <= a || b <= l)
return INF;
if (a <= l && r <= b)
return dat[k];
else {
int vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
int vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
return min(vl, vr);
}
}
int main() {
int n_, q;
scanf("%d %d", &n_, &q);
init(n_);
for (int i = 0; i < q; i++) {
int com, x, y;
scanf("%d %d %d", &com, &x, &y);
if (com == 0)
update(x, y);
if (com == 1)
printf("%d\n", query(x, y + 1, 0, 0, n));
}
return 0;
}
|
#include <algorithm>
#include <cstdio>
using namespace std;
typedef long long ll;
static const int INF = 2147483647;
static const int MAX_N = 1 << 18;
int n, dat[2 * MAX_N - 1];
void init(int n_) {
n = 1;
while (n < n_)
n *= 2;
for (int i = 0; i < 2 * n - 1; i++)
dat[i] = INF;
}
void update(int k, int a) {
k += n - 1;
dat[k] = a;
while (k > 0) {
k = (k - 1) / 2;
dat[k] = min(dat[k * 2 + 1], dat[k * 2 + 2]);
}
}
int query(int a, int b, int k, int l, int r) {
if (r <= a || b <= l)
return INF;
if (a <= l && r <= b)
return dat[k];
else {
int vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
int vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
return min(vl, vr);
}
}
int main() {
int n_, q;
scanf("%d %d", &n_, &q);
init(n_);
for (int i = 0; i < q; i++) {
int com, x, y;
scanf("%d %d %d", &com, &x, &y);
if (com == 0)
update(x, y);
if (com == 1)
printf("%d\n", query(x, y + 1, 0, 0, n));
}
return 0;
}
|
replace
| 5 | 6 | 5 | 6 |
0
| |
p02345
|
C++
|
Runtime Error
|
#include <algorithm>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define REP(i, j) for (int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for (int i = (int)(j); i < (int)(k); ++i)
#define P pair<int, int>
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
const int MAX_N = 100001;
const int INF = 2147483647;
// 要素の数、区間などを何からの値を覚えておく配列
int n, q, v[2 * MAX_N - 1];
// 初期化
void init(int _n) {
n = 1;
while (n < _n)
n *= 2;
REP(i, 2 * n - 1) v[i] = INF;
}
// 添字がkのものをaに更新
void update(int k, int a) {
k += n - 1;
v[k] = a;
while (k > 0) {
k = (k - 1) / 2;
v[k] = min(v[k * 2 + 1], v[k * 2 + 2]);
}
}
// [a, b)の最小値を求める
// kは今見ている添字、l, rはそれに対応する[l, r)
// 呼び出す時はquery(a, b, 0, 0, n)として呼ぶ
int query(int a, int b, int k, int l, int r) {
if (b <= l || a >= r)
return INF;
if (a <= l && b >= r)
return v[k];
int vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
int vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
return min(vl, vr);
}
int main() {
cin >> n >> q;
init(n);
REP(i, q) {
int Q, x, y;
cin >> Q >> x >> y;
if (Q)
cout << query(x, y + 1, 0, 0, n) << endl;
else
update(x, y);
}
return 0;
}
|
#include <algorithm>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define REP(i, j) for (int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for (int i = (int)(j); i < (int)(k); ++i)
#define P pair<int, int>
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
const int MAX_N = 200001;
const int INF = 2147483647;
// 要素の数、区間などを何からの値を覚えておく配列
int n, q, v[2 * MAX_N - 1];
// 初期化
void init(int _n) {
n = 1;
while (n < _n)
n *= 2;
REP(i, 2 * n - 1) v[i] = INF;
}
// 添字がkのものをaに更新
void update(int k, int a) {
k += n - 1;
v[k] = a;
while (k > 0) {
k = (k - 1) / 2;
v[k] = min(v[k * 2 + 1], v[k * 2 + 2]);
}
}
// [a, b)の最小値を求める
// kは今見ている添字、l, rはそれに対応する[l, r)
// 呼び出す時はquery(a, b, 0, 0, n)として呼ぶ
int query(int a, int b, int k, int l, int r) {
if (b <= l || a >= r)
return INF;
if (a <= l && b >= r)
return v[k];
int vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
int vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
return min(vl, vr);
}
int main() {
cin >> n >> q;
init(n);
REP(i, q) {
int Q, x, y;
cin >> Q >> x >> y;
if (Q)
cout << query(x, y + 1, 0, 0, n) << endl;
else
update(x, y);
}
return 0;
}
|
replace
| 25 | 26 | 25 | 26 |
0
| |
p02346
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
#define REP(i, a, b) for (int i = (a); i < (b); i++)
#define RREP(i, a, b) for (int i = (a); i >= (b); i--)
#define pq priority_queue
#define P pair<int, int>
#define P2 pair<int, P>
#define P3 pair<int, P2>
typedef long long ll;
typedef long double ld;
using namespace std;
const int INF = 1e9, MOD = 1e9 + 7,
around[] = {0, 1, 1, -1, -1, 0, -1, 1, 0, 0};
const int vx[] = {1, 0, -1, 0}, vy[] = {0, 1, 0, -1};
const ll LINF = 1e18;
const ld PI = abs(acos(-1));
const int sqrtN = 512;
struct SqrtDecomposition {
vector<int> data;
vector<int> bucketSum;
int N, K;
SqrtDecomposition(int n) : N(n) {
K = (N + sqrtN - 1) / sqrtN;
data.assign(n, 0);
bucketSum.assign(K, 0);
}
void add(int k, int x) {
data[k] += x;
k = k / sqrtN;
int sum = 0;
for (int i = k * sqrtN; i < (k + 1) * sqrtN; i++) {
sum += data[i];
}
bucketSum[k] = sum;
}
int get(int a, int b) {
int sum = 0;
for (int k = 0; k < K; k++) {
int l = k * sqrtN, r = (k + 1) * sqrtN;
if (b <= l || r <= a)
continue;
if (a <= l && r <= b)
sum += bucketSum[k];
else {
for (int i = max(a, l); i < min(b, r); i++) {
sum += data[i];
}
}
}
return sum;
}
};
int main() {
int n, q;
cin >> n >> q;
SqrtDecomposition data(n);
REP(i, 0, q) {
int a, b, c;
cin >> a >> b >> c;
if (a == 0)
data.add(b, c);
else if (a == 1)
cout << data.get(b, c + 1) << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
#define REP(i, a, b) for (int i = (a); i < (b); i++)
#define RREP(i, a, b) for (int i = (a); i >= (b); i--)
#define pq priority_queue
#define P pair<int, int>
#define P2 pair<int, P>
#define P3 pair<int, P2>
typedef long long ll;
typedef long double ld;
using namespace std;
const int INF = 1e9, MOD = 1e9 + 7,
around[] = {0, 1, 1, -1, -1, 0, -1, 1, 0, 0};
const int vx[] = {1, 0, -1, 0}, vy[] = {0, 1, 0, -1};
const ll LINF = 1e18;
const ld PI = abs(acos(-1));
const int sqrtN = 512;
struct SqrtDecomposition {
vector<int> data;
vector<int> bucketSum;
int N, K;
SqrtDecomposition(int n) : N(n) {
K = (N + sqrtN - 1) / sqrtN;
data.assign(K * sqrtN, 0);
bucketSum.assign(K, 0);
}
void add(int k, int x) {
data[k] += x;
k = k / sqrtN;
int sum = 0;
for (int i = k * sqrtN; i < (k + 1) * sqrtN; i++) {
sum += data[i];
}
bucketSum[k] = sum;
}
int get(int a, int b) {
int sum = 0;
for (int k = 0; k < K; k++) {
int l = k * sqrtN, r = (k + 1) * sqrtN;
if (b <= l || r <= a)
continue;
if (a <= l && r <= b)
sum += bucketSum[k];
else {
for (int i = max(a, l); i < min(b, r); i++) {
sum += data[i];
}
}
}
return sum;
}
};
int main() {
int n, q;
cin >> n >> q;
SqrtDecomposition data(n);
REP(i, 0, q) {
int a, b, c;
cin >> a >> b >> c;
if (a == 0)
data.add(b, c);
else if (a == 1)
cout << data.get(b, c + 1) << endl;
}
return 0;
}
|
replace
| 24 | 25 | 24 | 25 |
0
| |
p02346
|
C++
|
Runtime Error
|
#include <algorithm>
#include <iostream>
using namespace std;
#define N 131072
int tree[N * 2];
int getsum(int a, int b, int k, int l, int r) {
if (r <= a || b <= l)
return 0;
if (a <= l && r <= b)
return tree[k];
int m = (r) / 2;
return getsum(a, b, k * 2 + 1, l, m) + getsum(a, b, k * 2 + 2, m, r);
}
void add(int a, int b) {
a += N - 1;
tree[a] += b;
while (a) {
a = (a - 1) / 2;
tree[a] += b;
}
}
int main() {
int n, q;
int c, a, b;
cin >> n >> q;
for (int i = 0; i < q; i++) {
cin >> c >> a >> b;
a--;
if (c == 1)
cout << getsum(a, b, 0, 0, N) << endl;
else
add(a, b);
}
return 0;
}
|
#include <algorithm>
#include <iostream>
using namespace std;
#define N 131072
int tree[N * 2];
int getsum(int a, int b, int k, int l, int r) {
if (r <= a || b <= l)
return 0;
if (a <= l && r <= b)
return tree[k];
int m = (l + r) / 2;
return getsum(a, b, k * 2 + 1, l, m) + getsum(a, b, k * 2 + 2, m, r);
}
void add(int a, int b) {
a += N - 1;
tree[a] += b;
while (a) {
a = (a - 1) / 2;
tree[a] += b;
}
}
int main() {
int n, q;
int c, a, b;
cin >> n >> q;
for (int i = 0; i < q; i++) {
cin >> c >> a >> b;
a--;
if (c == 1)
cout << getsum(a, b, 0, 0, N) << endl;
else
add(a, b);
}
return 0;
}
|
replace
| 10 | 11 | 10 | 11 |
0
| |
p02346
|
C++
|
Runtime Error
|
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef pair<int, pii> pip;
typedef vector<pip> vip;
const double PI = acos(-1);
const double EPS = 1e-8;
const double inf = 1e8;
template <class T> struct Treap {
struct node_t {
T val;
T sum;
node_t *lch, *rch;
int pri;
int cnt;
node_t(T v, int p) : val(v), pri(p), cnt(1), sum(v) { lch = rch = NULL; }
};
node_t *root;
Treap() : root(NULL) {}
int count(node_t *t) { return t ? t->cnt : 0; }
T sum(node_t *t) {
return t ? t->sum : 0;
} //-------------------!!!!!!!!!!!!!!!!!!!
int size() { return count(root); }
T combine(T a, T b) { return a + b; }
node_t *update(node_t *t) {
t->cnt = count(t->lch) + count(t->rch) + 1;
t->sum = combine(combine(sum(t->lch), sum(t->rch)), t->val);
return t;
}
node_t *merge(node_t *l, node_t *r) {
if (!l || !r)
return l ? l : r;
if (l->pri > r->pri) {
l->rch = merge(l->rch, r);
return update(l);
} else {
r->lch = merge(l, r->lch);
return update(r);
}
}
pair<node_t *, node_t *> split(node_t *t, int k) {
if (!t)
return mp((node_t *)NULL, (node_t *)NULL);
if (k <= count(t->lch)) {
pair<node_t *, node_t *> s = split(t->lch, k);
t->lch = s.second;
return mp(s.first, update(t));
} else {
pair<node_t *, node_t *> s = split(t->rch, k - count(t->lch) - 1);
t->rch = s.first;
return mp(update(t), s.second);
}
}
node_t *insert(node_t *t, int k, T val, int pri) {
pair<node_t *, node_t *> s = split(t, k);
t = merge(s.first, new node_t(val, pri));
t = merge(t, s.second);
return update(t);
}
node_t *erase(node_t *t, int k) {
pair<node_t *, node_t *> s1, s2;
s2 = split(t, k + 1);
s1 = split(s2.first, k);
t = merge(s1.first, s2.second);
return update(t);
}
node_t *find(node_t *t, int k) {
int c = count(t->lch);
if (k < c)
return find(t->lch, k);
if (k == c)
return t;
return find(t->rch, k - c - 1);
}
void shift(int l, int r) {
pair<node_t *, node_t *> a, b, c;
c = split(root, r);
b = split(c.first, r - 1);
a = split(b.first, l);
root = merge(a.first, b.second);
root = merge(root, a.second);
root = merge(root, c.second);
}
int query(int l, int r, node_t *t) {
int sz = count(t);
if (sz <= l || r <= 0)
return 0; //------------!!!!!!!!!!!!!!!
if (l <= 0 && r >= sz)
return sum(t);
int c = count(t->lch);
int lv = query(l, r, t->lch);
int rv = query(l - c - 1, r - c - 1, t->rch);
int res = combine(lv, rv);
if (l <= c && c < r)
res = combine(res, t->val);
return res;
}
void insert(int k, T val) { root = insert(root, k, val, rand()); }
void erase(int k) { root = erase(root, k); }
node_t *find(int k) { return find(root, k); }
int query(int l, int r) { return query(l, r, root); }
};
int main() {
srand(time(NULL));
int n, q;
cin >> n >> q;
Treap<int> t;
// t.insert(inf,0);
rep(i, n) t.insert(i, 0);
while (q--) {
int a, b, c;
cin >> a >> b >> c;
if (a == 1) {
b--;
c--;
cout << t.query(b, c + 1) << endl;
} else {
b--;
int r = t.find(b)->val;
t.erase(b);
t.insert(b, r + c);
}
}
}
|
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef pair<int, pii> pip;
typedef vector<pip> vip;
const double PI = acos(-1);
const double EPS = 1e-8;
const double inf = 1e8;
template <class T> struct Treap {
struct node_t {
T val;
T sum;
node_t *lch, *rch;
int pri;
int cnt;
node_t(T v, int p) : val(v), pri(p), cnt(1), sum(v) { lch = rch = NULL; }
};
node_t *root;
Treap() : root(NULL) {}
int count(node_t *t) { return t ? t->cnt : 0; }
T sum(node_t *t) {
return t ? t->sum : 0;
} //-------------------!!!!!!!!!!!!!!!!!!!
int size() { return count(root); }
T combine(T a, T b) { return a + b; }
node_t *update(node_t *t) {
t->cnt = count(t->lch) + count(t->rch) + 1;
t->sum = combine(combine(sum(t->lch), sum(t->rch)), t->val);
return t;
}
node_t *merge(node_t *l, node_t *r) {
if (!l || !r)
return l ? l : r;
if (l->pri > r->pri) {
l->rch = merge(l->rch, r);
return update(l);
} else {
r->lch = merge(l, r->lch);
return update(r);
}
}
pair<node_t *, node_t *> split(node_t *t, int k) {
if (!t)
return mp((node_t *)NULL, (node_t *)NULL);
if (k <= count(t->lch)) {
pair<node_t *, node_t *> s = split(t->lch, k);
t->lch = s.second;
return mp(s.first, update(t));
} else {
pair<node_t *, node_t *> s = split(t->rch, k - count(t->lch) - 1);
t->rch = s.first;
return mp(update(t), s.second);
}
}
node_t *insert(node_t *t, int k, T val, int pri) {
pair<node_t *, node_t *> s = split(t, k);
t = merge(s.first, new node_t(val, pri));
t = merge(t, s.second);
return update(t);
}
node_t *erase(node_t *t, int k) {
pair<node_t *, node_t *> s1, s2;
s2 = split(t, k + 1);
s1 = split(s2.first, k);
t = merge(s1.first, s2.second);
return update(t);
}
node_t *find(node_t *t, int k) {
int c = count(t->lch);
if (k < c)
return find(t->lch, k);
if (k == c)
return t;
return find(t->rch, k - c - 1);
}
void shift(int l, int r) {
pair<node_t *, node_t *> a, b, c;
c = split(root, r);
b = split(c.first, r - 1);
a = split(b.first, l);
root = merge(a.first, b.second);
root = merge(root, a.second);
root = merge(root, c.second);
}
int query(int l, int r, node_t *t) {
int sz = count(t);
if (sz <= l || r <= 0)
return 0; //------------!!!!!!!!!!!!!!!
if (l <= 0 && r >= sz)
return sum(t);
int c = count(t->lch);
int lv = query(l, r, t->lch);
int rv = query(l - c - 1, r - c - 1, t->rch);
int res = combine(lv, rv);
if (l <= c && c < r)
res = combine(res, t->val);
return res;
}
void insert(int k, T val) { root = insert(root, k, val, rand()); }
void erase(int k) { root = erase(root, k); }
node_t *find(int k) { return find(root, k); }
int query(int l, int r) { return query(l, r, root); }
};
int main() {
srand(time(NULL));
int n, q;
cin >> n >> q;
Treap<int> t;
t.insert(inf, 0);
rep(i, n) t.insert(i, 0);
while (q--) {
int a, b, c;
cin >> a >> b >> c;
if (a == 1) {
b--;
c--;
cout << t.query(b, c + 1) << endl;
} else {
b--;
int r = t.find(b)->val;
t.erase(b);
t.insert(b, r + c);
}
}
}
|
replace
| 137 | 138 | 137 | 138 |
0
| |
p02346
|
C++
|
Runtime Error
|
#include <algorithm> // require sort next_permutation count __gcd reverse etc.
#include <cctype> // require tolower, toupper
#include <cfloat>
#include <climits>
#include <cmath> // require fabs
#include <cstdio> // require scanf printf
#include <cstdlib> // require abs exit atof atoi
#include <cstring> // require memset
#include <ctime> // require srand
#include <deque>
#include <fstream> // require freopen
#include <functional>
#include <iomanip> // require setw
#include <iostream>
#include <limits>
#include <map>
#include <numeric> // require accumulate
#include <queue>
#include <set>
#include <sstream> // require stringstream
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(A) A.begin(), A.end()
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int MAX_N = 10005;
int bit[MAX_N], n;
void add(int i, int x) {
while (i <= n) {
bit[i] += x;
i += i & (-i);
} // end while
}
int sum(int i) {
int s = 0;
while (i > 0) {
s += bit[i];
i -= i & (-i);
} // end while
return s;
}
void init_bit(void) {
n = 0;
memset(bit, 0, sizeof(bit));
}
int main() {
init_bit();
int q;
scanf("%d %d", &n, &q);
rep(i, q) {
int com, x, y;
scanf("%d %d %d", &com, &x, &y);
if (com == 0) {
add(x, y);
} else {
int s = sum(y);
int t = sum(x - 1);
int res = s - t;
printf("%d\n", res);
} // end if
} // end rep
return 0;
}
|
#include <algorithm> // require sort next_permutation count __gcd reverse etc.
#include <cctype> // require tolower, toupper
#include <cfloat>
#include <climits>
#include <cmath> // require fabs
#include <cstdio> // require scanf printf
#include <cstdlib> // require abs exit atof atoi
#include <cstring> // require memset
#include <ctime> // require srand
#include <deque>
#include <fstream> // require freopen
#include <functional>
#include <iomanip> // require setw
#include <iostream>
#include <limits>
#include <map>
#include <numeric> // require accumulate
#include <queue>
#include <set>
#include <sstream> // require stringstream
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(A) A.begin(), A.end()
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int MAX_N = 100005;
int bit[MAX_N], n;
void add(int i, int x) {
while (i <= n) {
bit[i] += x;
i += i & (-i);
} // end while
}
int sum(int i) {
int s = 0;
while (i > 0) {
s += bit[i];
i -= i & (-i);
} // end while
return s;
}
void init_bit(void) {
n = 0;
memset(bit, 0, sizeof(bit));
}
int main() {
init_bit();
int q;
scanf("%d %d", &n, &q);
rep(i, q) {
int com, x, y;
scanf("%d %d %d", &com, &x, &y);
if (com == 0) {
add(x, y);
} else {
int s = sum(y);
int t = sum(x - 1);
int res = s - t;
printf("%d\n", res);
} // end if
} // end rep
return 0;
}
|
replace
| 31 | 32 | 31 | 32 |
0
| |
p02346
|
C++
|
Runtime Error
|
#include <algorithm>
#include <iostream>
#include <vector>
#define MAX 100000
using namespace std;
vector<int> S(MAX, 0);
int N;
void update(int i, int x) {
i += (N - 1);
S[i] += x;
while (i > 0) {
i = (i - 1) / 2;
S[i] += x;
}
}
int find(int s, int t, int k, int l, int r) {
if (r <= s || t <= l)
return 0;
if (s <= l && r <= t)
return S[k];
int u, v;
u = find(s, t, k * 2 + 1, l, (l + r) / 2);
v = find(s, t, k * 2 + 2, (l + r) / 2, r);
return u + v;
}
int main() {
int n, q;
cin >> n >> q;
N = 1;
while (N < n)
N *= 2;
for (int i = 0; i < q; i++) {
int com, x, y;
cin >> com >> x >> y;
if (com == 0) {
update(x - 1, y);
} else {
int ret = find(x - 1, y, 0, 0, N);
cout << ret << endl;
}
}
return 0;
}
|
#include <algorithm>
#include <iostream>
#include <vector>
#define MAX 100000
using namespace std;
vector<int> S(MAX * 4, 0);
int N;
void update(int i, int x) {
i += (N - 1);
S[i] += x;
while (i > 0) {
i = (i - 1) / 2;
S[i] += x;
}
}
int find(int s, int t, int k, int l, int r) {
if (r <= s || t <= l)
return 0;
if (s <= l && r <= t)
return S[k];
int u, v;
u = find(s, t, k * 2 + 1, l, (l + r) / 2);
v = find(s, t, k * 2 + 2, (l + r) / 2, r);
return u + v;
}
int main() {
int n, q;
cin >> n >> q;
N = 1;
while (N < n)
N *= 2;
for (int i = 0; i < q; i++) {
int com, x, y;
cin >> com >> x >> y;
if (com == 0) {
update(x - 1, y);
} else {
int ret = find(x - 1, y, 0, 0, N);
cout << ret << endl;
}
}
return 0;
}
|
replace
| 8 | 9 | 8 | 9 |
0
| |
p02346
|
C++
|
Time Limit Exceeded
|
#include <algorithm>
#include <array>
#include <cstdint>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <utility>
#include <vector>
#define INF 1000000000
#define MOD 1000000007
#define rep(i, a, b) for (uint32 i = (a); i < (b); ++i)
#define bitget(a, b) (((a) >> (b)) & 1)
#define ALL(x) (x).begin(), (x).end()
#define C(x) std::cout << #x << " : " << x << std::endl
#define scanf scanf_s
using int32 = std::int_fast32_t;
using int64 = std::int_fast64_t;
using uint32 = std::uint_fast32_t;
using uint64 = std::uint_fast64_t;
template <typename Monoid, typename Operand> struct RBnode {
RBnode *ch[2], *par;
Monoid value, sum;
Operand lazy;
uint32 size, height;
bool color, reversed; // true=black
};
template <typename Monoid, typename Operand> class RBTrees {
using Node = RBnode<Monoid, Operand>;
public:
std::vector<Node> nodes;
uint32 itr;
bool balanced;
Monoid insdata;
RBTrees(size_t maxsize) {
nodes.resize(maxsize + 1);
itr = 0;
nodes[0].ch[0] = &nodes[0];
nodes[0].ch[1] = &nodes[0];
nodes[0].par = nullptr;
nodes[0].size = 0;
nodes[0].height = 1;
nodes[0].color = 1;
}
Node *nil(void) { return &nodes[0]; }
Node *create(Monoid &value) {
nodes[++itr].ch[0] = &nodes[0];
nodes[itr].ch[1] = &nodes[0];
nodes[itr].par = nullptr;
nodes[itr].value = value;
nodes[itr].sum = value;
nodes[itr].lazy.inactive();
nodes[itr].size = 1;
nodes[itr].height = 2;
nodes[itr].color = 1;
nodes[itr].reversed = 0;
return &nodes[itr];
}
Node *update(Node *node) {
node->sum = node->value;
node->height = node->color + 1;
if (node->ch[0] != &nodes[0]) {
push(node->ch[0]);
node->sum = node->ch[0]->sum + node->sum;
node->height = node->ch[0]->height + node->color;
}
if (node->ch[1] != &nodes[0]) {
push(node->ch[1]);
node->sum = node->sum + node->ch[1]->sum;
node->height = node->ch[1]->height + node->color;
}
node->size = node->ch[0]->size + 1 + node->ch[1]->size;
node->ch[0]->par = node;
node->ch[1]->par = node;
return node;
}
Node *push(Node *node) {
node->ch[0]->lazy = node->ch[0]->lazy * node->lazy;
node->ch[1]->lazy = node->ch[1]->lazy * node->lazy;
node->value = node->value * node->lazy;
node->sum = node->sum * node->lazy;
node->lazy.inactive();
if (node->reversed) {
node->ch[0]->reversed ^= 1;
node->ch[1]->reversed ^= 1;
std::swap(node->ch[0], node->ch[1]);
node->sum.reverse();
node->value.reverse();
node->reversed = 0;
}
return node;
}
Node *rotate(Node *node, uint32 dir) {
Node *t = node->ch[dir ^ 1];
node->ch[dir ^ 1] = t->ch[dir];
t->color = node->color;
node->color = 0;
t->ch[dir] = update(node);
t->par = nullptr;
return update(t);
}
Node *insert(Node *root, const Monoid &value, uint32 position) {
insdata = value;
root = _subins(root, position);
if (!root->color) {
root->height += 1;
root->color = 1;
}
return root;
}
Node *_subins(Node *node, uint32 pos) {
if (node == &nodes[0]) {
create(insdata);
nodes[itr].color = 0;
nodes[itr].height = 1;
balanced = 1;
return &nodes[itr];
}
push(node);
if (node->ch[0]->size < pos) {
node->ch[1] = _subins(node->ch[1], pos - node->ch[0]->size - 1);
if (balanced) {
return _inbal(update(node), 1);
}
} else {
node->ch[0] = _subins(node->ch[0], pos);
if (balanced) {
return _inbal(update(node), 0);
}
}
return update(node);
}
Node *flip(Node *node) {
node->ch[0]->color = 1;
node->ch[0]->height += 1;
node->ch[1]->color = 1;
node->ch[1]->height += 1;
node->color = 0;
return node;
}
Node *_inbal(Node *node, uint32 dir) {
if (!node->color) {
return node;
}
if (!node->ch[dir]->ch[dir ^ 1]->color) {
node->ch[dir] = rotate(node->ch[dir], dir);
}
if (node->ch[dir]->ch[dir]->color) {
balanced = 0;
return node;
}
if (node->ch[dir ^ 1]->color) {
balanced = 0;
return rotate(node, dir ^ 1);
}
return flip(node);
}
Node *access(Node *root, uint32 position) {
while (1) {
if (position > root->ch[0]->size) {
position -= root->ch[0]->size + 1;
root = root->ch[1];
} else if (position < root->ch[0]->size) {
root = root->ch[0];
} else {
return root;
}
}
}
Node *erase(Node *root, uint32 position) {
push(root);
if (root->ch[0]->size > position) {
root->ch[0] = erase(root->ch[0], position);
if (balanced) {
return _erbal(update(root), 0);
}
return update(root);
} else if (root->ch[0]->size < position) {
root->ch[1] = erase(root->ch[1], position - root->ch[0]->size - 1);
if (balanced) {
return _erbal(update(root), 1);
}
return update(root);
}
if (root->ch[0] != &nodes[0]) {
if (root->ch[1] != &nodes[0]) {
root->ch[1] = erase(root->ch[1], 0);
root->value = insdata;
if (balanced) {
return _erbal(update(root), 1);
}
return update(root);
}
root->ch[0]->color = 1;
++root->ch[0]->height;
balanced = 0;
return root->ch[0];
} else {
insdata = root->value;
if (root->ch[1] != &nodes[0]) {
root->ch[1]->color = 1;
++root->ch[1]->height;
balanced = 0;
return root->ch[1];
}
balanced = root->color;
return &nodes[0];
}
}
Node *_erbal(Node *node, uint32 dir) {
if (node->ch[dir ^ 1]->ch[0]->color && node->ch[dir ^ 1]->ch[1]->color) {
node->height = node->ch[dir ^ 1]->height + node->color;
if (node->ch[dir ^ 1]->color) {
if (node->color) {
node->ch[dir ^ 1]->color = 0;
node->ch[dir ^ 1]->height -= 1;
node->height -= 1;
return node;
}
node->ch[dir ^ 1]->color = 0;
node->ch[dir ^ 1]->height -= 1;
node->color = 1;
balanced = 0;
return node;
}
node = rotate(node, dir);
node->ch[dir] = _erbal(node->ch[dir], dir);
}
balanced = 0;
if (!node->ch[dir ^ 1]->ch[dir]->color) {
node->ch[dir ^ 1] = rotate(node->ch[dir ^ 1], dir ^ 1);
}
node = rotate(node, dir);
++node->height;
++node->ch[0]->height;
node->ch[0]->color = 1;
++node->ch[1]->height;
node->ch[1]->color = 1;
return node;
}
Monoid range(Node *root, uint32 begin, uint32 end) {
Node *t = root;
Monoid ret;
while (1) {
if (t->ch[0]->size < begin) {
begin -= t->ch[0]->size + 1;
end -= t->ch[0]->size + 1;
t = t->ch[1];
continue;
}
if (t->ch[0]->size < end) {
end -= t->ch[0]->size + 1;
ret = t->value;
root = t->ch[0];
t = t->ch[1];
break;
}
t = t->ch[0];
}
while (root != &nodes[0]) {
if (root->ch[0]->size < begin) {
begin -= root->ch[0]->size + 1;
root = root->ch[1];
continue;
}
if (root->ch[1] != &nodes[0])
ret = (root->value + root->ch[1]->sum) + ret;
else
ret = root->value + ret;
root = root->ch[0];
}
while (t != &nodes[0]) {
if (t->ch[0]->size < end) {
end -= t->ch[0]->size + 1;
if (t->ch[0] != &nodes[0])
ret = ret + (t->ch[0]->sum + t->value);
else
ret = ret + t->value;
t = t->ch[1];
continue;
}
t = t->ch[0];
}
return ret;
}
void show(Node *node) {
std::cout << "(";
if (node->ch[0] != &nodes[0]) {
show(node->ch[0]);
std::cout << "←";
}
std::cout << node->value.e << " " << node->color << " " << node->height;
if (node->ch[1] != &nodes[0]) {
std::cout << "→";
show(node->ch[1]);
}
std::cout << ")";
return;
}
};
struct SUB {
int64 e;
bool a;
SUB() { set(); }
SUB(int64 x, bool y) {
e = x;
a = y;
}
void set() { a = false; }
void inactive(void) {
a = false;
return;
}
SUB operator*(const SUB &other) {
if (other.a)
return other;
return *this;
}
};
struct SUM {
int64 e;
int64 s;
SUM() { set(); }
SUM(int64 x, int64 y) {
e = x;
s = y;
}
void set() {
e = 0;
s = 1;
}
void reverse(void) { return; }
SUM operator+(const SUM &other) { return SUM(e + other.e, s + other.s); };
SUM operator*(const SUB &other) {
if (other.a)
return SUM(s * other.e, s);
return *this;
}
};
int main(void) {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
uint32 n, q;
std::cin >> n >> q;
RBTrees<SUM, SUB> T(n + q);
RBnode<SUM, SUB> *r = T.nil();
int64 com, y;
uint32 x;
rep(i, 0, n) { r = T.insert(r, SUM(0, 1), 0); }
// T.show(r);
// std::cout << "\n";
rep(i, 0, q) {
std::cin >> com >> x >> y;
if (com == 0) {
y = y + T.access(r, x - 1)->value.e;
r = T.erase(r, x - 1);
// T.show(r);
// std::cout << "\n";
r = T.insert(r, SUM(y, 1), x - 1);
// T.show(r);
// std::cout << "\n";
} else {
std::cout << T.range(r, x - 1, y).e << "\n";
}
}
return 0;
}
|
#include <algorithm>
#include <array>
#include <cstdint>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <utility>
#include <vector>
#define INF 1000000000
#define MOD 1000000007
#define rep(i, a, b) for (uint32 i = (a); i < (b); ++i)
#define bitget(a, b) (((a) >> (b)) & 1)
#define ALL(x) (x).begin(), (x).end()
#define C(x) std::cout << #x << " : " << x << std::endl
#define scanf scanf_s
using int32 = std::int_fast32_t;
using int64 = std::int_fast64_t;
using uint32 = std::uint_fast32_t;
using uint64 = std::uint_fast64_t;
template <typename Monoid, typename Operand> struct RBnode {
RBnode *ch[2], *par;
Monoid value, sum;
Operand lazy;
uint32 size, height;
bool color, reversed; // true=black
};
template <typename Monoid, typename Operand> class RBTrees {
using Node = RBnode<Monoid, Operand>;
public:
std::vector<Node> nodes;
uint32 itr;
bool balanced;
Monoid insdata;
RBTrees(size_t maxsize) {
nodes.resize(maxsize + 1);
itr = 0;
nodes[0].ch[0] = &nodes[0];
nodes[0].ch[1] = &nodes[0];
nodes[0].par = nullptr;
nodes[0].size = 0;
nodes[0].height = 1;
nodes[0].color = 1;
}
Node *nil(void) { return &nodes[0]; }
Node *create(Monoid &value) {
nodes[++itr].ch[0] = &nodes[0];
nodes[itr].ch[1] = &nodes[0];
nodes[itr].par = nullptr;
nodes[itr].value = value;
nodes[itr].sum = value;
nodes[itr].lazy.inactive();
nodes[itr].size = 1;
nodes[itr].height = 2;
nodes[itr].color = 1;
nodes[itr].reversed = 0;
return &nodes[itr];
}
Node *update(Node *node) {
node->sum = node->value;
node->height = node->color + 1;
if (node->ch[0] != &nodes[0]) {
push(node->ch[0]);
node->sum = node->ch[0]->sum + node->sum;
node->height = node->ch[0]->height + node->color;
}
if (node->ch[1] != &nodes[0]) {
push(node->ch[1]);
node->sum = node->sum + node->ch[1]->sum;
node->height = node->ch[1]->height + node->color;
}
node->size = node->ch[0]->size + 1 + node->ch[1]->size;
node->ch[0]->par = node;
node->ch[1]->par = node;
return node;
}
Node *push(Node *node) {
node->ch[0]->lazy = node->ch[0]->lazy * node->lazy;
node->ch[1]->lazy = node->ch[1]->lazy * node->lazy;
node->value = node->value * node->lazy;
node->sum = node->sum * node->lazy;
node->lazy.inactive();
if (node->reversed) {
node->ch[0]->reversed ^= 1;
node->ch[1]->reversed ^= 1;
std::swap(node->ch[0], node->ch[1]);
node->sum.reverse();
node->value.reverse();
node->reversed = 0;
}
return node;
}
Node *rotate(Node *node, uint32 dir) {
Node *t = node->ch[dir ^ 1];
node->ch[dir ^ 1] = t->ch[dir];
t->color = node->color;
node->color = 0;
t->ch[dir] = update(node);
t->par = nullptr;
return update(t);
}
Node *insert(Node *root, const Monoid &value, uint32 position) {
insdata = value;
root = _subins(root, position);
if (!root->color) {
root->height += 1;
root->color = 1;
}
return root;
}
Node *_subins(Node *node, uint32 pos) {
if (node == &nodes[0]) {
create(insdata);
nodes[itr].color = 0;
nodes[itr].height = 1;
balanced = 1;
return &nodes[itr];
}
push(node);
if (node->ch[0]->size < pos) {
node->ch[1] = _subins(node->ch[1], pos - node->ch[0]->size - 1);
if (balanced) {
return _inbal(update(node), 1);
}
} else {
node->ch[0] = _subins(node->ch[0], pos);
if (balanced) {
return _inbal(update(node), 0);
}
}
return update(node);
}
Node *flip(Node *node) {
node->ch[0]->color = 1;
node->ch[0]->height += 1;
node->ch[1]->color = 1;
node->ch[1]->height += 1;
node->color = 0;
return node;
}
Node *_inbal(Node *node, uint32 dir) {
if (!node->color) {
return node;
}
if (!node->ch[dir]->ch[dir ^ 1]->color) {
node->ch[dir] = rotate(node->ch[dir], dir);
}
if (node->ch[dir]->ch[dir]->color) {
balanced = 0;
return node;
}
if (node->ch[dir ^ 1]->color) {
balanced = 0;
return rotate(node, dir ^ 1);
}
return flip(node);
}
Node *access(Node *root, uint32 position) {
while (1) {
if (position > root->ch[0]->size) {
position -= root->ch[0]->size + 1;
root = root->ch[1];
} else if (position < root->ch[0]->size) {
root = root->ch[0];
} else {
return root;
}
}
}
Node *erase(Node *root, uint32 position) {
push(root);
if (root->ch[0]->size > position) {
root->ch[0] = erase(root->ch[0], position);
if (balanced) {
return _erbal(update(root), 0);
}
return update(root);
} else if (root->ch[0]->size < position) {
root->ch[1] = erase(root->ch[1], position - root->ch[0]->size - 1);
if (balanced) {
return _erbal(update(root), 1);
}
return update(root);
}
if (root->ch[0] != &nodes[0]) {
if (root->ch[1] != &nodes[0]) {
root->ch[1] = erase(root->ch[1], 0);
root->value = insdata;
if (balanced) {
return _erbal(update(root), 1);
}
return update(root);
}
root->ch[0]->color = 1;
++root->ch[0]->height;
balanced = 0;
return root->ch[0];
} else {
insdata = root->value;
if (root->ch[1] != &nodes[0]) {
root->ch[1]->color = 1;
++root->ch[1]->height;
balanced = 0;
return root->ch[1];
}
balanced = root->color;
return &nodes[0];
}
}
Node *_erbal(Node *node, uint32 dir) {
if (node->ch[dir ^ 1]->ch[0]->color && node->ch[dir ^ 1]->ch[1]->color) {
node->height = node->ch[dir ^ 1]->height + node->color;
if (node->ch[dir ^ 1]->color) {
if (node->color) {
node->ch[dir ^ 1]->color = 0;
node->ch[dir ^ 1]->height -= 1;
node->height -= 1;
return node;
}
node->ch[dir ^ 1]->color = 0;
node->ch[dir ^ 1]->height -= 1;
node->color = 1;
balanced = 0;
return node;
}
node = rotate(node, dir);
node->ch[dir] = _erbal(node->ch[dir], dir);
node->height = node->ch[dir]->height + node->color;
return node;
}
balanced = 0;
if (!node->ch[dir ^ 1]->ch[dir]->color) {
node->ch[dir ^ 1] = rotate(node->ch[dir ^ 1], dir ^ 1);
}
node = rotate(node, dir);
++node->height;
++node->ch[0]->height;
node->ch[0]->color = 1;
++node->ch[1]->height;
node->ch[1]->color = 1;
return node;
}
Monoid range(Node *root, uint32 begin, uint32 end) {
Node *t = root;
Monoid ret;
while (1) {
if (t->ch[0]->size < begin) {
begin -= t->ch[0]->size + 1;
end -= t->ch[0]->size + 1;
t = t->ch[1];
continue;
}
if (t->ch[0]->size < end) {
end -= t->ch[0]->size + 1;
ret = t->value;
root = t->ch[0];
t = t->ch[1];
break;
}
t = t->ch[0];
}
while (root != &nodes[0]) {
if (root->ch[0]->size < begin) {
begin -= root->ch[0]->size + 1;
root = root->ch[1];
continue;
}
if (root->ch[1] != &nodes[0])
ret = (root->value + root->ch[1]->sum) + ret;
else
ret = root->value + ret;
root = root->ch[0];
}
while (t != &nodes[0]) {
if (t->ch[0]->size < end) {
end -= t->ch[0]->size + 1;
if (t->ch[0] != &nodes[0])
ret = ret + (t->ch[0]->sum + t->value);
else
ret = ret + t->value;
t = t->ch[1];
continue;
}
t = t->ch[0];
}
return ret;
}
void show(Node *node) {
std::cout << "(";
if (node->ch[0] != &nodes[0]) {
show(node->ch[0]);
std::cout << "←";
}
std::cout << node->value.e << " " << node->color << " " << node->height;
if (node->ch[1] != &nodes[0]) {
std::cout << "→";
show(node->ch[1]);
}
std::cout << ")";
return;
}
};
struct SUB {
int64 e;
bool a;
SUB() { set(); }
SUB(int64 x, bool y) {
e = x;
a = y;
}
void set() { a = false; }
void inactive(void) {
a = false;
return;
}
SUB operator*(const SUB &other) {
if (other.a)
return other;
return *this;
}
};
struct SUM {
int64 e;
int64 s;
SUM() { set(); }
SUM(int64 x, int64 y) {
e = x;
s = y;
}
void set() {
e = 0;
s = 1;
}
void reverse(void) { return; }
SUM operator+(const SUM &other) { return SUM(e + other.e, s + other.s); };
SUM operator*(const SUB &other) {
if (other.a)
return SUM(s * other.e, s);
return *this;
}
};
int main(void) {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
uint32 n, q;
std::cin >> n >> q;
RBTrees<SUM, SUB> T(n + q);
RBnode<SUM, SUB> *r = T.nil();
int64 com, y;
uint32 x;
rep(i, 0, n) { r = T.insert(r, SUM(0, 1), 0); }
// T.show(r);
// std::cout << "\n";
rep(i, 0, q) {
std::cin >> com >> x >> y;
if (com == 0) {
y = y + T.access(r, x - 1)->value.e;
r = T.erase(r, x - 1);
// T.show(r);
// std::cout << "\n";
r = T.insert(r, SUM(y, 1), x - 1);
// T.show(r);
// std::cout << "\n";
} else {
std::cout << T.range(r, x - 1, y).e << "\n";
}
}
return 0;
}
|
insert
| 236 | 236 | 236 | 238 |
TLE
| |
p02346
|
C++
|
Runtime Error
|
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
struct SegTree {
public:
int n;
const int MAX = 100000;
vector<int> tree;
SegTree(int n) {
tree = vector<int>(MAX, 0);
SegTree::n = 1;
while (SegTree::n < n)
SegTree::n *= 2;
}
void update(int i, int x) {
i += (n - 1);
tree[i] += x;
while (i > 0) {
i = (i - 1) / 2;
tree[i] += x;
}
}
int find(int s, int t) { return find_sub(s, t, 0, 0, n); }
private:
int find_sub(int s, int t, int k, int l, int r) {
if (r <= s || t <= l)
return 0;
if (s <= l && r <= t)
return tree[k];
int u, v;
u = find_sub(s, t, k * 2 + 1, l, (l + r) / 2);
v = find_sub(s, t, k * 2 + 2, (l + r) / 2, r);
return u + v;
}
};
int main() {
int n, q;
cin >> n >> q;
SegTree st(n);
for (int i = 0; i < q; i++) {
int com, x, y;
cin >> com >> x >> y;
if (com == 0) {
st.update(x - 1, y);
} else {
int ret = st.find(x - 1, y);
cout << ret << endl;
}
}
return 0;
}
|
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
struct SegTree {
public:
int n;
const int MAX = 100000;
vector<int> tree;
SegTree(int n) {
tree = vector<int>(MAX * 4, 0);
SegTree::n = 1;
while (SegTree::n < n)
SegTree::n *= 2;
}
void update(int i, int x) {
i += (n - 1);
tree[i] += x;
while (i > 0) {
i = (i - 1) / 2;
tree[i] += x;
}
}
int find(int s, int t) { return find_sub(s, t, 0, 0, n); }
private:
int find_sub(int s, int t, int k, int l, int r) {
if (r <= s || t <= l)
return 0;
if (s <= l && r <= t)
return tree[k];
int u, v;
u = find_sub(s, t, k * 2 + 1, l, (l + r) / 2);
v = find_sub(s, t, k * 2 + 2, (l + r) / 2, r);
return u + v;
}
};
int main() {
int n, q;
cin >> n >> q;
SegTree st(n);
for (int i = 0; i < q; i++) {
int com, x, y;
cin >> com >> x >> y;
if (com == 0) {
st.update(x - 1, y);
} else {
int ret = st.find(x - 1, y);
cout << ret << endl;
}
}
return 0;
}
|
replace
| 14 | 15 | 14 | 15 |
0
| |
p02346
|
C++
|
Time Limit Exceeded
|
#include <iostream>
using namespace std;
int n;
long long int dat[300000];
void init(int nn) {
n = 1;
while (n < nn)
n *= 2;
for (int i = 0; i < n * 2 - 1; i++) {
dat[i] = 0ll;
}
}
void add(int x, int i) {
x += n - 1;
dat[x] += i;
while (x) {
x = (x - 1) / 2;
dat[x] = dat[x * 2 + 1] + dat[x * 2 + 2];
}
}
long long int sum(int s, int t, int k, int l, int r) {
if (t <= l || r <= s)
return 0;
if (l == s && t == r)
return dat[k];
return sum(s, t, k * 2 + 1, l, (l + r) / 2) +
sum(s, t, k * 2 + 2, (l + r) / 2, r);
}
int main() {
int q;
cin >> n >> q;
init(n);
for (int i = 0; i < q; i++) {
int a, b, c;
cin >> a >> b >> c;
if (a == 0) {
add(b - 1, c);
} else {
cout << sum(b - 1, c, 0, 0, n) << endl;
}
}
return 0;
}
|
#include <iostream>
using namespace std;
int n;
long long int dat[300000];
void init(int nn) {
n = 1;
while (n < nn)
n *= 2;
for (int i = 0; i < n * 2 - 1; i++) {
dat[i] = 0ll;
}
}
void add(int x, int i) {
x += n - 1;
dat[x] += i;
while (x) {
x = (x - 1) / 2;
dat[x] = dat[x * 2 + 1] + dat[x * 2 + 2];
}
}
long long int sum(int s, int t, int k, int l, int r) {
if (t <= l || r <= s)
return 0;
if (s <= l && r <= t)
return dat[k];
return sum(s, t, k * 2 + 1, l, (l + r) / 2) +
sum(s, t, k * 2 + 2, (l + r) / 2, r);
}
int main() {
int q;
cin >> n >> q;
init(n);
for (int i = 0; i < q; i++) {
int a, b, c;
cin >> a >> b >> c;
if (a == 0) {
add(b - 1, c);
} else {
cout << sum(b - 1, c, 0, 0, n) << endl;
}
}
return 0;
}
|
replace
| 27 | 28 | 27 | 28 |
TLE
| |
p02346
|
C++
|
Runtime Error
|
#include <algorithm>
#include <bitset>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
#define VARIABLE(x) cerr << #x << "=" << x << endl
#define BINARY(x) static_cast<bitset<16>>(x);
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, m, n) for (int i = m; i < (int)(n); i++)
#define if_range(x, y, w, h) \
if (0 <= (int)(x) && (int)(x) < (int)(w) && 0 <= (int)(y) && \
(int)(y) < (int)(h))
const int INF = 100000000;
typedef double D;
const double EPS = 1e-8;
const double PI = 3.14159;
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, -1, 0, 1};
using namespace std;
typedef pair<int, int> P;
/** DSL_2 - B : RangeQuery - Range Sum Query **/
const int MAX_N = 1 << 6;
class RSQ {
public:
int n, dat[2 * MAX_N - 1];
void init(int n_) {
n = 1;
while (n < n_)
n *= 2;
for (int i = 0; i < 2 * n - 1; i++)
dat[i] = 0;
}
RSQ() {}
void add(int k, int a) {
k += n - 1;
dat[k] += a;
while (k > 0) {
k = (k - 1) / 2;
dat[k] += a;
}
}
int query(int a, int b, int k, int l, int r) {
if (r <= a || b <= l)
return 0;
if (a <= l && r <= b) {
return dat[k];
} else {
int vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
int vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
return vl + vr;
}
}
};
int main() {
int N, Q;
cin >> N >> Q;
RSQ ST;
ST.init(N);
rep(i, Q) {
int c, x, y;
cin >> c >> x >> y;
if (c) {
cout << ST.query(x - 1, y, 0, 0, ST.n) << endl;
} else {
ST.add(x - 1, y);
}
}
}
|
#include <algorithm>
#include <bitset>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
#define VARIABLE(x) cerr << #x << "=" << x << endl
#define BINARY(x) static_cast<bitset<16>>(x);
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, m, n) for (int i = m; i < (int)(n); i++)
#define if_range(x, y, w, h) \
if (0 <= (int)(x) && (int)(x) < (int)(w) && 0 <= (int)(y) && \
(int)(y) < (int)(h))
const int INF = 100000000;
typedef double D;
const double EPS = 1e-8;
const double PI = 3.14159;
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, -1, 0, 1};
using namespace std;
typedef pair<int, int> P;
/** DSL_2 - B : RangeQuery - Range Sum Query **/
const int MAX_N = 1 << 17;
class RSQ {
public:
int n, dat[2 * MAX_N - 1];
void init(int n_) {
n = 1;
while (n < n_)
n *= 2;
for (int i = 0; i < 2 * n - 1; i++)
dat[i] = 0;
}
RSQ() {}
void add(int k, int a) {
k += n - 1;
dat[k] += a;
while (k > 0) {
k = (k - 1) / 2;
dat[k] += a;
}
}
int query(int a, int b, int k, int l, int r) {
if (r <= a || b <= l)
return 0;
if (a <= l && r <= b) {
return dat[k];
} else {
int vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
int vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
return vl + vr;
}
}
};
int main() {
int N, Q;
cin >> N >> Q;
RSQ ST;
ST.init(N);
rep(i, Q) {
int c, x, y;
cin >> c >> x >> y;
if (c) {
cout << ST.query(x - 1, y, 0, 0, ST.n) << endl;
} else {
ST.add(x - 1, y);
}
}
}
|
replace
| 33 | 34 | 33 | 34 |
0
| |
p02346
|
C++
|
Runtime Error
|
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int seg[200005];
int n, q;
void add(int a, int b) {
a += n - 1;
seg[a] += b;
while (a > 0) {
a = (a - 1) / 2;
seg[a] += b;
}
}
int sum(int a, int b, int l, int m, int k) {
if (a <= l && m <= b)
return seg[k];
if (m < a || b < l)
return 0;
return sum(a, b, l, (l + m - 1) / 2, k * 2 + 1) +
sum(a, b, (l + m + 1) / 2, m, k * 2 + 2);
}
int main(void) {
cin >> n >> q;
int n1 = 1;
for (; n1 < n; n1 *= 2)
;
n = n1;
for (int i = 0; i < q; i++) {
int c, x, y;
cin >> c >> x >> y;
x--;
if (c == 0) {
add(x, y);
} else {
y--;
cout << sum(x, y, 0, n - 1, 0) << endl;
}
}
}
|
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int seg[20000000];
int n, q;
void add(int a, int b) {
a += n - 1;
seg[a] += b;
while (a > 0) {
a = (a - 1) / 2;
seg[a] += b;
}
}
int sum(int a, int b, int l, int m, int k) {
if (a <= l && m <= b)
return seg[k];
if (m < a || b < l)
return 0;
return sum(a, b, l, (l + m - 1) / 2, k * 2 + 1) +
sum(a, b, (l + m + 1) / 2, m, k * 2 + 2);
}
int main(void) {
cin >> n >> q;
int n1 = 1;
for (; n1 < n; n1 *= 2)
;
n = n1;
for (int i = 0; i < q; i++) {
int c, x, y;
cin >> c >> x >> y;
x--;
if (c == 0) {
add(x, y);
} else {
y--;
cout << sum(x, y, 0, n - 1, 0) << endl;
}
}
}
|
replace
| 7 | 8 | 7 | 8 |
0
| |
p02346
|
C++
|
Runtime Error
|
#include <iostream>
using namespace std;
int n, q, bit[100001];
void add(int a, int b) {
bit[a] += b;
while (a <= n) {
a += a & -a;
bit[a] += b;
}
}
int sum(int i) {
if (i == 0)
return 0;
return bit[i] + sum(i - (i & -i));
}
int main(void) {
cin >> n >> q;
for (int i = 0; i < q; i++) {
int c, x, y;
cin >> c >> x >> y;
if (c == 0) {
add(x, y);
} else {
cout << sum(y) - sum(x - 1) << endl;
}
}
}
|
#include <iostream>
using namespace std;
int n, q, bit[10000001];
void add(int a, int b) {
bit[a] += b;
while (a <= n) {
a += a & -a;
bit[a] += b;
}
}
int sum(int i) {
if (i == 0)
return 0;
return bit[i] + sum(i - (i & -i));
}
int main(void) {
cin >> n >> q;
for (int i = 0; i < q; i++) {
int c, x, y;
cin >> c >> x >> y;
if (c == 0) {
add(x, y);
} else {
cout << sum(y) - sum(x - 1) << endl;
}
}
}
|
replace
| 2 | 3 | 2 | 3 |
0
| |
p02346
|
C++
|
Runtime Error
|
#include <iostream>
#define MAX 250000
using namespace std;
int D[MAX];
int n = 1;
void initRSQ(int n_) {
while (n < n_)
n *= 2;
for (int i = 0; i < 2 * n - 1; i++)
D[i] = 0;
}
void add(int k, int a) {
k += n - 1;
D[k] += a;
while (k > 0) {
k = (k - 1) / 2;
D[k] = D[2 * k + 1] + D[2 * k + 2];
}
}
int query(int a, int b, int k, int l, int r) {
if (r <= a || b <= l)
return 0;
if (a <= l && r <= b)
return D[k];
int vleft, vright;
vleft = query(a, b, 2 * k + 1, l, (l + r) / 2);
vright = query(a, b, 2 * k + 2, (l + r) / 2, r);
return vleft + vright;
}
int getSum(int a, int b) { return query(a, b, 0, 0, n); }
int main() {
int q, com, x, y, n_, min;
cin >> n_ >> q;
initRSQ(n_);
for (int i = 0; i < q; i++) {
cin >> com >> x >> y;
x--;
if (com == 0)
add(x, y);
else
cout << getSum(x, y) << endl;
}
return 0;
}
|
#include <iostream>
#define MAX 300000
using namespace std;
int D[MAX];
int n = 1;
void initRSQ(int n_) {
while (n < n_)
n *= 2;
for (int i = 0; i < 2 * n - 1; i++)
D[i] = 0;
}
void add(int k, int a) {
k += n - 1;
D[k] += a;
while (k > 0) {
k = (k - 1) / 2;
D[k] = D[2 * k + 1] + D[2 * k + 2];
}
}
int query(int a, int b, int k, int l, int r) {
if (r <= a || b <= l)
return 0;
if (a <= l && r <= b)
return D[k];
int vleft, vright;
vleft = query(a, b, 2 * k + 1, l, (l + r) / 2);
vright = query(a, b, 2 * k + 2, (l + r) / 2, r);
return vleft + vright;
}
int getSum(int a, int b) { return query(a, b, 0, 0, n); }
int main() {
int q, com, x, y, n_, min;
cin >> n_ >> q;
initRSQ(n_);
for (int i = 0; i < q; i++) {
cin >> com >> x >> y;
x--;
if (com == 0)
add(x, y);
else
cout << getSum(x, y) << endl;
}
return 0;
}
|
replace
| 1 | 2 | 1 | 2 |
0
| |
p02346
|
C++
|
Runtime Error
|
#include <algorithm>
#include <iostream>
using namespace std;
template <typename T, int MAX_N> struct segment_tree_sum {
T arr[MAX_N << 1];
int N;
segment_tree_sum(int n) {
N = 1;
while (N < n)
N *= 2;
for (int i = 0; i < 2 * N - 1; ++i) {
arr[i] = (T)0;
}
}
void add(int i, T v) {
int j = i + N - 1;
arr[j] += v;
while (j > 0) {
j = (j - 1) / 2;
arr[j] += v;
}
}
T query(int a, int b) { return query(a, b, 0, 0, N); }
T query(int a, int b, int k, int l, int r) {
if (r <= a || b <= l)
return 0;
if (a <= l && r <= b)
return arr[k];
int m = (l + r) / 2;
return query(a, b, 2 * k + 1, l, m) + query(a, b, 2 * k + 2, m, r);
}
};
int N, Q;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cin >> N >> Q;
segment_tree_sum<int, 101010> segt(N + 1);
for (int j = 0; j < Q; ++j) {
int c, x, y;
cin >> c >> x >> y;
if (c == 0) {
segt.add(x, y);
} else {
cout << segt.query(x, y + 1) << endl;
}
}
return 0;
}
|
#include <algorithm>
#include <iostream>
using namespace std;
template <typename T, int MAX_N> struct segment_tree_sum {
T arr[MAX_N << 1];
int N;
segment_tree_sum(int n) {
N = 1;
while (N < n)
N *= 2;
for (int i = 0; i < 2 * N - 1; ++i) {
arr[i] = (T)0;
}
}
void add(int i, T v) {
int j = i + N - 1;
arr[j] += v;
while (j > 0) {
j = (j - 1) / 2;
arr[j] += v;
}
}
T query(int a, int b) { return query(a, b, 0, 0, N); }
T query(int a, int b, int k, int l, int r) {
if (r <= a || b <= l)
return 0;
if (a <= l && r <= b)
return arr[k];
int m = (l + r) / 2;
return query(a, b, 2 * k + 1, l, m) + query(a, b, 2 * k + 2, m, r);
}
};
int N, Q;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cin >> N >> Q;
segment_tree_sum<int, 201010> segt(N + 1);
for (int j = 0; j < Q; ++j) {
int c, x, y;
cin >> c >> x >> y;
if (c == 0) {
segt.add(x, y);
} else {
cout << segt.query(x, y + 1) << endl;
}
}
return 0;
}
|
replace
| 46 | 47 | 46 | 47 |
0
| |
p02346
|
C++
|
Runtime Error
|
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;
const int maxn = 10005;
int bit[maxn];
int n;
int get_sum(int i) {
int ret = 0;
while (i > 0) {
ret += bit[i];
i -= i & -i;
}
return ret;
}
void add(int i, int x) {
while (i <= n) {
bit[i] += x;
i += i & -i;
}
}
int main(void) {
int op, q, a, b;
string str;
ios::sync_with_stdio(false);
cin >> n >> q;
while (q--) {
cin >> op >> a >> b;
if (op == 0)
add(a, b);
else
cout << get_sum(b) - get_sum(a - 1) << endl;
}
return 0;
}
|
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;
const int maxn = 100005;
int bit[maxn];
int n;
int get_sum(int i) {
int ret = 0;
while (i > 0) {
ret += bit[i];
i -= i & -i;
}
return ret;
}
void add(int i, int x) {
while (i <= n) {
bit[i] += x;
i += i & -i;
}
}
int main(void) {
int op, q, a, b;
string str;
ios::sync_with_stdio(false);
cin >> n >> q;
while (q--) {
cin >> op >> a >> b;
if (op == 0)
add(a, b);
else
cout << get_sum(b) - get_sum(a - 1) << endl;
}
return 0;
}
|
replace
| 6 | 7 | 6 | 7 |
0
| |
p02346
|
C++
|
Time Limit Exceeded
|
#include <bits/stdc++.h>
using namespace std;
struct SegTree {
vector<int> node;
int n;
SegTree(int sz) {
n = 1;
while (sz > n)
n *= 2;
node.resize(2 * n - 1, 0);
}
void add(int p, int x) {
p += n - 1;
node[p] += x;
do {
p = (p - 1) / 2;
node[p] += x;
} while (p);
}
int get(int a, int b, int k = 0, int l = 0, int r = -1) {
if (r < 0)
r = n;
if (b <= l || r <= a)
return 0;
if (a <= l && r <= b) {
return node[k];
}
return get(a, b, k * 2 + 1, l, (l + r) / 2) +
get(a, b, k * 2 + 2, (r + l) / 2, r);
}
};
int main() {
int n, q;
cin >> n >> q;
SegTree s(n);
if (n == 1) {
int s = 0;
while (q--) {
int c, x, y;
cin >> c >> x >> y;
if (c == 0) {
s += y;
} else {
cout << s << endl;
}
}
}
while (q--) {
int c, x, y;
cin >> c >> x >> y;
if (c == 0) {
s.add(x - 1, y);
} else {
cout << s.get(x - 1, y) << endl;
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
struct SegTree {
vector<int> node;
int n;
SegTree(int sz) {
n = 1;
while (sz > n)
n *= 2;
node.resize(2 * n - 1, 0);
}
void add(int p, int x) {
p += n - 1;
node[p] += x;
do {
p = (p - 1) / 2;
node[p] += x;
} while (p);
}
int get(int a, int b, int k = 0, int l = 0, int r = -1) {
if (r < 0)
r = n;
if (b <= l || r <= a)
return 0;
if (a <= l && r <= b) {
return node[k];
}
return get(a, b, k * 2 + 1, l, (l + r) / 2) +
get(a, b, k * 2 + 2, (r + l) / 2, r);
}
};
int main() {
int n, q;
cin >> n >> q;
SegTree s(n);
if (n == 1) {
int s = 0;
while (q--) {
int c, x, y;
cin >> c >> x >> y;
if (c == 0) {
s += y;
} else {
cout << s << endl;
}
}
return 0;
}
while (q--) {
int c, x, y;
cin >> c >> x >> y;
if (c == 0) {
s.add(x - 1, y);
} else {
cout << s.get(x - 1, y) << endl;
}
}
}
|
insert
| 49 | 49 | 49 | 50 |
TLE
| |
p02346
|
C++
|
Runtime Error
|
#define _CRT_SECURE_NO_WARNINGS
#define NDEBUG
#include <array>
#include <bitset>
#include <cassert>
#include <memory>
template <::std::size_t Bitlength, class Size = ::std::size_t>
class binary_trie {
public:
using value_type = ::std::bitset<Bitlength>;
using size_type = Size;
private:
class node_type {
public:
::std::array<::std::unique_ptr<node_type>, 2> child;
typename binary_trie::size_type size;
node_type() : size(0) {}
};
using pointer = ::std::unique_ptr<node_type>;
using const_pointer = ::std::unique_ptr<const node_type>;
value_type lazy;
pointer root;
static size_type size(const node_type *const ptr) noexcept {
return ptr ? ptr->size : size_type(0);
}
public:
binary_trie() : lazy(), root() {}
bool empty() const noexcept { return !root; }
size_type size() const noexcept { return size(root.get()); }
size_type count(const value_type &x) const {
::std::size_t i = Bitlength;
const node_type *ptr = root.get();
while (ptr && i--)
ptr = ptr->child[x.test(i) ^ lazy.test(i)].get();
return size(ptr);
}
value_type find_by_order(size_type k) const {
assert(k < size());
value_type ret;
::std::size_t i = Bitlength;
const node_type *ptr = root.get();
while (i--) {
if (size(ptr->child[lazy.test(i)].get()) <= k) {
k -= size(ptr->child[lazy.test(i)].get());
ptr = ptr->child[lazy.test(i) ^ 1].get();
ret.set(i);
} else {
ptr = ptr->child[lazy.test(i)].get();
}
}
return ret;
}
size_type order_of_key(const value_type &x) const {
assert(count(x));
size_type ret = 0;
::std::size_t i = Bitlength;
const node_type *ptr = root.get();
while (i--) {
if (x.test(i) ^ lazy.test(i)) {
ret += size(ptr->child[lazy.test(i)].get());
ptr = ptr->child[lazy.test(i) ^ 1].get();
} else {
ptr = ptr->child[lazy.test(i)].get();
}
}
return ret;
}
void clear() { root.reset(); }
void insert(const value_type &x) {
::std::size_t i = Bitlength;
if (!root)
root = ::std::make_unique<node_type>();
node_type *ptr = root.get();
while (++ptr->size, i--) {
if (!ptr->child[x.test(i) ^ lazy.test(i)])
ptr->child[x.test(i) ^ lazy.test(i)] = ::std::make_unique<node_type>();
ptr = ptr->child[x.test(i) ^ lazy.test(i)].get();
}
}
void insert(const value_type &x, const size_type k) {
::std::size_t i = Bitlength;
if (!root)
root = ::std::make_unique<node_type>();
node_type *ptr = root.get();
while (ptr->size += k, i--) {
if (!ptr->child[x.test(i) ^ lazy.test(i)])
ptr->child[x.test(i) ^ lazy.test(i)] = ::std::make_unique<node_type>();
ptr = ptr->child[x.test(i) ^ lazy.test(i)].get();
}
}
void erase(const value_type &x) {
assert(count(x));
::std::size_t i = Bitlength;
node_type *ptr = root.get();
while (--ptr->size, i--)
ptr = ptr->child[x.test(i) ^ lazy.test(i)].get();
}
void erase(const value_type &x, const size_type k) {
assert(k <= count(x));
::std::size_t i = Bitlength;
node_type *ptr = root.get();
while (ptr->size -= k, i--)
ptr = ptr->child[x.test(i) ^ lazy.test(i)].get();
}
void xor_all(const value_type &x) { lazy ^= x; }
};
#include <cstdio>
int main() {
using uint = unsigned int;
uint n, q;
scanf("%u%u", &n, &q);
binary_trie<17> s;
while (n--)
s.insert(n, 0);
while (q--) {
uint com, x, y;
scanf("%u%u%u", &com, &x, &y);
if (com) {
printf("%u\n", s.order_of_key(y) - s.order_of_key(x - 1));
} else {
s.insert(x - 1, y);
}
}
return 0;
}
|
#define _CRT_SECURE_NO_WARNINGS
#define NDEBUG
#include <array>
#include <bitset>
#include <cassert>
#include <memory>
template <::std::size_t Bitlength, class Size = ::std::size_t>
class binary_trie {
public:
using value_type = ::std::bitset<Bitlength>;
using size_type = Size;
private:
class node_type {
public:
::std::array<::std::unique_ptr<node_type>, 2> child;
typename binary_trie::size_type size;
node_type() : size(0) {}
};
using pointer = ::std::unique_ptr<node_type>;
using const_pointer = ::std::unique_ptr<const node_type>;
value_type lazy;
pointer root;
static size_type size(const node_type *const ptr) noexcept {
return ptr ? ptr->size : size_type(0);
}
public:
binary_trie() : lazy(), root() {}
bool empty() const noexcept { return !root; }
size_type size() const noexcept { return size(root.get()); }
size_type count(const value_type &x) const {
::std::size_t i = Bitlength;
const node_type *ptr = root.get();
while (ptr && i--)
ptr = ptr->child[x.test(i) ^ lazy.test(i)].get();
return size(ptr);
}
value_type find_by_order(size_type k) const {
assert(k < size());
value_type ret;
::std::size_t i = Bitlength;
const node_type *ptr = root.get();
while (i--) {
if (size(ptr->child[lazy.test(i)].get()) <= k) {
k -= size(ptr->child[lazy.test(i)].get());
ptr = ptr->child[lazy.test(i) ^ 1].get();
ret.set(i);
} else {
ptr = ptr->child[lazy.test(i)].get();
}
}
return ret;
}
size_type order_of_key(const value_type &x) const {
assert(count(x));
size_type ret = 0;
::std::size_t i = Bitlength;
const node_type *ptr = root.get();
while (i--) {
if (x.test(i) ^ lazy.test(i)) {
ret += size(ptr->child[lazy.test(i)].get());
ptr = ptr->child[lazy.test(i) ^ 1].get();
} else {
ptr = ptr->child[lazy.test(i)].get();
}
}
return ret;
}
void clear() { root.reset(); }
void insert(const value_type &x) {
::std::size_t i = Bitlength;
if (!root)
root = ::std::make_unique<node_type>();
node_type *ptr = root.get();
while (++ptr->size, i--) {
if (!ptr->child[x.test(i) ^ lazy.test(i)])
ptr->child[x.test(i) ^ lazy.test(i)] = ::std::make_unique<node_type>();
ptr = ptr->child[x.test(i) ^ lazy.test(i)].get();
}
}
void insert(const value_type &x, const size_type k) {
::std::size_t i = Bitlength;
if (!root)
root = ::std::make_unique<node_type>();
node_type *ptr = root.get();
while (ptr->size += k, i--) {
if (!ptr->child[x.test(i) ^ lazy.test(i)])
ptr->child[x.test(i) ^ lazy.test(i)] = ::std::make_unique<node_type>();
ptr = ptr->child[x.test(i) ^ lazy.test(i)].get();
}
}
void erase(const value_type &x) {
assert(count(x));
::std::size_t i = Bitlength;
node_type *ptr = root.get();
while (--ptr->size, i--)
ptr = ptr->child[x.test(i) ^ lazy.test(i)].get();
}
void erase(const value_type &x, const size_type k) {
assert(k <= count(x));
::std::size_t i = Bitlength;
node_type *ptr = root.get();
while (ptr->size -= k, i--)
ptr = ptr->child[x.test(i) ^ lazy.test(i)].get();
}
void xor_all(const value_type &x) { lazy ^= x; }
};
#include <cstdio>
int main() {
using uint = unsigned int;
uint n, q;
scanf("%u%u", &n, &q);
binary_trie<17> s;
for (uint i = 0; i <= n; ++i)
s.insert(i, 0);
while (q--) {
uint com, x, y;
scanf("%u%u%u", &com, &x, &y);
if (com) {
printf("%u\n", s.order_of_key(y) - s.order_of_key(x - 1));
} else {
s.insert(x - 1, y);
}
}
return 0;
}
|
replace
| 123 | 125 | 123 | 125 |
0
| |
p02346
|
C++
|
Runtime Error
|
#include <algorithm>
#include <cstdio>
using namespace std;
#define MAX 100001
int n, seg[MAX * 2 - 1];
void init(int size) {
n = 1;
while (n < size)
n *= 2;
}
int query(int a, int b, int k, int l, int r) {
if (r <= a || b <= l)
return 0;
if (a <= l && r <= b)
return seg[k];
int vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
int vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
return vl + vr;
}
void update(int i, int x) {
i += n - 1;
seg[i] += x;
while (i > 0) {
i = (i - 1) / 2;
seg[i] = seg[2 * i + 1] + seg[2 * i + 2];
}
}
int main() {
int size, q;
scanf("%d %d", &size, &q);
init(size);
while (q--) {
int com, x, y;
scanf("%d %d %d", &com, &x, &y);
if (com == 0)
update(x - 1, y);
else
printf("%d\n", query(x - 1, y, 0, 0, n));
}
}
|
#include <algorithm>
#include <cstdio>
using namespace std;
#define MAX (1 << 17)
int n, seg[MAX * 2 - 1];
void init(int size) {
n = 1;
while (n < size)
n *= 2;
}
int query(int a, int b, int k, int l, int r) {
if (r <= a || b <= l)
return 0;
if (a <= l && r <= b)
return seg[k];
int vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
int vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
return vl + vr;
}
void update(int i, int x) {
i += n - 1;
seg[i] += x;
while (i > 0) {
i = (i - 1) / 2;
seg[i] = seg[2 * i + 1] + seg[2 * i + 2];
}
}
int main() {
int size, q;
scanf("%d %d", &size, &q);
init(size);
while (q--) {
int com, x, y;
scanf("%d %d %d", &com, &x, &y);
if (com == 0)
update(x - 1, y);
else
printf("%d\n", query(x - 1, y, 0, 0, n));
}
}
|
replace
| 3 | 4 | 3 | 4 |
0
| |
p02346
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
template <class T> struct treap {
#define treapInf INT_MAX
public:
struct node {
T val;
node *lch, *rch;
int pri, sz;
T sum, mini;
node(T val, int pri) : val(val), pri(pri), sz(1), sum(val), mini(val) {
lch = rch = NULL;
}
};
node *root;
treap() : root(NULL) { srand(time(NULL)); }
private:
inline int size(node *t) { return !t ? 0 : t->sz; }
inline T sum(node *t) { return !t ? 0 : t->sum; }
inline T mini(node *t) { return !t ? treapInf : t->mini; }
node *update(node *t) {
// if(!t)return t;
t->sz = size(t->lch) + size(t->rch) + 1;
t->sum = sum(t->lch) + sum(t->rch) + t->val;
t->mini = min(min(mini(t->lch), mini(t->rch)), t->val);
return t;
}
node *merge(node *l, node *r) {
if (!l || !r)
return l ? l : r;
if (l->pri > r->pri) {
l->rch = merge(l->rch, r);
return update(l);
} else {
r->lch = merge(l, r->lch);
return update(r);
}
}
pair<node *, node *> split(node *t, int k) {
if (!t)
return make_pair((node *)NULL, (node *)NULL);
if (k <= size(t->lch)) {
pair<node *, node *> s = split(t->lch, k);
t->lch = s.second;
return make_pair(s.first, update(t));
} else {
pair<node *, node *> s = split(t->rch, k - size(t->lch) - 1);
t->rch = s.first;
return make_pair(update(t), s.second);
}
}
node *insert(node *t, int k, T val, int pri) {
pair<node *, node *> s = split(t, k);
t = merge(s.first, new node(val, pri));
t = merge(t, s.second);
return update(t);
}
node *erase(node *t, int k) {
pair<node *, node *> s1, s2;
s2 = split(t, k + 1);
s1 = split(s2.first, k);
return update(t = merge(s1.first, s2.second));
}
node *find(node *t, int k) {
// if(!t)return t;
int c = size(t->lch);
if (k < c)
return find(t->lch, k);
if (k == c)
return t;
return find(t->rch, k - c - 1);
}
T rangeMinimumQuery(int l, int r, node *t) {
int sz = size(t);
if (r <= 0 || sz <= l)
return treapInf;
if (l <= 0 && sz <= r)
return mini(t);
sz = size(t->lch);
T vl = rangeMinimumQuery(l, r, t->lch);
T vr = rangeMinimumQuery(l - sz - 1, r - sz - 1, t->rch);
T res = min(vl, vr);
if (l <= sz && sz < r)
res = min(res, t->val);
return res;
}
T rangeSumQuery(int l, int r, node *t) {
int sz = size(t);
if (r <= 0 || sz <= l)
return 0;
if (l <= 0 && sz <= r)
return sum(t);
sz = size(t->lch);
T vl = rangeSumQuery(l, r, t->lch);
T vr = rangeSumQuery(l - sz - 1, r - sz - 1, t->rch);
T res = vl + vr;
if (l <= sz && sz < r)
res += t->val;
return res;
}
void debug(node *t) {
if (!t)
return;
debug(t->lch);
cout << t->val << " ";
debug(t->rch);
}
public:
void insert(int k, T val) { root = insert(root, k, val, rand()); }
void erase(int k) { root = erase(root, k); }
node *find(int k) { return find(root, k); }
void add(int k, T v) {
node *a = find(k);
T tmp = a->val;
erase(k);
insert(k, tmp + v);
}
void update(int k, T v) {
erase(k);
insert(k, v);
}
T rangeMinimumQuery(int l, int r) { return rangeMinimumQuery(l, r, root); }
T rangeSumQuery(int l, int r) { return rangeSumQuery(l, r, root); }
void debug() {
debug(root);
cout << endl;
}
};
int main() {
int n, q;
cin >> n >> q;
treap<int> t;
for (int i = 0; i < n; i++)
t.insert(i, 0);
while (q--) {
int com, x, y;
cin >> com >> x >> y;
x--;
if (com == 0) {
t.add(x, y);
} else {
cout << t.rangeSumQuery(x, y) << endl;
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
template <class T> struct treap {
#define treapInf INT_MAX
public:
struct node {
T val;
node *lch, *rch;
int pri, sz;
T sum, mini;
node(T val, int pri) : val(val), pri(pri), sz(1), sum(val), mini(val) {
lch = rch = NULL;
}
};
node *root;
treap() : root(NULL) { srand(time(NULL)); }
private:
inline int size(node *t) { return !t ? 0 : t->sz; }
inline T sum(node *t) { return !t ? 0 : t->sum; }
inline T mini(node *t) { return !t ? treapInf : t->mini; }
node *update(node *t) {
if (!t)
return t;
t->sz = size(t->lch) + size(t->rch) + 1;
t->sum = sum(t->lch) + sum(t->rch) + t->val;
t->mini = min(min(mini(t->lch), mini(t->rch)), t->val);
return t;
}
node *merge(node *l, node *r) {
if (!l || !r)
return l ? l : r;
if (l->pri > r->pri) {
l->rch = merge(l->rch, r);
return update(l);
} else {
r->lch = merge(l, r->lch);
return update(r);
}
}
pair<node *, node *> split(node *t, int k) {
if (!t)
return make_pair((node *)NULL, (node *)NULL);
if (k <= size(t->lch)) {
pair<node *, node *> s = split(t->lch, k);
t->lch = s.second;
return make_pair(s.first, update(t));
} else {
pair<node *, node *> s = split(t->rch, k - size(t->lch) - 1);
t->rch = s.first;
return make_pair(update(t), s.second);
}
}
node *insert(node *t, int k, T val, int pri) {
pair<node *, node *> s = split(t, k);
t = merge(s.first, new node(val, pri));
t = merge(t, s.second);
return update(t);
}
node *erase(node *t, int k) {
pair<node *, node *> s1, s2;
s2 = split(t, k + 1);
s1 = split(s2.first, k);
return update(t = merge(s1.first, s2.second));
}
node *find(node *t, int k) {
// if(!t)return t;
int c = size(t->lch);
if (k < c)
return find(t->lch, k);
if (k == c)
return t;
return find(t->rch, k - c - 1);
}
T rangeMinimumQuery(int l, int r, node *t) {
int sz = size(t);
if (r <= 0 || sz <= l)
return treapInf;
if (l <= 0 && sz <= r)
return mini(t);
sz = size(t->lch);
T vl = rangeMinimumQuery(l, r, t->lch);
T vr = rangeMinimumQuery(l - sz - 1, r - sz - 1, t->rch);
T res = min(vl, vr);
if (l <= sz && sz < r)
res = min(res, t->val);
return res;
}
T rangeSumQuery(int l, int r, node *t) {
int sz = size(t);
if (r <= 0 || sz <= l)
return 0;
if (l <= 0 && sz <= r)
return sum(t);
sz = size(t->lch);
T vl = rangeSumQuery(l, r, t->lch);
T vr = rangeSumQuery(l - sz - 1, r - sz - 1, t->rch);
T res = vl + vr;
if (l <= sz && sz < r)
res += t->val;
return res;
}
void debug(node *t) {
if (!t)
return;
debug(t->lch);
cout << t->val << " ";
debug(t->rch);
}
public:
void insert(int k, T val) { root = insert(root, k, val, rand()); }
void erase(int k) { root = erase(root, k); }
node *find(int k) { return find(root, k); }
void add(int k, T v) {
node *a = find(k);
T tmp = a->val;
erase(k);
insert(k, tmp + v);
}
void update(int k, T v) {
erase(k);
insert(k, v);
}
T rangeMinimumQuery(int l, int r) { return rangeMinimumQuery(l, r, root); }
T rangeSumQuery(int l, int r) { return rangeSumQuery(l, r, root); }
void debug() {
debug(root);
cout << endl;
}
};
int main() {
int n, q;
cin >> n >> q;
treap<int> t;
for (int i = 0; i < n; i++)
t.insert(i, 0);
while (q--) {
int com, x, y;
cin >> com >> x >> y;
x--;
if (com == 0) {
t.add(x, y);
} else {
cout << t.rangeSumQuery(x, y) << endl;
}
}
return 0;
}
|
replace
| 27 | 28 | 27 | 29 |
0
| |
p02346
|
C++
|
Time Limit Exceeded
|
#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;
template <typename T> struct Add {
T operator()(T x, T y) { return x + y; }
};
template <class T, class Functor> struct SegmentTree {
size_t n;
T e;
vector<T> tree;
Functor F;
SegmentTree(size_t m, T e = T()) : n(1), e(e) {
while (n < m)
n <<= 1;
tree.assign(n << 1, e);
}
T range_query(size_t a, size_t b, size_t i = 1, size_t l = 0, size_t r = -1) {
if (!~r)
r = n;
if (a <= l && r <= b)
return tree[i];
T vl = e, vr = e;
size_t c = (l + r) >> 1;
if (a < c)
vl = F(e, range_query(a, b, i << 1 | 0, l, c));
if (c < b)
vr = F(e, range_query(a, b, i << 1 | 1, c, r));
return F(vl, vr);
}
void update(size_t i, T x) {
i += n;
tree[i] = x;
while (i >>= 1)
tree[i] = F(tree[i << 1 | 0], tree[i << 1 | 1]);
}
void add(size_t i, T x) {
i += n;
tree[i] += x;
while (i >>= 1)
tree[i] = F(tree[i << 1 | 0], tree[i << 1 | 1]);
}
};
int main() {
size_t n, q;
scanf("%zu %zu", &n, &q);
SegmentTree<int, Add<int>> tree(n);
for (size_t i = 0; i < q; ++i) {
int com;
scanf("%d", &com);
if (com) {
size_t s, t;
scanf("%zu %zu", &s, &t);
printf("%d\n", tree.range_query(s, t + 1));
} else {
size_t i;
int x;
scanf("%zu %d", &i, &x);
tree.add(i, x);
}
}
return 0;
}
|
#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;
template <typename T> struct Add {
T operator()(T x, T y) { return x + y; }
};
template <class T, class Functor> struct SegmentTree {
size_t n;
T e;
vector<T> tree;
Functor F;
SegmentTree(size_t m, T e = T()) : n(1), e(e) {
while (n <= m)
n <<= 1;
tree.assign(n << 1, e);
}
T range_query(size_t a, size_t b, size_t i = 1, size_t l = 0, size_t r = -1) {
if (!~r)
r = n;
if (a <= l && r <= b)
return tree[i];
T vl = e, vr = e;
size_t c = (l + r) >> 1;
if (a < c)
vl = F(e, range_query(a, b, i << 1 | 0, l, c));
if (c < b)
vr = F(e, range_query(a, b, i << 1 | 1, c, r));
return F(vl, vr);
}
void update(size_t i, T x) {
i += n;
tree[i] = x;
while (i >>= 1)
tree[i] = F(tree[i << 1 | 0], tree[i << 1 | 1]);
}
void add(size_t i, T x) {
i += n;
tree[i] += x;
while (i >>= 1)
tree[i] = F(tree[i << 1 | 0], tree[i << 1 | 1]);
}
};
int main() {
size_t n, q;
scanf("%zu %zu", &n, &q);
SegmentTree<int, Add<int>> tree(n);
for (size_t i = 0; i < q; ++i) {
int com;
scanf("%d", &com);
if (com) {
size_t s, t;
scanf("%zu %zu", &s, &t);
printf("%d\n", tree.range_query(s, t + 1));
} else {
size_t i;
int x;
scanf("%zu %d", &i, &x);
tree.add(i, x);
}
}
return 0;
}
|
replace
| 16 | 17 | 16 | 17 |
TLE
| |
p02346
|
C++
|
Runtime Error
|
#include <algorithm>
#include <cassert>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define rep(i, n) FOR(i, 0, n)
#define DEBUG(x) cout << #x << ": " << x << endl
#define vint vector<int>
#define vdouble vector<double>
#define vstring vector<string>
using namespace std;
#include <map>
#include <queue>
#include <set>
typedef long long ll;
typedef unsigned long long ull;
// const int MAX_N = 1000000;
const int INFTY = (1 << 21); // 2097152
// const ll INFTY = (1LL << 60);
const ll MD = 1000000007LL;
// fprintf(stderr, "%d %lld \n", x, xll);
// segtree
const int MAX_N = 200000; // 1 << 20; // > 10^6
class SegTree {
public:
int n;
vector<ll> dat;
ll dummy = 0; // (1 << 31) - 1;
SegTree(int _n) { init(_n); }
void init(int n_) {
dat = vector<ll>(2 * MAX_N - 1);
n = 1;
while (n < n_)
n *= 2;
// initialize
rep(i, 2 * n - 1) dat[i] = dummy;
}
void update(int k, ll a) {
// update from bottom
k += n - 1;
dat[k] += a;
while (k > 0) {
k = (k - 1) / 2;
dat[k] = dat[k * 2 + 1] + dat[k * 2 + 2];
}
}
// sum of [a, b)
ll query(int a, int b) { return _query(a, b, 0, 0, n); }
// sum of [a, b)
// node k, corresponding [l, r)
ll _query(int a, int b, int k, int l, int r) {
if (r <= a || b <= l)
return dummy;
if (a <= l && r <= b)
return dat[k];
else {
int vl = _query(a, b, k * 2 + 1, l, (l + r) / 2);
int vr = _query(a, b, k * 2 + 2, (l + r) / 2, r);
return vl + vr;
}
}
};
int N, Q;
int main() {
cin >> N >> Q;
SegTree ST(N);
rep(q, Q) {
int c, x, y;
cin >> c >> x >> y;
if (c == 0) {
// update
x--;
ST.update(x, y);
} else {
// find
x--;
y--;
cout << ST.query(x, y + 1) << endl;
}
}
rep(i, ST.n * 2 - 1) { fprintf(stderr, "%d ", ST.dat[i]); }
fprintf(stderr, "\n");
}
|
#include <algorithm>
#include <cassert>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define rep(i, n) FOR(i, 0, n)
#define DEBUG(x) cout << #x << ": " << x << endl
#define vint vector<int>
#define vdouble vector<double>
#define vstring vector<string>
using namespace std;
#include <map>
#include <queue>
#include <set>
typedef long long ll;
typedef unsigned long long ull;
// const int MAX_N = 1000000;
const int INFTY = (1 << 21); // 2097152
// const ll INFTY = (1LL << 60);
const ll MD = 1000000007LL;
// fprintf(stderr, "%d %lld \n", x, xll);
// segtree
const int MAX_N = 200000; // 1 << 20; // > 10^6
class SegTree {
public:
int n;
vector<ll> dat;
ll dummy = 0; // (1 << 31) - 1;
SegTree(int _n) { init(_n); }
void init(int n_) {
dat = vector<ll>(2 * MAX_N - 1);
n = 1;
while (n < n_)
n *= 2;
// initialize
rep(i, 2 * n - 1) dat[i] = dummy;
}
void update(int k, ll a) {
// update from bottom
k += n - 1;
dat[k] += a;
while (k > 0) {
k = (k - 1) / 2;
dat[k] = dat[k * 2 + 1] + dat[k * 2 + 2];
}
}
// sum of [a, b)
ll query(int a, int b) { return _query(a, b, 0, 0, n); }
// sum of [a, b)
// node k, corresponding [l, r)
ll _query(int a, int b, int k, int l, int r) {
if (r <= a || b <= l)
return dummy;
if (a <= l && r <= b)
return dat[k];
else {
int vl = _query(a, b, k * 2 + 1, l, (l + r) / 2);
int vr = _query(a, b, k * 2 + 2, (l + r) / 2, r);
return vl + vr;
}
}
};
int N, Q;
int main() {
cin >> N >> Q;
SegTree ST(N);
rep(q, Q) {
int c, x, y;
cin >> c >> x >> y;
if (c == 0) {
// update
x--;
ST.update(x, y);
} else {
// find
x--;
y--;
cout << ST.query(x, y + 1) << endl;
}
}
// rep(i, ST.n * 2 - 1){fprintf(stderr, "%d ", ST.dat[i]); } fprintf(stderr,
// "\n");
}
|
replace
| 100 | 102 | 100 | 102 |
0
|
6 3 3 1 2 3 0
|
p02346
|
C++
|
Memory Limit Exceeded
|
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define rrep(i, a, b) for (int i = a; i >= b; i--)
#define fore(i, a) for (auto &i : a)
#pragma GCC optimize("-O3")
using namespace std;
void _main();
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
_main();
}
//---------------------------------------------------------------------------------------------------
typedef int V;
#define NV 6010101
#define def 0
struct PersistentSegmentTree { //[L,R)
V comp(V a, V b) { return a + b; }
// -- template ---------------------
struct S {
int l, r;
V x;
} v[NV];
int cnt = 1, n = -1;
void init(int _n) {
n = _n;
rep(i, 0, NV) v[i].l = v[i].r = 0, v[i].x = def;
}
void add(int &root, int l, int r, int i, int val) {
v[cnt] = v[root];
root = cnt++;
if (l + 1 == r) {
v[root].x += val;
return;
}
int mi = (l + r) / 2;
if (i < mi)
add(v[root].l, l, mi, i, val);
else
add(v[root].r, mi, r, i, val);
v[root].x = comp(v[v[root].l].x, v[v[root].r].x);
}
void add(int &root, int i, int val) {
assert(0 < n);
add(root, 0, n, i, val);
}
void update(int &root, int l, int r, int i, int val) {
v[cnt] = v[root];
root = cnt++;
if (l + 1 == r) {
v[root].x = val;
return;
}
int mi = (l + r) / 2;
if (i < mi)
update(v[root].l, l, mi, i, val);
else
update(v[root].r, mi, r, i, val);
v[root].x = comp(v[v[root].l].x, v[v[root].r].x);
}
void update(int &root, int i, int val) {
assert(0 < n);
update(root, 0, n, i, val);
}
int query(int a, int b, int l, int r, int k) {
assert(0 < n);
if (v[b].x - v[a].x < k)
return -2;
if (l + 1 == r)
return l;
int mi = (l + r) / 2;
int re = query(v[a].l, v[b].l, l, mi, k); // v[0] always 0
if (re < 0)
re = query(v[a].r, v[b].r, mi, r, k);
return re;
}
int allget(int root) { return v[root].x; }
// todo
int get(int root, int l, int r, int L, int R) {
if (l >= r)
return def;
if (l == L && r == R)
return v[root].x;
int mi = (L + R) / 2;
V le = get(v[root].l, max(l, L), min(r, mi), L, mi);
V ri = get(v[root].r, max(l, mi), min(r, R), mi, R);
return comp(le, ri);
}
int get(int root, int l, int r) { return get(root, l, r, 0, n); }
};
/*---------------------------------------------------------------------------------------------------
????????????????????????????????? ??§?????§
??????????????? ??§?????§ ???????´<_??? ?????? Welcome to My Coding Space!
???????????? ??? ?´_???`??????/??? ???i
?????????????????????????????? ??? |???|
????????? /?????? /??£??£??£??£/??????|
??? ???_(__??????/??? ???/ .| .|????????????
??? ????????????/????????????/??????u??????
---------------------------------------------------------------------------------------------------*/
int N, Q;
PersistentSegmentTree pst;
int root = 0;
//---------------------------------------------------------------------------------------------------
void _main() {
pst.init(101010);
cin >> N >> Q;
rep(_, 0, Q) {
int c, x, y;
cin >> c >> x >> y;
x--;
if (c == 0)
pst.add(root, x, y);
else
printf("%d\n", pst.get(root, x, y));
}
}
|
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define rrep(i, a, b) for (int i = a; i >= b; i--)
#define fore(i, a) for (auto &i : a)
#pragma GCC optimize("-O3")
using namespace std;
void _main();
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
_main();
}
//---------------------------------------------------------------------------------------------------
typedef int V;
#define NV 3010101
#define def 0
struct PersistentSegmentTree { //[L,R)
V comp(V a, V b) { return a + b; }
// -- template ---------------------
struct S {
int l, r;
V x;
} v[NV];
int cnt = 1, n = -1;
void init(int _n) {
n = _n;
rep(i, 0, NV) v[i].l = v[i].r = 0, v[i].x = def;
}
void add(int &root, int l, int r, int i, int val) {
v[cnt] = v[root];
root = cnt++;
if (l + 1 == r) {
v[root].x += val;
return;
}
int mi = (l + r) / 2;
if (i < mi)
add(v[root].l, l, mi, i, val);
else
add(v[root].r, mi, r, i, val);
v[root].x = comp(v[v[root].l].x, v[v[root].r].x);
}
void add(int &root, int i, int val) {
assert(0 < n);
add(root, 0, n, i, val);
}
void update(int &root, int l, int r, int i, int val) {
v[cnt] = v[root];
root = cnt++;
if (l + 1 == r) {
v[root].x = val;
return;
}
int mi = (l + r) / 2;
if (i < mi)
update(v[root].l, l, mi, i, val);
else
update(v[root].r, mi, r, i, val);
v[root].x = comp(v[v[root].l].x, v[v[root].r].x);
}
void update(int &root, int i, int val) {
assert(0 < n);
update(root, 0, n, i, val);
}
int query(int a, int b, int l, int r, int k) {
assert(0 < n);
if (v[b].x - v[a].x < k)
return -2;
if (l + 1 == r)
return l;
int mi = (l + r) / 2;
int re = query(v[a].l, v[b].l, l, mi, k); // v[0] always 0
if (re < 0)
re = query(v[a].r, v[b].r, mi, r, k);
return re;
}
int allget(int root) { return v[root].x; }
// todo
int get(int root, int l, int r, int L, int R) {
if (l >= r)
return def;
if (l == L && r == R)
return v[root].x;
int mi = (L + R) / 2;
V le = get(v[root].l, max(l, L), min(r, mi), L, mi);
V ri = get(v[root].r, max(l, mi), min(r, R), mi, R);
return comp(le, ri);
}
int get(int root, int l, int r) { return get(root, l, r, 0, n); }
};
/*---------------------------------------------------------------------------------------------------
????????????????????????????????? ??§?????§
??????????????? ??§?????§ ???????´<_??? ?????? Welcome to My Coding Space!
???????????? ??? ?´_???`??????/??? ???i
?????????????????????????????? ??? |???|
????????? /?????? /??£??£??£??£/??????|
??? ???_(__??????/??? ???/ .| .|????????????
??? ????????????/????????????/??????u??????
---------------------------------------------------------------------------------------------------*/
int N, Q;
PersistentSegmentTree pst;
int root = 0;
//---------------------------------------------------------------------------------------------------
void _main() {
pst.init(101010);
cin >> N >> Q;
rep(_, 0, Q) {
int c, x, y;
cin >> c >> x >> y;
x--;
if (c == 0)
pst.add(root, x, y);
else
printf("%d\n", pst.get(root, x, y));
}
}
|
replace
| 14 | 15 | 14 | 15 |
MLE
| |
p02347
|
C++
|
Time Limit Exceeded
|
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
typedef pair<ll, ll> Pll;
typedef vector<int> Vi;
// typedef tuple<int, int, int> T;
#define FOR(i, s, x) for (int i = s; i < (int)(x); i++)
#define REP(i, x) FOR(i, 0, x)
#define ALL(c) c.begin(), c.end()
#define DUMP(x) cerr << #x << " = " << (x) << endl
#define UNIQUE(c) sort(ALL(c)), c.erase(unique(ALL(c)), c.end())
const int dr[4] = {-1, 0, 1, 0};
const int dc[4] = {0, 1, 0, -1};
template <typename T> struct Data {
using type = T;
type data;
Data(type data) : data(data) {}
static bool compare(const Data<type> &data1, const Data<type> &data2,
size_t base) {
const size_t dim = data1.data.size();
for (size_t i = 0; i < dim; i++) {
if (data1.data[(i + base) % dim] != data2.data[(i + base) % dim]) {
return data1.data[(i + base) % dim] < data2.data[(i + base) % dim];
}
}
return false;
}
bool operator==(const Data<type> &data) const {
return this->data == data.data;
}
};
struct Node {
size_t dim, idx;
Node *left, *right;
};
template <typename T> struct Region {
T low, high;
Region(T low, T high) : low(low), high(high){};
bool include(T data) const {
size_t dim = this->low.size();
for (size_t i = 0; i < dim; i++) {
if (data[i] < low[i] or high[i] < data[i]) {
return false;
}
}
return true;
}
bool include(Region<T> region) const {
size_t dim = this->low.size();
for (size_t bit = 0; bit < (1U << dim); bit++) {
T data;
for (size_t i = 0; i < dim; i++) {
if (bit >> i & 1) {
data[i] = region.low[i];
} else {
data[i] = region.high[i];
}
}
if (not this->include(data)) {
return false;
}
}
return true;
}
bool overlap(Region<T> region) const {
size_t dim = this->low.size();
for (size_t bit = 0; bit < (1U << dim); bit++) {
T data;
for (size_t i = 0; i < dim; i++) {
if (bit >> i & 1) {
data[i] = region.low[i];
} else {
data[i] = region.high[i];
}
}
if (this->include(data)) {
return true;
}
}
return false;
}
friend ostream &operator<<(ostream &os, const Region<T> ®ion) {
for (size_t i = 0; i < region.low.size(); i++) {
if (i > 0) {
os << 'x';
}
os << '[' << region.low[i] << ',' << region.high[i] << ']';
}
return os;
}
};
template <typename DataType> struct KDtree {
using T = typename DataType::type;
using VT = typename T::value_type;
size_t dim;
size_t leaf_dim;
std::vector<DataType> data;
VT min_range, max_range;
Node *root;
KDtree(VT min_range = std::numeric_limits<VT>::min(),
VT max_range = std::numeric_limits<VT>::max())
: dim(std::tuple_size<T>::value), min_range(min_range),
max_range(max_range) {}
Node *create_node(size_t dim, size_t idx) {
Node *node = (Node *)malloc(sizeof(Node));
if (node == NULL) {
return NULL;
}
node->dim = dim;
node->idx = idx;
node->left = node->right = NULL;
return node;
}
Node *build(std::vector<std::vector<size_t>> data, size_t depth) {
// std::cout << "Build Depth: " << depth << ", Size: ";
// REP(i, this->dim) std::cout << data[i].size() << (i + 1 == this->dim ?
// '\n' : ' ');
if (data[0].size() == 1) {
return create_node(leaf_dim, data[0][0]);
} else {
std::vector<std::vector<size_t>> left_data(this->dim),
right_data(this->dim);
size_t median_idx =
static_cast<size_t>(static_cast<int>(data[0].size() - 1) / 2);
size_t median = data[depth % this->dim][median_idx];
for (size_t i = 0; i < this->dim; i++) {
for (size_t j : data[i]) {
if (this->data[j] == this->data[median] or
DataType::compare(this->data[j], this->data[median],
depth % this->dim)) {
left_data[i].emplace_back(j);
} else {
right_data[i].emplace_back(j);
}
}
}
// std::cout << "Left: " << left_data[0].size() << ", Right: " <<
// right_data[0].size() << std::endl;
assert(left_data[0].size() > 0 and right_data[0].size() > 0);
Node *node = create_node(depth % this->dim, median);
node->left = build(left_data, depth + 1);
node->right = build(right_data, depth + 1);
return node;
}
}
void build_kd_tree(std::vector<DataType> &data) {
// std::cout << "Start Build..." << std::endl;
assert(data.size() > 0);
this->data = data;
this->leaf_dim = this->dim;
std::vector<std::vector<size_t>> _data(this->dim,
std::vector<size_t>(data.size()));
for (size_t i = 0; i < this->dim; i++) {
std::iota(_data[i].begin(), _data[i].end(), 0);
sort(_data[i].begin(), _data[i].end(),
[&](const size_t &a, const size_t &b) {
return DataType::compare(this->data[a], this->data[b], i);
});
}
this->root = build(_data, 0);
// std::cout << "End Build" << std::endl;
}
void report_subtree(Node *node, std::vector<size_t> &output) const {
if (node->dim == this->leaf_dim) {
output.emplace_back(node->idx);
} else {
report_subtree(node->left, output);
report_subtree(node->right, output);
}
}
std::vector<size_t> range_query(Region<T> query_region) {
T node_low, node_high;
node_low.fill(this->min_range);
node_high.fill(this->max_range);
Region<T> node_region(node_low, node_high);
std::vector<size_t> output;
query(this->root, query_region, node_region, output);
return output;
}
void query(Node *node, Region<T> query_region, Region<T> node_region,
std::vector<size_t> &output) {
// std::cout << "(Node, Dim) = (" << node->idx << ',' << node->dim << "), "
// << node_region << ' ' << query_region << std::endl;
T data = this->data[node->idx].data;
if (node->dim == this->leaf_dim) {
if (query_region.include(data)) {
output.emplace_back(node->idx);
}
} else {
Region<T> left_region = node_region, right_region = node_region;
Region<T> left_query_region = query_region,
right_query_region = query_region;
left_region.high[node->dim] = data[node->dim];
right_region.low[node->dim] = data[node->dim];
if (query_region.low[node->dim] <= data[node->dim] and
data[node->dim] < query_region.high[node->dim]) {
left_query_region.high[node->dim] = data[node->dim];
}
if (data[node->dim] <= query_region.high[node->dim] and
query_region.low[node->dim] < data[node->dim]) {
right_query_region.low[node->dim] = data[node->dim];
}
// std::cout << left_region << ' ' << right_region << std::endl;
// std::cout << left_query_region << ' ' << right_query_region <<
// std::endl;
if (left_query_region.include(left_region)) {
report_subtree(node->left, output);
} else if (left_region.overlap(left_query_region)) {
query(node->left, left_query_region, left_region, output);
}
if (right_query_region.include(right_region)) {
report_subtree(node->right, output);
} else if (right_region.overlap(right_query_region)) {
query(node->right, right_query_region, right_region, output);
}
}
}
void output() { output_kdtree(this->root); }
void output_kdtree(Node *node) {
std::cout << "idx: " << node->idx << ", (";
for (size_t i = 0; i < this->dim; i++) {
if (i > 0) {
std::cout << ',';
}
std::cout << this->data[node->idx].data[i];
}
std::cout << ") ";
if (node->dim != this->leaf_dim) {
std::cout << "(left, right) = (" << node->left->idx << ','
<< node->right->idx << ')' << std::endl;
output_kdtree(node->left);
output_kdtree(node->right);
} else {
std::cout << std::endl;
}
}
};
int main() {
// use scanf in CodeForces!
cin.tie(0);
ios_base::sync_with_stdio(false);
using DataType = Data<std::array<int, 2>>;
KDtree<DataType> kdtree;
std::vector<DataType> points;
int N;
cin >> N;
REP(i, N) {
int x, y;
cin >> x >> y;
DataType::type tmp = {x, y};
points.emplace_back(DataType(tmp));
}
kdtree.build_kd_tree(points);
std::vector<size_t> output;
kdtree.report_subtree(kdtree.root, output);
// for (size_t i : output) std::cout << i << ' ';
// std::cout << std::endl;
// kdtree.output();
///*
int Q;
cin >> Q;
REP(i, Q) {
int sx, tx, sy, ty;
std::cin >> sx >> tx >> sy >> ty;
DataType::type low = {sx, sy}, high = {tx, ty};
Region<DataType::type> query_region(low, high);
std::vector<size_t> out = kdtree.range_query(query_region);
sort(ALL(out));
for (size_t i : out)
std::cout << i << std::endl;
std::cout << std::endl;
}
//*/
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
typedef pair<ll, ll> Pll;
typedef vector<int> Vi;
// typedef tuple<int, int, int> T;
#define FOR(i, s, x) for (int i = s; i < (int)(x); i++)
#define REP(i, x) FOR(i, 0, x)
#define ALL(c) c.begin(), c.end()
#define DUMP(x) cerr << #x << " = " << (x) << endl
#define UNIQUE(c) sort(ALL(c)), c.erase(unique(ALL(c)), c.end())
const int dr[4] = {-1, 0, 1, 0};
const int dc[4] = {0, 1, 0, -1};
template <typename T> struct Data {
using type = T;
type data;
Data(type data) : data(data) {}
static bool compare(const Data<type> &data1, const Data<type> &data2,
size_t base) {
const size_t dim = data1.data.size();
for (size_t i = 0; i < dim; i++) {
if (data1.data[(i + base) % dim] != data2.data[(i + base) % dim]) {
return data1.data[(i + base) % dim] < data2.data[(i + base) % dim];
}
}
return false;
}
bool operator==(const Data<type> &data) const {
return this->data == data.data;
}
};
struct Node {
size_t dim, idx;
Node *left, *right;
};
template <typename T> struct Region {
T low, high;
Region(T low, T high) : low(low), high(high){};
bool include(T data) const {
size_t dim = this->low.size();
for (size_t i = 0; i < dim; i++) {
if (data[i] < low[i] or high[i] < data[i]) {
return false;
}
}
return true;
}
bool include(Region<T> region) const {
size_t dim = this->low.size();
for (size_t bit = 0; bit < (1U << dim); bit++) {
T data;
for (size_t i = 0; i < dim; i++) {
if (bit >> i & 1) {
data[i] = region.low[i];
} else {
data[i] = region.high[i];
}
}
if (not this->include(data)) {
return false;
}
}
return true;
}
bool overlap(Region<T> region) const {
size_t dim = this->low.size();
for (size_t bit = 0; bit < (1U << dim); bit++) {
T data;
for (size_t i = 0; i < dim; i++) {
if (bit >> i & 1) {
data[i] = region.low[i];
} else {
data[i] = region.high[i];
}
}
if (this->include(data)) {
return true;
}
}
return false;
}
friend ostream &operator<<(ostream &os, const Region<T> ®ion) {
for (size_t i = 0; i < region.low.size(); i++) {
if (i > 0) {
os << 'x';
}
os << '[' << region.low[i] << ',' << region.high[i] << ']';
}
return os;
}
};
template <typename DataType> struct KDtree {
using T = typename DataType::type;
using VT = typename T::value_type;
size_t dim;
size_t leaf_dim;
std::vector<DataType> data;
VT min_range, max_range;
Node *root;
KDtree(VT min_range = std::numeric_limits<VT>::min(),
VT max_range = std::numeric_limits<VT>::max())
: dim(std::tuple_size<T>::value), min_range(min_range),
max_range(max_range) {}
Node *create_node(size_t dim, size_t idx) {
Node *node = (Node *)malloc(sizeof(Node));
if (node == NULL) {
return NULL;
}
node->dim = dim;
node->idx = idx;
node->left = node->right = NULL;
return node;
}
Node *build(std::vector<std::vector<size_t>> data, size_t depth) {
// std::cout << "Build Depth: " << depth << ", Size: ";
// REP(i, this->dim) std::cout << data[i].size() << (i + 1 == this->dim ?
// '\n' : ' ');
if (data[0].size() == 1) {
return create_node(leaf_dim, data[0][0]);
} else {
std::vector<std::vector<size_t>> left_data(this->dim),
right_data(this->dim);
size_t median_idx =
static_cast<size_t>(static_cast<int>(data[0].size() - 1) / 2);
size_t median = data[depth % this->dim][median_idx];
for (size_t i = 0; i < this->dim; i++) {
for (size_t j : data[i]) {
if (this->data[j] == this->data[median] or
DataType::compare(this->data[j], this->data[median],
depth % this->dim)) {
left_data[i].emplace_back(j);
} else {
right_data[i].emplace_back(j);
}
}
}
// std::cout << "Left: " << left_data[0].size() << ", Right: " <<
// right_data[0].size() << std::endl;
assert(left_data[0].size() > 0 and right_data[0].size() > 0);
Node *node = create_node(depth % this->dim, median);
node->left = build(left_data, depth + 1);
node->right = build(right_data, depth + 1);
return node;
}
}
void build_kd_tree(std::vector<DataType> &data) {
// std::cout << "Start Build..." << std::endl;
assert(data.size() > 0);
this->data = data;
this->leaf_dim = this->dim;
std::vector<std::vector<size_t>> _data(this->dim,
std::vector<size_t>(data.size()));
for (size_t i = 0; i < this->dim; i++) {
std::iota(_data[i].begin(), _data[i].end(), 0);
sort(_data[i].begin(), _data[i].end(),
[&](const size_t &a, const size_t &b) {
return DataType::compare(this->data[a], this->data[b], i);
});
}
this->root = build(_data, 0);
// std::cout << "End Build" << std::endl;
}
void report_subtree(Node *node, std::vector<size_t> &output) const {
if (node->dim == this->leaf_dim) {
output.emplace_back(node->idx);
} else {
report_subtree(node->left, output);
report_subtree(node->right, output);
}
}
std::vector<size_t> range_query(Region<T> query_region) {
T node_low, node_high;
node_low.fill(this->min_range);
node_high.fill(this->max_range);
Region<T> node_region(node_low, node_high);
std::vector<size_t> output;
query(this->root, query_region, node_region, output);
return output;
}
void query(Node *node, Region<T> query_region, Region<T> node_region,
std::vector<size_t> &output) {
// std::cout << "(Node, Dim) = (" << node->idx << ',' << node->dim << "), "
// << node_region << ' ' << query_region << std::endl;
T data = this->data[node->idx].data;
if (node->dim == this->leaf_dim) {
if (query_region.include(data)) {
output.emplace_back(node->idx);
}
} else {
Region<T> left_region = node_region, right_region = node_region;
Region<T> left_query_region = query_region,
right_query_region = query_region;
left_region.high[node->dim] = data[node->dim];
right_region.low[node->dim] = data[node->dim];
if (query_region.low[node->dim] <= data[node->dim] and
data[node->dim] < query_region.high[node->dim]) {
left_query_region.high[node->dim] = data[node->dim];
}
if (data[node->dim] <= query_region.high[node->dim] and
query_region.low[node->dim] < data[node->dim]) {
right_query_region.low[node->dim] = data[node->dim];
}
// std::cout << left_region << ' ' << right_region << std::endl;
// std::cout << left_query_region << ' ' << right_query_region <<
// std::endl;
if (left_query_region.include(left_region)) {
report_subtree(node->left, output);
} else if (left_region.overlap(left_query_region)) {
query(node->left, left_query_region, left_region, output);
}
if (right_query_region.include(right_region)) {
report_subtree(node->right, output);
} else if (right_region.overlap(right_query_region)) {
query(node->right, right_query_region, right_region, output);
}
}
}
void output() { output_kdtree(this->root); }
void output_kdtree(Node *node) {
std::cout << "idx: " << node->idx << ", (";
for (size_t i = 0; i < this->dim; i++) {
if (i > 0) {
std::cout << ',';
}
std::cout << this->data[node->idx].data[i];
}
std::cout << ") ";
if (node->dim != this->leaf_dim) {
std::cout << "(left, right) = (" << node->left->idx << ','
<< node->right->idx << ')' << std::endl;
output_kdtree(node->left);
output_kdtree(node->right);
} else {
std::cout << std::endl;
}
}
};
int main() {
// use scanf in CodeForces!
cin.tie(0);
ios_base::sync_with_stdio(false);
using DataType = Data<std::array<int, 2>>;
KDtree<DataType> kdtree;
std::vector<DataType> points;
int N;
cin >> N;
REP(i, N) {
int x, y;
cin >> x >> y;
DataType::type tmp = {x, y};
points.emplace_back(DataType(tmp));
}
kdtree.build_kd_tree(points);
std::vector<size_t> output;
kdtree.report_subtree(kdtree.root, output);
// for (size_t i : output) std::cout << i << ' ';
// std::cout << std::endl;
// kdtree.output();
///*
int Q;
cin >> Q;
REP(i, Q) {
int sx, tx, sy, ty;
std::cin >> sx >> tx >> sy >> ty;
DataType::type low = {sx, sy}, high = {tx, ty};
Region<DataType::type> query_region(low, high);
std::vector<size_t> out = kdtree.range_query(query_region);
sort(ALL(out));
for (size_t i : out)
printf("%d\n", i);
printf("\n");
}
//*/
return 0;
}
|
replace
| 295 | 297 | 295 | 297 |
TLE
| |
p02347
|
C++
|
Time Limit Exceeded
|
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
class Node {
public:
int p, location, l, r;
};
class Point {
public:
int id;
long x, y;
Point() {}
Point(int id, long x, long y) {
this->id = id;
this->x = x;
this->y = y;
}
bool operator<(const Point &another) const { return id < another.id; }
};
const int MAX_N = 500000;
Node T[MAX_N];
Point P[MAX_N];
int N, NP = 0;
vector<Point> ANS;
bool compX(const Point &a, const Point &b) { return a.x < b.x; }
bool compY(const Point &a, const Point &b) { return a.y < b.y; }
int make1DTree(int l, int r, int d) {
if (l >= r)
return -1;
int m = (l + r) / 2;
int t = NP++;
if (d % 2 == 0)
sort(P + l, P + r, compX);
else
sort(P + l, P + r, compY);
T[t].location = m;
T[t].l = make1DTree(l, m, d + 1);
T[t].r = make1DTree(m + 1, r, d + 1);
return t;
}
void find(int v, long sx, long tx, long sy, long ty, int d) {
long x = P[T[v].location].x;
long y = P[T[v].location].y;
if (sx <= x && x <= tx && sy <= y && y <= ty)
ANS.push_back(P[T[v].location]);
if (d % 2 == 0) {
if (T[v].l != -1 && sx <= x)
find(T[v].l, sx, tx, sy, ty, d + 1);
if (T[v].r != -1 && x <= tx)
find(T[v].r, sx, tx, sy, ty, d + 1);
} else {
if (T[v].l != -1 && sy <= y)
find(T[v].l, sx, tx, sy, ty, d + 1);
if (T[v].r != -1 && y <= ty)
find(T[v].r, sx, tx, sy, ty, d + 1);
}
}
int main() {
cin >> N;
for (int i = 0; i < N; i++) {
long x, y;
scanf("%ld %ld", &x, &y);
// cin >> x >> y;
Point p(i, x, y);
P[i] = p;
T[i].l = T[i].r = T[i].p = -1;
}
int root = make1DTree(0, N, 0);
int q;
cin >> q;
for (int i = 0; i < q; i++) {
long sx, tx, sy, ty;
scanf("%ld %ld %ld %ld", &sx, &tx, &sy, &ty);
// cin >> sx >> tx >> sy >> ty;
find(root, sx, tx, sy, ty, 0);
sort(ANS.begin(), ANS.end());
for (int j = 0; j < ANS.size(); j++)
cout << ANS[j].id << endl;
cout << endl;
ANS.clear();
}
return 0;
}
|
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
class Node {
public:
int p, location, l, r;
};
class Point {
public:
int id;
long x, y;
Point() {}
Point(int id, long x, long y) {
this->id = id;
this->x = x;
this->y = y;
}
bool operator<(const Point &another) const { return id < another.id; }
};
const int MAX_N = 500000;
Node T[MAX_N];
Point P[MAX_N];
int N, NP = 0;
vector<Point> ANS;
bool compX(const Point &a, const Point &b) { return a.x < b.x; }
bool compY(const Point &a, const Point &b) { return a.y < b.y; }
int make1DTree(int l, int r, int d) {
if (l >= r)
return -1;
int m = (l + r) / 2;
int t = NP++;
if (d % 2 == 0)
sort(P + l, P + r, compX);
else
sort(P + l, P + r, compY);
T[t].location = m;
T[t].l = make1DTree(l, m, d + 1);
T[t].r = make1DTree(m + 1, r, d + 1);
return t;
}
void find(int v, long sx, long tx, long sy, long ty, int d) {
long x = P[T[v].location].x;
long y = P[T[v].location].y;
if (sx <= x && x <= tx && sy <= y && y <= ty)
ANS.push_back(P[T[v].location]);
if (d % 2 == 0) {
if (T[v].l != -1 && sx <= x)
find(T[v].l, sx, tx, sy, ty, d + 1);
if (T[v].r != -1 && x <= tx)
find(T[v].r, sx, tx, sy, ty, d + 1);
} else {
if (T[v].l != -1 && sy <= y)
find(T[v].l, sx, tx, sy, ty, d + 1);
if (T[v].r != -1 && y <= ty)
find(T[v].r, sx, tx, sy, ty, d + 1);
}
}
int main() {
cin >> N;
for (int i = 0; i < N; i++) {
long x, y;
scanf("%ld %ld", &x, &y);
// cin >> x >> y;
Point p(i, x, y);
P[i] = p;
T[i].l = T[i].r = T[i].p = -1;
}
int root = make1DTree(0, N, 0);
int q;
cin >> q;
for (int i = 0; i < q; i++) {
long sx, tx, sy, ty;
scanf("%ld %ld %ld %ld", &sx, &tx, &sy, &ty);
// cin >> sx >> tx >> sy >> ty;
find(root, sx, tx, sy, ty, 0);
sort(ANS.begin(), ANS.end());
for (int j = 0; j < ANS.size(); j++)
printf("%d\n", ANS[j].id);
// cout << ANS[j].id << endl;
cout << endl;
ANS.clear();
}
return 0;
}
|
replace
| 102 | 103 | 102 | 104 |
TLE
| |
p02347
|
C++
|
Time Limit Exceeded
|
#include <algorithm>
#include <array>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
using ll = long long int;
using ull = unsigned long long int;
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define rrep(i, a, b) for (int i = (a); i > (b); --i)
#define REP(i, a, b) for (int i = (a); i <= (b); ++i)
#define RREP(i, a, b) for (int i = (a); i >= (b); --i)
#define llrep(i, a, b) for (ll i = (a); i < (b); ++i)
#define llrrep(i, a, b) for (ll i = (a); i > (b); --i)
#define llREP(i, a, b) for (ll i = (a); i <= (b); ++i)
#define llRREP(i, a, b) for (ll i = (a); i >= (b); --i)
#define ullrep(i, a, b) for (ull i = (a); i < (b); ++i)
#define ullrrep(i, a, b) for (ull i = (a); i > (b); --i)
#define ullREP(i, a, b) for (ull i = (a); i <= (b); ++i)
#define ullRREP(i, a, b) for (ull i = (a); i >= (b); --i)
template <typename T = int> class UnionFind {
public:
explicit UnionFind(unsigned long long n) : rank_(n, 0), p_(n, 0) {
for (T i = 0; i < n; i++) {
MakeSet(i);
}
}
void MakeSet(T x) {
p_[x] = x;
rank_[x] = 0;
}
bool Same(T x, T y) { return FindSet(x) == FindSet(y); }
void Link(T x, T y) {
if (rank_[x] > rank_[y]) {
p_[y] = x;
} else {
p_[x] = y;
if (rank_[x] == rank_[y]) {
rank_[y]++;
}
}
}
void Unite(T x, T y) { Link(FindSet(x), FindSet(y)); }
T FindSet(T x) {
if (x != p_[x]) {
p_[x] = FindSet(p_[x]);
}
return p_[x];
}
private:
std::vector<T> rank_, p_;
};
template <typename NumT = ull, typename CostT = ull> class Dijkstra {
public:
Dijkstra(unsigned long long num, CostT max_cost)
: num_(num), max_cost_(max_cost),
v_(num, std::vector<CostT>(num, max_cost)), c_(num, 0),
d_(num, max_cost), p_(num, 0){};
void ComputeWithQ(NumT start) {
d_[start] = 0;
p_[start] = start;
priority_queue<std::pair<CostT, NumT>> pq;
pq.push(make_pair(0, start));
while (!pq.empty()) {
auto node = pq.top();
pq.pop();
auto ni = node.second;
auto c = -node.first;
c_[ni] = 1;
if (d_[ni] < c) {
continue;
}
d_[ni] = c;
for (int i = 0; i < num_; i++) {
if (v_[ni][i] == max_cost_) {
continue;
}
if ((c_[i] != 1) && (d_[i] > (v_[ni][i] + d_[ni]))) {
p_[i] = ni;
d_[i] = v_[ni][i] + d_[ni];
pq.push(make_pair(-d_[i], i));
}
}
}
}
void Compute(int start) {
d_[start] = 0;
p_[start] = start;
while (true) {
int m = -1;
int min_d = max_cost_;
for (int i = 0; i < num_; i++) {
if ((c_[i] == 0) && (min_d > d_[i])) {
min_d = d_[i];
m = i;
}
}
if (m < 0) {
break;
}
c_[m] = 1;
for (int i = 0; i < num_; i++) {
if ((c_[i] == 0) && (d_[i] > (d_[m] + v_[m][i]))) {
d_[i] = d_[m] + v_[m][i];
p_[i] = m;
}
}
}
}
void Set(NumT s, NumT d, CostT c) { v_[s][d] = c; }
CostT Distance(NumT d) { return d_[d]; }
public:
NumT num_;
CostT max_cost_;
std::vector<std::vector<CostT>> v_;
std::vector<int> c_;
std::vector<int> d_;
std::vector<NumT> p_;
};
int nibutan(int *ary, int ok, int ng) {
bool is_valid;
while (std::abs(ok - ng) > 1) {
int mid = (ok + ng) / 2;
// is_valid = check(mid);
if (is_valid) {
ok = mid;
} else {
ng = mid;
}
}
return ok;
}
template <typename NumT = int, typename CostT = unsigned long long>
class DijkstraQ {
public:
DijkstraQ(unsigned long long num, CostT max_cost)
: adj_(num, std::vector<std::pair<NumT, CostT>>()), c_(num, 0),
d_(num, max_cost) {}
void Compute(NumT start) {
std::priority_queue<std::pair<CostT, NumT>> q;
d_[start] = 0;
q.push(std::make_pair(0, start));
while (!q.empty()) {
std::pair<CostT, NumT> node = q.top();
q.pop();
auto n = node.second;
auto c = -node.first;
c_[n] = 1;
if (c > d_[n]) {
continue;
}
d_[n] = c;
for (NumT i = 0; i < adj_[n].size(); i++) {
auto nn = adj_[n][i].first;
auto nc = adj_[n][i].second;
if (c_[nn] > 0) {
continue;
}
if (d_[nn] > (c + nc)) {
d_[nn] = c + nc;
q.push(std::make_pair(-d_[nn], nn));
}
}
}
}
void Set(NumT s, NumT d, CostT c) { adj_[s].push_back(std::make_pair(d, c)); }
CostT Distance(NumT d) { return d_[d]; }
std::vector<CostT> d_;
std::vector<int> c_;
std::vector<std::vector<std::pair<NumT, CostT>>> adj_;
};
// x >= y
template <typename T> inline T gcd(T x, T y) { return y ? gcd(y, x % y) : x; }
template <typename T> inline T lcm(T x, T y) { return (x / gcd(x, y)) * y; }
// return gcd(a, b)
// x, y satisfy ax + by = gcd(a, b)
ll xgcd(ll a, ll b, ll &x, ll &y) {
if (b == 0) {
x = 1;
y = 0;
return a;
}
ll d = xgcd(b, a % b, y, x);
y -= a / b * x;
return d;
}
bool check(int *A, int i) { return A[i] < 39; }
int N;
int A[110][110];
int C[110];
int D[110];
class D2Tree {
public:
struct node {
int id, x, y;
};
struct tree {
int location, l, r;
};
D2Tree(int n) : v_(n, {-1, -1, -1}), t_(n, {-1, -1, -1}), n_(n), np_(0) {}
int MakeTree(int l, int r, int depth) {
if (l >= r) {
return -1;
}
int m = (l + r) / 2;
int t = np_++;
if (depth % 2 == 0) {
sort(&v_[l], &v_[r], [&](auto &l, auto &r) { return l.x < r.x; });
} else {
sort(&v_[l], &v_[r], [&](auto &l, auto &r) { return l.y < r.y; });
}
t_[t].location = m;
t_[t].l = MakeTree(l, m, depth + 1);
t_[t].r = MakeTree(m + 1, r, depth + 1);
return t;
}
void find(vector<int> &find_nodes, int ix, int sx, int tx, int sy, int ty,
int depth) {
int x = v_[t_[ix].location].x;
int y = v_[t_[ix].location].y;
if ((sx <= x) && (x <= tx) && (sy <= y) && (y <= ty)) {
find_nodes.push_back(v_[t_[ix].location].id);
}
if (depth % 2 == 0) {
if ((t_[ix].l != -1) && (sx <= x)) {
find(find_nodes, t_[ix].l, sx, tx, sy, ty, depth + 1);
}
if ((t_[ix].r != -1) && (x <= tx)) {
find(find_nodes, t_[ix].r, sx, tx, sy, ty, depth + 1);
}
} else {
if ((t_[ix].l != -1) && (sy <= y)) {
find(find_nodes, t_[ix].l, sx, tx, sy, ty, depth + 1);
}
if ((t_[ix].r != -1) && (y <= ty)) {
find(find_nodes, t_[ix].r, sx, tx, sy, ty, depth + 1);
}
}
}
vector<node> v_;
vector<tree> t_;
int n_;
int np_;
};
int main() {
int N;
cin >> N;
D2Tree d2(N);
rep(i, 0, N) {
int x, y;
std::scanf("%d %d", &d2.v_[i].x, &d2.v_[i].y);
d2.v_[i].id = i;
}
int q;
cin >> q;
d2.MakeTree(0, N, 0);
vector<int> v;
rep(i, 0, q) {
v.clear();
int sx, tx, sy, ty;
std::scanf("%d %d %d %d", &sx, &tx, &sy, &ty);
d2.find(v, 0, sx, tx, sy, ty, 0);
sort(v.begin(), v.end());
for (auto i : v) {
cout << i << endl;
}
cout << endl;
}
}
|
#include <algorithm>
#include <array>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
using ll = long long int;
using ull = unsigned long long int;
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define rrep(i, a, b) for (int i = (a); i > (b); --i)
#define REP(i, a, b) for (int i = (a); i <= (b); ++i)
#define RREP(i, a, b) for (int i = (a); i >= (b); --i)
#define llrep(i, a, b) for (ll i = (a); i < (b); ++i)
#define llrrep(i, a, b) for (ll i = (a); i > (b); --i)
#define llREP(i, a, b) for (ll i = (a); i <= (b); ++i)
#define llRREP(i, a, b) for (ll i = (a); i >= (b); --i)
#define ullrep(i, a, b) for (ull i = (a); i < (b); ++i)
#define ullrrep(i, a, b) for (ull i = (a); i > (b); --i)
#define ullREP(i, a, b) for (ull i = (a); i <= (b); ++i)
#define ullRREP(i, a, b) for (ull i = (a); i >= (b); --i)
template <typename T = int> class UnionFind {
public:
explicit UnionFind(unsigned long long n) : rank_(n, 0), p_(n, 0) {
for (T i = 0; i < n; i++) {
MakeSet(i);
}
}
void MakeSet(T x) {
p_[x] = x;
rank_[x] = 0;
}
bool Same(T x, T y) { return FindSet(x) == FindSet(y); }
void Link(T x, T y) {
if (rank_[x] > rank_[y]) {
p_[y] = x;
} else {
p_[x] = y;
if (rank_[x] == rank_[y]) {
rank_[y]++;
}
}
}
void Unite(T x, T y) { Link(FindSet(x), FindSet(y)); }
T FindSet(T x) {
if (x != p_[x]) {
p_[x] = FindSet(p_[x]);
}
return p_[x];
}
private:
std::vector<T> rank_, p_;
};
template <typename NumT = ull, typename CostT = ull> class Dijkstra {
public:
Dijkstra(unsigned long long num, CostT max_cost)
: num_(num), max_cost_(max_cost),
v_(num, std::vector<CostT>(num, max_cost)), c_(num, 0),
d_(num, max_cost), p_(num, 0){};
void ComputeWithQ(NumT start) {
d_[start] = 0;
p_[start] = start;
priority_queue<std::pair<CostT, NumT>> pq;
pq.push(make_pair(0, start));
while (!pq.empty()) {
auto node = pq.top();
pq.pop();
auto ni = node.second;
auto c = -node.first;
c_[ni] = 1;
if (d_[ni] < c) {
continue;
}
d_[ni] = c;
for (int i = 0; i < num_; i++) {
if (v_[ni][i] == max_cost_) {
continue;
}
if ((c_[i] != 1) && (d_[i] > (v_[ni][i] + d_[ni]))) {
p_[i] = ni;
d_[i] = v_[ni][i] + d_[ni];
pq.push(make_pair(-d_[i], i));
}
}
}
}
void Compute(int start) {
d_[start] = 0;
p_[start] = start;
while (true) {
int m = -1;
int min_d = max_cost_;
for (int i = 0; i < num_; i++) {
if ((c_[i] == 0) && (min_d > d_[i])) {
min_d = d_[i];
m = i;
}
}
if (m < 0) {
break;
}
c_[m] = 1;
for (int i = 0; i < num_; i++) {
if ((c_[i] == 0) && (d_[i] > (d_[m] + v_[m][i]))) {
d_[i] = d_[m] + v_[m][i];
p_[i] = m;
}
}
}
}
void Set(NumT s, NumT d, CostT c) { v_[s][d] = c; }
CostT Distance(NumT d) { return d_[d]; }
public:
NumT num_;
CostT max_cost_;
std::vector<std::vector<CostT>> v_;
std::vector<int> c_;
std::vector<int> d_;
std::vector<NumT> p_;
};
int nibutan(int *ary, int ok, int ng) {
bool is_valid;
while (std::abs(ok - ng) > 1) {
int mid = (ok + ng) / 2;
// is_valid = check(mid);
if (is_valid) {
ok = mid;
} else {
ng = mid;
}
}
return ok;
}
template <typename NumT = int, typename CostT = unsigned long long>
class DijkstraQ {
public:
DijkstraQ(unsigned long long num, CostT max_cost)
: adj_(num, std::vector<std::pair<NumT, CostT>>()), c_(num, 0),
d_(num, max_cost) {}
void Compute(NumT start) {
std::priority_queue<std::pair<CostT, NumT>> q;
d_[start] = 0;
q.push(std::make_pair(0, start));
while (!q.empty()) {
std::pair<CostT, NumT> node = q.top();
q.pop();
auto n = node.second;
auto c = -node.first;
c_[n] = 1;
if (c > d_[n]) {
continue;
}
d_[n] = c;
for (NumT i = 0; i < adj_[n].size(); i++) {
auto nn = adj_[n][i].first;
auto nc = adj_[n][i].second;
if (c_[nn] > 0) {
continue;
}
if (d_[nn] > (c + nc)) {
d_[nn] = c + nc;
q.push(std::make_pair(-d_[nn], nn));
}
}
}
}
void Set(NumT s, NumT d, CostT c) { adj_[s].push_back(std::make_pair(d, c)); }
CostT Distance(NumT d) { return d_[d]; }
std::vector<CostT> d_;
std::vector<int> c_;
std::vector<std::vector<std::pair<NumT, CostT>>> adj_;
};
// x >= y
template <typename T> inline T gcd(T x, T y) { return y ? gcd(y, x % y) : x; }
template <typename T> inline T lcm(T x, T y) { return (x / gcd(x, y)) * y; }
// return gcd(a, b)
// x, y satisfy ax + by = gcd(a, b)
ll xgcd(ll a, ll b, ll &x, ll &y) {
if (b == 0) {
x = 1;
y = 0;
return a;
}
ll d = xgcd(b, a % b, y, x);
y -= a / b * x;
return d;
}
bool check(int *A, int i) { return A[i] < 39; }
int N;
int A[110][110];
int C[110];
int D[110];
class D2Tree {
public:
struct node {
int id, x, y;
};
struct tree {
int location, l, r;
};
D2Tree(int n) : v_(n, {-1, -1, -1}), t_(n, {-1, -1, -1}), n_(n), np_(0) {}
int MakeTree(int l, int r, int depth) {
if (l >= r) {
return -1;
}
int m = (l + r) / 2;
int t = np_++;
if (depth % 2 == 0) {
sort(&v_[l], &v_[r], [&](auto &l, auto &r) { return l.x < r.x; });
} else {
sort(&v_[l], &v_[r], [&](auto &l, auto &r) { return l.y < r.y; });
}
t_[t].location = m;
t_[t].l = MakeTree(l, m, depth + 1);
t_[t].r = MakeTree(m + 1, r, depth + 1);
return t;
}
void find(vector<int> &find_nodes, int ix, int sx, int tx, int sy, int ty,
int depth) {
int x = v_[t_[ix].location].x;
int y = v_[t_[ix].location].y;
if ((sx <= x) && (x <= tx) && (sy <= y) && (y <= ty)) {
find_nodes.push_back(v_[t_[ix].location].id);
}
if (depth % 2 == 0) {
if ((t_[ix].l != -1) && (sx <= x)) {
find(find_nodes, t_[ix].l, sx, tx, sy, ty, depth + 1);
}
if ((t_[ix].r != -1) && (x <= tx)) {
find(find_nodes, t_[ix].r, sx, tx, sy, ty, depth + 1);
}
} else {
if ((t_[ix].l != -1) && (sy <= y)) {
find(find_nodes, t_[ix].l, sx, tx, sy, ty, depth + 1);
}
if ((t_[ix].r != -1) && (y <= ty)) {
find(find_nodes, t_[ix].r, sx, tx, sy, ty, depth + 1);
}
}
}
vector<node> v_;
vector<tree> t_;
int n_;
int np_;
};
int main() {
int N;
cin >> N;
D2Tree d2(N);
rep(i, 0, N) {
int x, y;
std::scanf("%d %d", &d2.v_[i].x, &d2.v_[i].y);
d2.v_[i].id = i;
}
int q;
cin >> q;
d2.MakeTree(0, N, 0);
vector<int> v;
rep(i, 0, q) {
v.clear();
int sx, tx, sy, ty;
std::scanf("%d %d %d %d", &sx, &tx, &sy, &ty);
d2.find(v, 0, sx, tx, sy, ty, 0);
sort(v.begin(), v.end());
for (auto i : v) {
printf("%d\n", i);
}
printf("\n");
}
}
|
replace
| 314 | 317 | 314 | 317 |
TLE
| |
p02347
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef pair<int, int> P;
vector<P> dat[1000001];
struct st {
int a, b, c;
} p[500000];
bool operator<(st a, st b) { return P(a.a, a.b) < P(b.a, b.b); }
void init(int k, int l, int r) {
if (r - l == 1) {
dat[k].push_back(P(p[l].b, p[l].c));
return;
}
int lb = k * 2 + 1, rb = k * 2 + 2;
init(lb, l, (l + r) / 2);
init(rb, (l + r) / 2, r);
dat[k].resize(r - l);
merge(dat[lb].begin(), dat[lb].end(), dat[rb].begin(), dat[rb].end(),
dat[k].begin());
}
vector<int> query(int a, int b, int c, int d, int k, int l, int r) {
if (b <= l || r <= a)
return vector<int>();
if (a <= l && r <= b) {
int u = upper_bound(dat[k].begin(), dat[k].end(), P(d, INT_MAX)) -
dat[k].begin();
int v = upper_bound(dat[k].begin(), dat[k].end(), P(c, INT_MIN)) -
dat[k].begin();
vector<int> res;
for (int i = v; i < u; i++)
res.push_back(dat[k][i].second);
return res;
}
auto lb = query(a, b, c, d, k * 2 + 1, l, (l + r) / 2);
auto rb = query(a, b, c, d, k * 2 + 2, (l + r) / 2, r);
for (int i : rb)
lb.push_back(i);
return lb;
}
int main() {
int n;
scanf("%d", &n);
rep(i, n) scanf("%d%d", &p[i].a, &p[i].b), p[i].c = i;
sort(p, p + n);
init(0, 0, n);
int q;
scanf("%d", &q);
rep(i, q) {
int sx, tx, sy, ty;
scanf("%d%d%d%d", &sx, &tx, &sy, &ty);
int a = lower_bound(p, p + n, st{sx, INT_MIN, 0}) - p;
int b = lower_bound(p, p + n, st{tx, INT_MAX, 0}) - p;
auto res = query(a, b, sy, ty, 0, 0, n);
sort(res.begin(), res.end());
for (int j : res)
printf("%d\n", j);
puts("");
}
}
|
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef pair<int, int> P;
vector<P> dat[1500000];
struct st {
int a, b, c;
} p[500000];
bool operator<(st a, st b) { return P(a.a, a.b) < P(b.a, b.b); }
void init(int k, int l, int r) {
if (r - l == 1) {
dat[k].push_back(P(p[l].b, p[l].c));
return;
}
int lb = k * 2 + 1, rb = k * 2 + 2;
init(lb, l, (l + r) / 2);
init(rb, (l + r) / 2, r);
dat[k].resize(r - l);
merge(dat[lb].begin(), dat[lb].end(), dat[rb].begin(), dat[rb].end(),
dat[k].begin());
}
vector<int> query(int a, int b, int c, int d, int k, int l, int r) {
if (b <= l || r <= a)
return vector<int>();
if (a <= l && r <= b) {
int u = upper_bound(dat[k].begin(), dat[k].end(), P(d, INT_MAX)) -
dat[k].begin();
int v = upper_bound(dat[k].begin(), dat[k].end(), P(c, INT_MIN)) -
dat[k].begin();
vector<int> res;
for (int i = v; i < u; i++)
res.push_back(dat[k][i].second);
return res;
}
auto lb = query(a, b, c, d, k * 2 + 1, l, (l + r) / 2);
auto rb = query(a, b, c, d, k * 2 + 2, (l + r) / 2, r);
for (int i : rb)
lb.push_back(i);
return lb;
}
int main() {
int n;
scanf("%d", &n);
rep(i, n) scanf("%d%d", &p[i].a, &p[i].b), p[i].c = i;
sort(p, p + n);
init(0, 0, n);
int q;
scanf("%d", &q);
rep(i, q) {
int sx, tx, sy, ty;
scanf("%d%d%d%d", &sx, &tx, &sy, &ty);
int a = lower_bound(p, p + n, st{sx, INT_MIN, 0}) - p;
int b = lower_bound(p, p + n, st{tx, INT_MAX, 0}) - p;
auto res = query(a, b, sy, ty, 0, 0, n);
sort(res.begin(), res.end());
for (int j : res)
printf("%d\n", j);
puts("");
}
}
|
replace
| 5 | 6 | 5 | 6 |
0
| |
p02347
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef pair<int, int> P;
vector<P> dat[1010000];
struct st {
int a, b, c;
} p[500000];
bool operator<(st a, st b) { return P(a.a, a.b) < P(b.a, b.b); }
void init(int k, int l, int r) {
if (r - l == 1) {
dat[k].push_back(P(p[l].b, p[l].c));
return;
}
int lb = k * 2 + 1, rb = k * 2 + 2;
init(lb, l, (l + r) / 2);
init(rb, (l + r) / 2, r);
dat[k].resize(r - l);
merge(dat[lb].begin(), dat[lb].end(), dat[rb].begin(), dat[rb].end(),
dat[k].begin());
}
vector<int> query(int a, int b, int c, int d, int k, int l, int r) {
if (b <= l || r <= a)
return vector<int>();
if (a <= l && r <= b) {
int u = upper_bound(dat[k].begin(), dat[k].end(), P(d, INT_MAX)) -
dat[k].begin();
int v = upper_bound(dat[k].begin(), dat[k].end(), P(c, INT_MIN)) -
dat[k].begin();
vector<int> res;
for (int i = v; i < u; i++)
res.push_back(dat[k][i].second);
return res;
}
auto lb = query(a, b, c, d, k * 2 + 1, l, (l + r) / 2);
auto rb = query(a, b, c, d, k * 2 + 2, (l + r) / 2, r);
for (int i : rb)
lb.push_back(i);
return lb;
}
int main() {
int n;
scanf("%d", &n);
rep(i, n) scanf("%d%d", &p[i].a, &p[i].b), p[i].c = i;
sort(p, p + n);
init(0, 0, n);
int q;
scanf("%d", &q);
rep(i, q) {
int sx, tx, sy, ty;
scanf("%d%d%d%d", &sx, &tx, &sy, &ty);
int a = lower_bound(p, p + n, st{sx, INT_MIN, 0}) - p;
int b = lower_bound(p, p + n, st{tx, INT_MAX, 0}) - p;
auto res = query(a, b, sy, ty, 0, 0, n);
sort(res.begin(), res.end());
for (int j : res)
printf("%d\n", j);
puts("");
}
}
|
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef pair<int, int> P;
vector<P> dat[1050000];
struct st {
int a, b, c;
} p[500000];
bool operator<(st a, st b) { return P(a.a, a.b) < P(b.a, b.b); }
void init(int k, int l, int r) {
if (r - l == 1) {
dat[k].push_back(P(p[l].b, p[l].c));
return;
}
int lb = k * 2 + 1, rb = k * 2 + 2;
init(lb, l, (l + r) / 2);
init(rb, (l + r) / 2, r);
dat[k].resize(r - l);
merge(dat[lb].begin(), dat[lb].end(), dat[rb].begin(), dat[rb].end(),
dat[k].begin());
}
vector<int> query(int a, int b, int c, int d, int k, int l, int r) {
if (b <= l || r <= a)
return vector<int>();
if (a <= l && r <= b) {
int u = upper_bound(dat[k].begin(), dat[k].end(), P(d, INT_MAX)) -
dat[k].begin();
int v = upper_bound(dat[k].begin(), dat[k].end(), P(c, INT_MIN)) -
dat[k].begin();
vector<int> res;
for (int i = v; i < u; i++)
res.push_back(dat[k][i].second);
return res;
}
auto lb = query(a, b, c, d, k * 2 + 1, l, (l + r) / 2);
auto rb = query(a, b, c, d, k * 2 + 2, (l + r) / 2, r);
for (int i : rb)
lb.push_back(i);
return lb;
}
int main() {
int n;
scanf("%d", &n);
rep(i, n) scanf("%d%d", &p[i].a, &p[i].b), p[i].c = i;
sort(p, p + n);
init(0, 0, n);
int q;
scanf("%d", &q);
rep(i, q) {
int sx, tx, sy, ty;
scanf("%d%d%d%d", &sx, &tx, &sy, &ty);
int a = lower_bound(p, p + n, st{sx, INT_MIN, 0}) - p;
int b = lower_bound(p, p + n, st{tx, INT_MAX, 0}) - p;
auto res = query(a, b, sy, ty, 0, 0, n);
sort(res.begin(), res.end());
for (int j : res)
printf("%d\n", j);
puts("");
}
}
|
replace
| 5 | 6 | 5 | 6 |
0
| |
p02347
|
C++
|
Memory Limit Exceeded
|
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef pair<int, int> P;
vector<P> dat[10000000];
struct st {
int a, b, c;
} p[500000];
bool operator<(st a, st b) { return P(a.a, a.b) < P(b.a, b.b); }
void init(int k, int l, int r) {
if (r - l == 1) {
dat[k].push_back(P(p[l].b, p[l].c));
return;
}
int lb = k * 2 + 1, rb = k * 2 + 2;
init(lb, l, (l + r) / 2);
init(rb, (l + r) / 2, r);
dat[k].resize(r - l);
merge(dat[lb].begin(), dat[lb].end(), dat[rb].begin(), dat[rb].end(),
dat[k].begin());
}
vector<int> query(int a, int b, int c, int d, int k, int l, int r) {
if (b <= l || r <= a)
return vector<int>();
if (a <= l && r <= b) {
int u = upper_bound(dat[k].begin(), dat[k].end(), P(d, INT_MAX)) -
dat[k].begin();
int v = upper_bound(dat[k].begin(), dat[k].end(), P(c, INT_MIN)) -
dat[k].begin();
vector<int> res;
for (int i = v; i < u; i++)
res.push_back(dat[k][i].second);
return res;
}
auto lb = query(a, b, c, d, k * 2 + 1, l, (l + r) / 2);
auto rb = query(a, b, c, d, k * 2 + 2, (l + r) / 2, r);
for (int i : rb)
lb.push_back(i);
return lb;
}
int main() {
int n;
scanf("%d", &n);
rep(i, n) scanf("%d%d", &p[i].a, &p[i].b), p[i].c = i;
sort(p, p + n);
init(0, 0, n);
int q;
scanf("%d", &q);
rep(i, q) {
int sx, tx, sy, ty;
scanf("%d%d%d%d", &sx, &tx, &sy, &ty);
int a = lower_bound(p, p + n, st{sx, INT_MIN, 0}) - p;
int b = lower_bound(p, p + n, st{tx, INT_MAX, 0}) - p;
auto res = query(a, b, sy, ty, 0, 0, n);
sort(res.begin(), res.end());
for (int j : res)
printf("%d\n", j);
puts("");
}
}
|
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef pair<int, int> P;
vector<P> dat[2000000];
struct st {
int a, b, c;
} p[500000];
bool operator<(st a, st b) { return P(a.a, a.b) < P(b.a, b.b); }
void init(int k, int l, int r) {
if (r - l == 1) {
dat[k].push_back(P(p[l].b, p[l].c));
return;
}
int lb = k * 2 + 1, rb = k * 2 + 2;
init(lb, l, (l + r) / 2);
init(rb, (l + r) / 2, r);
dat[k].resize(r - l);
merge(dat[lb].begin(), dat[lb].end(), dat[rb].begin(), dat[rb].end(),
dat[k].begin());
}
vector<int> query(int a, int b, int c, int d, int k, int l, int r) {
if (b <= l || r <= a)
return vector<int>();
if (a <= l && r <= b) {
int u = upper_bound(dat[k].begin(), dat[k].end(), P(d, INT_MAX)) -
dat[k].begin();
int v = upper_bound(dat[k].begin(), dat[k].end(), P(c, INT_MIN)) -
dat[k].begin();
vector<int> res;
for (int i = v; i < u; i++)
res.push_back(dat[k][i].second);
return res;
}
auto lb = query(a, b, c, d, k * 2 + 1, l, (l + r) / 2);
auto rb = query(a, b, c, d, k * 2 + 2, (l + r) / 2, r);
for (int i : rb)
lb.push_back(i);
return lb;
}
int main() {
int n;
scanf("%d", &n);
rep(i, n) scanf("%d%d", &p[i].a, &p[i].b), p[i].c = i;
sort(p, p + n);
init(0, 0, n);
int q;
scanf("%d", &q);
rep(i, q) {
int sx, tx, sy, ty;
scanf("%d%d%d%d", &sx, &tx, &sy, &ty);
int a = lower_bound(p, p + n, st{sx, INT_MIN, 0}) - p;
int b = lower_bound(p, p + n, st{tx, INT_MAX, 0}) - p;
auto res = query(a, b, sy, ty, 0, 0, n);
sort(res.begin(), res.end());
for (int j : res)
printf("%d\n", j);
puts("");
}
}
|
replace
| 5 | 6 | 5 | 6 |
MLE
| |
p02347
|
C++
|
Time Limit Exceeded
|
// Drawing heavily from 2579705 by vjudge2
// http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=2579705#1
#include <algorithm>
#include <iostream>
#include <set>
#include <vector>
using namespace std;
#define NMAX 500010
int dim = 0, tree_idx = 1;
struct Point {
int xy[2], id, left, right;
bool operator<(const Point &other) const { return xy[dim] < other.xy[dim]; }
} P[NMAX], tree[NMAX];
istream &operator>>(istream &is, Point &p) { return is >> p.xy[0] >> p.xy[1]; }
int Build(int L, int R, int d) {
int M = (L + R) / 2;
dim = d;
nth_element(P + L, P + M, P + R + 1); // now, P + M is the median of P+L...P+R
int curr = ++tree_idx;
tree[curr] = P[M]; // store values at internal nodes, too
if (L < M)
tree[curr].left = Build(L, M - 1, !d);
if (R > M)
tree[curr].right = Build(M + 1, R, !d);
// cout << "Built " << curr << " (" << tree[curr].xy[0] << ", "
// << tree[curr].xy[1] << ") " << tree[curr].left << " " <<
// tree[curr].right
// << endl;
return curr;
}
void Find(int curr, int d, int x1, int x2, int y1, int y2, set<int> &res) {
// cout << "Find " << curr << " " << d << endl;
if (curr == 0)
return;
if (tree[curr].xy[0] >= x1 and tree[curr].xy[0] <= x2 and
tree[curr].xy[1] >= y1 and tree[curr].xy[1] <= y2) {
// cout << "Match at " << curr << endl;
res.insert(tree[curr].id);
}
if (d == 0) {
if (x1 <= tree[curr].xy[0]) {
Find(tree[curr].left, !d, x1, x2, y1, y2, res);
}
if (x2 >= tree[curr].xy[0]) {
Find(tree[curr].right, !d, x1, x2, y1, y2, res);
}
} else {
if (y1 <= tree[curr].xy[1]) {
Find(tree[curr].left, !d, x1, x2, y1, y2, res);
}
if (y2 >= tree[curr].xy[1]) {
Find(tree[curr].right, !d, x1, x2, y1, y2, res);
}
}
}
int main() {
int n, q, x1, x2, y1, y2;
cin >> n;
for (int i = 0; i < n; ++i) {
P[i].id = i;
cin >> P[i];
}
int root = Build(0, n - 1, 0);
set<int> res;
cin >> q;
while (cin >> x1 >> x2 >> y1 >> y2) {
res.clear();
Find(root, 0, x1, x2, y1, y2, res);
for (int x : res)
cout << x << endl;
cout << endl;
}
}
|
// Drawing heavily from 2579705 by vjudge2
// http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=2579705#1
#include <algorithm>
#include <iostream>
#include <set>
#include <vector>
using namespace std;
#define NMAX 500010
int dim = 0, tree_idx = 1;
struct Point {
int xy[2], id, left, right;
bool operator<(const Point &other) const { return xy[dim] < other.xy[dim]; }
} P[NMAX], tree[NMAX];
istream &operator>>(istream &is, Point &p) { return is >> p.xy[0] >> p.xy[1]; }
int Build(int L, int R, int d) {
int M = (L + R) / 2;
dim = d;
nth_element(P + L, P + M, P + R + 1); // now, P + M is the median of P+L...P+R
int curr = ++tree_idx;
tree[curr] = P[M]; // store values at internal nodes, too
if (L < M)
tree[curr].left = Build(L, M - 1, !d);
if (R > M)
tree[curr].right = Build(M + 1, R, !d);
// cout << "Built " << curr << " (" << tree[curr].xy[0] << ", "
// << tree[curr].xy[1] << ") " << tree[curr].left << " " <<
// tree[curr].right
// << endl;
return curr;
}
void Find(int curr, int d, int x1, int x2, int y1, int y2, set<int> &res) {
// cout << "Find " << curr << " " << d << endl;
if (curr == 0)
return;
if (tree[curr].xy[0] >= x1 and tree[curr].xy[0] <= x2 and
tree[curr].xy[1] >= y1 and tree[curr].xy[1] <= y2) {
// cout << "Match at " << curr << endl;
res.insert(tree[curr].id);
}
if (d == 0) {
if (x1 <= tree[curr].xy[0]) {
Find(tree[curr].left, !d, x1, x2, y1, y2, res);
}
if (x2 >= tree[curr].xy[0]) {
Find(tree[curr].right, !d, x1, x2, y1, y2, res);
}
} else {
if (y1 <= tree[curr].xy[1]) {
Find(tree[curr].left, !d, x1, x2, y1, y2, res);
}
if (y2 >= tree[curr].xy[1]) {
Find(tree[curr].right, !d, x1, x2, y1, y2, res);
}
}
}
int main() {
cin.tie(0);
ios_base::sync_with_stdio(false);
int n, q, x1, x2, y1, y2;
cin >> n;
for (int i = 0; i < n; ++i) {
P[i].id = i;
cin >> P[i];
}
int root = Build(0, n - 1, 0);
set<int> res;
cin >> q;
while (cin >> x1 >> x2 >> y1 >> y2) {
res.clear();
Find(root, 0, x1, x2, y1, y2, res);
for (int x : res)
cout << x << endl;
cout << endl;
}
}
|
insert
| 62 | 62 | 62 | 64 |
TLE
| |
p02347
|
C++
|
Runtime Error
|
#include <iostream>
using namespace std;
#include <algorithm> // sort.
#include <cstdio> // scanf, printf.
#include <utility> // pair.
typedef pair<int, pair<int, int>> Point;
inline int pos_x(Point p) { return p.second.first; }
inline int pos_y(Point p) { return p.second.second; }
bool cmp_x(const Point &p1, const Point &p2) { return pos_x(p1) < pos_x(p2); }
bool cmp_y(const Point &p1, const Point &p2) { return pos_y(p1) < pos_y(p2); }
class Node {
public:
Point *mark;
Node *less, *more;
int depth;
Node() : mark(NULL), less(NULL), more(NULL), depth(0){};
Node(Point *pos, Node *l, Node *m, int d)
: mark(pos), less(l), more(m), depth(d){};
};
Node *make_tree(Point S[], int left, int right, int d) {
if (left > right)
return NULL;
if (left == right) {
Node *leaf = new Node;
*leaf = Node(&S[left], NULL, NULL, d);
return leaf;
}
if (d & 1) {
sort(S + left, S + right + 1, cmp_x);
} else {
sort(S + left, S + right + 1, cmp_y);
}
int mid = (left + right) / 2;
Node *kd_n = new Node;
kd_n->mark = &S[mid];
kd_n->less = make_tree(S, left, mid - 1, d + 1);
kd_n->more = make_tree(S, mid + 1, right, d + 1);
kd_n->depth = d;
return kd_n;
}
int cnt;
void search_points(Point S[], int sx, int tx, int sy, int ty, Node *kd_n) {
if (kd_n == NULL)
return;
int a = pos_x(*(kd_n->mark)), b = pos_y(*(kd_n->mark));
if (sx <= a && a <= tx && sy <= b && b <= ty) {
S[cnt] = *(kd_n->mark);
cnt++;
}
int d = kd_n->depth;
if (d & 1) {
if (sx <= a) {
search_points(S, sx, tx, sy, ty, kd_n->less);
}
if (a <= tx) {
search_points(S, sx, tx, sy, ty, kd_n->more);
}
} else {
if (sy <= b) {
search_points(S, sx, tx, sy, ty, kd_n->less);
}
if (b <= ty) {
search_points(S, sx, tx, sy, ty, kd_n->more);
}
}
return;
}
int main() {
int i, a, b, n;
Point P[500], Q[100];
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%d %d", &a, &b);
P[i] = make_pair(i, make_pair(a, b));
}
Node *root;
root = make_tree(P, 0, n - 1, 0);
int q, sx, tx, sy, ty;
scanf("%d", &q);
while (q) {
cnt = 0;
scanf("%d %d %d %d", &sx, &tx, &sy, &ty);
search_points(Q, sx, tx, sy, ty, root);
sort(Q, Q + cnt);
for (i = 0; i < cnt; i++) {
printf("%d\n", Q[i].first);
}
printf("\n");
q--;
}
return 0;
}
|
#include <iostream>
using namespace std;
#include <algorithm> // sort.
#include <cstdio> // scanf, printf.
#include <utility> // pair.
typedef pair<int, pair<int, int>> Point;
inline int pos_x(Point p) { return p.second.first; }
inline int pos_y(Point p) { return p.second.second; }
bool cmp_x(const Point &p1, const Point &p2) { return pos_x(p1) < pos_x(p2); }
bool cmp_y(const Point &p1, const Point &p2) { return pos_y(p1) < pos_y(p2); }
class Node {
public:
Point *mark;
Node *less, *more;
int depth;
Node() : mark(NULL), less(NULL), more(NULL), depth(0){};
Node(Point *pos, Node *l, Node *m, int d)
: mark(pos), less(l), more(m), depth(d){};
};
Node *make_tree(Point S[], int left, int right, int d) {
if (left > right)
return NULL;
if (left == right) {
Node *leaf = new Node;
*leaf = Node(&S[left], NULL, NULL, d);
return leaf;
}
if (d & 1) {
sort(S + left, S + right + 1, cmp_x);
} else {
sort(S + left, S + right + 1, cmp_y);
}
int mid = (left + right) / 2;
Node *kd_n = new Node;
kd_n->mark = &S[mid];
kd_n->less = make_tree(S, left, mid - 1, d + 1);
kd_n->more = make_tree(S, mid + 1, right, d + 1);
kd_n->depth = d;
return kd_n;
}
int cnt;
void search_points(Point S[], int sx, int tx, int sy, int ty, Node *kd_n) {
if (kd_n == NULL)
return;
int a = pos_x(*(kd_n->mark)), b = pos_y(*(kd_n->mark));
if (sx <= a && a <= tx && sy <= b && b <= ty) {
S[cnt] = *(kd_n->mark);
cnt++;
}
int d = kd_n->depth;
if (d & 1) {
if (sx <= a) {
search_points(S, sx, tx, sy, ty, kd_n->less);
}
if (a <= tx) {
search_points(S, sx, tx, sy, ty, kd_n->more);
}
} else {
if (sy <= b) {
search_points(S, sx, tx, sy, ty, kd_n->less);
}
if (b <= ty) {
search_points(S, sx, tx, sy, ty, kd_n->more);
}
}
return;
}
int main() {
int i, a, b, n;
Point P[500000], Q[100];
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%d %d", &a, &b);
P[i] = make_pair(i, make_pair(a, b));
}
Node *root;
root = make_tree(P, 0, n - 1, 0);
int q, sx, tx, sy, ty;
scanf("%d", &q);
while (q) {
cnt = 0;
scanf("%d %d %d %d", &sx, &tx, &sy, &ty);
search_points(Q, sx, tx, sy, ty, root);
sort(Q, Q + cnt);
for (i = 0; i < cnt; i++) {
printf("%d\n", Q[i].first);
}
printf("\n");
q--;
}
return 0;
}
|
replace
| 82 | 83 | 82 | 83 |
0
| |
p02347
|
C++
|
Runtime Error
|
#include "bits/stdc++.h"
using namespace std;
#ifdef _DEBUG
#include "dump.hpp"
#else
#define dump(...)
#endif
// #define int long long
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(c) begin(c), end(c)
const int INF =
sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9) + 7;
const int NIL = -1;
const double PI = acos(-1);
const double EPS = 1e-9;
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return true;
}
return false;
}
struct Point {
int x, y, id;
Point() {}
Point(int x, int y, int id = 0) : x(x), y(y), id(id) {}
bool operator<(const Point &p) const { return id < p.id; }
void print() { printf("%d\n", id); }
};
bool lessX(const Point &p1, const Point &p2) {
return (p1.x == p2.x) ? (p1.y < p2.y) : (p1.x < p2.x);
}
bool lessY(const Point &p1, const Point &p2) {
return (p1.y == p2.y) ? (p1.x < p2.x) : (p1.y < p2.y);
}
struct KDtree {
struct Node {
int index, l, r;
Node() : index(NIL), l(NIL), r(NIL) {}
};
int n, np, root;
vector<Point> points;
vector<Node> nodes;
KDtree(const vector<Point> &ps) : n(ps.size()), np(0), points(ps), nodes(n) {
root = makeKDtree(0, n, true);
}
int makeKDtree(int l, int r, bool is_x_base = true) {
if (!(l < r))
return NIL;
int t = np++, mid = (l + r) / 2;
vector<Point>::iterator first = points.begin() + l,
nth = points.begin() + mid,
last = points.begin() + r;
if (is_x_base)
nth_element(first, nth, last, lessX);
else
nth_element(first, nth, last, lessY);
nodes[t].index = mid;
nodes[t].l = makeKDtree(l, mid, !is_x_base);
nodes[t].r = makeKDtree(mid + 1, r, !is_x_base);
return t;
}
vector<Point> ret;
bool inrange(Point &p, Point &s, Point &t) {
return (s.x <= p.x && p.x <= t.x) && (s.y <= p.y && p.y <= t.y);
}
vector<Point> find(Point s, Point t) {
ret.clear();
return find(root, s, t, true);
}
vector<Point> find(int v, Point s, Point t, bool is_x_base = true) {
auto p = points[nodes[v].index];
if (inrange(p, s, t))
ret.emplace_back(p);
if (is_x_base) {
if (nodes[v].l != NIL && s.x <= p.x)
find(nodes[v].l, s, t, !is_x_base);
if (nodes[v].r != NIL && p.x <= t.x)
find(nodes[v].r, s, t, !is_x_base);
} else {
if (nodes[v].l != NIL && s.y <= p.y)
find(nodes[v].l, s, t, !is_x_base);
if (nodes[v].r != NIL && p.y <= t.y)
find(nodes[v].r, s, t, !is_x_base);
}
return ret;
}
};
signed main() {
int n;
scanf("%d", &n);
vector<Point> ps(n);
rep(i, 0, n) {
int x, y;
scanf("%d%d", x, y);
ps[i] = Point(x, y, i);
}
KDtree tree(ps);
int q;
scanf("%d", &q);
rep(i, 0, q) {
int sx, tx, sy, ty;
scanf("%d%d%d%d", &sx, &tx, &sy, &ty);
auto ans = tree.find(Point(sx, sy), Point(tx, ty));
sort(all(ans));
rep(j, 0, ans.size()) ans[j].print();
printf("\n");
}
return 0;
}
|
#include "bits/stdc++.h"
using namespace std;
#ifdef _DEBUG
#include "dump.hpp"
#else
#define dump(...)
#endif
// #define int long long
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(c) begin(c), end(c)
const int INF =
sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9) + 7;
const int NIL = -1;
const double PI = acos(-1);
const double EPS = 1e-9;
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return true;
}
return false;
}
struct Point {
int x, y, id;
Point() {}
Point(int x, int y, int id = 0) : x(x), y(y), id(id) {}
bool operator<(const Point &p) const { return id < p.id; }
void print() { printf("%d\n", id); }
};
bool lessX(const Point &p1, const Point &p2) {
return (p1.x == p2.x) ? (p1.y < p2.y) : (p1.x < p2.x);
}
bool lessY(const Point &p1, const Point &p2) {
return (p1.y == p2.y) ? (p1.x < p2.x) : (p1.y < p2.y);
}
struct KDtree {
struct Node {
int index, l, r;
Node() : index(NIL), l(NIL), r(NIL) {}
};
int n, np, root;
vector<Point> points;
vector<Node> nodes;
KDtree(const vector<Point> &ps) : n(ps.size()), np(0), points(ps), nodes(n) {
root = makeKDtree(0, n, true);
}
int makeKDtree(int l, int r, bool is_x_base = true) {
if (!(l < r))
return NIL;
int t = np++, mid = (l + r) / 2;
vector<Point>::iterator first = points.begin() + l,
nth = points.begin() + mid,
last = points.begin() + r;
if (is_x_base)
nth_element(first, nth, last, lessX);
else
nth_element(first, nth, last, lessY);
nodes[t].index = mid;
nodes[t].l = makeKDtree(l, mid, !is_x_base);
nodes[t].r = makeKDtree(mid + 1, r, !is_x_base);
return t;
}
vector<Point> ret;
bool inrange(Point &p, Point &s, Point &t) {
return (s.x <= p.x && p.x <= t.x) && (s.y <= p.y && p.y <= t.y);
}
vector<Point> find(Point s, Point t) {
ret.clear();
return find(root, s, t, true);
}
vector<Point> find(int v, Point s, Point t, bool is_x_base = true) {
auto p = points[nodes[v].index];
if (inrange(p, s, t))
ret.emplace_back(p);
if (is_x_base) {
if (nodes[v].l != NIL && s.x <= p.x)
find(nodes[v].l, s, t, !is_x_base);
if (nodes[v].r != NIL && p.x <= t.x)
find(nodes[v].r, s, t, !is_x_base);
} else {
if (nodes[v].l != NIL && s.y <= p.y)
find(nodes[v].l, s, t, !is_x_base);
if (nodes[v].r != NIL && p.y <= t.y)
find(nodes[v].r, s, t, !is_x_base);
}
return ret;
}
};
signed main() {
int n;
scanf("%d", &n);
vector<Point> ps(n);
rep(i, 0, n) {
int x, y;
scanf("%d%d", &x, &y);
ps[i] = Point(x, y, i);
}
KDtree tree(ps);
int q;
scanf("%d", &q);
rep(i, 0, q) {
int sx, tx, sy, ty;
scanf("%d%d%d%d", &sx, &tx, &sy, &ty);
auto ans = tree.find(Point(sx, sy), Point(tx, ty));
sort(all(ans));
rep(j, 0, ans.size()) ans[j].print();
printf("\n");
}
return 0;
}
|
replace
| 104 | 105 | 104 | 105 |
-11
| |
p02347
|
C++
|
Time Limit Exceeded
|
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
class Node {
public:
int location;
int p, l, r;
Node() {}
};
class Point {
public:
int id, x, y;
Point() {}
Point(int id, int x, int y) : id(id), x(x), y(y) {}
bool operator<(const Point &p) { return id < p.id; }
};
const int MAX = 100000;
const int NIL = -1;
int n;
Point P[MAX];
Node T[MAX];
int np;
bool lessX(const Point &p1, const Point &p2) { return p1.x < p2.x; }
bool lessY(const Point &p1, const Point &p2) { return p1.y < p2.y; }
int makeKDTree(int l, int r, int d) {
if (!(l < r))
return NIL;
int mid = (l + r) / 2;
int t = np++;
if (d % 2 == 0)
sort(P + l, P + r, lessX);
else
sort(P + l, P + r, lessY);
T[t].location = mid;
T[t].l = makeKDTree(l, mid, d + 1);
T[t].r = makeKDTree(mid + 1, r, d + 1);
return t;
}
void find(int v, int sx, int tx, int sy, int ty, int d, vector<Point> &ans) {
int x = P[T[v].location].x;
int y = P[T[v].location].y;
if (sx <= x && x <= tx && sy <= y && y <= ty)
ans.push_back(P[T[v].location]);
if (d % 2 == 0) {
if (T[v].l != NIL)
if (sx <= x)
find(T[v].l, sx, tx, sy, ty, d + 1, ans);
if (T[v].r != NIL)
if (x <= tx)
find(T[v].r, sx, tx, sy, ty, d + 1, ans);
} else {
if (T[v].l != NIL)
if (sy <= y)
find(T[v].l, sx, tx, sy, ty, d + 1, ans);
if (T[v].r != NIL)
if (y <= ty)
find(T[v].r, sx, tx, sy, ty, d + 1, ans);
}
}
int main() {
int x, y;
cin >> n;
rep(i, n) {
scanf("%d %d", &x, &y);
P[i] = Point(i, x, y);
T[i].l = T[i].r = T[i].p = NIL;
}
np = 0;
int root = makeKDTree(0, n, 0);
int q;
cin >> q;
int sx, sy, tx, ty;
vector<Point> ans;
rep(i, q) {
scanf("%d %d %d %d", &sx, &tx, &sy, &ty);
ans.clear();
find(root, sx, tx, sy, ty, 0, ans);
sort(ans.begin(), ans.end());
rep(j, ans.size()) printf("%d\n", ans[j].id);
cout << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
class Node {
public:
int location;
int p, l, r;
Node() {}
};
class Point {
public:
int id, x, y;
Point() {}
Point(int id, int x, int y) : id(id), x(x), y(y) {}
bool operator<(const Point &p) { return id < p.id; }
};
static const int MAX = 1000000;
static const int NIL = -1;
int n;
Point P[MAX];
Node T[MAX];
int np;
bool lessX(const Point &p1, const Point &p2) { return p1.x < p2.x; }
bool lessY(const Point &p1, const Point &p2) { return p1.y < p2.y; }
int makeKDTree(int l, int r, int d) {
if (!(l < r))
return NIL;
int mid = (l + r) / 2;
int t = np++;
if (d % 2 == 0)
sort(P + l, P + r, lessX);
else
sort(P + l, P + r, lessY);
T[t].location = mid;
T[t].l = makeKDTree(l, mid, d + 1);
T[t].r = makeKDTree(mid + 1, r, d + 1);
return t;
}
void find(int v, int sx, int tx, int sy, int ty, int d, vector<Point> &ans) {
int x = P[T[v].location].x;
int y = P[T[v].location].y;
if (sx <= x && x <= tx && sy <= y && y <= ty)
ans.push_back(P[T[v].location]);
if (d % 2 == 0) {
if (T[v].l != NIL)
if (sx <= x)
find(T[v].l, sx, tx, sy, ty, d + 1, ans);
if (T[v].r != NIL)
if (x <= tx)
find(T[v].r, sx, tx, sy, ty, d + 1, ans);
} else {
if (T[v].l != NIL)
if (sy <= y)
find(T[v].l, sx, tx, sy, ty, d + 1, ans);
if (T[v].r != NIL)
if (y <= ty)
find(T[v].r, sx, tx, sy, ty, d + 1, ans);
}
}
int main() {
int x, y;
cin >> n;
rep(i, n) {
scanf("%d %d", &x, &y);
P[i] = Point(i, x, y);
T[i].l = T[i].r = T[i].p = NIL;
}
np = 0;
int root = makeKDTree(0, n, 0);
int q;
cin >> q;
int sx, sy, tx, ty;
vector<Point> ans;
rep(i, q) {
scanf("%d %d %d %d", &sx, &tx, &sy, &ty);
ans.clear();
find(root, sx, tx, sy, ty, 0, ans);
sort(ans.begin(), ans.end());
rep(j, ans.size()) printf("%d\n", ans[j].id);
cout << endl;
}
return 0;
}
|
replace
| 16 | 18 | 16 | 18 |
TLE
| |
p02348
|
C++
|
Runtime Error
|
#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define INF ((1 << 30) - 1 + (1 << 30))
#define EPS 1.0e-6
using namespace std;
typedef long long ll;
int n, q;
int m[210000];
int init(int n) {
int k = 1;
while (k < n) {
k *= 2;
}
for (int i = 0; i < 2 * k; i++) {
m[i] = INF;
}
return k;
}
//[l,r)???x?????´??°??????.[bottom,top),node??????????????????
void update(int x, int l, int r, int bottom = 0, int top = n, int node = 1) {
if (l <= bottom && top <= r) {
m[node] = x;
return;
}
if (top <= l || r <= bottom)
return;
int mid = (bottom + top) / 2;
//-INF???????????????????????¨?????????????????????????????¨?????¨???
if (m[node] != -INF) {
m[2 * node] = m[node];
m[2 * node + 1] = m[node];
m[node] = -INF;
}
update(x, l, r, bottom, mid, 2 * node);
update(x, l, r, mid, top, 2 * node + 1);
}
int find(int i, int bottom = 0, int top = n, int node = 1) {
if (i < bottom || top <= i)
return -INF;
if (m[node] != -INF)
return m[node];
int mid = (bottom + top) / 2;
int l = find(i, bottom, mid, 2 * node);
int r = find(i, mid, top, 2 * node + 1);
return max(l, r);
}
int main() {
cin >> n >> q;
n = init(n);
int a, s, t, x, k;
for (int i = 0; i < q; i++) {
cin >> a;
if (a == 0) {
cin >> s >> t >> x;
update(x, s, t + 1);
}
if (a == 1) {
cin >> k;
cout << find(k) << endl;
}
}
return 0;
}
|
#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define INF ((1 << 30) - 1 + (1 << 30))
#define EPS 1.0e-6
using namespace std;
typedef long long ll;
int n, q;
int m[420000];
int init(int n) {
int k = 1;
while (k < n) {
k *= 2;
}
for (int i = 0; i < 2 * k; i++) {
m[i] = INF;
}
return k;
}
//[l,r)???x?????´??°??????.[bottom,top),node??????????????????
void update(int x, int l, int r, int bottom = 0, int top = n, int node = 1) {
if (l <= bottom && top <= r) {
m[node] = x;
return;
}
if (top <= l || r <= bottom)
return;
int mid = (bottom + top) / 2;
//-INF???????????????????????¨?????????????????????????????¨?????¨???
if (m[node] != -INF) {
m[2 * node] = m[node];
m[2 * node + 1] = m[node];
m[node] = -INF;
}
update(x, l, r, bottom, mid, 2 * node);
update(x, l, r, mid, top, 2 * node + 1);
}
int find(int i, int bottom = 0, int top = n, int node = 1) {
if (i < bottom || top <= i)
return -INF;
if (m[node] != -INF)
return m[node];
int mid = (bottom + top) / 2;
int l = find(i, bottom, mid, 2 * node);
int r = find(i, mid, top, 2 * node + 1);
return max(l, r);
}
int main() {
cin >> n >> q;
n = init(n);
int a, s, t, x, k;
for (int i = 0; i < q; i++) {
cin >> a;
if (a == 0) {
cin >> s >> t >> x;
update(x, s, t + 1);
}
if (a == 1) {
cin >> k;
cout << find(k) << endl;
}
}
return 0;
}
|
replace
| 19 | 20 | 19 | 20 |
0
| |
p02348
|
C++
|
Runtime Error
|
#include <stdio.h>
const int INF = 2147483647;
struct segment_tree {
int map[200010];
void build(int now, int l, int r) {
if (l == r) {
map[now] = INF;
return;
}
map[now] = -1;
int mid = (l + r) / 2;
build(now * 2 + 1, l, mid);
build(now * 2 + 2, mid + 1, r);
return;
}
void change(int n, int now, int L, int R, int l, int r) {
if (R < l || L > r)
return;
if (L >= l && R <= r) {
map[now] = n;
return;
}
if (map[now] != -1) {
map[now * 2 + 1] = map[now * 2 + 2] = map[now];
map[now] = -1;
}
int mid = (L + R) / 2;
change(n, now * 2 + 1, L, mid, l, r);
change(n, now * 2 + 2, mid + 1, R, l, r);
return;
}
int find(int now, int L, int R, int n) {
if (L == R)
return map[now];
if (map[now] != -1) {
map[now * 2 + 1] = map[now * 2 + 2] = map[now];
int temp = map[now];
map[now] = -1;
return temp;
}
int mid = (L + R) / 2;
if (mid >= n)
return find(now * 2 + 1, L, mid, n);
else
return find(now * 2 + 2, mid + 1, R, n);
}
};
int main() {
int n, m, k, l, r;
segment_tree arr;
scanf("%d%d", &n, &m);
arr.build(0, 0, n - 1);
while (m--) {
scanf("%d", &k);
if (k == 0) {
scanf("%d%d%d", &l, &r, &k);
arr.change(k, 0, 0, n - 1, l, r);
} else {
scanf("%d", &k);
printf("%d\n", arr.find(0, 0, n - 1, k));
}
}
}
|
#include <stdio.h>
const int INF = 2147483647;
struct segment_tree {
int map[2000010];
void build(int now, int l, int r) {
if (l == r) {
map[now] = INF;
return;
}
map[now] = -1;
int mid = (l + r) / 2;
build(now * 2 + 1, l, mid);
build(now * 2 + 2, mid + 1, r);
return;
}
void change(int n, int now, int L, int R, int l, int r) {
if (R < l || L > r)
return;
if (L >= l && R <= r) {
map[now] = n;
return;
}
if (map[now] != -1) {
map[now * 2 + 1] = map[now * 2 + 2] = map[now];
map[now] = -1;
}
int mid = (L + R) / 2;
change(n, now * 2 + 1, L, mid, l, r);
change(n, now * 2 + 2, mid + 1, R, l, r);
return;
}
int find(int now, int L, int R, int n) {
if (L == R)
return map[now];
if (map[now] != -1) {
map[now * 2 + 1] = map[now * 2 + 2] = map[now];
int temp = map[now];
map[now] = -1;
return temp;
}
int mid = (L + R) / 2;
if (mid >= n)
return find(now * 2 + 1, L, mid, n);
else
return find(now * 2 + 2, mid + 1, R, n);
}
};
int main() {
int n, m, k, l, r;
segment_tree arr;
scanf("%d%d", &n, &m);
arr.build(0, 0, n - 1);
while (m--) {
scanf("%d", &k);
if (k == 0) {
scanf("%d%d%d", &l, &r, &k);
arr.change(k, 0, 0, n - 1, l, r);
} else {
scanf("%d", &k);
printf("%d\n", arr.find(0, 0, n - 1, k));
}
}
}
|
replace
| 3 | 4 | 3 | 4 |
0
| |
p02349
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
int n, z;
pair<int, int> st[4 * 100000];
int op(int iz, int der) { return st[iz].first + st[der].first; }
void push(int v, int l, int r) {
int m = (r + l) / 2;
st[2 * v].second += st[v].second;
st[2 * v].first += st[v].second * (m - l);
st[2 * v + 1].second += st[v].second;
st[2 * v + 1].first += st[v].second * (r - m);
st[v].second = 0;
}
void u(int v, int l, int r, int i, int j, int x) {
push(v, l, r);
if (j < l || i >= r) {
return;
} else if (i <= l && r - 1 <= j) {
st[v].second += x;
st[v].first += (r - l) * x;
} else {
int m = (r + l) / 2;
u(2 * v, l, m, i, j, x);
u(2 * v + 1, m, r, i, j, x);
st[v].first = op(2 * v, 2 * v + 1);
}
}
int q(int v, int l, int r, int x) {
push(v, l, r);
if (x < l || x >= r) {
return 0;
} else {
if (r - l == 1) {
return st[v].first;
} else {
int m = (r + l) / 2;
return max(q(2 * v, l, m, x), q(2 * v + 1, m, r, x));
}
}
}
int main() {
cin >> n >> z;
int a, b, c, d;
while (z--) {
cin >> a;
if (a == 0) {
cin >> b >> c >> d;
u(1, 1, n + 1, min(b, c), max(b, c), d);
} else {
cin >> b;
cout << q(1, 1, n + 1, b) << '\n';
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, z;
pair<int, int> st[4 * 100000];
int op(int iz, int der) { return st[iz].first + st[der].first; }
void push(int v, int l, int r) {
if (r - l == 1)
return;
int m = (r + l) / 2;
st[2 * v].second += st[v].second;
st[2 * v].first += st[v].second * (m - l);
st[2 * v + 1].second += st[v].second;
st[2 * v + 1].first += st[v].second * (r - m);
st[v].second = 0;
}
void u(int v, int l, int r, int i, int j, int x) {
push(v, l, r);
if (j < l || i >= r) {
return;
} else if (i <= l && r - 1 <= j) {
st[v].second += x;
st[v].first += (r - l) * x;
} else {
int m = (r + l) / 2;
u(2 * v, l, m, i, j, x);
u(2 * v + 1, m, r, i, j, x);
st[v].first = op(2 * v, 2 * v + 1);
}
}
int q(int v, int l, int r, int x) {
push(v, l, r);
if (x < l || x >= r) {
return 0;
} else {
if (r - l == 1) {
return st[v].first;
} else {
int m = (r + l) / 2;
return max(q(2 * v, l, m, x), q(2 * v + 1, m, r, x));
}
}
}
int main() {
cin >> n >> z;
int a, b, c, d;
while (z--) {
cin >> a;
if (a == 0) {
cin >> b >> c >> d;
u(1, 1, n + 1, min(b, c), max(b, c), d);
} else {
cin >> b;
cout << q(1, 1, n + 1, b) << '\n';
}
}
return 0;
}
|
insert
| 9 | 9 | 9 | 11 |
0
| |
p02349
|
C++
|
Runtime Error
|
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int INF = 1e9;
const ll LINF = 1e18;
template <class S, class T>
ostream &operator<<(ostream &out, const pair<S, T> &o) {
out << "(" << o.first << "," << o.second << ")";
return out;
}
template <class T> ostream &operator<<(ostream &out, const vector<T> V) {
for (int i = 0; i < V.size(); i++) {
out << V[i];
if (i != V.size() - 1)
out << " ";
}
return out;
}
template <class T>
ostream &operator<<(ostream &out, const vector<vector<T>> Mat) {
for (int i = 0; i < Mat.size(); i++) {
if (i != 0)
out << endl;
out << Mat[i];
}
return out;
}
template <class S, class T>
ostream &operator<<(ostream &out, const map<S, T> mp) {
out << "{ ";
for (auto it = mp.begin(); it != mp.end(); it++) {
out << it->first << ":" << it->second;
if (mp.size() - 1 != distance(mp.begin(), it))
out << ", ";
}
out << " }";
return out;
}
// ================================================= //
/*starry sky tree
add(O(logN)):区間[a, b)に値xを加算する
getMin(O(logN)): 区間の最小値を得る
*/
static const int MAX_SIZE = 100050;
typedef long long ll;
ll segMin[2 * MAX_SIZE - 1], segAdd[2 * MAX_SIZE - 1];
void add(int a, int b, int x, int k = 0, int l = 0, int r = MAX_SIZE) {
if (r <= a || b <= l)
return; // もし交差しない区間であれば終える.
if (a <= l &&
r <= b) { // もし今みている区間[l, r)が[a, b)に完全に内包されていれば
segAdd[k] += x; // 区間[l, r)にkを加算する.
return;
}
// 子の区間に(必要があれば)xを加算する.
add(a, b, x, k * 2 + 1, l, (l + r) / 2);
add(a, b, x, k * 2 + 2, (l + r) / 2, r);
// 親の区間の最小値は, 子の区間の最小値 + 自分に一様に加算されている値
// である.一様に加算される値は更新しなくて良い.
segMin[k] = min(segMin[k * 2 + 1] + segAdd[k * 2 + 1],
segMin[k * 2 + 2] + segAdd[k * 2 + 2]);
}
ll getMin(int a, int b, int k = 0, int l = 0, int r = MAX_SIZE) {
if (r <= a || b <= l)
return (LLONG_MAX);
if (a <= l && r <= b)
return (segMin[k] +
segAdd[k]); // 完全に内包されていれば,その区間の最小値を返す.
ll left = getMin(a, b, k * 2 + 1, l, (l + r) / 2); // 子の区間の最小値を求める.
ll right = getMin(a, b, k * 2 + 2, (l + r) / 2, r); // 子の区間の最小値を求める
// 親の区間の最小値は, 子の区間の最小値 + 自分に一様に加算されている値
return (min(left, right) + segAdd[k]);
}
int main(void) {
cin.tie(0);
ios::sync_with_stdio(false);
int n, q;
cin >> n >> q;
// SegTree ST(n);
// BIT bit(n);
while (q--) {
int com;
cin >> com;
if (com == 0) {
int s, t, x;
cin >> s >> t >> x;
// ST.update(s-1,t,x);
add(s - 1, t, x);
} else {
int i;
cin >> i;
// cout << ST.query(i-1,i) << endl;
cout << getMin(i - 1, i) << endl;
}
}
return 0;
}
|
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int INF = 1e9;
const ll LINF = 1e18;
template <class S, class T>
ostream &operator<<(ostream &out, const pair<S, T> &o) {
out << "(" << o.first << "," << o.second << ")";
return out;
}
template <class T> ostream &operator<<(ostream &out, const vector<T> V) {
for (int i = 0; i < V.size(); i++) {
out << V[i];
if (i != V.size() - 1)
out << " ";
}
return out;
}
template <class T>
ostream &operator<<(ostream &out, const vector<vector<T>> Mat) {
for (int i = 0; i < Mat.size(); i++) {
if (i != 0)
out << endl;
out << Mat[i];
}
return out;
}
template <class S, class T>
ostream &operator<<(ostream &out, const map<S, T> mp) {
out << "{ ";
for (auto it = mp.begin(); it != mp.end(); it++) {
out << it->first << ":" << it->second;
if (mp.size() - 1 != distance(mp.begin(), it))
out << ", ";
}
out << " }";
return out;
}
// ================================================= //
/*starry sky tree
add(O(logN)):区間[a, b)に値xを加算する
getMin(O(logN)): 区間の最小値を得る
*/
static const int MAX_SIZE = 1 << 17;
typedef long long ll;
ll segMin[2 * MAX_SIZE - 1], segAdd[2 * MAX_SIZE - 1];
void add(int a, int b, int x, int k = 0, int l = 0, int r = MAX_SIZE) {
if (r <= a || b <= l)
return; // もし交差しない区間であれば終える.
if (a <= l &&
r <= b) { // もし今みている区間[l, r)が[a, b)に完全に内包されていれば
segAdd[k] += x; // 区間[l, r)にkを加算する.
return;
}
// 子の区間に(必要があれば)xを加算する.
add(a, b, x, k * 2 + 1, l, (l + r) / 2);
add(a, b, x, k * 2 + 2, (l + r) / 2, r);
// 親の区間の最小値は, 子の区間の最小値 + 自分に一様に加算されている値
// である.一様に加算される値は更新しなくて良い.
segMin[k] = min(segMin[k * 2 + 1] + segAdd[k * 2 + 1],
segMin[k * 2 + 2] + segAdd[k * 2 + 2]);
}
ll getMin(int a, int b, int k = 0, int l = 0, int r = MAX_SIZE) {
if (r <= a || b <= l)
return (LLONG_MAX);
if (a <= l && r <= b)
return (segMin[k] +
segAdd[k]); // 完全に内包されていれば,その区間の最小値を返す.
ll left = getMin(a, b, k * 2 + 1, l, (l + r) / 2); // 子の区間の最小値を求める.
ll right = getMin(a, b, k * 2 + 2, (l + r) / 2, r); // 子の区間の最小値を求める
// 親の区間の最小値は, 子の区間の最小値 + 自分に一様に加算されている値
return (min(left, right) + segAdd[k]);
}
int main(void) {
cin.tie(0);
ios::sync_with_stdio(false);
int n, q;
cin >> n >> q;
// SegTree ST(n);
// BIT bit(n);
while (q--) {
int com;
cin >> com;
if (com == 0) {
int s, t, x;
cin >> s >> t >> x;
// ST.update(s-1,t,x);
add(s - 1, t, x);
} else {
int i;
cin >> i;
// cout << ST.query(i-1,i) << endl;
cout << getMin(i - 1, i) << endl;
}
}
return 0;
}
|
replace
| 47 | 48 | 47 | 48 |
0
| |
p02349
|
C++
|
Runtime Error
|
#include <stdio.h>
const int INF = 2147483647;
struct segment_tree {
int map[200010];
void build(int now, int l, int r) {
map[now] = 0;
if (l == r)
return;
int mid = (l + r) / 2;
build(now * 2 + 1, l, mid);
build(now * 2 + 2, mid + 1, r);
return;
}
void change(int n, int now, int L, int R, int l, int r) {
if (R < l || L > r)
return;
if (L >= l && R <= r) {
map[now] += n;
return;
}
map[now * 2 + 1] += map[now];
map[now * 2 + 2] += map[now];
map[now] = 0;
int mid = (L + R) / 2;
change(n, now * 2 + 1, L, mid, l, r);
change(n, now * 2 + 2, mid + 1, R, l, r);
return;
}
int find(int now, int L, int R, int n) {
if (L == R)
return map[now];
map[now * 2 + 1] += map[now];
map[now * 2 + 2] += map[now];
map[now] = 0;
int mid = (L + R) / 2;
if (mid >= n)
return find(now * 2 + 1, L, mid, n);
else
return find(now * 2 + 2, mid + 1, R, n);
}
};
int main() {
int n, m, k, l, r;
segment_tree arr;
scanf("%d%d", &n, &m);
arr.build(0, 1, n);
while (m--) {
scanf("%d", &k);
if (k == 0) {
scanf("%d%d%d", &l, &r, &k);
arr.change(k, 0, 1, n, l, r);
} else {
scanf("%d", &k);
printf("%d\n", arr.find(0, 1, n, k));
}
}
}
|
#include <stdio.h>
const int INF = 2147483647;
struct segment_tree {
int map[2000010];
void build(int now, int l, int r) {
map[now] = 0;
if (l == r)
return;
int mid = (l + r) / 2;
build(now * 2 + 1, l, mid);
build(now * 2 + 2, mid + 1, r);
return;
}
void change(int n, int now, int L, int R, int l, int r) {
if (R < l || L > r)
return;
if (L >= l && R <= r) {
map[now] += n;
return;
}
map[now * 2 + 1] += map[now];
map[now * 2 + 2] += map[now];
map[now] = 0;
int mid = (L + R) / 2;
change(n, now * 2 + 1, L, mid, l, r);
change(n, now * 2 + 2, mid + 1, R, l, r);
return;
}
int find(int now, int L, int R, int n) {
if (L == R)
return map[now];
map[now * 2 + 1] += map[now];
map[now * 2 + 2] += map[now];
map[now] = 0;
int mid = (L + R) / 2;
if (mid >= n)
return find(now * 2 + 1, L, mid, n);
else
return find(now * 2 + 2, mid + 1, R, n);
}
};
int main() {
int n, m, k, l, r;
segment_tree arr;
scanf("%d%d", &n, &m);
arr.build(0, 1, n);
while (m--) {
scanf("%d", &k);
if (k == 0) {
scanf("%d%d%d", &l, &r, &k);
arr.change(k, 0, 1, n, l, r);
} else {
scanf("%d", &k);
printf("%d\n", arr.find(0, 1, n, k));
}
}
}
|
replace
| 3 | 4 | 3 | 4 |
0
| |
p02349
|
C++
|
Runtime Error
|
/*
* s????????????????´??????????s'????????????????´?????????¨????????¨
* i < l s'[i] = s[i]
* l <= i <= r s'[i] = s[i] + x*i - x*(l-1)
* r < i s'[i] = s[i] + x*(r-l+1)
* ?????????BIT???????????¨??????????£?
* ????´°????????¬
* dsl2_g,poj3468??¨??????
*/
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <string>
#include <vector>
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define RFOR(i, a, b) for (int i = (a)-1; i >= (b); i--)
#define ll long long
#define ull unsigned long long
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
const int INF = 1e9;
const int MOD = 1e9 + 7;
using namespace std;
// BIT
struct BIT {
int N;
ll data[20100];
void init(int n) {
N = n;
REP(i, N + 1) data[i] = 0;
}
void add(int a, ll w) {
for (int x = a; x <= N; x += x & -x)
data[x] += w;
}
// bit[1] + ... + bit[a]
ll sum(int a) {
ll ret = 0;
for (int x = a; x > 0; x -= x & -x)
ret += data[x];
return ret;
}
};
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n, q;
cin >> n >> q;
BIT bit0, bit1;
bit0.init(n + 1);
bit1.init(n + 1);
REP(i, q) {
int c;
cin >> c;
if (!c) {
int l, r;
cin >> l >> r;
int x;
cin >> x;
bit0.add(l, x);
bit0.add(r + 1, -x);
bit1.add(l, -(l - 1) * x);
bit1.add(r + 1, r * x);
} else {
int a;
cin >> a;
cout << bit0.sum(a) * a + bit1.sum(a) -
(bit0.sum(a - 1) * (a - 1) + bit1.sum(a - 1))
<< endl;
}
}
return 0;
}
|
/*
* s????????????????´??????????s'????????????????´?????????¨????????¨
* i < l s'[i] = s[i]
* l <= i <= r s'[i] = s[i] + x*i - x*(l-1)
* r < i s'[i] = s[i] + x*(r-l+1)
* ?????????BIT???????????¨??????????£?
* ????´°????????¬
* dsl2_g,poj3468??¨??????
*/
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <string>
#include <vector>
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define RFOR(i, a, b) for (int i = (a)-1; i >= (b); i--)
#define ll long long
#define ull unsigned long long
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
const int INF = 1e9;
const int MOD = 1e9 + 7;
using namespace std;
// BIT
struct BIT {
int N;
ll data[200100];
void init(int n) {
N = n;
REP(i, N + 1) data[i] = 0;
}
void add(int a, ll w) {
for (int x = a; x <= N; x += x & -x)
data[x] += w;
}
// bit[1] + ... + bit[a]
ll sum(int a) {
ll ret = 0;
for (int x = a; x > 0; x -= x & -x)
ret += data[x];
return ret;
}
};
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n, q;
cin >> n >> q;
BIT bit0, bit1;
bit0.init(n + 1);
bit1.init(n + 1);
REP(i, q) {
int c;
cin >> c;
if (!c) {
int l, r;
cin >> l >> r;
int x;
cin >> x;
bit0.add(l, x);
bit0.add(r + 1, -x);
bit1.add(l, -(l - 1) * x);
bit1.add(r + 1, r * x);
} else {
int a;
cin >> a;
cout << bit0.sum(a) * a + bit1.sum(a) -
(bit0.sum(a - 1) * (a - 1) + bit1.sum(a - 1))
<< endl;
}
}
return 0;
}
|
replace
| 37 | 38 | 37 | 38 |
0
| |
p02350
|
C++
|
Time Limit Exceeded
|
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, l, r) for (int i = (int)(l); i < (int)(r); i++)
#define ALL(x) x.begin(), x.end()
template <typename T> bool chmax(T &a, const T &b) {
return a < b ? (a = b, true) : false;
}
template <typename T> bool chmin(T &a, const T &b) {
return b < a ? (a = b, true) : false;
}
typedef long long ll;
const int BUCKET = 400, SIZE = 300;
int N, Q;
bool updateFlag[BUCKET];
ll lazyUpdate[BUCKET];
ll bucketMin[BUCKET];
ll A[100000];
const ll INF = (1ll << 31) - 1;
void update(int l, int r, ll val) {
FOR(i, 0, BUCKET) {
int bucketL = i * SIZE, bucketR = (i + 1) * SIZE;
if (r <= bucketL || bucketR <= l)
continue;
if (l <= bucketL && bucketR <= r) {
lazyUpdate[i] = val;
updateFlag[i] = true;
continue;
}
if (updateFlag[i]) {
FOR(j, bucketL, bucketR) { A[j] = lazyUpdate[i]; }
updateFlag[i] = false;
}
bucketMin[i] = INF;
FOR(j, bucketL, bucketR) {
if (l <= j && j < r) {
A[j] = val;
}
chmin(bucketMin[i], A[j]);
}
}
}
ll getMin(int l, int r) {
ll res = INF;
FOR(i, 0, BUCKET) {
int bucketL = i * SIZE, bucketR = (i + 1) * SIZE;
if (r <= bucketL || bucketR <= l)
continue;
if (l <= bucketL && bucketR <= r) {
if (updateFlag[i]) {
chmin(res, lazyUpdate[i]);
} else {
chmin(res, bucketMin[i]);
}
}
if (updateFlag[i]) {
FOR(j, bucketL, bucketR) { A[j] = lazyUpdate[i]; }
updateFlag[i] = false;
}
bucketMin[i] = INF;
FOR(j, bucketL, bucketR) {
if (l <= j && j < r) {
chmin(res, A[j]);
}
chmin(bucketMin[i], A[j]);
}
}
return res;
}
int main() {
scanf("%d%d", &N, &Q);
vector<ll> ans;
fill(A, A + N, INF);
fill(bucketMin, bucketMin + BUCKET, INF);
FOR(i, 0, Q) {
int com;
scanf("%d", &com);
if (com == 0) {
int s, t, x;
scanf("%d%d%d", &s, &t, &x);
update(s, t + 1, x);
} else {
int s, t;
scanf("%d%d", &s, &t);
ans.push_back(getMin(s, t + 1));
}
}
FOR(i, 0, ans.size()) { printf("%lld\n", ans[i]); }
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, l, r) for (int i = (int)(l); i < (int)(r); i++)
#define ALL(x) x.begin(), x.end()
template <typename T> bool chmax(T &a, const T &b) {
return a < b ? (a = b, true) : false;
}
template <typename T> bool chmin(T &a, const T &b) {
return b < a ? (a = b, true) : false;
}
typedef long long ll;
const int BUCKET = 400, SIZE = 300;
int N, Q;
bool updateFlag[BUCKET];
ll lazyUpdate[BUCKET];
ll bucketMin[BUCKET];
ll A[100000];
const ll INF = (1ll << 31) - 1;
void update(int l, int r, ll val) {
FOR(i, 0, BUCKET) {
int bucketL = i * SIZE, bucketR = (i + 1) * SIZE;
if (r <= bucketL || bucketR <= l)
continue;
if (l <= bucketL && bucketR <= r) {
lazyUpdate[i] = val;
updateFlag[i] = true;
continue;
}
if (updateFlag[i]) {
FOR(j, bucketL, bucketR) { A[j] = lazyUpdate[i]; }
updateFlag[i] = false;
}
bucketMin[i] = INF;
FOR(j, bucketL, bucketR) {
if (l <= j && j < r) {
A[j] = val;
}
chmin(bucketMin[i], A[j]);
}
}
}
ll getMin(int l, int r) {
ll res = INF;
FOR(i, 0, BUCKET) {
int bucketL = i * SIZE, bucketR = (i + 1) * SIZE;
if (r <= bucketL || bucketR <= l)
continue;
if (l <= bucketL && bucketR <= r) {
if (updateFlag[i]) {
chmin(res, lazyUpdate[i]);
} else {
chmin(res, bucketMin[i]);
}
continue;
}
if (updateFlag[i]) {
FOR(j, bucketL, bucketR) { A[j] = lazyUpdate[i]; }
updateFlag[i] = false;
}
bucketMin[i] = INF;
FOR(j, bucketL, bucketR) {
if (l <= j && j < r) {
chmin(res, A[j]);
}
chmin(bucketMin[i], A[j]);
}
}
return res;
}
int main() {
scanf("%d%d", &N, &Q);
vector<ll> ans;
fill(A, A + N, INF);
fill(bucketMin, bucketMin + BUCKET, INF);
FOR(i, 0, Q) {
int com;
scanf("%d", &com);
if (com == 0) {
int s, t, x;
scanf("%d%d%d", &s, &t, &x);
update(s, t + 1, x);
} else {
int s, t;
scanf("%d%d", &s, &t);
ans.push_back(getMin(s, t + 1));
}
}
FOR(i, 0, ans.size()) { printf("%lld\n", ans[i]); }
return 0;
}
|
insert
| 57 | 57 | 57 | 58 |
TLE
| |
p02350
|
C++
|
Runtime Error
|
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define mind(a, b) (a > b ? b : a)
#define maxd(a, b) (a > b ? a : b)
#define absd(x) (x < 0 ? -(x) : x)
#define pow2(x) ((x) * (x))
#define rep(i, n) for (int i = 0; i < n; ++i)
#define repr(i, n) for (int i = n - 1; i >= 0; --i)
#define repl(i, s, n) for (int i = s; i <= n; ++i)
#define replr(i, s, n) for (int i = n; i >= s; --i)
#define repf(i, s, n, j) for (int i = s; i <= n; i += j)
#define repe(e, obj) for (auto e : obj)
#define SP << " " <<
#define COL << " : " <<
#define COM << ", " <<
#define ARR << " -> " <<
#define PNT(STR) cout << STR << endl
#define POS(X, Y) "(" << X << ", " << Y << ")"
#define DEB(A) " (" << #A << ") " << A
#define DEBREP(i, n, val) \
for (int i = 0; i < n; ++i) \
cout << val << " "; \
cout << endl
#define ALL(V) (V).begin(), (V).end()
#define INF 1000000007
#define INFLL 1000000000000000007LL
#define EPS 1e-9
typedef unsigned int uint;
typedef unsigned long ulong;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
#define P_TYPE int
typedef pair<P_TYPE, P_TYPE> P;
typedef pair<P, P_TYPE> PI;
typedef pair<P_TYPE, P> IP;
typedef pair<P, P> PP;
typedef priority_queue<P, vector<P>, greater<P>> pvqueue;
#define N (1 << 10) //(1 << 18)
#define LV 10 // 18
class SegmentTree {
const ll inf = (1LL << 31) - 1;
int n0, n;
ll data[4 * N], lazy[4 * N];
int ids[2 * LV], cur = 0;
void update_ids(int l, int r) {
int l0 = (l + n0), r0 = (r + n0);
int lb = (l0 & -l0) >> 1, rb = (r0 & -r0) >> 1;
l0 >>= 1;
r0 >>= 1;
cur = 0;
while (l0 > 0 && l0 < r0) {
if (!rb)
ids[cur++] = r0;
if (!lb)
ids[cur++] = l0;
lb >>= 1;
rb >>= 1;
l0 >>= 1;
r0 >>= 1;
}
while (l0 > 0) {
ids[cur++] = l0;
l0 >>= 1;
}
}
void propagates() {
repr(i, cur) {
int k = ids[i];
ll v = lazy[k - 1];
if (v == -1)
continue;
lazy[2 * k - 1] = data[2 * k - 1] = v;
lazy[2 * k] = data[2 * k] = v;
lazy[k - 1] = -1;
}
}
public:
SegmentTree(int n) : n(n) {
n0 = 1;
while (n0 < n)
n0 <<= 1;
rep(i, 2 * n0) data[i] = inf, lazy[i] = -1;
}
void update(int l, int r, ll x) {
update_ids(l, r);
propagates();
int l0 = l + n0, r0 = r + n0;
while (l0 < r0) {
if (r0 & 1) {
--r0;
lazy[r0 - 1] = data[r0 - 1] = x;
}
if (l0 & 1) {
lazy[l0 - 1] = data[l0 - 1] = x;
++l0;
}
l0 >>= 1;
r0 >>= 1;
}
rep(i, cur) {
int k = ids[i];
data[k - 1] = min(data[2 * k - 1], data[2 * k]);
}
}
ll query(int l, int r) {
update_ids(l, r);
propagates();
int l0 = l + n0, r0 = r + n0;
ll s = inf;
while (l0 < r0) {
if (r0 & 1) {
--r0;
s = min(s, data[r0 - 1]);
}
if (l0 & 1) {
s = min(s, data[l0 - 1]);
++l0;
}
l0 >>= 1;
r0 >>= 1;
}
return s;
}
};
int n, q;
int main() {
cin >> n >> q;
SegmentTree st(n);
rep(i, q) {
int c, s, t, x;
cin >> c;
switch (c) {
case 0:
cin >> s >> t >> x;
st.update(s, t + 1, x);
break;
case 1:
cin >> s >> t;
cout << st.query(s, t + 1) << endl;
break;
}
}
return 0;
}
|
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define mind(a, b) (a > b ? b : a)
#define maxd(a, b) (a > b ? a : b)
#define absd(x) (x < 0 ? -(x) : x)
#define pow2(x) ((x) * (x))
#define rep(i, n) for (int i = 0; i < n; ++i)
#define repr(i, n) for (int i = n - 1; i >= 0; --i)
#define repl(i, s, n) for (int i = s; i <= n; ++i)
#define replr(i, s, n) for (int i = n; i >= s; --i)
#define repf(i, s, n, j) for (int i = s; i <= n; i += j)
#define repe(e, obj) for (auto e : obj)
#define SP << " " <<
#define COL << " : " <<
#define COM << ", " <<
#define ARR << " -> " <<
#define PNT(STR) cout << STR << endl
#define POS(X, Y) "(" << X << ", " << Y << ")"
#define DEB(A) " (" << #A << ") " << A
#define DEBREP(i, n, val) \
for (int i = 0; i < n; ++i) \
cout << val << " "; \
cout << endl
#define ALL(V) (V).begin(), (V).end()
#define INF 1000000007
#define INFLL 1000000000000000007LL
#define EPS 1e-9
typedef unsigned int uint;
typedef unsigned long ulong;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
#define P_TYPE int
typedef pair<P_TYPE, P_TYPE> P;
typedef pair<P, P_TYPE> PI;
typedef pair<P_TYPE, P> IP;
typedef pair<P, P> PP;
typedef priority_queue<P, vector<P>, greater<P>> pvqueue;
#define N (1 << 18)
#define LV 18
class SegmentTree {
const ll inf = (1LL << 31) - 1;
int n0, n;
ll data[4 * N], lazy[4 * N];
int ids[2 * LV], cur = 0;
void update_ids(int l, int r) {
int l0 = (l + n0), r0 = (r + n0);
int lb = (l0 & -l0) >> 1, rb = (r0 & -r0) >> 1;
l0 >>= 1;
r0 >>= 1;
cur = 0;
while (l0 > 0 && l0 < r0) {
if (!rb)
ids[cur++] = r0;
if (!lb)
ids[cur++] = l0;
lb >>= 1;
rb >>= 1;
l0 >>= 1;
r0 >>= 1;
}
while (l0 > 0) {
ids[cur++] = l0;
l0 >>= 1;
}
}
void propagates() {
repr(i, cur) {
int k = ids[i];
ll v = lazy[k - 1];
if (v == -1)
continue;
lazy[2 * k - 1] = data[2 * k - 1] = v;
lazy[2 * k] = data[2 * k] = v;
lazy[k - 1] = -1;
}
}
public:
SegmentTree(int n) : n(n) {
n0 = 1;
while (n0 < n)
n0 <<= 1;
rep(i, 2 * n0) data[i] = inf, lazy[i] = -1;
}
void update(int l, int r, ll x) {
update_ids(l, r);
propagates();
int l0 = l + n0, r0 = r + n0;
while (l0 < r0) {
if (r0 & 1) {
--r0;
lazy[r0 - 1] = data[r0 - 1] = x;
}
if (l0 & 1) {
lazy[l0 - 1] = data[l0 - 1] = x;
++l0;
}
l0 >>= 1;
r0 >>= 1;
}
rep(i, cur) {
int k = ids[i];
data[k - 1] = min(data[2 * k - 1], data[2 * k]);
}
}
ll query(int l, int r) {
update_ids(l, r);
propagates();
int l0 = l + n0, r0 = r + n0;
ll s = inf;
while (l0 < r0) {
if (r0 & 1) {
--r0;
s = min(s, data[r0 - 1]);
}
if (l0 & 1) {
s = min(s, data[l0 - 1]);
++l0;
}
l0 >>= 1;
r0 >>= 1;
}
return s;
}
};
int n, q;
int main() {
cin >> n >> q;
SegmentTree st(n);
rep(i, q) {
int c, s, t, x;
cin >> c;
switch (c) {
case 0:
cin >> s >> t >> x;
st.update(s, t + 1, x);
break;
case 1:
cin >> s >> t;
cout << st.query(s, t + 1) << endl;
break;
}
}
return 0;
}
|
replace
| 55 | 57 | 55 | 57 |
0
| |
p02350
|
C++
|
Runtime Error
|
/*vvv>
zzzzzI
.---. zzuzuI .vgggg&,.
+++++= dunkoI .WbbWo JMM9^```?TMB` ..&gNNg,. gggggggJ, qgggggggg]
(&&&&&&&&[ c+OA&J, (&&&&&&+J, .cJeAA&-. (&&&&&&&&x .&AA&=-.
+++++= dunkoI Xpbpbp JM#` (M#^ ?MMp MM| +TMN. JMF '
|yk ` dVY 7Vk, Vy XV cVf ?Y! JM V$ `
+++++= OunkoI Xppppp dMN .MM+ .MM MM| MM] JMMMMMM+
|@tqkoh) ,y0 (V$ yyyyyyyV7 VV JMWyZWr TWVVVVW&,
++++++ dZZZZ0 Xppppp ^HMN, _.db WMm, .MMF MM| ..MM` JMF .
|yk .WV&. .XW' yy 4yn. jyn +. JM #S
`++++` ?ZZZX= ?WWWW= -THMMMMH9^ (TMMMMM9! MMMMMMM"" JMMMMMMMME
|UU. ?TUUUUY= UU. (UU- ^7TUUUV7! JUUUUUUUU 7TUNKO*/
// Ricty Diminished
#include "bits/stdc++.h"
using namespace std;
typedef long long lint;
typedef vector<lint> liv;
// #define rep(i,n) for(int i=0;i<n;++i)
#define all(v) v.begin(), v.end()
#define linf 1152921504606846976
#define MAXN 100100
#define md 1000000007 // 998244353
#define pb push_back
#define _vcppunko4(tuple) _getname4 tuple
#define _getname4(_1, _2, _3, _4, name, ...) name
#define _getname3(_1, _2, _3, name, ...) name
#define _trep2(tuple) _rep2 tuple
#define _trep3(tuple) _rep3 tuple
#define _trep4(tuple) _rep4 tuple
#define _rep1(n) for (lint i = 0; i < n; ++i)
#define _rep2(i, n) for (lint i = 0; i < n; ++i)
#define _rep3(i, a, b) for (lint i = a; i < b; ++i)
#define _rep4(i, a, b, c) for (lint i = a; i < b; i += c)
#define _trrep2(tuple) _rrep2 tuple
#define _trrep3(tuple) _rrep3 tuple
#define _trrep4(tuple) _rrep4 tuple
#define _rrep1(n) for (lint i = n - 1; i >= 0; --i)
#define _rrep2(i, n) for (lint i = n - 1; i >= 0; --i)
#define _rrep3(i, a, b) for (lint i = b - 1; i >= a; --i)
#define _rrep4(i, a, b, c) \
for (lint i = a + (b - a - 1) / c * c; i >= a; i -= c)
template <class T> istream &operator>>(istream &is, vector<T> &vec);
template <class T, size_t size>
istream &operator>>(istream &is, array<T, size> &vec);
template <class T, class L> istream &operator>>(istream &is, pair<T, L> &p);
template <class T> ostream &operator<<(ostream &os, vector<T> &vec);
template <class T, class L> ostream &operator<<(ostream &os, pair<T, L> &p);
template <class T> istream &operator>>(istream &is, vector<T> &vec) {
for (T &x : vec)
is >> x;
return is;
}
template <class T, class L> istream &operator>>(istream &is, pair<T, L> &p) {
is >> p.first;
is >> p.second;
return is;
}
// template<class T>
// ostream& operator<<(ostream& os,vector<T>& vec){
// os<<vec[0];rep(i,1,vec.size())os<<' '<<vec[i];return os; } template<class T>
// ostream& operator<<(ostream& os,deque<T>& deq){
// os<<deq[0];rep(i,1,deq.size())os<<' '<<deq[i];return os; }
template <class T, class L> ostream &operator<<(ostream &os, pair<T, L> &p) {
os << p.first << " " << p.second;
return os;
}
inline void in() {}
template <class Head, class... Tail>
inline void in(Head &&head, Tail &&...tail) {
cin >> head;
in(move(tail)...);
}
template <class T> inline bool out(T t) {
cout << t << '\n';
return 0;
}
inline bool out() {
cout << '\n';
return 0;
}
template <class Head, class... Tail> inline bool out(Head head, Tail... tail) {
cout << head << ' ';
out(move(tail)...);
return 0;
}
template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); }
template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}
template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) {
t = v;
}
template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) {
for (auto &e : t)
fill_v(e, v);
} // http://beet-aizu.hatenablog.com/entry/2018/04/08/145516
#define rep(...) \
_vcppunko4((__VA_ARGS__, _trep4, _trep3, _trep2, _rep1))((__VA_ARGS__))
#define rrep(...) \
_vcppunko4((__VA_ARGS__, _trrep4, _trrep3, _trrep2, _rrep1))((__VA_ARGS__))
#define each(v) for (auto &i : v)
#define lin(...) \
lint __VA_ARGS__; \
in(__VA_ARGS__)
#define stin(...) \
string __VA_ARGS__; \
in(__VA_ARGS__)
#define vin(type, name, size) \
vector<type> name(size); \
in(name)
#define fi i.first
#define se i.second
#define YES(c) cout << ((c) ? "YES\n" : "NO\n"), 0
#define Yes(c) cout << ((c) ? "Yes\n" : "No\n"), 0
#define o(p) cout << p << endl, 0
#define deb(p) cerr << p << endl, 0
#define dd(n) cout << fixed << setprecision(n)
#define inf linf
//(T,f,e). ex.(lint,+,0) -> LazySegtree t(/*initial vector*/,[](lint a,lint
//b){return a+b},0);
enum lazytype {
lt_add,
lt_update,
lt_end
};
class LazySegtree {
public:
using T = lint;
using func = function<T(T, T)>;
int n, sz;
vector<T> node, lazy;
bool lazyFlag[140000];
// inline lint f(lint x,lint y){ return x|y; }
func f;
T e;
LazySegtree(vector<T> v, func f, T e) : f(f), e(e) {
sz = v.size();
n = 1;
while (n < sz)
n <<= 1;
node.resize(2 * n, e);
lazy.resize(2 * n, 0);
rep(i, sz) node[n + i] = v[i], lazyFlag[i] = 0;
for (int i = n - 1; i > 0; --i)
node[i] = f(node[2 * i], node[2 * i + 1]);
}
void eval(lazytype t, int k, int l, int r) {
switch (t) {
case lt_add: {
if (2 * n <= k || !lazy[k])
return;
node[k] += lazy[k];
if (r - l > 1) {
lazy[2 * k] += lazy[k] / 2;
lazy[2 * k + 1] += lazy[k] / 2;
}
lazy[k] = 0;
break;
}
case lt_update: {
if (2 * n <= k || !lazyFlag[k])
break;
node[k] = lazy[k];
if (r - l > 1) {
lazy[2 * k] = lazy[2 * k + 1] = lazy[k];
lazyFlag[2 * k] = lazyFlag[2 * k + 1] = 1;
}
lazyFlag[k] = 0;
break;
}
default:
break;
}
}
void update(int i, T x) {
node[i += n] = x;
while (i >>= 1)
node[i] = f(node[2 * i], node[2 * i + 1]);
}
void update(int a, int b, T x, int k = 1, int l = 0, int r = -1) {
if (r < 0)
r = n;
eval(lt_update, k, l, r);
if (b <= l || r <= a)
return;
if (a <= l && r <= b) {
lazy[k] = x;
lazyFlag[k] = 1;
eval(lt_update, k, l, r);
} else {
update(a, b, x, 2 * k, l, (l + r) / 2);
update(a, b, x, 2 * k + 1, (l + r) / 2, r);
node[k] = min(node[2 * k], node[2 * k + 1]);
}
}
void add(int a, int b, T x, int k = 1, int l = 0, int r = -1) {
if (r < 0)
r = n;
eval(lt_add, k, l, r);
if (b <= l || r <= a)
return;
if (a <= l && r <= b)
lazy[k] += (r - l) * x, eval(lt_add, k, l, r);
else {
add(a, b, x, 2 * k, l, (l + r) / 2);
add(a, b, x, 2 * k + 1, (l + r) / 2, r);
node[k] = f(node[2 * k], node[2 * k + 1]);
}
}
lint query(int a, int b, lazytype t, int k = 1, int l = 0, int r = -1) {
if (r < 0)
r = n;
if (r <= a || b <= l)
return e;
eval(t, k, l, r);
if (a <= l && r <= b)
return node[k];
return f(query(a, b, t, 2 * k, l, (l + r) / 2),
query(a, b, t, 2 * k + 1, (l + r) / 2, r));
}
}; // http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=3601468#1
int DSL_2_G() { // http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=3601468#1
lint n, q, com, s, t, x;
in(n, q);
vector<lint> a(n);
LazySegtree tree(
a, [](lint a, lint b) { return a + b; }, 0);
rep(q) {
in(com, s, t);
--s; // 0-indexed,half-closed
if (com)
o(tree.query(s, t, lt_add));
else
in(x), tree.add(s, t, x);
}
return 0;
}
int main() {
lint n, q, com, s, t, x;
in(n, q);
vector<lint> a(n, INT_MAX);
LazySegtree tree(
a, [](lint a, lint b) { return min(a, b); }, INT_MAX);
rep(q) {
in(com, s, t);
++t;
if (com)
o(tree.query(s, t, lt_update));
else
in(x), tree.update(s, t, x);
}
}
// sub-EOF
|
/*vvv>
zzzzzI
.---. zzuzuI .vgggg&,.
+++++= dunkoI .WbbWo JMM9^```?TMB` ..&gNNg,. gggggggJ, qgggggggg]
(&&&&&&&&[ c+OA&J, (&&&&&&+J, .cJeAA&-. (&&&&&&&&x .&AA&=-.
+++++= dunkoI Xpbpbp JM#` (M#^ ?MMp MM| +TMN. JMF '
|yk ` dVY 7Vk, Vy XV cVf ?Y! JM V$ `
+++++= OunkoI Xppppp dMN .MM+ .MM MM| MM] JMMMMMM+
|@tqkoh) ,y0 (V$ yyyyyyyV7 VV JMWyZWr TWVVVVW&,
++++++ dZZZZ0 Xppppp ^HMN, _.db WMm, .MMF MM| ..MM` JMF .
|yk .WV&. .XW' yy 4yn. jyn +. JM #S
`++++` ?ZZZX= ?WWWW= -THMMMMH9^ (TMMMMM9! MMMMMMM"" JMMMMMMMME
|UU. ?TUUUUY= UU. (UU- ^7TUUUV7! JUUUUUUUU 7TUNKO*/
// Ricty Diminished
#include "bits/stdc++.h"
using namespace std;
typedef long long lint;
typedef vector<lint> liv;
// #define rep(i,n) for(int i=0;i<n;++i)
#define all(v) v.begin(), v.end()
#define linf 1152921504606846976
#define MAXN 100100
#define md 1000000007 // 998244353
#define pb push_back
#define _vcppunko4(tuple) _getname4 tuple
#define _getname4(_1, _2, _3, _4, name, ...) name
#define _getname3(_1, _2, _3, name, ...) name
#define _trep2(tuple) _rep2 tuple
#define _trep3(tuple) _rep3 tuple
#define _trep4(tuple) _rep4 tuple
#define _rep1(n) for (lint i = 0; i < n; ++i)
#define _rep2(i, n) for (lint i = 0; i < n; ++i)
#define _rep3(i, a, b) for (lint i = a; i < b; ++i)
#define _rep4(i, a, b, c) for (lint i = a; i < b; i += c)
#define _trrep2(tuple) _rrep2 tuple
#define _trrep3(tuple) _rrep3 tuple
#define _trrep4(tuple) _rrep4 tuple
#define _rrep1(n) for (lint i = n - 1; i >= 0; --i)
#define _rrep2(i, n) for (lint i = n - 1; i >= 0; --i)
#define _rrep3(i, a, b) for (lint i = b - 1; i >= a; --i)
#define _rrep4(i, a, b, c) \
for (lint i = a + (b - a - 1) / c * c; i >= a; i -= c)
template <class T> istream &operator>>(istream &is, vector<T> &vec);
template <class T, size_t size>
istream &operator>>(istream &is, array<T, size> &vec);
template <class T, class L> istream &operator>>(istream &is, pair<T, L> &p);
template <class T> ostream &operator<<(ostream &os, vector<T> &vec);
template <class T, class L> ostream &operator<<(ostream &os, pair<T, L> &p);
template <class T> istream &operator>>(istream &is, vector<T> &vec) {
for (T &x : vec)
is >> x;
return is;
}
template <class T, class L> istream &operator>>(istream &is, pair<T, L> &p) {
is >> p.first;
is >> p.second;
return is;
}
// template<class T>
// ostream& operator<<(ostream& os,vector<T>& vec){
// os<<vec[0];rep(i,1,vec.size())os<<' '<<vec[i];return os; } template<class T>
// ostream& operator<<(ostream& os,deque<T>& deq){
// os<<deq[0];rep(i,1,deq.size())os<<' '<<deq[i];return os; }
template <class T, class L> ostream &operator<<(ostream &os, pair<T, L> &p) {
os << p.first << " " << p.second;
return os;
}
inline void in() {}
template <class Head, class... Tail>
inline void in(Head &&head, Tail &&...tail) {
cin >> head;
in(move(tail)...);
}
template <class T> inline bool out(T t) {
cout << t << '\n';
return 0;
}
inline bool out() {
cout << '\n';
return 0;
}
template <class Head, class... Tail> inline bool out(Head head, Tail... tail) {
cout << head << ' ';
out(move(tail)...);
return 0;
}
template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); }
template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}
template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) {
t = v;
}
template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) {
for (auto &e : t)
fill_v(e, v);
} // http://beet-aizu.hatenablog.com/entry/2018/04/08/145516
#define rep(...) \
_vcppunko4((__VA_ARGS__, _trep4, _trep3, _trep2, _rep1))((__VA_ARGS__))
#define rrep(...) \
_vcppunko4((__VA_ARGS__, _trrep4, _trrep3, _trrep2, _rrep1))((__VA_ARGS__))
#define each(v) for (auto &i : v)
#define lin(...) \
lint __VA_ARGS__; \
in(__VA_ARGS__)
#define stin(...) \
string __VA_ARGS__; \
in(__VA_ARGS__)
#define vin(type, name, size) \
vector<type> name(size); \
in(name)
#define fi i.first
#define se i.second
#define YES(c) cout << ((c) ? "YES\n" : "NO\n"), 0
#define Yes(c) cout << ((c) ? "Yes\n" : "No\n"), 0
#define o(p) cout << p << endl, 0
#define deb(p) cerr << p << endl, 0
#define dd(n) cout << fixed << setprecision(n)
#define inf linf
//(T,f,e). ex.(lint,+,0) -> LazySegtree t(/*initial vector*/,[](lint a,lint
//b){return a+b},0);
enum lazytype {
lt_add,
lt_update,
lt_end
};
class LazySegtree {
public:
using T = lint;
using func = function<T(T, T)>;
int n, sz;
vector<T> node, lazy;
bool lazyFlag[2 * 140000];
// inline lint f(lint x,lint y){ return x|y; }
func f;
T e;
LazySegtree(vector<T> v, func f, T e) : f(f), e(e) {
sz = v.size();
n = 1;
while (n < sz)
n <<= 1;
node.resize(2 * n, e);
lazy.resize(2 * n, 0);
rep(i, sz) node[n + i] = v[i], lazyFlag[i] = 0;
for (int i = n - 1; i > 0; --i)
node[i] = f(node[2 * i], node[2 * i + 1]);
}
void eval(lazytype t, int k, int l, int r) {
switch (t) {
case lt_add: {
if (2 * n <= k || !lazy[k])
return;
node[k] += lazy[k];
if (r - l > 1) {
lazy[2 * k] += lazy[k] / 2;
lazy[2 * k + 1] += lazy[k] / 2;
}
lazy[k] = 0;
break;
}
case lt_update: {
if (2 * n <= k || !lazyFlag[k])
break;
node[k] = lazy[k];
if (r - l > 1) {
lazy[2 * k] = lazy[2 * k + 1] = lazy[k];
lazyFlag[2 * k] = lazyFlag[2 * k + 1] = 1;
}
lazyFlag[k] = 0;
break;
}
default:
break;
}
}
void update(int i, T x) {
node[i += n] = x;
while (i >>= 1)
node[i] = f(node[2 * i], node[2 * i + 1]);
}
void update(int a, int b, T x, int k = 1, int l = 0, int r = -1) {
if (r < 0)
r = n;
eval(lt_update, k, l, r);
if (b <= l || r <= a)
return;
if (a <= l && r <= b) {
lazy[k] = x;
lazyFlag[k] = 1;
eval(lt_update, k, l, r);
} else {
update(a, b, x, 2 * k, l, (l + r) / 2);
update(a, b, x, 2 * k + 1, (l + r) / 2, r);
node[k] = min(node[2 * k], node[2 * k + 1]);
}
}
void add(int a, int b, T x, int k = 1, int l = 0, int r = -1) {
if (r < 0)
r = n;
eval(lt_add, k, l, r);
if (b <= l || r <= a)
return;
if (a <= l && r <= b)
lazy[k] += (r - l) * x, eval(lt_add, k, l, r);
else {
add(a, b, x, 2 * k, l, (l + r) / 2);
add(a, b, x, 2 * k + 1, (l + r) / 2, r);
node[k] = f(node[2 * k], node[2 * k + 1]);
}
}
lint query(int a, int b, lazytype t, int k = 1, int l = 0, int r = -1) {
if (r < 0)
r = n;
if (r <= a || b <= l)
return e;
eval(t, k, l, r);
if (a <= l && r <= b)
return node[k];
return f(query(a, b, t, 2 * k, l, (l + r) / 2),
query(a, b, t, 2 * k + 1, (l + r) / 2, r));
}
}; // http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=3601468#1
int DSL_2_G() { // http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=3601468#1
lint n, q, com, s, t, x;
in(n, q);
vector<lint> a(n);
LazySegtree tree(
a, [](lint a, lint b) { return a + b; }, 0);
rep(q) {
in(com, s, t);
--s; // 0-indexed,half-closed
if (com)
o(tree.query(s, t, lt_add));
else
in(x), tree.add(s, t, x);
}
return 0;
}
int main() {
lint n, q, com, s, t, x;
in(n, q);
vector<lint> a(n, INT_MAX);
LazySegtree tree(
a, [](lint a, lint b) { return min(a, b); }, INT_MAX);
rep(q) {
in(com, s, t);
++t;
if (com)
o(tree.query(s, t, lt_update));
else
in(x), tree.update(s, t, x);
}
}
// sub-EOF
|
replace
| 147 | 148 | 147 | 148 |
0
| |
p02350
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
int n, q;
struct Node {
int min = 0x7fffffff, lazy = -1;
} seg[400005];
void update(int u, int l, int r, int ql, int qr, int v) {
if (seg[u].lazy != -1) {
seg[u << 1].lazy = seg[u << 1 | 1].lazy = seg[u].lazy;
seg[u].lazy = -1;
}
if (l == ql && r == qr) {
seg[u].lazy = v;
return;
}
int m = (l + r) >> 1;
if (qr <= m)
update(u << 1, l, m, ql, qr, v);
else if (ql > m)
update(u << 1 | 1, m + 1, r, ql, qr, v);
else
update(u << 1, l, m, ql, m, v), update(u << 1 | 1, m + 1, r, m + 1, qr, v);
int lm = seg[u << 1].lazy != -1 ? seg[u << 1].lazy : seg[u << 1].min;
int rm =
seg[u << 1 | 1].lazy != -1 ? seg[u << 1 | 1].lazy : seg[u << 1 | 1].min;
seg[u].min = min(lm, rm);
}
int query(int u, int l, int r, int ql, int qr) {
if (seg[u].lazy != -1)
return seg[u].lazy;
if (l == ql && r == qr)
return seg[u].min;
int m = (l + r) >> 1;
if (qr <= m)
return query(u << 1, l, m, ql, qr);
else if (ql > m)
return query(u << 1 | 1, m + 1, r, ql, qr);
else
return min(query(u << 1, l, m, ql, m),
query(u << 1 | 1, m + 1, r, m + 1, qr));
}
int main() {
cin >> n >> q;
for (int i = 0; i < q; i++) {
int o;
cin >> o;
if (o == 0) {
// update
int l, r, v;
cin >> l >> r >> v;
update(1, 0, n - 1, l, r, v);
} else {
int l, r;
cin >> l >> r;
cout << query(1, 0, n - 1, l, r) << endl;
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
int n, q;
struct Node {
int min = 0x7fffffff, lazy = -1;
} seg[800005];
void update(int u, int l, int r, int ql, int qr, int v) {
if (seg[u].lazy != -1) {
seg[u << 1].lazy = seg[u << 1 | 1].lazy = seg[u].lazy;
seg[u].lazy = -1;
}
if (l == ql && r == qr) {
seg[u].lazy = v;
return;
}
int m = (l + r) >> 1;
if (qr <= m)
update(u << 1, l, m, ql, qr, v);
else if (ql > m)
update(u << 1 | 1, m + 1, r, ql, qr, v);
else
update(u << 1, l, m, ql, m, v), update(u << 1 | 1, m + 1, r, m + 1, qr, v);
int lm = seg[u << 1].lazy != -1 ? seg[u << 1].lazy : seg[u << 1].min;
int rm =
seg[u << 1 | 1].lazy != -1 ? seg[u << 1 | 1].lazy : seg[u << 1 | 1].min;
seg[u].min = min(lm, rm);
}
int query(int u, int l, int r, int ql, int qr) {
if (seg[u].lazy != -1)
return seg[u].lazy;
if (l == ql && r == qr)
return seg[u].min;
int m = (l + r) >> 1;
if (qr <= m)
return query(u << 1, l, m, ql, qr);
else if (ql > m)
return query(u << 1 | 1, m + 1, r, ql, qr);
else
return min(query(u << 1, l, m, ql, m),
query(u << 1 | 1, m + 1, r, m + 1, qr));
}
int main() {
cin >> n >> q;
for (int i = 0; i < q; i++) {
int o;
cin >> o;
if (o == 0) {
// update
int l, r, v;
cin >> l >> r >> v;
update(1, 0, n - 1, l, r, v);
} else {
int l, r;
cin >> l >> r;
cout << query(1, 0, n - 1, l, r) << endl;
}
}
}
|
replace
| 7 | 8 | 7 | 8 |
0
| |
p02350
|
C++
|
Runtime Error
|
#include <algorithm>
#include <iostream>
constexpr int MAX_N = 100000, INF = 2147483647;
int n, q;
int seg[MAX_N * 4], lazy[MAX_N * 4];
bool flag[MAX_N * 4];
void init(int size) {
n = 1;
while (n < size)
n *= 2;
for (int i = 0; i < n * 2 - 1; ++i) {
seg[i] = INF;
lazy[i] = INF;
}
}
void eval(int k, int l, int r) {
if (flag[k]) {
seg[k] = lazy[k];
if (r - l > 1) {
lazy[k * 2 + 1] = lazy[k * 2 + 2] = lazy[k];
flag[k * 2 + 1] = flag[k * 2 + 2] = true;
}
}
flag[k] = false;
}
void update(int a, int b, int v, int k, int l, int r) {
eval(k, l, r);
if (r <= a || b <= l)
return;
if (a <= l && r <= b) {
lazy[k] = v;
flag[k] = true;
eval(k, l, r);
return;
}
update(a, b, v, k * 2 + 1, l, (l + r) / 2);
update(a, b, v, k * 2 + 2, (l + r) / 2, r);
seg[k] = std::min(seg[k * 2 + 1], seg[k * 2 + 2]);
}
int query(int a, int b, int k, int l, int r) {
eval(k, l, r);
if (r <= a || b <= l)
return INF;
if (a <= l && r <= b)
return seg[k];
return std::min(query(a, b, k * 2 + 1, l, (l + r) / 2),
query(a, b, k * 2 + 2, (l + r) / 2, r));
}
int main() {
std::cin >> n >> q;
init(n);
int com, s, t, x;
for (int i = 0; i < q; ++i) {
std::cin >> com >> s >> t;
if (com == 0) {
std::cin >> x;
update(s, t + 1, x, 0, 0, n);
} else
std::cout << query(s, t + 1, 0, 0, n) << std::endl;
}
system("pause");
return 0;
}
|
#include <algorithm>
#include <iostream>
constexpr int MAX_N = 100000, INF = 2147483647;
int n, q;
int seg[MAX_N * 4], lazy[MAX_N * 4];
bool flag[MAX_N * 4];
void init(int size) {
n = 1;
while (n < size)
n *= 2;
for (int i = 0; i < n * 2 - 1; ++i) {
seg[i] = INF;
lazy[i] = INF;
}
}
void eval(int k, int l, int r) {
if (flag[k]) {
seg[k] = lazy[k];
if (r - l > 1) {
lazy[k * 2 + 1] = lazy[k * 2 + 2] = lazy[k];
flag[k * 2 + 1] = flag[k * 2 + 2] = true;
}
}
flag[k] = false;
}
void update(int a, int b, int v, int k, int l, int r) {
eval(k, l, r);
if (r <= a || b <= l)
return;
if (a <= l && r <= b) {
lazy[k] = v;
flag[k] = true;
eval(k, l, r);
return;
}
update(a, b, v, k * 2 + 1, l, (l + r) / 2);
update(a, b, v, k * 2 + 2, (l + r) / 2, r);
seg[k] = std::min(seg[k * 2 + 1], seg[k * 2 + 2]);
}
int query(int a, int b, int k, int l, int r) {
eval(k, l, r);
if (r <= a || b <= l)
return INF;
if (a <= l && r <= b)
return seg[k];
return std::min(query(a, b, k * 2 + 1, l, (l + r) / 2),
query(a, b, k * 2 + 2, (l + r) / 2, r));
}
int main() {
std::cin >> n >> q;
init(n);
int com, s, t, x;
for (int i = 0; i < q; ++i) {
std::cin >> com >> s >> t;
if (com == 0) {
std::cin >> x;
update(s, t + 1, x, 0, 0, n);
} else
std::cout << query(s, t + 1, 0, 0, n) << std::endl;
}
return 0;
}
|
delete
| 96 | 98 | 96 | 96 |
0
|
sh: 1: pause: not found
|
p02351
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
using Int = long long;
template <typename T, typename E> struct SegmentTree {
typedef function<T(T, T)> F;
typedef function<T(T, E)> G;
typedef function<T(E, E)> H;
typedef function<E(E, int)> P;
int n;
F f;
G g;
H h;
P p;
T d1;
E d0;
vector<T> dat;
vector<E> laz;
SegmentTree(){};
SegmentTree(
int n_, F f, G g, H h, T d1, E d0, vector<T> v = vector<T>(),
P p = [](E a, int b) { return a; })
: f(f), g(g), h(h), d1(d1), d0(d0), p(p) {
init(n_);
if (n_ == (int)v.size())
build(n_, v);
}
void init(int n_) {
n = 1;
while (n < n_)
n *= 2;
dat.clear();
dat.resize(2 * n - 1, d1);
laz.clear();
laz.resize(2 * n - 1, d0);
}
void build(int n_, vector<T> v) {
for (int i = 0; i < n_; i++)
dat[i + n - 1] = v[i];
for (int i = n - 2; i >= 0; i--)
dat[i] = f(dat[i * 2 + 1], dat[i * 2 + 2]);
}
void update(int a, int b, E x, int k, int l, int r) {
if (r <= a || b <= l)
return;
if (a <= l && r <= b) {
laz[k] = h(laz[k], x);
return;
}
update(a, b, x, k * 2 + 1, l, (l + r) / 2);
update(a, b, x, k * 2 + 2, (l + r) / 2, r);
dat[k] = f(g(dat[k * 2 + 1], p(laz[k * 2 + 1], (r - l) >> 1)),
g(dat[k * 2 + 2], p(laz[k * 2 + 2], (r - l) >> 1)));
}
void update(int a, int b, E x) { update(a, b, x, 0, 0, n); }
T query(int a, int b, int k, int l, int r) {
if (r <= a || b <= l)
return d1;
if (a <= l && r <= b)
return g(dat[k], p(laz[k], r - l));
T vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
T vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
return g(f(vl, vr), p(laz[k], min(r, b) - max(l, a)));
}
T query(int a, int b) { return query(a, b, 0, 0, n); }
};
signed main() {
int n, q;
cin >> n >> q;
SegmentTree<Int, Int> ch(
n, [](Int a, Int b) { return a + b; }, [](Int a, Int b) { return a + b; },
[](Int a, Int b) { return a + b; }, 0, 0, vector<Int>(n, 0),
[](Int a, int b) {
cerr << b << endl;
return a * b;
});
for (int i = 0; i < q; i++) {
int c, s, t, x;
cin >> c;
if (c) {
cin >> s >> t;
cout << ch.query(s - 1, t) << endl;
} else {
cin >> s >> t >> x;
ch.update(s - 1, t, x);
}
}
}
/*
verified on 2017/10/15
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DSL_2_G
*/
|
#include <bits/stdc++.h>
using namespace std;
using Int = long long;
template <typename T, typename E> struct SegmentTree {
typedef function<T(T, T)> F;
typedef function<T(T, E)> G;
typedef function<T(E, E)> H;
typedef function<E(E, int)> P;
int n;
F f;
G g;
H h;
P p;
T d1;
E d0;
vector<T> dat;
vector<E> laz;
SegmentTree(){};
SegmentTree(
int n_, F f, G g, H h, T d1, E d0, vector<T> v = vector<T>(),
P p = [](E a, int b) { return a; })
: f(f), g(g), h(h), d1(d1), d0(d0), p(p) {
init(n_);
if (n_ == (int)v.size())
build(n_, v);
}
void init(int n_) {
n = 1;
while (n < n_)
n *= 2;
dat.clear();
dat.resize(2 * n - 1, d1);
laz.clear();
laz.resize(2 * n - 1, d0);
}
void build(int n_, vector<T> v) {
for (int i = 0; i < n_; i++)
dat[i + n - 1] = v[i];
for (int i = n - 2; i >= 0; i--)
dat[i] = f(dat[i * 2 + 1], dat[i * 2 + 2]);
}
void update(int a, int b, E x, int k, int l, int r) {
if (r <= a || b <= l)
return;
if (a <= l && r <= b) {
laz[k] = h(laz[k], x);
return;
}
update(a, b, x, k * 2 + 1, l, (l + r) / 2);
update(a, b, x, k * 2 + 2, (l + r) / 2, r);
dat[k] = f(g(dat[k * 2 + 1], p(laz[k * 2 + 1], (r - l) >> 1)),
g(dat[k * 2 + 2], p(laz[k * 2 + 2], (r - l) >> 1)));
}
void update(int a, int b, E x) { update(a, b, x, 0, 0, n); }
T query(int a, int b, int k, int l, int r) {
if (r <= a || b <= l)
return d1;
if (a <= l && r <= b)
return g(dat[k], p(laz[k], r - l));
T vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
T vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
return g(f(vl, vr), p(laz[k], min(r, b) - max(l, a)));
}
T query(int a, int b) { return query(a, b, 0, 0, n); }
};
signed main() {
int n, q;
cin >> n >> q;
SegmentTree<Int, Int> ch(
n, [](Int a, Int b) { return a + b; }, [](Int a, Int b) { return a + b; },
[](Int a, Int b) { return a + b; }, 0, 0, vector<Int>(n, 0),
[](Int a, int b) { return a * b; });
for (int i = 0; i < q; i++) {
int c, s, t, x;
cin >> c;
if (c) {
cin >> s >> t;
cout << ch.query(s - 1, t) << endl;
} else {
cin >> s >> t >> x;
ch.update(s - 1, t, x);
}
}
}
/*
verified on 2017/10/15
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DSL_2_G
*/
|
replace
| 73 | 77 | 73 | 74 |
0
|
2
2
1
1
1
1
2
2
1
1
2
2
2
2
1
1
1
1
2
|
p02351
|
C++
|
Runtime Error
|
// #pragma GCC optimize ("-O3")
#include <bits/stdc++.h>
using namespace std;
//@起動時
struct initon {
initon() {
cin.tie(0);
ios::sync_with_stdio(false);
cout.setf(ios::fixed);
cout.precision(16);
srand((unsigned)clock() + (unsigned)time(NULL));
};
} __initon;
// 衝突対策
#define ws ___ws
struct T {
int f, s, t;
T() { f = -1, s = -1, t = -1; }
T(int f, int s, int t) : f(f), s(s), t(t) {}
bool operator<(const T &r) const {
return f != r.f ? f < r.f : s != r.s ? s < r.s : t < r.t;
// return f != r.f ? f > r.f : s != r.s ? s > r.s : t > r.t; 大きい順
}
bool operator>(const T &r) const {
return f != r.f ? f > r.f : s != r.s ? s > r.s : t > r.t;
// return f != r.f ? f > r.f : s != r.s ? s > r.s : t > r.t; 小さい順
}
bool operator==(const T &r) const { return f == r.f && s == r.s && t == r.t; }
bool operator!=(const T &r) const { return f != r.f || s != r.s || t != r.t; }
int operator[](int i) {
assert(i < 3);
return i == 0 ? f : i == 1 ? s : t;
}
};
#define int long long
#define ll long long
#define double long double
#define ull unsigned long long
using dou = double;
using itn = int;
using str = string;
using bo = bool;
#define au auto
using P = pair<ll, ll>;
#define fi first
#define se second
#define vec vector
#define beg begin
#define rbeg rbegin
#define con continue
#define bre break
#define brk break
#define is ==
// マクロ省略系 コンテナ
using vi = vector<int>;
#define _overloadvvi(_1, _2, _3, _4, name, ...) name
#define vvi0() vec<vi>
#define vvi1(a) vec<vi> a
#define vvi2(a, b) vec<vi> a(b)
#define vvi3(a, b, c) vec<vi> a(b, vi(c))
#define vvi4(a, b, c, d) vec<vi> a(b, vi(c, d))
#define vvi(...) \
_overloadvvi(__VA_ARGS__, vvi4, vvi3, vvi2, vvi1, vvi0)(__VA_ARGS__)
using vl = vector<ll>;
#define _overloadvvl(_1, _2, _3, _4, name, ...) name
#define vvl1(a) vec<vl> a
#define vvl2(a, b) vec<vl> a(b)
#define vvl3(a, b, c) vec<vl> a(b, vl(c))
#define vvl4(a, b, c, d) vec<vl> a(b, vl(c, d))
#define vvl(...) _overloadvvl(__VA_ARGS__, vvl4, vvl3, vvl2, vvl1)(__VA_ARGS__)
using vb = vector<bool>;
#define _overloadvvb(_1, _2, _3, _4, name, ...) name
#define vvb1(a) vec<vb> a
#define vvb2(a, b) vec<vb> a(b)
#define vvb3(a, b, c) vec<vb> a(b, vb(c))
#define vvb4(a, b, c, d) vec<vb> a(b, vb(c, d))
#define vvb(...) _overloadvvb(__VA_ARGS__, vvb4, vvb3, vvb2, vvb1)(__VA_ARGS__)
using vs = vector<string>;
#define _overloadvvs(_1, _2, _3, _4, name, ...) name
#define vvs1(a) vec<vs> a
#define vvs2(a, b) vec<vs> a(b)
#define vvs3(a, b, c) vec<vs> a(b, vs(c))
#define vvs4(a, b, c, d) vec<vs> a(b, vs(c, d))
#define vvs(...) _overloadvvs(__VA_ARGS__, vvs4, vvs3, vvs2, vvs1)(__VA_ARGS__)
using vd = vector<double>;
#define _overloadvvd(_1, _2, _3, _4, name, ...) name
#define vvd1(a) vec<vd> a
#define vvd2(a, b) vec<vd> a(b)
#define vvd3(a, b, c) vec<vd> a(b, vd(c))
#define vvd4(a, b, c, d) vec<vd> a(b, vd(c, d))
#define vvd(...) _overloadvvd(__VA_ARGS__, vvd4, vvd3, vvd2, vvd1)(__VA_ARGS__)
using vc = vector<char>;
#define _overloadvvc(_1, _2, _3, _4, name, ...) name
#define vvc1(a) vec<vc> a
#define vvc2(a, b) vec<vc> a(b)
#define vvc3(a, b, c) vec<vc> a(b, vc(c))
#define vvc4(a, b, c, d) vec<vc> a(b, vc(c, d))
#define vvc(...) _overloadvvc(__VA_ARGS__, vvc4, vvc3, vvc2, vvc1)(__VA_ARGS__)
using vp = vector<P>;
#define _overloadvvp(_1, _2, _3, _4, name, ...) name
#define vvp1(a) vec<vp> a
#define vvp2(a, b) vec<vp> a(b)
#define vvp3(a, b, c) vec<vp> a(b, vp(c))
#define vvp4(a, b, c, d) vec<vp> a(b, vp(c, d))
using vt = vector<T>;
#define _overloadvvt(_1, _2, _3, _4, name, ...) name
#define vvt1(a) vec<vt> a
#define vvt2(a, b) vec<vt> a(b)
#define vvt3(a, b, c) vec<vt> a(b, vt(c))
#define vvt4(a, b, c, d) vec<vt> a(b, vt(c, d))
#define v3i(a, b, c, d) vector<vector<vi>> a(b, vector<vi>(c, vi(d)))
#define v3d(a, b, c, d) vector<vector<vd>> a(b, vector<vd>(c, vd(d)))
#define v3m(a, b, c, d) vector<vector<vm>> a(b, vector<vm>(c, vm(d)))
#define _vvi vector<vi>
#define _vvl vector<vl>
#define _vvb vector<vb>
#define _vvs vector<vs>
#define _vvd vector<vd>
#define _vvc vector<vc>
#define _vvp vector<vp>
#define PQ priority_queue<ll, vector<ll>, greater<ll>>
#define tos to_string
using mapi = map<int, int>;
using mapd = map<dou, int>;
using mapc = map<char, int>;
using maps = map<str, int>;
using seti = set<int>;
using setd = set<dou>;
using setc = set<char>;
using sets = set<str>;
using qui = queue<int>;
#define bset bitset
#define uset unordered_set
#define mset multiset
#define umap unordered_map
#define umapi unordered_map<int, int>
#define umapp unordered_map<P, int>
#define mmap multimap
// マクロ 繰り返し
#define _overloadrep(_1, _2, _3, _4, name, ...) name
#define _rep(i, n) for (int i = 0, _lim = n; i < _lim; i++)
#define repi(i, m, n) for (int i = m, _lim = n; i < _lim; i++)
#define repadd(i, m, n, ad) for (int i = m, _lim = n; i < _lim; i += ad)
#define rep(...) _overloadrep(__VA_ARGS__, repadd, repi, _rep, )(__VA_ARGS__)
#define _rer(i, n) for (int i = n; i >= 0; i--)
#define reri(i, m, n) for (int i = m, _lim = n; i >= _lim; i--)
#define rerdec(i, m, n, dec) for (int i = m, _lim = n; i >= _lim; i -= dec)
#define rer(...) _overloadrep(__VA_ARGS__, rerdec, reri, _rer, )(__VA_ARGS__)
#define fora(a, b) for (auto &&a : b)
#define forg(gi, ve) \
for (int gi = 0, f, t, c; \
gi < ve.size() && (f = ve[gi].f, t = ve[gi].t, c = ve[gi].c, true); \
gi++)
#define fort(gi, ve) \
for (int gi = 0, f, t, c; \
gi < ve.size() && (f = ve[gi].f, t = ve[gi].t, c = ve[gi].c, true); \
gi++) \
if (t != p)
// #define fort(gi, ve) for (int gi = 0, f, t, c;gi<ve.size()&& (gi+=
// (ve[gi].t==p))< ve.size() && (f = ve[gi].f,t=ve[gi].t, c = ve[gi].c,true);
// gi++)
// マクロ 定数
#define k3 1010
#define k4 10101
#define k5 101010
#define k6 1010101
#define k7 10101010
const int inf = (int)1e9 + 100;
const ll linf = (ll)1e18 + 100;
const double eps = 1e-9;
const double PI = 3.1415926535897932384626433832795029L;
ll ma = numeric_limits<ll>::min();
ll mi = numeric_limits<ll>::max();
const int y4[] = {-1, 1, 0, 0};
const int x4[] = {0, 0, -1, 1};
const int y8[] = {0, 1, 0, -1, -1, 1, 1, -1};
const int x8[] = {1, 0, -1, 0, 1, -1, 1, -1};
// マクロ省略形 関数等
#define arsz(a) (sizeof(a) / sizeof(a[0]))
#define sz(a) ((int)(a).size())
#define rs resize
#define mp make_pair
#define pb push_back
#define pf push_front
#define eb emplace_back
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
inline void sort(string &a) { sort(a.begin(), a.end()); }
template <class T> inline void sort(vector<T> &a) { sort(a.begin(), a.end()); };
template <class T> inline void sort(vector<T> &a, int len) {
sort(a.begin(), a.begin() + len);
};
template <class T, class F> inline void sort(vector<T> &a, F f) {
sort(a.begin(), a.end(), [&](T l, T r) { return f(l) < f(r); });
};
enum ___pcomparator { fisi, fisd, fdsi, fdsd, sifi, sifd, sdfi, sdfd };
inline void sort(vector<P> &a, ___pcomparator type) {
switch (type) {
case fisi:
sort(all(a),
[&](P l, P r) { return l.fi != r.fi ? l.fi < r.fi : l.se < r.se; });
break;
case fisd:
sort(all(a),
[&](P l, P r) { return l.fi != r.fi ? l.fi < r.fi : l.se > r.se; });
break;
case fdsi:
sort(all(a),
[&](P l, P r) { return l.fi != r.fi ? l.fi > r.fi : l.se < r.se; });
break;
case fdsd:
sort(all(a),
[&](P l, P r) { return l.fi != r.fi ? l.fi > r.fi : l.se > r.se; });
break;
case sifi:
sort(all(a),
[&](P l, P r) { return l.se != r.se ? l.se < r.se : l.fi < r.fi; });
break;
case sifd:
sort(all(a),
[&](P l, P r) { return l.se != r.se ? l.se < r.se : l.fi > r.fi; });
break;
case sdfi:
sort(all(a),
[&](P l, P r) { return l.se != r.se ? l.se > r.se : l.fi < r.fi; });
break;
case sdfd:
sort(all(a),
[&](P l, P r) { return l.se != r.se ? l.se > r.se : l.fi > r.fi; });
break;
}
};
inline void sort(vector<T> &a, ___pcomparator type) {
switch (type) {
case fisi:
sort(all(a), [&](T l, T r) { return l.f != r.f ? l.f < r.f : l.s < r.s; });
break;
case fisd:
sort(all(a), [&](T l, T r) { return l.f != r.f ? l.f < r.f : l.s > r.s; });
break;
case fdsi:
sort(all(a), [&](T l, T r) { return l.f != r.f ? l.f > r.f : l.s < r.s; });
break;
case fdsd:
sort(all(a), [&](T l, T r) { return l.f != r.f ? l.f > r.f : l.s > r.s; });
break;
case sifi:
sort(all(a), [&](T l, T r) { return l.s != r.s ? l.s < r.s : l.f < r.f; });
break;
case sifd:
sort(all(a), [&](T l, T r) { return l.s != r.s ? l.s < r.s : l.f > r.f; });
break;
case sdfi:
sort(all(a), [&](T l, T r) { return l.s != r.s ? l.s > r.s : l.f < r.f; });
break;
case sdfd:
sort(all(a), [&](T l, T r) { return l.s != r.s ? l.s > r.s : l.f > r.f; });
break;
}
};
template <class T> inline void rsort(vector<T> &a) {
sort(a.begin(), a.end(), greater<T>());
};
template <class T> inline void rsort(vector<T> &a, int len) {
sort(a.begin(), a.begin() + len, greater<T>());
};
template <class U, class F> inline void rsort(vector<U> &a, F f) {
sort(a.begin(), a.end(), [&](U l, U r) { return f(l) > f(r); });
};
template <class U> inline void sortp(vector<U> &a, vector<U> &b) {
vp c;
int n = sz(a);
assert(n == sz(b));
rep(i, n) c.eb(a[i], b[i]);
sort(c);
rep(i, n) {
a[i] = c[i].first;
b[i] = c[i].second;
;
}
};
// F = T<T>
// 例えばreturn p.fi + p.se;
template <class U, class F> inline void sortp(vector<U> &a, vector<U> &b, F f) {
vp c;
int n = sz(a);
assert(n == sz(b));
rep(i, n) c.eb(a[i], b[i]);
sort(c, f);
rep(i, n) {
a[i] = c[i].first;
b[i] = c[i].second;
}
};
template <class U, class F>
inline void sortp(vector<U> &a, vector<U> &b, char type) {
vp c;
int n = sz(a);
assert(n == sz(b));
rep(i, n) c.eb(a[i], b[i]);
sort(c, type);
rep(i, n) {
a[i] = c[i].first;
b[i] = c[i].second;
}
};
template <class U> inline void rsortp(vector<U> &a, vector<U> &b) {
vp c;
int n = sz(a);
assert(n == sz(b));
rep(i, n) c.eb(a[i], b[i]);
rsort(c);
rep(i, n) {
a[i] = c[i].first;
b[i] = c[i].second;
}
};
template <class U, class F>
inline void rsortp(vector<U> &a, vector<U> &b, F f) {
vp c;
int n = sz(a);
assert(n == sz(b));
rep(i, n) c.eb(a[i], b[i]);
rsort(c, f);
rep(i, n) {
a[i] = c[i].first;
b[i] = c[i].second;
}
};
template <class U> inline void sortt(vector<U> &a, vector<U> &b, vector<U> &c) {
vt r;
int n = sz(a);
assert(n == sz(b));
assert(n == sz(c));
rep(i, n) r.eb(a[i], b[i], c[i]);
sort(r);
rep(i, n) {
a[i] = r[i].f;
b[i] = r[i].s;
c[i] = r[i].t;
}
};
template <class U, class F>
inline void sortt(vector<U> &a, vector<U> &b, vector<U> &c, F f) {
vt r;
int n = sz(a);
assert(n == sz(b));
assert(n == sz(c));
rep(i, n) r.eb(a[i], b[i], c[i]);
sort(r, f);
rep(i, n) {
a[i] = r[i].f;
b[i] = r[i].s;
c[i] = r[i].t;
}
};
template <class U, class F>
inline void rsortt(vector<U> &a, vector<U> &b, vector<U> &c, F f) {
vt r;
int n = sz(a);
assert(n == sz(b));
assert(n == sz(c));
rep(i, n) r.eb(a[i], b[i], c[i]);
rsort(r, f);
rep(i, n) {
a[i] = r[i].f;
b[i] = r[i].s;
c[i] = r[i].t;
}
};
template <class T> inline void sort2(vector<vector<T>> &a) {
for (int i = 0, n = a.size(); i < n; i++)
sort(a[i]);
}
template <class T> inline void rsort2(vector<vector<T>> &a) {
for (int i = 0, n = a.size(); i < n; i++)
rsort(a[i]);
}
template <typename A, size_t N, typename T> void fill(A (&a)[N], const T &v) {
rep(i, N) a[i] = v;
}
template <typename A, size_t N, size_t O, typename T>
void fill(A (&a)[N][O], const T &v) {
rep(i, N) rep(j, O) a[i][j] = v;
}
template <typename A, size_t N, size_t O, size_t P, typename T>
void fill(A (&a)[N][O][P], const T &v) {
rep(i, N) rep(j, O) rep(k, P) a[i][j][k] = v;
}
template <typename A, size_t N, size_t O, size_t P, size_t Q, typename T>
void fill(A (&a)[N][O][P][Q], const T &v) {
rep(i, N) rep(j, O) rep(k, P) rep(l, Q) a[i][j][k][l] = v;
}
template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R,
typename T>
void fill(A (&a)[N][O][P][Q][R], const T &v) {
rep(i, N) rep(j, O) rep(k, P) rep(l, Q) rep(m, R) a[i][j][k][l][m] = v;
}
template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R,
size_t S, typename T>
void fill(A (&a)[N][O][P][Q][R][S], const T &v) {
rep(i, N) rep(j, O) rep(k, P) rep(l, Q) rep(m, R) rep(n, S)
a[i][j][k][l][m][n] = v;
}
template <typename V, typename T> void fill(V &xx, const T vall) { xx = vall; }
template <typename V, typename T> void fill(vector<V> &vecc, const T vall) {
for (auto &&vx : vecc)
fill(vx, vall);
}
//@汎用便利関数 入力
template <typename T = int> T _in() {
T x;
cin >> x;
return (x);
}
#define _overloadin(_1, _2, _3, _4, name, ...) name
#define in0() _in()
#define in1(a) cin >> a
#define in2(a, b) cin >> a >> b
#define in3(a, b, c) cin >> a >> b >> c
#define in4(a, b, c, d) cin >> a >> b >> c >> d
#define in(...) _overloadin(__VA_ARGS__, in4, in3, in2, in1, in0)(__VA_ARGS__)
#define _overloaddin(_1, _2, _3, _4, name, ...) name
#define din1(a) \
int a; \
cin >> a
#define din2(a, b) \
int a, b; \
cin >> a >> b
#define din3(a, b, c) \
int a, b, c; \
cin >> a >> b >> c
#define din4(a, b, c, d) \
int a, b, c, d; \
cin >> a >> b >> c >> d
#define din(...) _overloadin(__VA_ARGS__, din4, din3, din2, din1)(__VA_ARGS__)
#define _overloaddind(_1, _2, _3, _4, name, ...) name
#define din1d(a) \
int a; \
cin >> a; \
a--
#define din2d(a, b) \
int a, b; \
cin >> a >> b; \
a--, b--
#define din3d(a, b, c) \
int a, b, c; \
cin >> a >> b >> c; \
a--, b--, c--
#define din4d(a, b, c, d) \
int a, b, c, d; \
cin >> a >> b >> c >> d; \
; \
a--, b--, c--, d--
#define dind(...) \
_overloaddind(__VA_ARGS__, din4d, din3d, din2d, din1d)(__VA_ARGS__)
string sin() { return _in<string>(); }
ll lin() { return _in<ll>(); }
#define na(a, n) \
a.resize(n); \
rep(i, n) cin >> a[i];
#define nao(a, n) \
a.resize(n + 1); \
rep(i, n) cin >> a[i + 1];
#define nad(a, n) \
a.resize(n); \
rep(i, n) { \
cin >> a[i]; \
a[i]--; \
}
#define na2(a, b, n) \
a.resize(n), b.resize(n); \
rep(i, n) cin >> a[i] >> b[i];
#define na2d(a, b, n) \
a.resize(n), b.resize(n); \
rep(i, n) { \
cin >> a[i] >> b[i]; \
a[i]--, b[i]--; \
}
#define na3(a, b, c, n) \
a.resize(n), b.resize(n), c.resize(n); \
rep(i, n) cin >> a[i] >> b[i] >> c[i];
#define na3d(a, b, c, n) \
a.resize(n), b.resize(n), c.resize(n); \
rep(i, n) { \
cin >> a[i] >> b[i] >> c[i]; \
a[i]--, b[i]--, c[i]--; \
}
#define nt(a, h, w) \
resize(a, h, w); \
rep(hi, h) rep(wi, w) cin >> a[hi][wi];
#define ntd(a, h, w) \
rs(a, h, w); \
rep(hi, h) rep(wi, w) cin >> a[hi][wi], a[hi][wi]--;
#define ntp(a, h, w) \
fill(a, '#'); \
rep(hi, 1, h + 1) rep(wi, 1, w + 1) cin >> a[hi][wi];
// デバッグ
#define sp << " " <<
#define debugName(VariableName) #VariableName
#define _deb1(x) cerr << debugName(x) << " = " << x << endl
#define _deb2(x, y) \
cerr << debugName(x) << " = " << x << ", " << debugName(y) << " = " << y \
<< endl
#define _deb3(x, y, z) \
cerr << debugName(x) << " = " << x << ", " << debugName(y) << " = " << y \
<< ", " debugName(z) << " = " << z << endl
#define _deb4(x, y, z, a) \
cerr << debugName(x) << " = " << x << ", " << debugName(y) << " = " << y \
<< ", " << debugName(z) << " = " << z << ", " << debugName(a) << " = " \
<< a << endl
#define _deb5(x, y, z, a, b) \
cerr << debugName(x) << " = " << x << ", " << debugName(y) << " = " << y \
<< ", " << debugName(z) << " = " << z << ", " << debugName(a) << " = " \
<< a << ", " << debugName(b) << " = " << b << endl
#define _overloadebug(_1, _2, _3, _4, _5, name, ...) name
#define debug(...) \
_overloadebug(__VA_ARGS__, _deb5, _deb4, _deb3, _deb2, _deb1)(__VA_ARGS__)
#define deb(...) \
_overloadebug(__VA_ARGS__, _deb5, _deb4, _deb3, _deb2, _deb1)(__VA_ARGS__)
#define debugline(x) \
cerr << x << " " \
<< "(L:" << __LINE__ << ")" << '\n'
void ole() {
#ifdef _DEBUG
debugline("ole");
exit(0);
#endif
string a = "a";
rep(i, 30) a += a;
rep(i, 1 << 17) cout << a << endl;
cout << "OLE 出力長制限超過" << endl;
exit(0);
}
void tle() {
while (inf)
cout << inf << endl;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll gcd(vi b) {
ll res = b[0];
for (auto &&v : b)
res = gcd(v, res);
return res;
}
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
ll rev(ll a) {
ll res = 0;
while (a) {
res *= 10;
res += a % 10;
a /= 10;
}
return res;
}
template <class T> void rev(vector<T> &a) { reverse(all(a)); }
void rev(string &a) { reverse(all(a)); }
ll ceil(ll a, ll b) {
if (b == 0) {
debugline("ceil");
deb(a, b);
ole();
return -1;
} else
return (a + b - 1) / b;
}
ll sqrt(ll a) {
if (a < 0) {
debugline("sqrt");
deb(a);
ole();
}
ll res = (ll)std::sqrt(a);
while (res * res < a)
res++;
return res;
}
double log(double e, double x) { return log(x) / log(e); }
ll sig(ll t) { return (1 + t) * t / 2; }
ll sig(ll s, ll t) { return (s + t) * (t - s + 1) / 2; }
vi divisors(int v) {
vi res;
double lim = std::sqrt(v);
for (int i = 1; i <= lim; ++i) {
if (v % i == 0) {
res.pb(i);
if (i != v / i)
res.pb(v / i);
}
}
return res;
}
vb isPrime;
vi primes;
void setPrime() {
int len = 4010101;
isPrime.resize(4010101);
fill(isPrime, true);
isPrime[0] = isPrime[1] = false;
for (int i = 2; i <= sqrt(len) + 5; ++i) {
if (!isPrime[i])
continue;
for (int j = 2; i * j < len; ++j) {
isPrime[i * j] = false;
}
}
rep(i, len) if (isPrime[i]) primes.pb(i);
}
vi factorization(int v) {
int tv = v;
vi res;
if (isPrime.size() == 0)
setPrime();
for (auto &&p : primes) {
if (v % p == 0)
res.push_back(p);
while (v % p == 0) {
v /= p;
}
if (v == 1 || p * p > tv)
break;
}
if (v > 1)
res.pb(v);
return res;
}
inline bool inside(int h, int w, int H, int W) {
return h >= 0 && w >= 0 && h < H && w < W;
}
inline bool inside(int v, int l, int r) { return l <= v && v < r; }
#define ins inside
ll u(ll a) { return a < 0 ? 0 : a; }
template <class T> vector<T> u(const vector<T> &a) {
vector<T> ret = a;
fora(v, ret) v = u(v);
return ret;
}
#define MIN(a) numeric_limits<a>::min()
#define MAX(a) numeric_limits<a>::max()
int n, m, k, d, H, W, x, y, z, q;
int cou;
vi a, b, c;
vvi(s, 0, 0);
vvc(ba, 0, 0);
vp p;
int hbit(int n) {
n |= (n >> 1);
n |= (n >> 2);
n |= (n >> 4);
n |= (n >> 8);
n |= (n >> 16);
n |= (n >> 32);
return n - (n >> 1);
}
struct Monoid {
ll i, v;
Monoid(ll i, ll v) : i(i), v(v) {}
};
#define segMinl \
[](int i, int v) { return Monoid(i, v); }, \
[](Monoid a, Monoid b) { return a.v <= b.v ? a : b; }, \
[](int len, int x) { return x; }, Monoid(-1, MAX(ll))
#define segMinr \
[](int i, int v) { return Monoid(i, v); }, \
[](Monoid a, Monoid b) { return a.v < b.v ? a : b; }, \
[](int len, int x) { return x; }, Monoid(-1, MAX(ll))
#define segMaxl \
[](int i, int v) { return Monoid(i, v); }, \
[](Monoid a, Monoid b) { return a.v >= b.v ? a : b; }, \
[](int len, int x) { return x; }, Monoid(-1, MIN(ll))
#define segMaxr \
[](int i, int v) { return Monoid(i, v); }, \
[](Monoid a, Monoid b) { return a.v > b.v ? a : b; }, \
[](int len, int x) { return x; }, Monoid(-1, MIN(ll))
// 個数 最小値候補
#define segCouMin \
[](int cou, int v) { return Monoid(1, v); }, \
[](Monoid a, Monoid b) { \
int coum = 0; \
if (a.v <= b.v) \
coum += a.i; \
if (a.v >= b.v) \
coum += b.i; \
return Monoid(coum, min(a.v, b.v)); \
}, \
[](int len, int x) { return x; }, Monoid(0, MAX(ll))
#define segCouMax \
[](int cou, int v) { return Monoid(1, v); }, \
[](Monoid a, Monoid b) { \
int coum = 0; \
if (a.v >= b.v) \
coum += a.i; \
if (a.v <= b.v) \
coum += b.i; \
return Monoid(coum, max(a.v, b.v)); \
}, \
[](int len, int x) { return x; }, Monoid(0, MIN(ll))
#define segSum \
[](int i, int v) { return Monoid(i, v); }, \
[](Monoid a, Monoid b) { return Monoid(0, a.v + b.v); }, \
[](int len, int x) { return len * x; }, Monoid(0, 0)
#define segXor \
[](int i, int v) { return Monoid(i, v); }, \
[](Monoid a, Monoid b) { return Monoid(0, a.v ^ b.v); }, \
[](int len, int x) { return (len & 1) ? x : 0; }, Monoid(0, 0)
// 一つずつ初期化するのは大変
// vi を渡してセットするのがベスト
// updateは諦める
struct SegmentLazyAdd {
using fini = function<Monoid(int, int)>;
using func = function<Monoid(Monoid, Monoid)>;
using feval = function<ll(int, int)>;
int n;
vector<Monoid> seg;
vi lazy; // 全体に足された数を持つ 3333なら3
const fini toMonoid; // Monoidを初期化する
const func f; // Monoid同士の演算結果を返す
const feval getAdd; // len,xを受け取り、segに加える数を返す
const Monoid e;
vi arch; // 遅延評価を行うやつ
vi lens;
SegmentLazyAdd(vi a, fini toMonoid, func f, feval fe, Monoid e)
: f(f), toMonoid(toMonoid), getAdd(fe), e(e) {
n = 1;
int asz = a.size();
while (n < asz)
n *= 2;
seg.resize(2 * n - 1, e);
lazy.resize(2 * n - 1, 0);
lens.resize(2 * n - 1);
rep(i, asz) seg[i + n - 1] = toMonoid(i, a[i]);
rer(i, n - 2) seg[i] = f(seg[i * 2 + 1], seg[i * 2 + 2]);
rep(i, 2 * n - 1) lens[i] = n / hbit(i + 1);
}
void eval() {
rer(j, sz(arch) - 1) {
int i = arch[j];
// iは1index
// lazyは0index
int v = lazy[i - 1];
if (!v)
continue;
lazy[i * 2 - 1] += v;
lazy[i * 2] += v;
seg[i * 2 - 1].v += getAdd(lens[i * 2 - 1], v); //
seg[i * 2].v += getAdd(lens[i * 2], v);
lazy[i - 1] = 0;
}
}
void gindex(int l, int r) {
arch.clear();
l += n;
r += n;
int lm = (l / (l & -l)) >> 1;
int rm = (r / (r & -r)) >> 1;
while (l < r) {
if (r <= rm)
arch.pb(r);
if (l <= lm)
arch.pb(l);
l >>= 1;
r >>= 1;
}
while (l) {
arch.pb(l);
l >>= 1;
}
}
void add(int l, int r, int x) {
int L = l + n;
int R = r + n;
while (L < R) {
if (R & 1) {
R--;
lazy[R - 1] += x;
seg[R - 1].v += getAdd(lens[R - 1], x);
}
if (L & 1) {
lazy[L - 1] += x;
seg[L - 1].v += getAdd(lens[L - 1], x);
L++;
}
L >>= 1;
R >>= 1;
}
gindex(l, r);
fora(i, arch) {
seg[i - 1] = f(seg[i * 2 - 1], seg[i * 2]);
seg[i - 1].v += getAdd(lens[i - 1], lazy[i - 1]);
}
}
Monoid get(int l, int r) {
gindex(l, r);
eval();
int L = l + n;
int R = r + n;
Monoid retl = e, retr = e;
while (L < R) {
if (R & 1) {
R--;
retr = f(seg[R - 1], retr);
}
if (L & 1) {
retl = f(retl, seg[L - 1]);
L++;
}
L >>= 1;
R >>= 1;
}
return f(retl, retr);
}
int geti(int l, int r) { return get(l, r).i; }
int getv(int l, int r) { return get(l, r).v; }
#ifdef _DEBUG
void debu(int len = 10) {
rep(i, min(n, min(len, n))) {
int v = getv(i, i + 1);
if (v == MIN(ll) || v == MAX(ll)) {
cerr << "e ";
} else {
cerr << v << " ";
}
}
cerr << "" << endl;
}
#else
inline void debu() { ; }
#endif
};
#define seg SegmentLazyAdd
#define raq SegmentLazyAdd
signed main() {
int n, q;
cin >> n >> q;
seg ch(vi(n), segSum);
for (int i = 0; i < q; i++) {
int c, s, t, x;
cin >> c;
if (c) {
cin >> s >> t;
cout << ch.getv(s - 1, t) << endl;
rep(i, 20) { ch.getv(s - 1, t); }
} else {
cin >> s >> t >> x;
ch.add(s - 1, t, x);
rep(i, 10) {
ch.add(s - 1, t, x);
ch.add(s - 1, t, -x);
}
}
}
return 0;
}
|
// #pragma GCC optimize ("-O3")
#include <bits/stdc++.h>
using namespace std;
//@起動時
struct initon {
initon() {
cin.tie(0);
ios::sync_with_stdio(false);
cout.setf(ios::fixed);
cout.precision(16);
srand((unsigned)clock() + (unsigned)time(NULL));
};
} __initon;
// 衝突対策
#define ws ___ws
struct T {
int f, s, t;
T() { f = -1, s = -1, t = -1; }
T(int f, int s, int t) : f(f), s(s), t(t) {}
bool operator<(const T &r) const {
return f != r.f ? f < r.f : s != r.s ? s < r.s : t < r.t;
// return f != r.f ? f > r.f : s != r.s ? s > r.s : t > r.t; 大きい順
}
bool operator>(const T &r) const {
return f != r.f ? f > r.f : s != r.s ? s > r.s : t > r.t;
// return f != r.f ? f > r.f : s != r.s ? s > r.s : t > r.t; 小さい順
}
bool operator==(const T &r) const { return f == r.f && s == r.s && t == r.t; }
bool operator!=(const T &r) const { return f != r.f || s != r.s || t != r.t; }
int operator[](int i) {
assert(i < 3);
return i == 0 ? f : i == 1 ? s : t;
}
};
#define int long long
#define ll long long
#define double long double
#define ull unsigned long long
using dou = double;
using itn = int;
using str = string;
using bo = bool;
#define au auto
using P = pair<ll, ll>;
#define fi first
#define se second
#define vec vector
#define beg begin
#define rbeg rbegin
#define con continue
#define bre break
#define brk break
#define is ==
// マクロ省略系 コンテナ
using vi = vector<int>;
#define _overloadvvi(_1, _2, _3, _4, name, ...) name
#define vvi0() vec<vi>
#define vvi1(a) vec<vi> a
#define vvi2(a, b) vec<vi> a(b)
#define vvi3(a, b, c) vec<vi> a(b, vi(c))
#define vvi4(a, b, c, d) vec<vi> a(b, vi(c, d))
#define vvi(...) \
_overloadvvi(__VA_ARGS__, vvi4, vvi3, vvi2, vvi1, vvi0)(__VA_ARGS__)
using vl = vector<ll>;
#define _overloadvvl(_1, _2, _3, _4, name, ...) name
#define vvl1(a) vec<vl> a
#define vvl2(a, b) vec<vl> a(b)
#define vvl3(a, b, c) vec<vl> a(b, vl(c))
#define vvl4(a, b, c, d) vec<vl> a(b, vl(c, d))
#define vvl(...) _overloadvvl(__VA_ARGS__, vvl4, vvl3, vvl2, vvl1)(__VA_ARGS__)
using vb = vector<bool>;
#define _overloadvvb(_1, _2, _3, _4, name, ...) name
#define vvb1(a) vec<vb> a
#define vvb2(a, b) vec<vb> a(b)
#define vvb3(a, b, c) vec<vb> a(b, vb(c))
#define vvb4(a, b, c, d) vec<vb> a(b, vb(c, d))
#define vvb(...) _overloadvvb(__VA_ARGS__, vvb4, vvb3, vvb2, vvb1)(__VA_ARGS__)
using vs = vector<string>;
#define _overloadvvs(_1, _2, _3, _4, name, ...) name
#define vvs1(a) vec<vs> a
#define vvs2(a, b) vec<vs> a(b)
#define vvs3(a, b, c) vec<vs> a(b, vs(c))
#define vvs4(a, b, c, d) vec<vs> a(b, vs(c, d))
#define vvs(...) _overloadvvs(__VA_ARGS__, vvs4, vvs3, vvs2, vvs1)(__VA_ARGS__)
using vd = vector<double>;
#define _overloadvvd(_1, _2, _3, _4, name, ...) name
#define vvd1(a) vec<vd> a
#define vvd2(a, b) vec<vd> a(b)
#define vvd3(a, b, c) vec<vd> a(b, vd(c))
#define vvd4(a, b, c, d) vec<vd> a(b, vd(c, d))
#define vvd(...) _overloadvvd(__VA_ARGS__, vvd4, vvd3, vvd2, vvd1)(__VA_ARGS__)
using vc = vector<char>;
#define _overloadvvc(_1, _2, _3, _4, name, ...) name
#define vvc1(a) vec<vc> a
#define vvc2(a, b) vec<vc> a(b)
#define vvc3(a, b, c) vec<vc> a(b, vc(c))
#define vvc4(a, b, c, d) vec<vc> a(b, vc(c, d))
#define vvc(...) _overloadvvc(__VA_ARGS__, vvc4, vvc3, vvc2, vvc1)(__VA_ARGS__)
using vp = vector<P>;
#define _overloadvvp(_1, _2, _3, _4, name, ...) name
#define vvp1(a) vec<vp> a
#define vvp2(a, b) vec<vp> a(b)
#define vvp3(a, b, c) vec<vp> a(b, vp(c))
#define vvp4(a, b, c, d) vec<vp> a(b, vp(c, d))
using vt = vector<T>;
#define _overloadvvt(_1, _2, _3, _4, name, ...) name
#define vvt1(a) vec<vt> a
#define vvt2(a, b) vec<vt> a(b)
#define vvt3(a, b, c) vec<vt> a(b, vt(c))
#define vvt4(a, b, c, d) vec<vt> a(b, vt(c, d))
#define v3i(a, b, c, d) vector<vector<vi>> a(b, vector<vi>(c, vi(d)))
#define v3d(a, b, c, d) vector<vector<vd>> a(b, vector<vd>(c, vd(d)))
#define v3m(a, b, c, d) vector<vector<vm>> a(b, vector<vm>(c, vm(d)))
#define _vvi vector<vi>
#define _vvl vector<vl>
#define _vvb vector<vb>
#define _vvs vector<vs>
#define _vvd vector<vd>
#define _vvc vector<vc>
#define _vvp vector<vp>
#define PQ priority_queue<ll, vector<ll>, greater<ll>>
#define tos to_string
using mapi = map<int, int>;
using mapd = map<dou, int>;
using mapc = map<char, int>;
using maps = map<str, int>;
using seti = set<int>;
using setd = set<dou>;
using setc = set<char>;
using sets = set<str>;
using qui = queue<int>;
#define bset bitset
#define uset unordered_set
#define mset multiset
#define umap unordered_map
#define umapi unordered_map<int, int>
#define umapp unordered_map<P, int>
#define mmap multimap
// マクロ 繰り返し
#define _overloadrep(_1, _2, _3, _4, name, ...) name
#define _rep(i, n) for (int i = 0, _lim = n; i < _lim; i++)
#define repi(i, m, n) for (int i = m, _lim = n; i < _lim; i++)
#define repadd(i, m, n, ad) for (int i = m, _lim = n; i < _lim; i += ad)
#define rep(...) _overloadrep(__VA_ARGS__, repadd, repi, _rep, )(__VA_ARGS__)
#define _rer(i, n) for (int i = n; i >= 0; i--)
#define reri(i, m, n) for (int i = m, _lim = n; i >= _lim; i--)
#define rerdec(i, m, n, dec) for (int i = m, _lim = n; i >= _lim; i -= dec)
#define rer(...) _overloadrep(__VA_ARGS__, rerdec, reri, _rer, )(__VA_ARGS__)
#define fora(a, b) for (auto &&a : b)
#define forg(gi, ve) \
for (int gi = 0, f, t, c; \
gi < ve.size() && (f = ve[gi].f, t = ve[gi].t, c = ve[gi].c, true); \
gi++)
#define fort(gi, ve) \
for (int gi = 0, f, t, c; \
gi < ve.size() && (f = ve[gi].f, t = ve[gi].t, c = ve[gi].c, true); \
gi++) \
if (t != p)
// #define fort(gi, ve) for (int gi = 0, f, t, c;gi<ve.size()&& (gi+=
// (ve[gi].t==p))< ve.size() && (f = ve[gi].f,t=ve[gi].t, c = ve[gi].c,true);
// gi++)
// マクロ 定数
#define k3 1010
#define k4 10101
#define k5 101010
#define k6 1010101
#define k7 10101010
const int inf = (int)1e9 + 100;
const ll linf = (ll)1e18 + 100;
const double eps = 1e-9;
const double PI = 3.1415926535897932384626433832795029L;
ll ma = numeric_limits<ll>::min();
ll mi = numeric_limits<ll>::max();
const int y4[] = {-1, 1, 0, 0};
const int x4[] = {0, 0, -1, 1};
const int y8[] = {0, 1, 0, -1, -1, 1, 1, -1};
const int x8[] = {1, 0, -1, 0, 1, -1, 1, -1};
// マクロ省略形 関数等
#define arsz(a) (sizeof(a) / sizeof(a[0]))
#define sz(a) ((int)(a).size())
#define rs resize
#define mp make_pair
#define pb push_back
#define pf push_front
#define eb emplace_back
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
inline void sort(string &a) { sort(a.begin(), a.end()); }
template <class T> inline void sort(vector<T> &a) { sort(a.begin(), a.end()); };
template <class T> inline void sort(vector<T> &a, int len) {
sort(a.begin(), a.begin() + len);
};
template <class T, class F> inline void sort(vector<T> &a, F f) {
sort(a.begin(), a.end(), [&](T l, T r) { return f(l) < f(r); });
};
enum ___pcomparator { fisi, fisd, fdsi, fdsd, sifi, sifd, sdfi, sdfd };
inline void sort(vector<P> &a, ___pcomparator type) {
switch (type) {
case fisi:
sort(all(a),
[&](P l, P r) { return l.fi != r.fi ? l.fi < r.fi : l.se < r.se; });
break;
case fisd:
sort(all(a),
[&](P l, P r) { return l.fi != r.fi ? l.fi < r.fi : l.se > r.se; });
break;
case fdsi:
sort(all(a),
[&](P l, P r) { return l.fi != r.fi ? l.fi > r.fi : l.se < r.se; });
break;
case fdsd:
sort(all(a),
[&](P l, P r) { return l.fi != r.fi ? l.fi > r.fi : l.se > r.se; });
break;
case sifi:
sort(all(a),
[&](P l, P r) { return l.se != r.se ? l.se < r.se : l.fi < r.fi; });
break;
case sifd:
sort(all(a),
[&](P l, P r) { return l.se != r.se ? l.se < r.se : l.fi > r.fi; });
break;
case sdfi:
sort(all(a),
[&](P l, P r) { return l.se != r.se ? l.se > r.se : l.fi < r.fi; });
break;
case sdfd:
sort(all(a),
[&](P l, P r) { return l.se != r.se ? l.se > r.se : l.fi > r.fi; });
break;
}
};
inline void sort(vector<T> &a, ___pcomparator type) {
switch (type) {
case fisi:
sort(all(a), [&](T l, T r) { return l.f != r.f ? l.f < r.f : l.s < r.s; });
break;
case fisd:
sort(all(a), [&](T l, T r) { return l.f != r.f ? l.f < r.f : l.s > r.s; });
break;
case fdsi:
sort(all(a), [&](T l, T r) { return l.f != r.f ? l.f > r.f : l.s < r.s; });
break;
case fdsd:
sort(all(a), [&](T l, T r) { return l.f != r.f ? l.f > r.f : l.s > r.s; });
break;
case sifi:
sort(all(a), [&](T l, T r) { return l.s != r.s ? l.s < r.s : l.f < r.f; });
break;
case sifd:
sort(all(a), [&](T l, T r) { return l.s != r.s ? l.s < r.s : l.f > r.f; });
break;
case sdfi:
sort(all(a), [&](T l, T r) { return l.s != r.s ? l.s > r.s : l.f < r.f; });
break;
case sdfd:
sort(all(a), [&](T l, T r) { return l.s != r.s ? l.s > r.s : l.f > r.f; });
break;
}
};
template <class T> inline void rsort(vector<T> &a) {
sort(a.begin(), a.end(), greater<T>());
};
template <class T> inline void rsort(vector<T> &a, int len) {
sort(a.begin(), a.begin() + len, greater<T>());
};
template <class U, class F> inline void rsort(vector<U> &a, F f) {
sort(a.begin(), a.end(), [&](U l, U r) { return f(l) > f(r); });
};
template <class U> inline void sortp(vector<U> &a, vector<U> &b) {
vp c;
int n = sz(a);
assert(n == sz(b));
rep(i, n) c.eb(a[i], b[i]);
sort(c);
rep(i, n) {
a[i] = c[i].first;
b[i] = c[i].second;
;
}
};
// F = T<T>
// 例えばreturn p.fi + p.se;
template <class U, class F> inline void sortp(vector<U> &a, vector<U> &b, F f) {
vp c;
int n = sz(a);
assert(n == sz(b));
rep(i, n) c.eb(a[i], b[i]);
sort(c, f);
rep(i, n) {
a[i] = c[i].first;
b[i] = c[i].second;
}
};
template <class U, class F>
inline void sortp(vector<U> &a, vector<U> &b, char type) {
vp c;
int n = sz(a);
assert(n == sz(b));
rep(i, n) c.eb(a[i], b[i]);
sort(c, type);
rep(i, n) {
a[i] = c[i].first;
b[i] = c[i].second;
}
};
template <class U> inline void rsortp(vector<U> &a, vector<U> &b) {
vp c;
int n = sz(a);
assert(n == sz(b));
rep(i, n) c.eb(a[i], b[i]);
rsort(c);
rep(i, n) {
a[i] = c[i].first;
b[i] = c[i].second;
}
};
template <class U, class F>
inline void rsortp(vector<U> &a, vector<U> &b, F f) {
vp c;
int n = sz(a);
assert(n == sz(b));
rep(i, n) c.eb(a[i], b[i]);
rsort(c, f);
rep(i, n) {
a[i] = c[i].first;
b[i] = c[i].second;
}
};
template <class U> inline void sortt(vector<U> &a, vector<U> &b, vector<U> &c) {
vt r;
int n = sz(a);
assert(n == sz(b));
assert(n == sz(c));
rep(i, n) r.eb(a[i], b[i], c[i]);
sort(r);
rep(i, n) {
a[i] = r[i].f;
b[i] = r[i].s;
c[i] = r[i].t;
}
};
template <class U, class F>
inline void sortt(vector<U> &a, vector<U> &b, vector<U> &c, F f) {
vt r;
int n = sz(a);
assert(n == sz(b));
assert(n == sz(c));
rep(i, n) r.eb(a[i], b[i], c[i]);
sort(r, f);
rep(i, n) {
a[i] = r[i].f;
b[i] = r[i].s;
c[i] = r[i].t;
}
};
template <class U, class F>
inline void rsortt(vector<U> &a, vector<U> &b, vector<U> &c, F f) {
vt r;
int n = sz(a);
assert(n == sz(b));
assert(n == sz(c));
rep(i, n) r.eb(a[i], b[i], c[i]);
rsort(r, f);
rep(i, n) {
a[i] = r[i].f;
b[i] = r[i].s;
c[i] = r[i].t;
}
};
template <class T> inline void sort2(vector<vector<T>> &a) {
for (int i = 0, n = a.size(); i < n; i++)
sort(a[i]);
}
template <class T> inline void rsort2(vector<vector<T>> &a) {
for (int i = 0, n = a.size(); i < n; i++)
rsort(a[i]);
}
template <typename A, size_t N, typename T> void fill(A (&a)[N], const T &v) {
rep(i, N) a[i] = v;
}
template <typename A, size_t N, size_t O, typename T>
void fill(A (&a)[N][O], const T &v) {
rep(i, N) rep(j, O) a[i][j] = v;
}
template <typename A, size_t N, size_t O, size_t P, typename T>
void fill(A (&a)[N][O][P], const T &v) {
rep(i, N) rep(j, O) rep(k, P) a[i][j][k] = v;
}
template <typename A, size_t N, size_t O, size_t P, size_t Q, typename T>
void fill(A (&a)[N][O][P][Q], const T &v) {
rep(i, N) rep(j, O) rep(k, P) rep(l, Q) a[i][j][k][l] = v;
}
template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R,
typename T>
void fill(A (&a)[N][O][P][Q][R], const T &v) {
rep(i, N) rep(j, O) rep(k, P) rep(l, Q) rep(m, R) a[i][j][k][l][m] = v;
}
template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R,
size_t S, typename T>
void fill(A (&a)[N][O][P][Q][R][S], const T &v) {
rep(i, N) rep(j, O) rep(k, P) rep(l, Q) rep(m, R) rep(n, S)
a[i][j][k][l][m][n] = v;
}
template <typename V, typename T> void fill(V &xx, const T vall) { xx = vall; }
template <typename V, typename T> void fill(vector<V> &vecc, const T vall) {
for (auto &&vx : vecc)
fill(vx, vall);
}
//@汎用便利関数 入力
template <typename T = int> T _in() {
T x;
cin >> x;
return (x);
}
#define _overloadin(_1, _2, _3, _4, name, ...) name
#define in0() _in()
#define in1(a) cin >> a
#define in2(a, b) cin >> a >> b
#define in3(a, b, c) cin >> a >> b >> c
#define in4(a, b, c, d) cin >> a >> b >> c >> d
#define in(...) _overloadin(__VA_ARGS__, in4, in3, in2, in1, in0)(__VA_ARGS__)
#define _overloaddin(_1, _2, _3, _4, name, ...) name
#define din1(a) \
int a; \
cin >> a
#define din2(a, b) \
int a, b; \
cin >> a >> b
#define din3(a, b, c) \
int a, b, c; \
cin >> a >> b >> c
#define din4(a, b, c, d) \
int a, b, c, d; \
cin >> a >> b >> c >> d
#define din(...) _overloadin(__VA_ARGS__, din4, din3, din2, din1)(__VA_ARGS__)
#define _overloaddind(_1, _2, _3, _4, name, ...) name
#define din1d(a) \
int a; \
cin >> a; \
a--
#define din2d(a, b) \
int a, b; \
cin >> a >> b; \
a--, b--
#define din3d(a, b, c) \
int a, b, c; \
cin >> a >> b >> c; \
a--, b--, c--
#define din4d(a, b, c, d) \
int a, b, c, d; \
cin >> a >> b >> c >> d; \
; \
a--, b--, c--, d--
#define dind(...) \
_overloaddind(__VA_ARGS__, din4d, din3d, din2d, din1d)(__VA_ARGS__)
string sin() { return _in<string>(); }
ll lin() { return _in<ll>(); }
#define na(a, n) \
a.resize(n); \
rep(i, n) cin >> a[i];
#define nao(a, n) \
a.resize(n + 1); \
rep(i, n) cin >> a[i + 1];
#define nad(a, n) \
a.resize(n); \
rep(i, n) { \
cin >> a[i]; \
a[i]--; \
}
#define na2(a, b, n) \
a.resize(n), b.resize(n); \
rep(i, n) cin >> a[i] >> b[i];
#define na2d(a, b, n) \
a.resize(n), b.resize(n); \
rep(i, n) { \
cin >> a[i] >> b[i]; \
a[i]--, b[i]--; \
}
#define na3(a, b, c, n) \
a.resize(n), b.resize(n), c.resize(n); \
rep(i, n) cin >> a[i] >> b[i] >> c[i];
#define na3d(a, b, c, n) \
a.resize(n), b.resize(n), c.resize(n); \
rep(i, n) { \
cin >> a[i] >> b[i] >> c[i]; \
a[i]--, b[i]--, c[i]--; \
}
#define nt(a, h, w) \
resize(a, h, w); \
rep(hi, h) rep(wi, w) cin >> a[hi][wi];
#define ntd(a, h, w) \
rs(a, h, w); \
rep(hi, h) rep(wi, w) cin >> a[hi][wi], a[hi][wi]--;
#define ntp(a, h, w) \
fill(a, '#'); \
rep(hi, 1, h + 1) rep(wi, 1, w + 1) cin >> a[hi][wi];
// デバッグ
#define sp << " " <<
#define debugName(VariableName) #VariableName
#define _deb1(x) cerr << debugName(x) << " = " << x << endl
#define _deb2(x, y) \
cerr << debugName(x) << " = " << x << ", " << debugName(y) << " = " << y \
<< endl
#define _deb3(x, y, z) \
cerr << debugName(x) << " = " << x << ", " << debugName(y) << " = " << y \
<< ", " debugName(z) << " = " << z << endl
#define _deb4(x, y, z, a) \
cerr << debugName(x) << " = " << x << ", " << debugName(y) << " = " << y \
<< ", " << debugName(z) << " = " << z << ", " << debugName(a) << " = " \
<< a << endl
#define _deb5(x, y, z, a, b) \
cerr << debugName(x) << " = " << x << ", " << debugName(y) << " = " << y \
<< ", " << debugName(z) << " = " << z << ", " << debugName(a) << " = " \
<< a << ", " << debugName(b) << " = " << b << endl
#define _overloadebug(_1, _2, _3, _4, _5, name, ...) name
#define debug(...) \
_overloadebug(__VA_ARGS__, _deb5, _deb4, _deb3, _deb2, _deb1)(__VA_ARGS__)
#define deb(...) \
_overloadebug(__VA_ARGS__, _deb5, _deb4, _deb3, _deb2, _deb1)(__VA_ARGS__)
#define debugline(x) \
cerr << x << " " \
<< "(L:" << __LINE__ << ")" << '\n'
void ole() {
#ifdef _DEBUG
debugline("ole");
exit(0);
#endif
string a = "a";
rep(i, 30) a += a;
rep(i, 1 << 17) cout << a << endl;
cout << "OLE 出力長制限超過" << endl;
exit(0);
}
void tle() {
while (inf)
cout << inf << endl;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll gcd(vi b) {
ll res = b[0];
for (auto &&v : b)
res = gcd(v, res);
return res;
}
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
ll rev(ll a) {
ll res = 0;
while (a) {
res *= 10;
res += a % 10;
a /= 10;
}
return res;
}
template <class T> void rev(vector<T> &a) { reverse(all(a)); }
void rev(string &a) { reverse(all(a)); }
ll ceil(ll a, ll b) {
if (b == 0) {
debugline("ceil");
deb(a, b);
ole();
return -1;
} else
return (a + b - 1) / b;
}
ll sqrt(ll a) {
if (a < 0) {
debugline("sqrt");
deb(a);
ole();
}
ll res = (ll)std::sqrt(a);
while (res * res < a)
res++;
return res;
}
double log(double e, double x) { return log(x) / log(e); }
ll sig(ll t) { return (1 + t) * t / 2; }
ll sig(ll s, ll t) { return (s + t) * (t - s + 1) / 2; }
vi divisors(int v) {
vi res;
double lim = std::sqrt(v);
for (int i = 1; i <= lim; ++i) {
if (v % i == 0) {
res.pb(i);
if (i != v / i)
res.pb(v / i);
}
}
return res;
}
vb isPrime;
vi primes;
void setPrime() {
int len = 4010101;
isPrime.resize(4010101);
fill(isPrime, true);
isPrime[0] = isPrime[1] = false;
for (int i = 2; i <= sqrt(len) + 5; ++i) {
if (!isPrime[i])
continue;
for (int j = 2; i * j < len; ++j) {
isPrime[i * j] = false;
}
}
rep(i, len) if (isPrime[i]) primes.pb(i);
}
vi factorization(int v) {
int tv = v;
vi res;
if (isPrime.size() == 0)
setPrime();
for (auto &&p : primes) {
if (v % p == 0)
res.push_back(p);
while (v % p == 0) {
v /= p;
}
if (v == 1 || p * p > tv)
break;
}
if (v > 1)
res.pb(v);
return res;
}
inline bool inside(int h, int w, int H, int W) {
return h >= 0 && w >= 0 && h < H && w < W;
}
inline bool inside(int v, int l, int r) { return l <= v && v < r; }
#define ins inside
ll u(ll a) { return a < 0 ? 0 : a; }
template <class T> vector<T> u(const vector<T> &a) {
vector<T> ret = a;
fora(v, ret) v = u(v);
return ret;
}
#define MIN(a) numeric_limits<a>::min()
#define MAX(a) numeric_limits<a>::max()
int n, m, k, d, H, W, x, y, z, q;
int cou;
vi a, b, c;
vvi(s, 0, 0);
vvc(ba, 0, 0);
vp p;
int hbit(int n) {
n |= (n >> 1);
n |= (n >> 2);
n |= (n >> 4);
n |= (n >> 8);
n |= (n >> 16);
n |= (n >> 32);
return n - (n >> 1);
}
struct Monoid {
ll i, v;
Monoid(ll i, ll v) : i(i), v(v) {}
};
#define segMinl \
[](int i, int v) { return Monoid(i, v); }, \
[](Monoid a, Monoid b) { return a.v <= b.v ? a : b; }, \
[](int len, int x) { return x; }, Monoid(-1, MAX(ll))
#define segMinr \
[](int i, int v) { return Monoid(i, v); }, \
[](Monoid a, Monoid b) { return a.v < b.v ? a : b; }, \
[](int len, int x) { return x; }, Monoid(-1, MAX(ll))
#define segMaxl \
[](int i, int v) { return Monoid(i, v); }, \
[](Monoid a, Monoid b) { return a.v >= b.v ? a : b; }, \
[](int len, int x) { return x; }, Monoid(-1, MIN(ll))
#define segMaxr \
[](int i, int v) { return Monoid(i, v); }, \
[](Monoid a, Monoid b) { return a.v > b.v ? a : b; }, \
[](int len, int x) { return x; }, Monoid(-1, MIN(ll))
// 個数 最小値候補
#define segCouMin \
[](int cou, int v) { return Monoid(1, v); }, \
[](Monoid a, Monoid b) { \
int coum = 0; \
if (a.v <= b.v) \
coum += a.i; \
if (a.v >= b.v) \
coum += b.i; \
return Monoid(coum, min(a.v, b.v)); \
}, \
[](int len, int x) { return x; }, Monoid(0, MAX(ll))
#define segCouMax \
[](int cou, int v) { return Monoid(1, v); }, \
[](Monoid a, Monoid b) { \
int coum = 0; \
if (a.v >= b.v) \
coum += a.i; \
if (a.v <= b.v) \
coum += b.i; \
return Monoid(coum, max(a.v, b.v)); \
}, \
[](int len, int x) { return x; }, Monoid(0, MIN(ll))
#define segSum \
[](int i, int v) { return Monoid(i, v); }, \
[](Monoid a, Monoid b) { return Monoid(0, a.v + b.v); }, \
[](int len, int x) { return len * x; }, Monoid(0, 0)
#define segXor \
[](int i, int v) { return Monoid(i, v); }, \
[](Monoid a, Monoid b) { return Monoid(0, a.v ^ b.v); }, \
[](int len, int x) { return (len & 1) ? x : 0; }, Monoid(0, 0)
// 一つずつ初期化するのは大変
// vi を渡してセットするのがベスト
// updateは諦める
struct SegmentLazyAdd {
using fini = function<Monoid(int, int)>;
using func = function<Monoid(Monoid, Monoid)>;
using feval = function<ll(int, int)>;
int n;
vector<Monoid> seg;
vi lazy; // 全体に足された数を持つ 3333なら3
const fini toMonoid; // Monoidを初期化する
const func f; // Monoid同士の演算結果を返す
const feval getAdd; // len,xを受け取り、segに加える数を返す
const Monoid e;
vi arch; // 遅延評価を行うやつ
vi lens;
SegmentLazyAdd(vi a, fini toMonoid, func f, feval fe, Monoid e)
: f(f), toMonoid(toMonoid), getAdd(fe), e(e) {
n = 1;
int asz = a.size();
while (n < asz)
n *= 2;
seg.resize(2 * n - 1, e);
lazy.resize(2 * n - 1, 0);
lens.resize(2 * n - 1);
rep(i, asz) seg[i + n - 1] = toMonoid(i, a[i]);
rer(i, n - 2) seg[i] = f(seg[i * 2 + 1], seg[i * 2 + 2]);
rep(i, 2 * n - 1) lens[i] = n / hbit(i + 1);
}
void eval() {
rer(j, sz(arch) - 1) {
int i = arch[j];
// iは1index
// lazyは0index
int v = lazy[i - 1];
if (!v)
continue;
lazy[i * 2 - 1] += v;
lazy[i * 2] += v;
seg[i * 2 - 1].v += getAdd(lens[i * 2 - 1], v); //
seg[i * 2].v += getAdd(lens[i * 2], v);
lazy[i - 1] = 0;
}
}
void gindex(int l, int r) {
arch.clear();
l += n;
r += n;
int lm = (l / (l & -l)) >> 1;
int rm = (r / (r & -r)) >> 1;
while (l < r) {
if (r <= rm)
arch.pb(r);
if (l <= lm)
arch.pb(l);
l >>= 1;
r >>= 1;
}
while (l) {
arch.pb(l);
l >>= 1;
}
}
void add(int l, int r, int x) {
int L = l + n;
int R = r + n;
while (L < R) {
if (R & 1) {
R--;
lazy[R - 1] += x;
seg[R - 1].v += getAdd(lens[R - 1], x);
}
if (L & 1) {
lazy[L - 1] += x;
seg[L - 1].v += getAdd(lens[L - 1], x);
L++;
}
L >>= 1;
R >>= 1;
}
gindex(l, r);
fora(i, arch) {
seg[i - 1] = f(seg[i * 2 - 1], seg[i * 2]);
seg[i - 1].v += getAdd(lens[i - 1], lazy[i - 1]);
}
}
Monoid get(int l, int r) {
gindex(l, r);
eval();
int L = l + n;
int R = r + n;
Monoid retl = e, retr = e;
while (L < R) {
if (R & 1) {
R--;
retr = f(seg[R - 1], retr);
}
if (L & 1) {
retl = f(retl, seg[L - 1]);
L++;
}
L >>= 1;
R >>= 1;
}
return f(retl, retr);
}
int geti(int l, int r) { return get(l, r).i; }
int getv(int l, int r) { return get(l, r).v; }
#ifdef _DEBUG
void debu(int len = 10) {
rep(i, min(n, min(len, n))) {
int v = getv(i, i + 1);
if (v == MIN(ll) || v == MAX(ll)) {
cerr << "e ";
} else {
cerr << v << " ";
}
}
cerr << "" << endl;
}
#else
inline void debu() { ; }
#endif
};
#define seg SegmentLazyAdd
#define raq SegmentLazyAdd
signed main() {
int n, q;
cin >> n >> q;
seg ch(vi(k5), segSum);
for (int i = 0; i < q; i++) {
int c, s, t, x;
cin >> c;
if (c) {
cin >> s >> t;
cout << ch.getv(s - 1, t) << endl;
rep(i, 20) { ch.getv(s - 1, t); }
} else {
cin >> s >> t >> x;
ch.add(s - 1, t, x);
rep(i, 10) {
ch.add(s - 1, t, x);
ch.add(s - 1, t, -x);
}
}
}
return 0;
}
|
replace
| 870 | 871 | 870 | 871 |
0
| |
p02351
|
C++
|
Runtime Error
|
// #pragma GCC optimize ("-O3")
#include <bits/stdc++.h>
using namespace std;
//@起動時
struct initon {
initon() {
cin.tie(0);
ios::sync_with_stdio(false);
cout.setf(ios::fixed);
cout.precision(16);
srand((unsigned)clock() + (unsigned)time(NULL));
};
} __initon;
// 衝突対策
#define ws ___ws
struct T {
int f, s, t;
T() { f = -1, s = -1, t = -1; }
T(int f, int s, int t) : f(f), s(s), t(t) {}
bool operator<(const T &r) const {
return f != r.f ? f < r.f : s != r.s ? s < r.s : t < r.t;
// return f != r.f ? f > r.f : s != r.s ? s > r.s : t > r.t; 大きい順
}
bool operator>(const T &r) const {
return f != r.f ? f > r.f : s != r.s ? s > r.s : t > r.t;
// return f != r.f ? f > r.f : s != r.s ? s > r.s : t > r.t; 小さい順
}
bool operator==(const T &r) const { return f == r.f && s == r.s && t == r.t; }
bool operator!=(const T &r) const { return f != r.f || s != r.s || t != r.t; }
int operator[](int i) {
assert(i < 3);
return i == 0 ? f : i == 1 ? s : t;
}
};
#define int long long
#define ll long long
#define double long double
#define ull unsigned long long
using dou = double;
using itn = int;
using str = string;
using bo = bool;
#define au auto
using P = pair<ll, ll>;
#define fi first
#define se second
#define vec vector
#define beg begin
#define rbeg rbegin
#define con continue
#define bre break
#define brk break
#define is ==
// マクロ省略系 コンテナ
using vi = vector<int>;
#define _overloadvvi(_1, _2, _3, _4, name, ...) name
#define vvi0() vec<vi>
#define vvi1(a) vec<vi> a
#define vvi2(a, b) vec<vi> a(b)
#define vvi3(a, b, c) vec<vi> a(b, vi(c))
#define vvi4(a, b, c, d) vec<vi> a(b, vi(c, d))
#define vvi(...) \
_overloadvvi(__VA_ARGS__, vvi4, vvi3, vvi2, vvi1, vvi0)(__VA_ARGS__)
using vl = vector<ll>;
#define _overloadvvl(_1, _2, _3, _4, name, ...) name
#define vvl1(a) vec<vl> a
#define vvl2(a, b) vec<vl> a(b)
#define vvl3(a, b, c) vec<vl> a(b, vl(c))
#define vvl4(a, b, c, d) vec<vl> a(b, vl(c, d))
#define vvl(...) _overloadvvl(__VA_ARGS__, vvl4, vvl3, vvl2, vvl1)(__VA_ARGS__)
using vb = vector<bool>;
#define _overloadvvb(_1, _2, _3, _4, name, ...) name
#define vvb1(a) vec<vb> a
#define vvb2(a, b) vec<vb> a(b)
#define vvb3(a, b, c) vec<vb> a(b, vb(c))
#define vvb4(a, b, c, d) vec<vb> a(b, vb(c, d))
#define vvb(...) _overloadvvb(__VA_ARGS__, vvb4, vvb3, vvb2, vvb1)(__VA_ARGS__)
using vs = vector<string>;
#define _overloadvvs(_1, _2, _3, _4, name, ...) name
#define vvs1(a) vec<vs> a
#define vvs2(a, b) vec<vs> a(b)
#define vvs3(a, b, c) vec<vs> a(b, vs(c))
#define vvs4(a, b, c, d) vec<vs> a(b, vs(c, d))
#define vvs(...) _overloadvvs(__VA_ARGS__, vvs4, vvs3, vvs2, vvs1)(__VA_ARGS__)
using vd = vector<double>;
#define _overloadvvd(_1, _2, _3, _4, name, ...) name
#define vvd1(a) vec<vd> a
#define vvd2(a, b) vec<vd> a(b)
#define vvd3(a, b, c) vec<vd> a(b, vd(c))
#define vvd4(a, b, c, d) vec<vd> a(b, vd(c, d))
#define vvd(...) _overloadvvd(__VA_ARGS__, vvd4, vvd3, vvd2, vvd1)(__VA_ARGS__)
using vc = vector<char>;
#define _overloadvvc(_1, _2, _3, _4, name, ...) name
#define vvc1(a) vec<vc> a
#define vvc2(a, b) vec<vc> a(b)
#define vvc3(a, b, c) vec<vc> a(b, vc(c))
#define vvc4(a, b, c, d) vec<vc> a(b, vc(c, d))
#define vvc(...) _overloadvvc(__VA_ARGS__, vvc4, vvc3, vvc2, vvc1)(__VA_ARGS__)
using vp = vector<P>;
#define _overloadvvp(_1, _2, _3, _4, name, ...) name
#define vvp1(a) vec<vp> a
#define vvp2(a, b) vec<vp> a(b)
#define vvp3(a, b, c) vec<vp> a(b, vp(c))
#define vvp4(a, b, c, d) vec<vp> a(b, vp(c, d))
using vt = vector<T>;
#define _overloadvvt(_1, _2, _3, _4, name, ...) name
#define vvt1(a) vec<vt> a
#define vvt2(a, b) vec<vt> a(b)
#define vvt3(a, b, c) vec<vt> a(b, vt(c))
#define vvt4(a, b, c, d) vec<vt> a(b, vt(c, d))
#define v3i(a, b, c, d) vector<vector<vi>> a(b, vector<vi>(c, vi(d)))
#define v3d(a, b, c, d) vector<vector<vd>> a(b, vector<vd>(c, vd(d)))
#define v3m(a, b, c, d) vector<vector<vm>> a(b, vector<vm>(c, vm(d)))
#define _vvi vector<vi>
#define _vvl vector<vl>
#define _vvb vector<vb>
#define _vvs vector<vs>
#define _vvd vector<vd>
#define _vvc vector<vc>
#define _vvp vector<vp>
#define PQ priority_queue<ll, vector<ll>, greater<ll>>
#define tos to_string
using mapi = map<int, int>;
using mapd = map<dou, int>;
using mapc = map<char, int>;
using maps = map<str, int>;
using seti = set<int>;
using setd = set<dou>;
using setc = set<char>;
using sets = set<str>;
using qui = queue<int>;
#define bset bitset
#define uset unordered_set
#define mset multiset
#define umap unordered_map
#define umapi unordered_map<int, int>
#define umapp unordered_map<P, int>
#define mmap multimap
// マクロ 繰り返し
#define _overloadrep(_1, _2, _3, _4, name, ...) name
#define _rep(i, n) for (int i = 0, _lim = n; i < _lim; i++)
#define repi(i, m, n) for (int i = m, _lim = n; i < _lim; i++)
#define repadd(i, m, n, ad) for (int i = m, _lim = n; i < _lim; i += ad)
#define rep(...) _overloadrep(__VA_ARGS__, repadd, repi, _rep, )(__VA_ARGS__)
#define _rer(i, n) for (int i = n; i >= 0; i--)
#define reri(i, m, n) for (int i = m, _lim = n; i >= _lim; i--)
#define rerdec(i, m, n, dec) for (int i = m, _lim = n; i >= _lim; i -= dec)
#define rer(...) _overloadrep(__VA_ARGS__, rerdec, reri, _rer, )(__VA_ARGS__)
#define fora(a, b) for (auto &&a : b)
#define forg(gi, ve) \
for (int gi = 0, f, t, c; \
gi < ve.size() && (f = ve[gi].f, t = ve[gi].t, c = ve[gi].c, true); \
gi++)
#define fort(gi, ve) \
for (int gi = 0, f, t, c; \
gi < ve.size() && (f = ve[gi].f, t = ve[gi].t, c = ve[gi].c, true); \
gi++) \
if (t != p)
// #define fort(gi, ve) for (int gi = 0, f, t, c;gi<ve.size()&& (gi+=
// (ve[gi].t==p))< ve.size() && (f = ve[gi].f,t=ve[gi].t, c = ve[gi].c,true);
// gi++)
// マクロ 定数
#define k3 1010
#define k4 10101
#define k5 101010
#define k6 1010101
#define k7 10101010
const int inf = (int)1e9 + 100;
const ll linf = (ll)1e18 + 100;
const double eps = 1e-9;
const double PI = 3.1415926535897932384626433832795029L;
ll ma = numeric_limits<ll>::min();
ll mi = numeric_limits<ll>::max();
const int y4[] = {-1, 1, 0, 0};
const int x4[] = {0, 0, -1, 1};
const int y8[] = {0, 1, 0, -1, -1, 1, 1, -1};
const int x8[] = {1, 0, -1, 0, 1, -1, 1, -1};
// マクロ省略形 関数等
#define arsz(a) (sizeof(a) / sizeof(a[0]))
#define sz(a) ((int)(a).size())
#define rs resize
#define mp make_pair
#define pb push_back
#define pf push_front
#define eb emplace_back
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
inline void sort(string &a) { sort(a.begin(), a.end()); }
template <class T> inline void sort(vector<T> &a) { sort(a.begin(), a.end()); };
template <class T> inline void sort(vector<T> &a, int len) {
sort(a.begin(), a.begin() + len);
};
template <class T, class F> inline void sort(vector<T> &a, F f) {
sort(a.begin(), a.end(), [&](T l, T r) { return f(l) < f(r); });
};
enum ___pcomparator { fisi, fisd, fdsi, fdsd, sifi, sifd, sdfi, sdfd };
inline void sort(vector<P> &a, ___pcomparator type) {
switch (type) {
case fisi:
sort(all(a),
[&](P l, P r) { return l.fi != r.fi ? l.fi < r.fi : l.se < r.se; });
break;
case fisd:
sort(all(a),
[&](P l, P r) { return l.fi != r.fi ? l.fi < r.fi : l.se > r.se; });
break;
case fdsi:
sort(all(a),
[&](P l, P r) { return l.fi != r.fi ? l.fi > r.fi : l.se < r.se; });
break;
case fdsd:
sort(all(a),
[&](P l, P r) { return l.fi != r.fi ? l.fi > r.fi : l.se > r.se; });
break;
case sifi:
sort(all(a),
[&](P l, P r) { return l.se != r.se ? l.se < r.se : l.fi < r.fi; });
break;
case sifd:
sort(all(a),
[&](P l, P r) { return l.se != r.se ? l.se < r.se : l.fi > r.fi; });
break;
case sdfi:
sort(all(a),
[&](P l, P r) { return l.se != r.se ? l.se > r.se : l.fi < r.fi; });
break;
case sdfd:
sort(all(a),
[&](P l, P r) { return l.se != r.se ? l.se > r.se : l.fi > r.fi; });
break;
}
};
inline void sort(vector<T> &a, ___pcomparator type) {
switch (type) {
case fisi:
sort(all(a), [&](T l, T r) { return l.f != r.f ? l.f < r.f : l.s < r.s; });
break;
case fisd:
sort(all(a), [&](T l, T r) { return l.f != r.f ? l.f < r.f : l.s > r.s; });
break;
case fdsi:
sort(all(a), [&](T l, T r) { return l.f != r.f ? l.f > r.f : l.s < r.s; });
break;
case fdsd:
sort(all(a), [&](T l, T r) { return l.f != r.f ? l.f > r.f : l.s > r.s; });
break;
case sifi:
sort(all(a), [&](T l, T r) { return l.s != r.s ? l.s < r.s : l.f < r.f; });
break;
case sifd:
sort(all(a), [&](T l, T r) { return l.s != r.s ? l.s < r.s : l.f > r.f; });
break;
case sdfi:
sort(all(a), [&](T l, T r) { return l.s != r.s ? l.s > r.s : l.f < r.f; });
break;
case sdfd:
sort(all(a), [&](T l, T r) { return l.s != r.s ? l.s > r.s : l.f > r.f; });
break;
}
};
template <class T> inline void rsort(vector<T> &a) {
sort(a.begin(), a.end(), greater<T>());
};
template <class T> inline void rsort(vector<T> &a, int len) {
sort(a.begin(), a.begin() + len, greater<T>());
};
template <class U, class F> inline void rsort(vector<U> &a, F f) {
sort(a.begin(), a.end(), [&](U l, U r) { return f(l) > f(r); });
};
template <class U> inline void sortp(vector<U> &a, vector<U> &b) {
vp c;
int n = sz(a);
assert(n == sz(b));
rep(i, n) c.eb(a[i], b[i]);
sort(c);
rep(i, n) {
a[i] = c[i].first;
b[i] = c[i].second;
;
}
};
// F = T<T>
// 例えばreturn p.fi + p.se;
template <class U, class F> inline void sortp(vector<U> &a, vector<U> &b, F f) {
vp c;
int n = sz(a);
assert(n == sz(b));
rep(i, n) c.eb(a[i], b[i]);
sort(c, f);
rep(i, n) {
a[i] = c[i].first;
b[i] = c[i].second;
}
};
template <class U, class F>
inline void sortp(vector<U> &a, vector<U> &b, char type) {
vp c;
int n = sz(a);
assert(n == sz(b));
rep(i, n) c.eb(a[i], b[i]);
sort(c, type);
rep(i, n) {
a[i] = c[i].first;
b[i] = c[i].second;
}
};
template <class U> inline void rsortp(vector<U> &a, vector<U> &b) {
vp c;
int n = sz(a);
assert(n == sz(b));
rep(i, n) c.eb(a[i], b[i]);
rsort(c);
rep(i, n) {
a[i] = c[i].first;
b[i] = c[i].second;
}
};
template <class U, class F>
inline void rsortp(vector<U> &a, vector<U> &b, F f) {
vp c;
int n = sz(a);
assert(n == sz(b));
rep(i, n) c.eb(a[i], b[i]);
rsort(c, f);
rep(i, n) {
a[i] = c[i].first;
b[i] = c[i].second;
}
};
template <class U> inline void sortt(vector<U> &a, vector<U> &b, vector<U> &c) {
vt r;
int n = sz(a);
assert(n == sz(b));
assert(n == sz(c));
rep(i, n) r.eb(a[i], b[i], c[i]);
sort(r);
rep(i, n) {
a[i] = r[i].f;
b[i] = r[i].s;
c[i] = r[i].t;
}
};
template <class U, class F>
inline void sortt(vector<U> &a, vector<U> &b, vector<U> &c, F f) {
vt r;
int n = sz(a);
assert(n == sz(b));
assert(n == sz(c));
rep(i, n) r.eb(a[i], b[i], c[i]);
sort(r, f);
rep(i, n) {
a[i] = r[i].f;
b[i] = r[i].s;
c[i] = r[i].t;
}
};
template <class U, class F>
inline void rsortt(vector<U> &a, vector<U> &b, vector<U> &c, F f) {
vt r;
int n = sz(a);
assert(n == sz(b));
assert(n == sz(c));
rep(i, n) r.eb(a[i], b[i], c[i]);
rsort(r, f);
rep(i, n) {
a[i] = r[i].f;
b[i] = r[i].s;
c[i] = r[i].t;
}
};
template <class T> inline void sort2(vector<vector<T>> &a) {
for (int i = 0, n = a.size(); i < n; i++)
sort(a[i]);
}
template <class T> inline void rsort2(vector<vector<T>> &a) {
for (int i = 0, n = a.size(); i < n; i++)
rsort(a[i]);
}
template <typename A, size_t N, typename T> void fill(A (&a)[N], const T &v) {
rep(i, N) a[i] = v;
}
template <typename A, size_t N, size_t O, typename T>
void fill(A (&a)[N][O], const T &v) {
rep(i, N) rep(j, O) a[i][j] = v;
}
template <typename A, size_t N, size_t O, size_t P, typename T>
void fill(A (&a)[N][O][P], const T &v) {
rep(i, N) rep(j, O) rep(k, P) a[i][j][k] = v;
}
template <typename A, size_t N, size_t O, size_t P, size_t Q, typename T>
void fill(A (&a)[N][O][P][Q], const T &v) {
rep(i, N) rep(j, O) rep(k, P) rep(l, Q) a[i][j][k][l] = v;
}
template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R,
typename T>
void fill(A (&a)[N][O][P][Q][R], const T &v) {
rep(i, N) rep(j, O) rep(k, P) rep(l, Q) rep(m, R) a[i][j][k][l][m] = v;
}
template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R,
size_t S, typename T>
void fill(A (&a)[N][O][P][Q][R][S], const T &v) {
rep(i, N) rep(j, O) rep(k, P) rep(l, Q) rep(m, R) rep(n, S)
a[i][j][k][l][m][n] = v;
}
template <typename V, typename T> void fill(V &xx, const T vall) { xx = vall; }
template <typename V, typename T> void fill(vector<V> &vecc, const T vall) {
for (auto &&vx : vecc)
fill(vx, vall);
}
//@汎用便利関数 入力
template <typename T = int> T _in() {
T x;
cin >> x;
return (x);
}
#define _overloadin(_1, _2, _3, _4, name, ...) name
#define in0() _in()
#define in1(a) cin >> a
#define in2(a, b) cin >> a >> b
#define in3(a, b, c) cin >> a >> b >> c
#define in4(a, b, c, d) cin >> a >> b >> c >> d
#define in(...) _overloadin(__VA_ARGS__, in4, in3, in2, in1, in0)(__VA_ARGS__)
#define _overloaddin(_1, _2, _3, _4, name, ...) name
#define din1(a) \
int a; \
cin >> a
#define din2(a, b) \
int a, b; \
cin >> a >> b
#define din3(a, b, c) \
int a, b, c; \
cin >> a >> b >> c
#define din4(a, b, c, d) \
int a, b, c, d; \
cin >> a >> b >> c >> d
#define din(...) _overloadin(__VA_ARGS__, din4, din3, din2, din1)(__VA_ARGS__)
#define _overloaddind(_1, _2, _3, _4, name, ...) name
#define din1d(a) \
int a; \
cin >> a; \
a--
#define din2d(a, b) \
int a, b; \
cin >> a >> b; \
a--, b--
#define din3d(a, b, c) \
int a, b, c; \
cin >> a >> b >> c; \
a--, b--, c--
#define din4d(a, b, c, d) \
int a, b, c, d; \
cin >> a >> b >> c >> d; \
; \
a--, b--, c--, d--
#define dind(...) \
_overloaddind(__VA_ARGS__, din4d, din3d, din2d, din1d)(__VA_ARGS__)
string sin() { return _in<string>(); }
ll lin() { return _in<ll>(); }
#define na(a, n) \
a.resize(n); \
rep(i, n) cin >> a[i];
#define nao(a, n) \
a.resize(n + 1); \
rep(i, n) cin >> a[i + 1];
#define nad(a, n) \
a.resize(n); \
rep(i, n) { \
cin >> a[i]; \
a[i]--; \
}
#define na2(a, b, n) \
a.resize(n), b.resize(n); \
rep(i, n) cin >> a[i] >> b[i];
#define na2d(a, b, n) \
a.resize(n), b.resize(n); \
rep(i, n) { \
cin >> a[i] >> b[i]; \
a[i]--, b[i]--; \
}
#define na3(a, b, c, n) \
a.resize(n), b.resize(n), c.resize(n); \
rep(i, n) cin >> a[i] >> b[i] >> c[i];
#define na3d(a, b, c, n) \
a.resize(n), b.resize(n), c.resize(n); \
rep(i, n) { \
cin >> a[i] >> b[i] >> c[i]; \
a[i]--, b[i]--, c[i]--; \
}
#define nt(a, h, w) \
resize(a, h, w); \
rep(hi, h) rep(wi, w) cin >> a[hi][wi];
#define ntd(a, h, w) \
rs(a, h, w); \
rep(hi, h) rep(wi, w) cin >> a[hi][wi], a[hi][wi]--;
#define ntp(a, h, w) \
fill(a, '#'); \
rep(hi, 1, h + 1) rep(wi, 1, w + 1) cin >> a[hi][wi];
// デバッグ
#define sp << " " <<
#define debugName(VariableName) #VariableName
#define _deb1(x) cerr << debugName(x) << " = " << x << endl
#define _deb2(x, y) \
cerr << debugName(x) << " = " << x << ", " << debugName(y) << " = " << y \
<< endl
#define _deb3(x, y, z) \
cerr << debugName(x) << " = " << x << ", " << debugName(y) << " = " << y \
<< ", " debugName(z) << " = " << z << endl
#define _deb4(x, y, z, a) \
cerr << debugName(x) << " = " << x << ", " << debugName(y) << " = " << y \
<< ", " << debugName(z) << " = " << z << ", " << debugName(a) << " = " \
<< a << endl
#define _deb5(x, y, z, a, b) \
cerr << debugName(x) << " = " << x << ", " << debugName(y) << " = " << y \
<< ", " << debugName(z) << " = " << z << ", " << debugName(a) << " = " \
<< a << ", " << debugName(b) << " = " << b << endl
#define _overloadebug(_1, _2, _3, _4, _5, name, ...) name
#define debug(...) \
_overloadebug(__VA_ARGS__, _deb5, _deb4, _deb3, _deb2, _deb1)(__VA_ARGS__)
#define deb(...) \
_overloadebug(__VA_ARGS__, _deb5, _deb4, _deb3, _deb2, _deb1)(__VA_ARGS__)
#define debugline(x) \
cerr << x << " " \
<< "(L:" << __LINE__ << ")" << '\n'
void ole() {
#ifdef _DEBUG
debugline("ole");
exit(0);
#endif
string a = "a";
rep(i, 30) a += a;
rep(i, 1 << 17) cout << a << endl;
cout << "OLE 出力長制限超過" << endl;
exit(0);
}
void tle() {
while (inf)
cout << inf << endl;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll gcd(vi b) {
ll res = b[0];
for (auto &&v : b)
res = gcd(v, res);
return res;
}
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
ll rev(ll a) {
ll res = 0;
while (a) {
res *= 10;
res += a % 10;
a /= 10;
}
return res;
}
template <class T> void rev(vector<T> &a) { reverse(all(a)); }
void rev(string &a) { reverse(all(a)); }
ll ceil(ll a, ll b) {
if (b == 0) {
debugline("ceil");
deb(a, b);
ole();
return -1;
} else
return (a + b - 1) / b;
}
ll sqrt(ll a) {
if (a < 0) {
debugline("sqrt");
deb(a);
ole();
}
ll res = (ll)std::sqrt(a);
while (res * res < a)
res++;
return res;
}
double log(double e, double x) { return log(x) / log(e); }
ll sig(ll t) { return (1 + t) * t / 2; }
ll sig(ll s, ll t) { return (s + t) * (t - s + 1) / 2; }
vi divisors(int v) {
vi res;
double lim = std::sqrt(v);
for (int i = 1; i <= lim; ++i) {
if (v % i == 0) {
res.pb(i);
if (i != v / i)
res.pb(v / i);
}
}
return res;
}
vb isPrime;
vi primes;
void setPrime() {
int len = 4010101;
isPrime.resize(4010101);
fill(isPrime, true);
isPrime[0] = isPrime[1] = false;
for (int i = 2; i <= sqrt(len) + 5; ++i) {
if (!isPrime[i])
continue;
for (int j = 2; i * j < len; ++j) {
isPrime[i * j] = false;
}
}
rep(i, len) if (isPrime[i]) primes.pb(i);
}
vi factorization(int v) {
int tv = v;
vi res;
if (isPrime.size() == 0)
setPrime();
for (auto &&p : primes) {
if (v % p == 0)
res.push_back(p);
while (v % p == 0) {
v /= p;
}
if (v == 1 || p * p > tv)
break;
}
if (v > 1)
res.pb(v);
return res;
}
inline bool inside(int h, int w, int H, int W) {
return h >= 0 && w >= 0 && h < H && w < W;
}
inline bool inside(int v, int l, int r) { return l <= v && v < r; }
#define ins inside
ll u(ll a) { return a < 0 ? 0 : a; }
template <class T> vector<T> u(const vector<T> &a) {
vector<T> ret = a;
fora(v, ret) v = u(v);
return ret;
}
#define MIN(a) numeric_limits<a>::min()
#define MAX(a) numeric_limits<a>::max()
int n, m, k, d, H, W, x, y, z, q;
int cou;
vi a, b, c;
vvi(s, 0, 0);
vvc(ba, 0, 0);
vp p;
#define segmin(M) \
[](M a, M b) { return a < b ? a : b; }, [](int len, M x) { return x; }, MAX(M)
#define segmax(M) \
[](M a, M b) { return a > b ? a : b; }, [](int len, M x) { return x; }, MIN(M)
#define segsum(M) \
[](M a, M b) { return a + b; }, [](int len, M x) { return len * x; }, 0
#define segxor(M) \
[](M a, M b) { return a ^ b; }, \
[](int len, M x) { return (len & 1) ? x : 0; }, 0
// 作用素同士の演算は足し算でいいと仮定している
template <class M> struct SegmentLazyAdd {
using func = function<M(M, M)>;
using feval = function<M(
int,
M /*作用素,Mとは違う型を使うのを横着している*/)>; // 区間の値に加算される値
int n;
vector<M> seg;
vi lazy; // 全体に足された数を持つ 3333なら3
const func f; // M同士の演算結果を返す
const feval getAdd; // len,xを受け取り、segに加える数を返す
const M e;
vi arch; // 遅延評価を行うやつ
vi lens;
SegmentLazyAdd(vi a, func f, feval fe, M e) : f(f), getAdd(fe), e(e) {
n = 1;
// rep(i, 100) a.pb(e);
int asz = a.size();
while (n < asz)
n <<= 1;
seg.resize(2 * n - 1, e);
lazy.resize(2 * n - 1, 0);
lens.resize(2 * n - 1);
rep(i, asz) seg[i + n - 1] = a[i];
rer(i, n - 2) seg[i] = f(seg[(i << 1) + 1], seg[(i << 1) + 2]);
int l = 1, v = n;
rep(i, 1, n << 1) {
if ((l << 1) == i) {
l <<= 1;
v >>= 1;
}
lens[i - 1] = v;
}
}
void eval() {
rer(j, sz(arch) - 1) {
int i = arch[j];
// iは1index
// lazyは0index
int v = lazy[i - 1];
if (!v)
continue;
lazy[i * 2 - 1] += v;
lazy[i * 2] += v;
seg[i * 2 - 1] += getAdd(lens[i * 2 - 1], v); //
seg[i * 2] += getAdd(lens[i * 2], v);
lazy[i - 1] = 0;
}
}
void gindex(int l, int r) {
arch.clear();
l += n;
r += n;
int lm = (l / (l & -l)) >> 1;
int rm = (r / (r & -r)) >> 1;
while (l < r) {
if (r <= rm)
arch.pb(r);
if (l <= lm)
arch.pb(l);
l >>= 1;
r >>= 1;
}
while (l) {
arch.pb(l);
l >>= 1;
}
}
void add(int l, int r, int x) {
int L = l + n;
int R = r + n;
while (L < R) {
if (R & 1) {
R--;
lazy[R - 1] += x;
seg[R - 1] += getAdd(lens[R - 1], x);
}
if (L & 1) {
lazy[L - 1] += x;
seg[L - 1] += getAdd(lens[L - 1], x);
L++;
}
L >>= 1;
R >>= 1;
}
gindex(l, r);
fora(i, arch) {
seg[i - 1] = f(seg[i * 2 - 1], seg[i * 2]);
seg[i - 1] += getAdd(lens[i - 1], lazy[i - 1]);
}
}
M get(int l, int r) {
gindex(l, r);
eval();
int L = l + n;
int R = r + n;
M retl = e, retr = e;
while (L < R) {
if (R & 1) {
R--;
retr = f(seg[R - 1], retr);
}
if (L & 1) {
retl = f(retl, seg[L - 1]);
L++;
}
L >>= 1;
R >>= 1;
}
return f(retl, retr);
}
#ifdef _DEBUG
void debu(int len = 10) {
rep(i, min(n, min(len, n))) {
int v = get(i, i + 1);
if (v == e) {
cerr << "e ";
} else {
cerr << v << " ";
}
}
cerr << "" << endl;
}
#else
inline void debu() { ; }
#endif
};
#define seg SegmentLazyAdd
#define raq SegmentLazyAdd
signed main() {
cin >> n >> q;
seg<int> st(vi(n), segsum(int));
rep(i, q) {
int c;
cin >> c;
int s, t;
cin >> s >> t;
if (c == 0) {
int x;
cin >> x;
st.add(s - 1, t, x);
} else {
cout << st.get(s - 1, t) << endl;
}
}
return 0;
}
|
// #pragma GCC optimize ("-O3")
#include <bits/stdc++.h>
using namespace std;
//@起動時
struct initon {
initon() {
cin.tie(0);
ios::sync_with_stdio(false);
cout.setf(ios::fixed);
cout.precision(16);
srand((unsigned)clock() + (unsigned)time(NULL));
};
} __initon;
// 衝突対策
#define ws ___ws
struct T {
int f, s, t;
T() { f = -1, s = -1, t = -1; }
T(int f, int s, int t) : f(f), s(s), t(t) {}
bool operator<(const T &r) const {
return f != r.f ? f < r.f : s != r.s ? s < r.s : t < r.t;
// return f != r.f ? f > r.f : s != r.s ? s > r.s : t > r.t; 大きい順
}
bool operator>(const T &r) const {
return f != r.f ? f > r.f : s != r.s ? s > r.s : t > r.t;
// return f != r.f ? f > r.f : s != r.s ? s > r.s : t > r.t; 小さい順
}
bool operator==(const T &r) const { return f == r.f && s == r.s && t == r.t; }
bool operator!=(const T &r) const { return f != r.f || s != r.s || t != r.t; }
int operator[](int i) {
assert(i < 3);
return i == 0 ? f : i == 1 ? s : t;
}
};
#define int long long
#define ll long long
#define double long double
#define ull unsigned long long
using dou = double;
using itn = int;
using str = string;
using bo = bool;
#define au auto
using P = pair<ll, ll>;
#define fi first
#define se second
#define vec vector
#define beg begin
#define rbeg rbegin
#define con continue
#define bre break
#define brk break
#define is ==
// マクロ省略系 コンテナ
using vi = vector<int>;
#define _overloadvvi(_1, _2, _3, _4, name, ...) name
#define vvi0() vec<vi>
#define vvi1(a) vec<vi> a
#define vvi2(a, b) vec<vi> a(b)
#define vvi3(a, b, c) vec<vi> a(b, vi(c))
#define vvi4(a, b, c, d) vec<vi> a(b, vi(c, d))
#define vvi(...) \
_overloadvvi(__VA_ARGS__, vvi4, vvi3, vvi2, vvi1, vvi0)(__VA_ARGS__)
using vl = vector<ll>;
#define _overloadvvl(_1, _2, _3, _4, name, ...) name
#define vvl1(a) vec<vl> a
#define vvl2(a, b) vec<vl> a(b)
#define vvl3(a, b, c) vec<vl> a(b, vl(c))
#define vvl4(a, b, c, d) vec<vl> a(b, vl(c, d))
#define vvl(...) _overloadvvl(__VA_ARGS__, vvl4, vvl3, vvl2, vvl1)(__VA_ARGS__)
using vb = vector<bool>;
#define _overloadvvb(_1, _2, _3, _4, name, ...) name
#define vvb1(a) vec<vb> a
#define vvb2(a, b) vec<vb> a(b)
#define vvb3(a, b, c) vec<vb> a(b, vb(c))
#define vvb4(a, b, c, d) vec<vb> a(b, vb(c, d))
#define vvb(...) _overloadvvb(__VA_ARGS__, vvb4, vvb3, vvb2, vvb1)(__VA_ARGS__)
using vs = vector<string>;
#define _overloadvvs(_1, _2, _3, _4, name, ...) name
#define vvs1(a) vec<vs> a
#define vvs2(a, b) vec<vs> a(b)
#define vvs3(a, b, c) vec<vs> a(b, vs(c))
#define vvs4(a, b, c, d) vec<vs> a(b, vs(c, d))
#define vvs(...) _overloadvvs(__VA_ARGS__, vvs4, vvs3, vvs2, vvs1)(__VA_ARGS__)
using vd = vector<double>;
#define _overloadvvd(_1, _2, _3, _4, name, ...) name
#define vvd1(a) vec<vd> a
#define vvd2(a, b) vec<vd> a(b)
#define vvd3(a, b, c) vec<vd> a(b, vd(c))
#define vvd4(a, b, c, d) vec<vd> a(b, vd(c, d))
#define vvd(...) _overloadvvd(__VA_ARGS__, vvd4, vvd3, vvd2, vvd1)(__VA_ARGS__)
using vc = vector<char>;
#define _overloadvvc(_1, _2, _3, _4, name, ...) name
#define vvc1(a) vec<vc> a
#define vvc2(a, b) vec<vc> a(b)
#define vvc3(a, b, c) vec<vc> a(b, vc(c))
#define vvc4(a, b, c, d) vec<vc> a(b, vc(c, d))
#define vvc(...) _overloadvvc(__VA_ARGS__, vvc4, vvc3, vvc2, vvc1)(__VA_ARGS__)
using vp = vector<P>;
#define _overloadvvp(_1, _2, _3, _4, name, ...) name
#define vvp1(a) vec<vp> a
#define vvp2(a, b) vec<vp> a(b)
#define vvp3(a, b, c) vec<vp> a(b, vp(c))
#define vvp4(a, b, c, d) vec<vp> a(b, vp(c, d))
using vt = vector<T>;
#define _overloadvvt(_1, _2, _3, _4, name, ...) name
#define vvt1(a) vec<vt> a
#define vvt2(a, b) vec<vt> a(b)
#define vvt3(a, b, c) vec<vt> a(b, vt(c))
#define vvt4(a, b, c, d) vec<vt> a(b, vt(c, d))
#define v3i(a, b, c, d) vector<vector<vi>> a(b, vector<vi>(c, vi(d)))
#define v3d(a, b, c, d) vector<vector<vd>> a(b, vector<vd>(c, vd(d)))
#define v3m(a, b, c, d) vector<vector<vm>> a(b, vector<vm>(c, vm(d)))
#define _vvi vector<vi>
#define _vvl vector<vl>
#define _vvb vector<vb>
#define _vvs vector<vs>
#define _vvd vector<vd>
#define _vvc vector<vc>
#define _vvp vector<vp>
#define PQ priority_queue<ll, vector<ll>, greater<ll>>
#define tos to_string
using mapi = map<int, int>;
using mapd = map<dou, int>;
using mapc = map<char, int>;
using maps = map<str, int>;
using seti = set<int>;
using setd = set<dou>;
using setc = set<char>;
using sets = set<str>;
using qui = queue<int>;
#define bset bitset
#define uset unordered_set
#define mset multiset
#define umap unordered_map
#define umapi unordered_map<int, int>
#define umapp unordered_map<P, int>
#define mmap multimap
// マクロ 繰り返し
#define _overloadrep(_1, _2, _3, _4, name, ...) name
#define _rep(i, n) for (int i = 0, _lim = n; i < _lim; i++)
#define repi(i, m, n) for (int i = m, _lim = n; i < _lim; i++)
#define repadd(i, m, n, ad) for (int i = m, _lim = n; i < _lim; i += ad)
#define rep(...) _overloadrep(__VA_ARGS__, repadd, repi, _rep, )(__VA_ARGS__)
#define _rer(i, n) for (int i = n; i >= 0; i--)
#define reri(i, m, n) for (int i = m, _lim = n; i >= _lim; i--)
#define rerdec(i, m, n, dec) for (int i = m, _lim = n; i >= _lim; i -= dec)
#define rer(...) _overloadrep(__VA_ARGS__, rerdec, reri, _rer, )(__VA_ARGS__)
#define fora(a, b) for (auto &&a : b)
#define forg(gi, ve) \
for (int gi = 0, f, t, c; \
gi < ve.size() && (f = ve[gi].f, t = ve[gi].t, c = ve[gi].c, true); \
gi++)
#define fort(gi, ve) \
for (int gi = 0, f, t, c; \
gi < ve.size() && (f = ve[gi].f, t = ve[gi].t, c = ve[gi].c, true); \
gi++) \
if (t != p)
// #define fort(gi, ve) for (int gi = 0, f, t, c;gi<ve.size()&& (gi+=
// (ve[gi].t==p))< ve.size() && (f = ve[gi].f,t=ve[gi].t, c = ve[gi].c,true);
// gi++)
// マクロ 定数
#define k3 1010
#define k4 10101
#define k5 101010
#define k6 1010101
#define k7 10101010
const int inf = (int)1e9 + 100;
const ll linf = (ll)1e18 + 100;
const double eps = 1e-9;
const double PI = 3.1415926535897932384626433832795029L;
ll ma = numeric_limits<ll>::min();
ll mi = numeric_limits<ll>::max();
const int y4[] = {-1, 1, 0, 0};
const int x4[] = {0, 0, -1, 1};
const int y8[] = {0, 1, 0, -1, -1, 1, 1, -1};
const int x8[] = {1, 0, -1, 0, 1, -1, 1, -1};
// マクロ省略形 関数等
#define arsz(a) (sizeof(a) / sizeof(a[0]))
#define sz(a) ((int)(a).size())
#define rs resize
#define mp make_pair
#define pb push_back
#define pf push_front
#define eb emplace_back
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
inline void sort(string &a) { sort(a.begin(), a.end()); }
template <class T> inline void sort(vector<T> &a) { sort(a.begin(), a.end()); };
template <class T> inline void sort(vector<T> &a, int len) {
sort(a.begin(), a.begin() + len);
};
template <class T, class F> inline void sort(vector<T> &a, F f) {
sort(a.begin(), a.end(), [&](T l, T r) { return f(l) < f(r); });
};
enum ___pcomparator { fisi, fisd, fdsi, fdsd, sifi, sifd, sdfi, sdfd };
inline void sort(vector<P> &a, ___pcomparator type) {
switch (type) {
case fisi:
sort(all(a),
[&](P l, P r) { return l.fi != r.fi ? l.fi < r.fi : l.se < r.se; });
break;
case fisd:
sort(all(a),
[&](P l, P r) { return l.fi != r.fi ? l.fi < r.fi : l.se > r.se; });
break;
case fdsi:
sort(all(a),
[&](P l, P r) { return l.fi != r.fi ? l.fi > r.fi : l.se < r.se; });
break;
case fdsd:
sort(all(a),
[&](P l, P r) { return l.fi != r.fi ? l.fi > r.fi : l.se > r.se; });
break;
case sifi:
sort(all(a),
[&](P l, P r) { return l.se != r.se ? l.se < r.se : l.fi < r.fi; });
break;
case sifd:
sort(all(a),
[&](P l, P r) { return l.se != r.se ? l.se < r.se : l.fi > r.fi; });
break;
case sdfi:
sort(all(a),
[&](P l, P r) { return l.se != r.se ? l.se > r.se : l.fi < r.fi; });
break;
case sdfd:
sort(all(a),
[&](P l, P r) { return l.se != r.se ? l.se > r.se : l.fi > r.fi; });
break;
}
};
inline void sort(vector<T> &a, ___pcomparator type) {
switch (type) {
case fisi:
sort(all(a), [&](T l, T r) { return l.f != r.f ? l.f < r.f : l.s < r.s; });
break;
case fisd:
sort(all(a), [&](T l, T r) { return l.f != r.f ? l.f < r.f : l.s > r.s; });
break;
case fdsi:
sort(all(a), [&](T l, T r) { return l.f != r.f ? l.f > r.f : l.s < r.s; });
break;
case fdsd:
sort(all(a), [&](T l, T r) { return l.f != r.f ? l.f > r.f : l.s > r.s; });
break;
case sifi:
sort(all(a), [&](T l, T r) { return l.s != r.s ? l.s < r.s : l.f < r.f; });
break;
case sifd:
sort(all(a), [&](T l, T r) { return l.s != r.s ? l.s < r.s : l.f > r.f; });
break;
case sdfi:
sort(all(a), [&](T l, T r) { return l.s != r.s ? l.s > r.s : l.f < r.f; });
break;
case sdfd:
sort(all(a), [&](T l, T r) { return l.s != r.s ? l.s > r.s : l.f > r.f; });
break;
}
};
template <class T> inline void rsort(vector<T> &a) {
sort(a.begin(), a.end(), greater<T>());
};
template <class T> inline void rsort(vector<T> &a, int len) {
sort(a.begin(), a.begin() + len, greater<T>());
};
template <class U, class F> inline void rsort(vector<U> &a, F f) {
sort(a.begin(), a.end(), [&](U l, U r) { return f(l) > f(r); });
};
template <class U> inline void sortp(vector<U> &a, vector<U> &b) {
vp c;
int n = sz(a);
assert(n == sz(b));
rep(i, n) c.eb(a[i], b[i]);
sort(c);
rep(i, n) {
a[i] = c[i].first;
b[i] = c[i].second;
;
}
};
// F = T<T>
// 例えばreturn p.fi + p.se;
template <class U, class F> inline void sortp(vector<U> &a, vector<U> &b, F f) {
vp c;
int n = sz(a);
assert(n == sz(b));
rep(i, n) c.eb(a[i], b[i]);
sort(c, f);
rep(i, n) {
a[i] = c[i].first;
b[i] = c[i].second;
}
};
template <class U, class F>
inline void sortp(vector<U> &a, vector<U> &b, char type) {
vp c;
int n = sz(a);
assert(n == sz(b));
rep(i, n) c.eb(a[i], b[i]);
sort(c, type);
rep(i, n) {
a[i] = c[i].first;
b[i] = c[i].second;
}
};
template <class U> inline void rsortp(vector<U> &a, vector<U> &b) {
vp c;
int n = sz(a);
assert(n == sz(b));
rep(i, n) c.eb(a[i], b[i]);
rsort(c);
rep(i, n) {
a[i] = c[i].first;
b[i] = c[i].second;
}
};
template <class U, class F>
inline void rsortp(vector<U> &a, vector<U> &b, F f) {
vp c;
int n = sz(a);
assert(n == sz(b));
rep(i, n) c.eb(a[i], b[i]);
rsort(c, f);
rep(i, n) {
a[i] = c[i].first;
b[i] = c[i].second;
}
};
template <class U> inline void sortt(vector<U> &a, vector<U> &b, vector<U> &c) {
vt r;
int n = sz(a);
assert(n == sz(b));
assert(n == sz(c));
rep(i, n) r.eb(a[i], b[i], c[i]);
sort(r);
rep(i, n) {
a[i] = r[i].f;
b[i] = r[i].s;
c[i] = r[i].t;
}
};
template <class U, class F>
inline void sortt(vector<U> &a, vector<U> &b, vector<U> &c, F f) {
vt r;
int n = sz(a);
assert(n == sz(b));
assert(n == sz(c));
rep(i, n) r.eb(a[i], b[i], c[i]);
sort(r, f);
rep(i, n) {
a[i] = r[i].f;
b[i] = r[i].s;
c[i] = r[i].t;
}
};
template <class U, class F>
inline void rsortt(vector<U> &a, vector<U> &b, vector<U> &c, F f) {
vt r;
int n = sz(a);
assert(n == sz(b));
assert(n == sz(c));
rep(i, n) r.eb(a[i], b[i], c[i]);
rsort(r, f);
rep(i, n) {
a[i] = r[i].f;
b[i] = r[i].s;
c[i] = r[i].t;
}
};
template <class T> inline void sort2(vector<vector<T>> &a) {
for (int i = 0, n = a.size(); i < n; i++)
sort(a[i]);
}
template <class T> inline void rsort2(vector<vector<T>> &a) {
for (int i = 0, n = a.size(); i < n; i++)
rsort(a[i]);
}
template <typename A, size_t N, typename T> void fill(A (&a)[N], const T &v) {
rep(i, N) a[i] = v;
}
template <typename A, size_t N, size_t O, typename T>
void fill(A (&a)[N][O], const T &v) {
rep(i, N) rep(j, O) a[i][j] = v;
}
template <typename A, size_t N, size_t O, size_t P, typename T>
void fill(A (&a)[N][O][P], const T &v) {
rep(i, N) rep(j, O) rep(k, P) a[i][j][k] = v;
}
template <typename A, size_t N, size_t O, size_t P, size_t Q, typename T>
void fill(A (&a)[N][O][P][Q], const T &v) {
rep(i, N) rep(j, O) rep(k, P) rep(l, Q) a[i][j][k][l] = v;
}
template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R,
typename T>
void fill(A (&a)[N][O][P][Q][R], const T &v) {
rep(i, N) rep(j, O) rep(k, P) rep(l, Q) rep(m, R) a[i][j][k][l][m] = v;
}
template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R,
size_t S, typename T>
void fill(A (&a)[N][O][P][Q][R][S], const T &v) {
rep(i, N) rep(j, O) rep(k, P) rep(l, Q) rep(m, R) rep(n, S)
a[i][j][k][l][m][n] = v;
}
template <typename V, typename T> void fill(V &xx, const T vall) { xx = vall; }
template <typename V, typename T> void fill(vector<V> &vecc, const T vall) {
for (auto &&vx : vecc)
fill(vx, vall);
}
//@汎用便利関数 入力
template <typename T = int> T _in() {
T x;
cin >> x;
return (x);
}
#define _overloadin(_1, _2, _3, _4, name, ...) name
#define in0() _in()
#define in1(a) cin >> a
#define in2(a, b) cin >> a >> b
#define in3(a, b, c) cin >> a >> b >> c
#define in4(a, b, c, d) cin >> a >> b >> c >> d
#define in(...) _overloadin(__VA_ARGS__, in4, in3, in2, in1, in0)(__VA_ARGS__)
#define _overloaddin(_1, _2, _3, _4, name, ...) name
#define din1(a) \
int a; \
cin >> a
#define din2(a, b) \
int a, b; \
cin >> a >> b
#define din3(a, b, c) \
int a, b, c; \
cin >> a >> b >> c
#define din4(a, b, c, d) \
int a, b, c, d; \
cin >> a >> b >> c >> d
#define din(...) _overloadin(__VA_ARGS__, din4, din3, din2, din1)(__VA_ARGS__)
#define _overloaddind(_1, _2, _3, _4, name, ...) name
#define din1d(a) \
int a; \
cin >> a; \
a--
#define din2d(a, b) \
int a, b; \
cin >> a >> b; \
a--, b--
#define din3d(a, b, c) \
int a, b, c; \
cin >> a >> b >> c; \
a--, b--, c--
#define din4d(a, b, c, d) \
int a, b, c, d; \
cin >> a >> b >> c >> d; \
; \
a--, b--, c--, d--
#define dind(...) \
_overloaddind(__VA_ARGS__, din4d, din3d, din2d, din1d)(__VA_ARGS__)
string sin() { return _in<string>(); }
ll lin() { return _in<ll>(); }
#define na(a, n) \
a.resize(n); \
rep(i, n) cin >> a[i];
#define nao(a, n) \
a.resize(n + 1); \
rep(i, n) cin >> a[i + 1];
#define nad(a, n) \
a.resize(n); \
rep(i, n) { \
cin >> a[i]; \
a[i]--; \
}
#define na2(a, b, n) \
a.resize(n), b.resize(n); \
rep(i, n) cin >> a[i] >> b[i];
#define na2d(a, b, n) \
a.resize(n), b.resize(n); \
rep(i, n) { \
cin >> a[i] >> b[i]; \
a[i]--, b[i]--; \
}
#define na3(a, b, c, n) \
a.resize(n), b.resize(n), c.resize(n); \
rep(i, n) cin >> a[i] >> b[i] >> c[i];
#define na3d(a, b, c, n) \
a.resize(n), b.resize(n), c.resize(n); \
rep(i, n) { \
cin >> a[i] >> b[i] >> c[i]; \
a[i]--, b[i]--, c[i]--; \
}
#define nt(a, h, w) \
resize(a, h, w); \
rep(hi, h) rep(wi, w) cin >> a[hi][wi];
#define ntd(a, h, w) \
rs(a, h, w); \
rep(hi, h) rep(wi, w) cin >> a[hi][wi], a[hi][wi]--;
#define ntp(a, h, w) \
fill(a, '#'); \
rep(hi, 1, h + 1) rep(wi, 1, w + 1) cin >> a[hi][wi];
// デバッグ
#define sp << " " <<
#define debugName(VariableName) #VariableName
#define _deb1(x) cerr << debugName(x) << " = " << x << endl
#define _deb2(x, y) \
cerr << debugName(x) << " = " << x << ", " << debugName(y) << " = " << y \
<< endl
#define _deb3(x, y, z) \
cerr << debugName(x) << " = " << x << ", " << debugName(y) << " = " << y \
<< ", " debugName(z) << " = " << z << endl
#define _deb4(x, y, z, a) \
cerr << debugName(x) << " = " << x << ", " << debugName(y) << " = " << y \
<< ", " << debugName(z) << " = " << z << ", " << debugName(a) << " = " \
<< a << endl
#define _deb5(x, y, z, a, b) \
cerr << debugName(x) << " = " << x << ", " << debugName(y) << " = " << y \
<< ", " << debugName(z) << " = " << z << ", " << debugName(a) << " = " \
<< a << ", " << debugName(b) << " = " << b << endl
#define _overloadebug(_1, _2, _3, _4, _5, name, ...) name
#define debug(...) \
_overloadebug(__VA_ARGS__, _deb5, _deb4, _deb3, _deb2, _deb1)(__VA_ARGS__)
#define deb(...) \
_overloadebug(__VA_ARGS__, _deb5, _deb4, _deb3, _deb2, _deb1)(__VA_ARGS__)
#define debugline(x) \
cerr << x << " " \
<< "(L:" << __LINE__ << ")" << '\n'
void ole() {
#ifdef _DEBUG
debugline("ole");
exit(0);
#endif
string a = "a";
rep(i, 30) a += a;
rep(i, 1 << 17) cout << a << endl;
cout << "OLE 出力長制限超過" << endl;
exit(0);
}
void tle() {
while (inf)
cout << inf << endl;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll gcd(vi b) {
ll res = b[0];
for (auto &&v : b)
res = gcd(v, res);
return res;
}
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
ll rev(ll a) {
ll res = 0;
while (a) {
res *= 10;
res += a % 10;
a /= 10;
}
return res;
}
template <class T> void rev(vector<T> &a) { reverse(all(a)); }
void rev(string &a) { reverse(all(a)); }
ll ceil(ll a, ll b) {
if (b == 0) {
debugline("ceil");
deb(a, b);
ole();
return -1;
} else
return (a + b - 1) / b;
}
ll sqrt(ll a) {
if (a < 0) {
debugline("sqrt");
deb(a);
ole();
}
ll res = (ll)std::sqrt(a);
while (res * res < a)
res++;
return res;
}
double log(double e, double x) { return log(x) / log(e); }
ll sig(ll t) { return (1 + t) * t / 2; }
ll sig(ll s, ll t) { return (s + t) * (t - s + 1) / 2; }
vi divisors(int v) {
vi res;
double lim = std::sqrt(v);
for (int i = 1; i <= lim; ++i) {
if (v % i == 0) {
res.pb(i);
if (i != v / i)
res.pb(v / i);
}
}
return res;
}
vb isPrime;
vi primes;
void setPrime() {
int len = 4010101;
isPrime.resize(4010101);
fill(isPrime, true);
isPrime[0] = isPrime[1] = false;
for (int i = 2; i <= sqrt(len) + 5; ++i) {
if (!isPrime[i])
continue;
for (int j = 2; i * j < len; ++j) {
isPrime[i * j] = false;
}
}
rep(i, len) if (isPrime[i]) primes.pb(i);
}
vi factorization(int v) {
int tv = v;
vi res;
if (isPrime.size() == 0)
setPrime();
for (auto &&p : primes) {
if (v % p == 0)
res.push_back(p);
while (v % p == 0) {
v /= p;
}
if (v == 1 || p * p > tv)
break;
}
if (v > 1)
res.pb(v);
return res;
}
inline bool inside(int h, int w, int H, int W) {
return h >= 0 && w >= 0 && h < H && w < W;
}
inline bool inside(int v, int l, int r) { return l <= v && v < r; }
#define ins inside
ll u(ll a) { return a < 0 ? 0 : a; }
template <class T> vector<T> u(const vector<T> &a) {
vector<T> ret = a;
fora(v, ret) v = u(v);
return ret;
}
#define MIN(a) numeric_limits<a>::min()
#define MAX(a) numeric_limits<a>::max()
int n, m, k, d, H, W, x, y, z, q;
int cou;
vi a, b, c;
vvi(s, 0, 0);
vvc(ba, 0, 0);
vp p;
#define segmin(M) \
[](M a, M b) { return a < b ? a : b; }, [](int len, M x) { return x; }, MAX(M)
#define segmax(M) \
[](M a, M b) { return a > b ? a : b; }, [](int len, M x) { return x; }, MIN(M)
#define segsum(M) \
[](M a, M b) { return a + b; }, [](int len, M x) { return len * x; }, 0
#define segxor(M) \
[](M a, M b) { return a ^ b; }, \
[](int len, M x) { return (len & 1) ? x : 0; }, 0
// 作用素同士の演算は足し算でいいと仮定している
template <class M> struct SegmentLazyAdd {
using func = function<M(M, M)>;
using feval = function<M(
int,
M /*作用素,Mとは違う型を使うのを横着している*/)>; // 区間の値に加算される値
int n;
vector<M> seg;
vi lazy; // 全体に足された数を持つ 3333なら3
const func f; // M同士の演算結果を返す
const feval getAdd; // len,xを受け取り、segに加える数を返す
const M e;
vi arch; // 遅延評価を行うやつ
vi lens;
SegmentLazyAdd(vi a, func f, feval fe, M e) : f(f), getAdd(fe), e(e) {
n = 1;
// rep(i, 100) a.pb(e);
int asz = a.size();
while (n < asz)
n <<= 1;
seg.resize(2 * n - 1, e);
lazy.resize(2 * n - 1, 0);
lens.resize(2 * n - 1);
rep(i, asz) seg[i + n - 1] = a[i];
rer(i, n - 2) seg[i] = f(seg[(i << 1) + 1], seg[(i << 1) + 2]);
int l = 1, v = n;
rep(i, 1, n << 1) {
if ((l << 1) == i) {
l <<= 1;
v >>= 1;
}
lens[i - 1] = v;
}
}
void eval() {
rer(j, sz(arch) - 1) {
int i = arch[j];
// iは1index
// lazyは0index
int v = lazy[i - 1];
if (!v)
continue;
lazy[i * 2 - 1] += v;
lazy[i * 2] += v;
seg[i * 2 - 1] += getAdd(lens[i * 2 - 1], v); //
seg[i * 2] += getAdd(lens[i * 2], v);
lazy[i - 1] = 0;
}
}
void gindex(int l, int r) {
arch.clear();
l += n;
r += n;
int lm = (l / (l & -l)) >> 1;
int rm = (r / (r & -r)) >> 1;
while (l < r) {
if (r <= rm)
arch.pb(r);
if (l <= lm)
arch.pb(l);
l >>= 1;
r >>= 1;
}
while (l) {
arch.pb(l);
l >>= 1;
}
}
void add(int l, int r, int x) {
int L = l + n;
int R = r + n;
while (L < R) {
if (R & 1) {
R--;
lazy[R - 1] += x;
seg[R - 1] += getAdd(lens[R - 1], x);
}
if (L & 1) {
lazy[L - 1] += x;
seg[L - 1] += getAdd(lens[L - 1], x);
L++;
}
L >>= 1;
R >>= 1;
}
gindex(l, r);
fora(i, arch) {
seg[i - 1] = f(seg[i * 2 - 1], seg[i * 2]);
seg[i - 1] += getAdd(lens[i - 1], lazy[i - 1]);
}
}
M get(int l, int r) {
gindex(l, r);
eval();
int L = l + n;
int R = r + n;
M retl = e, retr = e;
while (L < R) {
if (R & 1) {
R--;
retr = f(seg[R - 1], retr);
}
if (L & 1) {
retl = f(retl, seg[L - 1]);
L++;
}
L >>= 1;
R >>= 1;
}
return f(retl, retr);
}
#ifdef _DEBUG
void debu(int len = 10) {
rep(i, min(n, min(len, n))) {
int v = get(i, i + 1);
if (v == e) {
cerr << "e ";
} else {
cerr << v << " ";
}
}
cerr << "" << endl;
}
#else
inline void debu() { ; }
#endif
};
#define seg SegmentLazyAdd
#define raq SegmentLazyAdd
signed main() {
cin >> n >> q;
seg<int> st(vi(k5), segsum(int));
rep(i, q) {
int c;
cin >> c;
int s, t;
cin >> s >> t;
if (c == 0) {
int x;
cin >> x;
st.add(s - 1, t, x);
} else {
cout << st.get(s - 1, t) << endl;
}
}
return 0;
}
|
replace
| 820 | 821 | 820 | 821 |
0
| |
p02351
|
C++
|
Runtime Error
|
// #pragma GCC optimize ("-O3")
#include <bits/stdc++.h>
using namespace std;
//@起動時
struct initon {
initon() {
cin.tie(0);
ios::sync_with_stdio(false);
cout.setf(ios::fixed);
cout.precision(16);
srand((unsigned)clock() + (unsigned)time(NULL));
};
} __initon;
// 衝突対策
#define ws ___ws
struct T {
int f, s, t;
T() { f = -1, s = -1, t = -1; }
T(int f, int s, int t) : f(f), s(s), t(t) {}
bool operator<(const T &r) const {
return f != r.f ? f < r.f : s != r.s ? s < r.s : t < r.t;
// return f != r.f ? f > r.f : s != r.s ? s > r.s : t > r.t; 大きい順
}
bool operator>(const T &r) const {
return f != r.f ? f > r.f : s != r.s ? s > r.s : t > r.t;
// return f != r.f ? f > r.f : s != r.s ? s > r.s : t > r.t; 小さい順
}
bool operator==(const T &r) const { return f == r.f && s == r.s && t == r.t; }
bool operator!=(const T &r) const { return f != r.f || s != r.s || t != r.t; }
int operator[](int i) {
assert(i < 3);
return i == 0 ? f : i == 1 ? s : t;
}
};
#define int long long
#define ll long long
#define double long double
#define ull unsigned long long
using dou = double;
using itn = int;
using str = string;
using bo = bool;
#define au auto
using P = pair<ll, ll>;
#define fi first
#define se second
#define vec vector
#define beg begin
#define rbeg rbegin
#define con continue
#define bre break
#define brk break
#define is ==
// マクロ省略系 コンテナ
using vi = vector<int>;
#define _overloadvvi(_1, _2, _3, _4, name, ...) name
#define vvi0() vec<vi>
#define vvi1(a) vec<vi> a
#define vvi2(a, b) vec<vi> a(b)
#define vvi3(a, b, c) vec<vi> a(b, vi(c))
#define vvi4(a, b, c, d) vec<vi> a(b, vi(c, d))
#define vvi(...) \
_overloadvvi(__VA_ARGS__, vvi4, vvi3, vvi2, vvi1, vvi0)(__VA_ARGS__)
using vl = vector<ll>;
#define _overloadvvl(_1, _2, _3, _4, name, ...) name
#define vvl1(a) vec<vl> a
#define vvl2(a, b) vec<vl> a(b)
#define vvl3(a, b, c) vec<vl> a(b, vl(c))
#define vvl4(a, b, c, d) vec<vl> a(b, vl(c, d))
#define vvl(...) _overloadvvl(__VA_ARGS__, vvl4, vvl3, vvl2, vvl1)(__VA_ARGS__)
using vb = vector<bool>;
#define _overloadvvb(_1, _2, _3, _4, name, ...) name
#define vvb1(a) vec<vb> a
#define vvb2(a, b) vec<vb> a(b)
#define vvb3(a, b, c) vec<vb> a(b, vb(c))
#define vvb4(a, b, c, d) vec<vb> a(b, vb(c, d))
#define vvb(...) _overloadvvb(__VA_ARGS__, vvb4, vvb3, vvb2, vvb1)(__VA_ARGS__)
using vs = vector<string>;
#define _overloadvvs(_1, _2, _3, _4, name, ...) name
#define vvs1(a) vec<vs> a
#define vvs2(a, b) vec<vs> a(b)
#define vvs3(a, b, c) vec<vs> a(b, vs(c))
#define vvs4(a, b, c, d) vec<vs> a(b, vs(c, d))
#define vvs(...) _overloadvvs(__VA_ARGS__, vvs4, vvs3, vvs2, vvs1)(__VA_ARGS__)
using vd = vector<double>;
#define _overloadvvd(_1, _2, _3, _4, name, ...) name
#define vvd1(a) vec<vd> a
#define vvd2(a, b) vec<vd> a(b)
#define vvd3(a, b, c) vec<vd> a(b, vd(c))
#define vvd4(a, b, c, d) vec<vd> a(b, vd(c, d))
#define vvd(...) _overloadvvd(__VA_ARGS__, vvd4, vvd3, vvd2, vvd1)(__VA_ARGS__)
using vc = vector<char>;
#define _overloadvvc(_1, _2, _3, _4, name, ...) name
#define vvc1(a) vec<vc> a
#define vvc2(a, b) vec<vc> a(b)
#define vvc3(a, b, c) vec<vc> a(b, vc(c))
#define vvc4(a, b, c, d) vec<vc> a(b, vc(c, d))
#define vvc(...) _overloadvvc(__VA_ARGS__, vvc4, vvc3, vvc2, vvc1)(__VA_ARGS__)
using vp = vector<P>;
#define _overloadvvp(_1, _2, _3, _4, name, ...) name
#define vvp1(a) vec<vp> a
#define vvp2(a, b) vec<vp> a(b)
#define vvp3(a, b, c) vec<vp> a(b, vp(c))
#define vvp4(a, b, c, d) vec<vp> a(b, vp(c, d))
using vt = vector<T>;
#define _overloadvvt(_1, _2, _3, _4, name, ...) name
#define vvt1(a) vec<vt> a
#define vvt2(a, b) vec<vt> a(b)
#define vvt3(a, b, c) vec<vt> a(b, vt(c))
#define vvt4(a, b, c, d) vec<vt> a(b, vt(c, d))
#define v3i(a, b, c, d) vector<vector<vi>> a(b, vector<vi>(c, vi(d)))
#define v3d(a, b, c, d) vector<vector<vd>> a(b, vector<vd>(c, vd(d)))
#define v3m(a, b, c, d) vector<vector<vm>> a(b, vector<vm>(c, vm(d)))
#define _vvi vector<vi>
#define _vvl vector<vl>
#define _vvb vector<vb>
#define _vvs vector<vs>
#define _vvd vector<vd>
#define _vvc vector<vc>
#define _vvp vector<vp>
#define PQ priority_queue<ll, vector<ll>, greater<ll>>
#define tos to_string
using mapi = map<int, int>;
using mapd = map<dou, int>;
using mapc = map<char, int>;
using maps = map<str, int>;
using seti = set<int>;
using setd = set<dou>;
using setc = set<char>;
using sets = set<str>;
using qui = queue<int>;
#define bset bitset
#define uset unordered_set
#define mset multiset
#define umap unordered_map
#define umapi unordered_map<int, int>
#define umapp unordered_map<P, int>
#define mmap multimap
// マクロ 繰り返し
#define _overloadrep(_1, _2, _3, _4, name, ...) name
#define _rep(i, n) for (int i = 0, _lim = n; i < _lim; i++)
#define repi(i, m, n) for (int i = m, _lim = n; i < _lim; i++)
#define repadd(i, m, n, ad) for (int i = m, _lim = n; i < _lim; i += ad)
#define rep(...) _overloadrep(__VA_ARGS__, repadd, repi, _rep, )(__VA_ARGS__)
#define _rer(i, n) for (int i = n; i >= 0; i--)
#define reri(i, m, n) for (int i = m, _lim = n; i >= _lim; i--)
#define rerdec(i, m, n, dec) for (int i = m, _lim = n; i >= _lim; i -= dec)
#define rer(...) _overloadrep(__VA_ARGS__, rerdec, reri, _rer, )(__VA_ARGS__)
#define fora(a, b) for (auto &&a : b)
#define forg(gi, ve) \
for (int gi = 0, f, t, c; \
gi < ve.size() && (f = ve[gi].f, t = ve[gi].t, c = ve[gi].c, true); \
gi++)
#define fort(gi, ve) \
for (int gi = 0, f, t, c; \
gi < ve.size() && (f = ve[gi].f, t = ve[gi].t, c = ve[gi].c, true); \
gi++) \
if (t != p)
// #define fort(gi, ve) for (int gi = 0, f, t, c;gi<ve.size()&& (gi+=
// (ve[gi].t==p))< ve.size() && (f = ve[gi].f,t=ve[gi].t, c = ve[gi].c,true);
// gi++)
// マクロ 定数
#define k3 1010
#define k4 10101
#define k5 101010
#define k6 1010101
#define k7 10101010
const int inf = (int)1e9 + 100;
const ll linf = (ll)1e18 + 100;
const double eps = 1e-9;
const double PI = 3.1415926535897932384626433832795029L;
ll ma = numeric_limits<ll>::min();
ll mi = numeric_limits<ll>::max();
const int y4[] = {-1, 1, 0, 0};
const int x4[] = {0, 0, -1, 1};
const int y8[] = {0, 1, 0, -1, -1, 1, 1, -1};
const int x8[] = {1, 0, -1, 0, 1, -1, 1, -1};
// マクロ省略形 関数等
#define arsz(a) (sizeof(a) / sizeof(a[0]))
#define sz(a) ((int)(a).size())
#define rs resize
#define mp make_pair
#define pb push_back
#define pf push_front
#define eb emplace_back
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
inline void sort(string &a) { sort(a.begin(), a.end()); }
template <class T> inline void sort(vector<T> &a) { sort(a.begin(), a.end()); };
template <class T> inline void sort(vector<T> &a, int len) {
sort(a.begin(), a.begin() + len);
};
template <class T, class F> inline void sort(vector<T> &a, F f) {
sort(a.begin(), a.end(), [&](T l, T r) { return f(l) < f(r); });
};
enum ___pcomparator { fisi, fisd, fdsi, fdsd, sifi, sifd, sdfi, sdfd };
inline void sort(vector<P> &a, ___pcomparator type) {
switch (type) {
case fisi:
sort(all(a),
[&](P l, P r) { return l.fi != r.fi ? l.fi < r.fi : l.se < r.se; });
break;
case fisd:
sort(all(a),
[&](P l, P r) { return l.fi != r.fi ? l.fi < r.fi : l.se > r.se; });
break;
case fdsi:
sort(all(a),
[&](P l, P r) { return l.fi != r.fi ? l.fi > r.fi : l.se < r.se; });
break;
case fdsd:
sort(all(a),
[&](P l, P r) { return l.fi != r.fi ? l.fi > r.fi : l.se > r.se; });
break;
case sifi:
sort(all(a),
[&](P l, P r) { return l.se != r.se ? l.se < r.se : l.fi < r.fi; });
break;
case sifd:
sort(all(a),
[&](P l, P r) { return l.se != r.se ? l.se < r.se : l.fi > r.fi; });
break;
case sdfi:
sort(all(a),
[&](P l, P r) { return l.se != r.se ? l.se > r.se : l.fi < r.fi; });
break;
case sdfd:
sort(all(a),
[&](P l, P r) { return l.se != r.se ? l.se > r.se : l.fi > r.fi; });
break;
}
};
inline void sort(vector<T> &a, ___pcomparator type) {
switch (type) {
case fisi:
sort(all(a), [&](T l, T r) { return l.f != r.f ? l.f < r.f : l.s < r.s; });
break;
case fisd:
sort(all(a), [&](T l, T r) { return l.f != r.f ? l.f < r.f : l.s > r.s; });
break;
case fdsi:
sort(all(a), [&](T l, T r) { return l.f != r.f ? l.f > r.f : l.s < r.s; });
break;
case fdsd:
sort(all(a), [&](T l, T r) { return l.f != r.f ? l.f > r.f : l.s > r.s; });
break;
case sifi:
sort(all(a), [&](T l, T r) { return l.s != r.s ? l.s < r.s : l.f < r.f; });
break;
case sifd:
sort(all(a), [&](T l, T r) { return l.s != r.s ? l.s < r.s : l.f > r.f; });
break;
case sdfi:
sort(all(a), [&](T l, T r) { return l.s != r.s ? l.s > r.s : l.f < r.f; });
break;
case sdfd:
sort(all(a), [&](T l, T r) { return l.s != r.s ? l.s > r.s : l.f > r.f; });
break;
}
};
template <class T> inline void rsort(vector<T> &a) {
sort(a.begin(), a.end(), greater<T>());
};
template <class T> inline void rsort(vector<T> &a, int len) {
sort(a.begin(), a.begin() + len, greater<T>());
};
template <class U, class F> inline void rsort(vector<U> &a, F f) {
sort(a.begin(), a.end(), [&](U l, U r) { return f(l) > f(r); });
};
template <class U> inline void sortp(vector<U> &a, vector<U> &b) {
vp c;
int n = sz(a);
assert(n == sz(b));
rep(i, n) c.eb(a[i], b[i]);
sort(c);
rep(i, n) {
a[i] = c[i].first;
b[i] = c[i].second;
;
}
};
// F = T<T>
// 例えばreturn p.fi + p.se;
template <class U, class F> inline void sortp(vector<U> &a, vector<U> &b, F f) {
vp c;
int n = sz(a);
assert(n == sz(b));
rep(i, n) c.eb(a[i], b[i]);
sort(c, f);
rep(i, n) {
a[i] = c[i].first;
b[i] = c[i].second;
}
};
template <class U, class F>
inline void sortp(vector<U> &a, vector<U> &b, char type) {
vp c;
int n = sz(a);
assert(n == sz(b));
rep(i, n) c.eb(a[i], b[i]);
sort(c, type);
rep(i, n) {
a[i] = c[i].first;
b[i] = c[i].second;
}
};
template <class U> inline void rsortp(vector<U> &a, vector<U> &b) {
vp c;
int n = sz(a);
assert(n == sz(b));
rep(i, n) c.eb(a[i], b[i]);
rsort(c);
rep(i, n) {
a[i] = c[i].first;
b[i] = c[i].second;
}
};
template <class U, class F>
inline void rsortp(vector<U> &a, vector<U> &b, F f) {
vp c;
int n = sz(a);
assert(n == sz(b));
rep(i, n) c.eb(a[i], b[i]);
rsort(c, f);
rep(i, n) {
a[i] = c[i].first;
b[i] = c[i].second;
}
};
template <class U> inline void sortt(vector<U> &a, vector<U> &b, vector<U> &c) {
vt r;
int n = sz(a);
assert(n == sz(b));
assert(n == sz(c));
rep(i, n) r.eb(a[i], b[i], c[i]);
sort(r);
rep(i, n) {
a[i] = r[i].f;
b[i] = r[i].s;
c[i] = r[i].t;
}
};
template <class U, class F>
inline void sortt(vector<U> &a, vector<U> &b, vector<U> &c, F f) {
vt r;
int n = sz(a);
assert(n == sz(b));
assert(n == sz(c));
rep(i, n) r.eb(a[i], b[i], c[i]);
sort(r, f);
rep(i, n) {
a[i] = r[i].f;
b[i] = r[i].s;
c[i] = r[i].t;
}
};
template <class U, class F>
inline void rsortt(vector<U> &a, vector<U> &b, vector<U> &c, F f) {
vt r;
int n = sz(a);
assert(n == sz(b));
assert(n == sz(c));
rep(i, n) r.eb(a[i], b[i], c[i]);
rsort(r, f);
rep(i, n) {
a[i] = r[i].f;
b[i] = r[i].s;
c[i] = r[i].t;
}
};
template <class T> inline void sort2(vector<vector<T>> &a) {
for (int i = 0, n = a.size(); i < n; i++)
sort(a[i]);
}
template <class T> inline void rsort2(vector<vector<T>> &a) {
for (int i = 0, n = a.size(); i < n; i++)
rsort(a[i]);
}
template <typename A, size_t N, typename T> void fill(A (&a)[N], const T &v) {
rep(i, N) a[i] = v;
}
template <typename A, size_t N, size_t O, typename T>
void fill(A (&a)[N][O], const T &v) {
rep(i, N) rep(j, O) a[i][j] = v;
}
template <typename A, size_t N, size_t O, size_t P, typename T>
void fill(A (&a)[N][O][P], const T &v) {
rep(i, N) rep(j, O) rep(k, P) a[i][j][k] = v;
}
template <typename A, size_t N, size_t O, size_t P, size_t Q, typename T>
void fill(A (&a)[N][O][P][Q], const T &v) {
rep(i, N) rep(j, O) rep(k, P) rep(l, Q) a[i][j][k][l] = v;
}
template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R,
typename T>
void fill(A (&a)[N][O][P][Q][R], const T &v) {
rep(i, N) rep(j, O) rep(k, P) rep(l, Q) rep(m, R) a[i][j][k][l][m] = v;
}
template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R,
size_t S, typename T>
void fill(A (&a)[N][O][P][Q][R][S], const T &v) {
rep(i, N) rep(j, O) rep(k, P) rep(l, Q) rep(m, R) rep(n, S)
a[i][j][k][l][m][n] = v;
}
template <typename V, typename T> void fill(V &xx, const T vall) { xx = vall; }
template <typename V, typename T> void fill(vector<V> &vecc, const T vall) {
for (auto &&vx : vecc)
fill(vx, vall);
}
//@汎用便利関数 入力
template <typename T = int> T _in() {
T x;
cin >> x;
return (x);
}
#define _overloadin(_1, _2, _3, _4, name, ...) name
#define in0() _in()
#define in1(a) cin >> a
#define in2(a, b) cin >> a >> b
#define in3(a, b, c) cin >> a >> b >> c
#define in4(a, b, c, d) cin >> a >> b >> c >> d
#define in(...) _overloadin(__VA_ARGS__, in4, in3, in2, in1, in0)(__VA_ARGS__)
#define _overloaddin(_1, _2, _3, _4, name, ...) name
#define din1(a) \
int a; \
cin >> a
#define din2(a, b) \
int a, b; \
cin >> a >> b
#define din3(a, b, c) \
int a, b, c; \
cin >> a >> b >> c
#define din4(a, b, c, d) \
int a, b, c, d; \
cin >> a >> b >> c >> d
#define din(...) _overloadin(__VA_ARGS__, din4, din3, din2, din1)(__VA_ARGS__)
#define _overloaddind(_1, _2, _3, _4, name, ...) name
#define din1d(a) \
int a; \
cin >> a; \
a--
#define din2d(a, b) \
int a, b; \
cin >> a >> b; \
a--, b--
#define din3d(a, b, c) \
int a, b, c; \
cin >> a >> b >> c; \
a--, b--, c--
#define din4d(a, b, c, d) \
int a, b, c, d; \
cin >> a >> b >> c >> d; \
; \
a--, b--, c--, d--
#define dind(...) \
_overloaddind(__VA_ARGS__, din4d, din3d, din2d, din1d)(__VA_ARGS__)
string sin() { return _in<string>(); }
ll lin() { return _in<ll>(); }
#define na(a, n) \
a.resize(n); \
rep(i, n) cin >> a[i];
#define nao(a, n) \
a.resize(n + 1); \
rep(i, n) cin >> a[i + 1];
#define nad(a, n) \
a.resize(n); \
rep(i, n) { \
cin >> a[i]; \
a[i]--; \
}
#define na2(a, b, n) \
a.resize(n), b.resize(n); \
rep(i, n) cin >> a[i] >> b[i];
#define na2d(a, b, n) \
a.resize(n), b.resize(n); \
rep(i, n) { \
cin >> a[i] >> b[i]; \
a[i]--, b[i]--; \
}
#define na3(a, b, c, n) \
a.resize(n), b.resize(n), c.resize(n); \
rep(i, n) cin >> a[i] >> b[i] >> c[i];
#define na3d(a, b, c, n) \
a.resize(n), b.resize(n), c.resize(n); \
rep(i, n) { \
cin >> a[i] >> b[i] >> c[i]; \
a[i]--, b[i]--, c[i]--; \
}
#define nt(a, h, w) \
resize(a, h, w); \
rep(hi, h) rep(wi, w) cin >> a[hi][wi];
#define ntd(a, h, w) \
rs(a, h, w); \
rep(hi, h) rep(wi, w) cin >> a[hi][wi], a[hi][wi]--;
#define ntp(a, h, w) \
fill(a, '#'); \
rep(hi, 1, h + 1) rep(wi, 1, w + 1) cin >> a[hi][wi];
// デバッグ
#define sp << " " <<
#define debugName(VariableName) #VariableName
#define _deb1(x) cerr << debugName(x) << " = " << x << endl
#define _deb2(x, y) \
cerr << debugName(x) << " = " << x << ", " << debugName(y) << " = " << y \
<< endl
#define _deb3(x, y, z) \
cerr << debugName(x) << " = " << x << ", " << debugName(y) << " = " << y \
<< ", " debugName(z) << " = " << z << endl
#define _deb4(x, y, z, a) \
cerr << debugName(x) << " = " << x << ", " << debugName(y) << " = " << y \
<< ", " << debugName(z) << " = " << z << ", " << debugName(a) << " = " \
<< a << endl
#define _deb5(x, y, z, a, b) \
cerr << debugName(x) << " = " << x << ", " << debugName(y) << " = " << y \
<< ", " << debugName(z) << " = " << z << ", " << debugName(a) << " = " \
<< a << ", " << debugName(b) << " = " << b << endl
#define _overloadebug(_1, _2, _3, _4, _5, name, ...) name
#define debug(...) \
_overloadebug(__VA_ARGS__, _deb5, _deb4, _deb3, _deb2, _deb1)(__VA_ARGS__)
#define deb(...) \
_overloadebug(__VA_ARGS__, _deb5, _deb4, _deb3, _deb2, _deb1)(__VA_ARGS__)
#define debugline(x) \
cerr << x << " " \
<< "(L:" << __LINE__ << ")" << '\n'
void ole() {
#ifdef _DEBUG
debugline("ole");
exit(0);
#endif
string a = "a";
rep(i, 30) a += a;
rep(i, 1 << 17) cout << a << endl;
cout << "OLE 出力長制限超過" << endl;
exit(0);
}
void tle() {
while (inf)
cout << inf << endl;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll gcd(vi b) {
ll res = b[0];
for (auto &&v : b)
res = gcd(v, res);
return res;
}
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
ll rev(ll a) {
ll res = 0;
while (a) {
res *= 10;
res += a % 10;
a /= 10;
}
return res;
}
template <class T> void rev(vector<T> &a) { reverse(all(a)); }
void rev(string &a) { reverse(all(a)); }
ll ceil(ll a, ll b) {
if (b == 0) {
debugline("ceil");
deb(a, b);
ole();
return -1;
} else
return (a + b - 1) / b;
}
ll sqrt(ll a) {
if (a < 0) {
debugline("sqrt");
deb(a);
ole();
}
ll res = (ll)std::sqrt(a);
while (res * res < a)
res++;
return res;
}
double log(double e, double x) { return log(x) / log(e); }
ll sig(ll t) { return (1 + t) * t / 2; }
ll sig(ll s, ll t) { return (s + t) * (t - s + 1) / 2; }
vi divisors(int v) {
vi res;
double lim = std::sqrt(v);
for (int i = 1; i <= lim; ++i) {
if (v % i == 0) {
res.pb(i);
if (i != v / i)
res.pb(v / i);
}
}
return res;
}
vb isPrime;
vi primes;
void setPrime() {
int len = 4010101;
isPrime.resize(4010101);
fill(isPrime, true);
isPrime[0] = isPrime[1] = false;
for (int i = 2; i <= sqrt(len) + 5; ++i) {
if (!isPrime[i])
continue;
for (int j = 2; i * j < len; ++j) {
isPrime[i * j] = false;
}
}
rep(i, len) if (isPrime[i]) primes.pb(i);
}
vi factorization(int v) {
int tv = v;
vi res;
if (isPrime.size() == 0)
setPrime();
for (auto &&p : primes) {
if (v % p == 0)
res.push_back(p);
while (v % p == 0) {
v /= p;
}
if (v == 1 || p * p > tv)
break;
}
if (v > 1)
res.pb(v);
return res;
}
inline bool inside(int h, int w, int H, int W) {
return h >= 0 && w >= 0 && h < H && w < W;
}
inline bool inside(int v, int l, int r) { return l <= v && v < r; }
#define ins inside
ll u(ll a) { return a < 0 ? 0 : a; }
template <class T> vector<T> u(const vector<T> &a) {
vector<T> ret = a;
fora(v, ret) v = u(v);
return ret;
}
#define MIN(a) numeric_limits<a>::min()
#define MAX(a) numeric_limits<a>::max()
int n, m, k, d, H, W, x, y, z, q;
int cou;
vi a, b, c;
vvi(s, 0, 0);
vvc(ba, 0, 0);
vp p;
#define segmin(M) \
[](M a, M b) { return a < b ? a : b; }, [](int len, M x) { return x; }, MAX(M)
#define segmax(M) \
[](M a, M b) { return a > b ? a : b; }, [](int len, M x) { return x; }, MIN(M)
#define segsum(M) \
[](M a, M b) { return a + b; }, [](int len, M x) { return len * x; }, 0
#define segxor(M) \
[](M a, M b) { return a ^ b; }, \
[](int len, M x) { return (len & 1) ? x : 0; }, 0
// 作用素同士の演算は足し算でいいと仮定している
template <class M> struct SegmentLazyAdd {
using func = function<M(M, M)>;
using feval = function<M(
int,
M /*作用素,Mとは違う型を使うのを横着している*/)>; // 区間の値に加算される値
int n;
vector<M> seg;
vi lazy; // 全体に足された数を持つ 3333なら3
const func f; // M同士の演算結果を返す
const feval getAdd; // len,xを受け取り、segに加える数を返す
const M e;
vi arch; // 遅延評価を行うやつ
vi lens;
SegmentLazyAdd(vi a, func f, feval fe, M e) : f(f), getAdd(fe), e(e) {
n = 1;
// rep(i, 100) a.pb(e);
int asz = a.size();
while (n < asz)
n <<= 1;
seg.resize(2 * n - 1, e);
lazy.resize(2 * n - 1, 0);
lens.resize(2 * n - 1);
rep(i, asz) seg[i + n - 1] = a[i];
rer(i, n - 2) seg[i] = f(seg[(i << 1) + 1], seg[(i << 1) + 2]);
int l = 1, v = n;
rep(i, 1, n << 1) {
if ((l << 1) == i) {
l <<= 1;
v >>= 1;
}
lens[i - 1] = v;
}
}
void eval() {
rer(j, sz(arch) - 1) {
int i = arch[j];
// iは1index
// lazyは0index
int v = lazy[i - 1];
if (!v)
continue;
lazy[i * 2 - 1] += v;
lazy[i * 2] += v;
seg[i * 2 - 1] += getAdd(lens[i * 2 - 1], v); //
seg[i * 2] += getAdd(lens[i * 2], v);
lazy[i - 1] = 0;
}
}
void gindex(int l, int r) {
arch.clear();
l += n;
r += n;
int lm = (l / (l & -l)) >> 1;
int rm = (r / (r & -r)) >> 1;
while (l < r) {
if (r <= rm)
arch.pb(r);
if (l <= lm)
arch.pb(l);
l >>= 1;
r >>= 1;
}
while (l) {
arch.pb(l);
l >>= 1;
}
}
void add(int l, int r, int x) {
int L = l + n;
int R = r + n;
while (L < R) {
if (R & 1) {
R--;
lazy[R - 1] += x;
seg[R - 1] += getAdd(lens[R - 1], x);
}
if (L & 1) {
lazy[L - 1] += x;
seg[L - 1] += getAdd(lens[L - 1], x);
L++;
}
L >>= 1;
R >>= 1;
}
gindex(l, r);
fora(i, arch) {
seg[i - 1] = f(seg[i * 2 - 1], seg[i * 2]);
seg[i - 1] += getAdd(lens[i - 1], lazy[i - 1]);
}
}
M get(int l, int r) {
gindex(l, r);
eval();
int L = l + n;
int R = r + n;
M retl = e, retr = e;
while (L < R) {
if (R & 1) {
R--;
retr = f(seg[R - 1], retr);
}
if (L & 1) {
retl = f(retl, seg[L - 1]);
L++;
}
L >>= 1;
R >>= 1;
}
return f(retl, retr);
}
#ifdef _DEBUG
void debu(int len = 10) {
rep(i, min(n, min(len, n))) {
int v = get(i, i + 1);
if (v == e) {
cerr << "e ";
} else {
cerr << v << " ";
}
}
cerr << "" << endl;
}
#else
inline void debu() { ; }
#endif
};
#define seg SegmentLazyAdd
#define raq SegmentLazyAdd
signed main() {
cin >> n >> q;
seg<int> st(vi(n), segsum(int));
rep(i, q) {
int c;
cin >> c;
int s, t;
cin >> s >> t;
if (c == 0) {
int x;
cin >> x;
st.add(s - 1, t, x);
} else {
cout << st.get(s - 1, t) << endl;
}
}
return 0;
}
|
// #pragma GCC optimize ("-O3")
#include <bits/stdc++.h>
using namespace std;
//@起動時
struct initon {
initon() {
cin.tie(0);
ios::sync_with_stdio(false);
cout.setf(ios::fixed);
cout.precision(16);
srand((unsigned)clock() + (unsigned)time(NULL));
};
} __initon;
// 衝突対策
#define ws ___ws
struct T {
int f, s, t;
T() { f = -1, s = -1, t = -1; }
T(int f, int s, int t) : f(f), s(s), t(t) {}
bool operator<(const T &r) const {
return f != r.f ? f < r.f : s != r.s ? s < r.s : t < r.t;
// return f != r.f ? f > r.f : s != r.s ? s > r.s : t > r.t; 大きい順
}
bool operator>(const T &r) const {
return f != r.f ? f > r.f : s != r.s ? s > r.s : t > r.t;
// return f != r.f ? f > r.f : s != r.s ? s > r.s : t > r.t; 小さい順
}
bool operator==(const T &r) const { return f == r.f && s == r.s && t == r.t; }
bool operator!=(const T &r) const { return f != r.f || s != r.s || t != r.t; }
int operator[](int i) {
assert(i < 3);
return i == 0 ? f : i == 1 ? s : t;
}
};
#define int long long
#define ll long long
#define double long double
#define ull unsigned long long
using dou = double;
using itn = int;
using str = string;
using bo = bool;
#define au auto
using P = pair<ll, ll>;
#define fi first
#define se second
#define vec vector
#define beg begin
#define rbeg rbegin
#define con continue
#define bre break
#define brk break
#define is ==
// マクロ省略系 コンテナ
using vi = vector<int>;
#define _overloadvvi(_1, _2, _3, _4, name, ...) name
#define vvi0() vec<vi>
#define vvi1(a) vec<vi> a
#define vvi2(a, b) vec<vi> a(b)
#define vvi3(a, b, c) vec<vi> a(b, vi(c))
#define vvi4(a, b, c, d) vec<vi> a(b, vi(c, d))
#define vvi(...) \
_overloadvvi(__VA_ARGS__, vvi4, vvi3, vvi2, vvi1, vvi0)(__VA_ARGS__)
using vl = vector<ll>;
#define _overloadvvl(_1, _2, _3, _4, name, ...) name
#define vvl1(a) vec<vl> a
#define vvl2(a, b) vec<vl> a(b)
#define vvl3(a, b, c) vec<vl> a(b, vl(c))
#define vvl4(a, b, c, d) vec<vl> a(b, vl(c, d))
#define vvl(...) _overloadvvl(__VA_ARGS__, vvl4, vvl3, vvl2, vvl1)(__VA_ARGS__)
using vb = vector<bool>;
#define _overloadvvb(_1, _2, _3, _4, name, ...) name
#define vvb1(a) vec<vb> a
#define vvb2(a, b) vec<vb> a(b)
#define vvb3(a, b, c) vec<vb> a(b, vb(c))
#define vvb4(a, b, c, d) vec<vb> a(b, vb(c, d))
#define vvb(...) _overloadvvb(__VA_ARGS__, vvb4, vvb3, vvb2, vvb1)(__VA_ARGS__)
using vs = vector<string>;
#define _overloadvvs(_1, _2, _3, _4, name, ...) name
#define vvs1(a) vec<vs> a
#define vvs2(a, b) vec<vs> a(b)
#define vvs3(a, b, c) vec<vs> a(b, vs(c))
#define vvs4(a, b, c, d) vec<vs> a(b, vs(c, d))
#define vvs(...) _overloadvvs(__VA_ARGS__, vvs4, vvs3, vvs2, vvs1)(__VA_ARGS__)
using vd = vector<double>;
#define _overloadvvd(_1, _2, _3, _4, name, ...) name
#define vvd1(a) vec<vd> a
#define vvd2(a, b) vec<vd> a(b)
#define vvd3(a, b, c) vec<vd> a(b, vd(c))
#define vvd4(a, b, c, d) vec<vd> a(b, vd(c, d))
#define vvd(...) _overloadvvd(__VA_ARGS__, vvd4, vvd3, vvd2, vvd1)(__VA_ARGS__)
using vc = vector<char>;
#define _overloadvvc(_1, _2, _3, _4, name, ...) name
#define vvc1(a) vec<vc> a
#define vvc2(a, b) vec<vc> a(b)
#define vvc3(a, b, c) vec<vc> a(b, vc(c))
#define vvc4(a, b, c, d) vec<vc> a(b, vc(c, d))
#define vvc(...) _overloadvvc(__VA_ARGS__, vvc4, vvc3, vvc2, vvc1)(__VA_ARGS__)
using vp = vector<P>;
#define _overloadvvp(_1, _2, _3, _4, name, ...) name
#define vvp1(a) vec<vp> a
#define vvp2(a, b) vec<vp> a(b)
#define vvp3(a, b, c) vec<vp> a(b, vp(c))
#define vvp4(a, b, c, d) vec<vp> a(b, vp(c, d))
using vt = vector<T>;
#define _overloadvvt(_1, _2, _3, _4, name, ...) name
#define vvt1(a) vec<vt> a
#define vvt2(a, b) vec<vt> a(b)
#define vvt3(a, b, c) vec<vt> a(b, vt(c))
#define vvt4(a, b, c, d) vec<vt> a(b, vt(c, d))
#define v3i(a, b, c, d) vector<vector<vi>> a(b, vector<vi>(c, vi(d)))
#define v3d(a, b, c, d) vector<vector<vd>> a(b, vector<vd>(c, vd(d)))
#define v3m(a, b, c, d) vector<vector<vm>> a(b, vector<vm>(c, vm(d)))
#define _vvi vector<vi>
#define _vvl vector<vl>
#define _vvb vector<vb>
#define _vvs vector<vs>
#define _vvd vector<vd>
#define _vvc vector<vc>
#define _vvp vector<vp>
#define PQ priority_queue<ll, vector<ll>, greater<ll>>
#define tos to_string
using mapi = map<int, int>;
using mapd = map<dou, int>;
using mapc = map<char, int>;
using maps = map<str, int>;
using seti = set<int>;
using setd = set<dou>;
using setc = set<char>;
using sets = set<str>;
using qui = queue<int>;
#define bset bitset
#define uset unordered_set
#define mset multiset
#define umap unordered_map
#define umapi unordered_map<int, int>
#define umapp unordered_map<P, int>
#define mmap multimap
// マクロ 繰り返し
#define _overloadrep(_1, _2, _3, _4, name, ...) name
#define _rep(i, n) for (int i = 0, _lim = n; i < _lim; i++)
#define repi(i, m, n) for (int i = m, _lim = n; i < _lim; i++)
#define repadd(i, m, n, ad) for (int i = m, _lim = n; i < _lim; i += ad)
#define rep(...) _overloadrep(__VA_ARGS__, repadd, repi, _rep, )(__VA_ARGS__)
#define _rer(i, n) for (int i = n; i >= 0; i--)
#define reri(i, m, n) for (int i = m, _lim = n; i >= _lim; i--)
#define rerdec(i, m, n, dec) for (int i = m, _lim = n; i >= _lim; i -= dec)
#define rer(...) _overloadrep(__VA_ARGS__, rerdec, reri, _rer, )(__VA_ARGS__)
#define fora(a, b) for (auto &&a : b)
#define forg(gi, ve) \
for (int gi = 0, f, t, c; \
gi < ve.size() && (f = ve[gi].f, t = ve[gi].t, c = ve[gi].c, true); \
gi++)
#define fort(gi, ve) \
for (int gi = 0, f, t, c; \
gi < ve.size() && (f = ve[gi].f, t = ve[gi].t, c = ve[gi].c, true); \
gi++) \
if (t != p)
// #define fort(gi, ve) for (int gi = 0, f, t, c;gi<ve.size()&& (gi+=
// (ve[gi].t==p))< ve.size() && (f = ve[gi].f,t=ve[gi].t, c = ve[gi].c,true);
// gi++)
// マクロ 定数
#define k3 1010
#define k4 10101
#define k5 101010
#define k6 1010101
#define k7 10101010
const int inf = (int)1e9 + 100;
const ll linf = (ll)1e18 + 100;
const double eps = 1e-9;
const double PI = 3.1415926535897932384626433832795029L;
ll ma = numeric_limits<ll>::min();
ll mi = numeric_limits<ll>::max();
const int y4[] = {-1, 1, 0, 0};
const int x4[] = {0, 0, -1, 1};
const int y8[] = {0, 1, 0, -1, -1, 1, 1, -1};
const int x8[] = {1, 0, -1, 0, 1, -1, 1, -1};
// マクロ省略形 関数等
#define arsz(a) (sizeof(a) / sizeof(a[0]))
#define sz(a) ((int)(a).size())
#define rs resize
#define mp make_pair
#define pb push_back
#define pf push_front
#define eb emplace_back
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
inline void sort(string &a) { sort(a.begin(), a.end()); }
template <class T> inline void sort(vector<T> &a) { sort(a.begin(), a.end()); };
template <class T> inline void sort(vector<T> &a, int len) {
sort(a.begin(), a.begin() + len);
};
template <class T, class F> inline void sort(vector<T> &a, F f) {
sort(a.begin(), a.end(), [&](T l, T r) { return f(l) < f(r); });
};
enum ___pcomparator { fisi, fisd, fdsi, fdsd, sifi, sifd, sdfi, sdfd };
inline void sort(vector<P> &a, ___pcomparator type) {
switch (type) {
case fisi:
sort(all(a),
[&](P l, P r) { return l.fi != r.fi ? l.fi < r.fi : l.se < r.se; });
break;
case fisd:
sort(all(a),
[&](P l, P r) { return l.fi != r.fi ? l.fi < r.fi : l.se > r.se; });
break;
case fdsi:
sort(all(a),
[&](P l, P r) { return l.fi != r.fi ? l.fi > r.fi : l.se < r.se; });
break;
case fdsd:
sort(all(a),
[&](P l, P r) { return l.fi != r.fi ? l.fi > r.fi : l.se > r.se; });
break;
case sifi:
sort(all(a),
[&](P l, P r) { return l.se != r.se ? l.se < r.se : l.fi < r.fi; });
break;
case sifd:
sort(all(a),
[&](P l, P r) { return l.se != r.se ? l.se < r.se : l.fi > r.fi; });
break;
case sdfi:
sort(all(a),
[&](P l, P r) { return l.se != r.se ? l.se > r.se : l.fi < r.fi; });
break;
case sdfd:
sort(all(a),
[&](P l, P r) { return l.se != r.se ? l.se > r.se : l.fi > r.fi; });
break;
}
};
inline void sort(vector<T> &a, ___pcomparator type) {
switch (type) {
case fisi:
sort(all(a), [&](T l, T r) { return l.f != r.f ? l.f < r.f : l.s < r.s; });
break;
case fisd:
sort(all(a), [&](T l, T r) { return l.f != r.f ? l.f < r.f : l.s > r.s; });
break;
case fdsi:
sort(all(a), [&](T l, T r) { return l.f != r.f ? l.f > r.f : l.s < r.s; });
break;
case fdsd:
sort(all(a), [&](T l, T r) { return l.f != r.f ? l.f > r.f : l.s > r.s; });
break;
case sifi:
sort(all(a), [&](T l, T r) { return l.s != r.s ? l.s < r.s : l.f < r.f; });
break;
case sifd:
sort(all(a), [&](T l, T r) { return l.s != r.s ? l.s < r.s : l.f > r.f; });
break;
case sdfi:
sort(all(a), [&](T l, T r) { return l.s != r.s ? l.s > r.s : l.f < r.f; });
break;
case sdfd:
sort(all(a), [&](T l, T r) { return l.s != r.s ? l.s > r.s : l.f > r.f; });
break;
}
};
template <class T> inline void rsort(vector<T> &a) {
sort(a.begin(), a.end(), greater<T>());
};
template <class T> inline void rsort(vector<T> &a, int len) {
sort(a.begin(), a.begin() + len, greater<T>());
};
template <class U, class F> inline void rsort(vector<U> &a, F f) {
sort(a.begin(), a.end(), [&](U l, U r) { return f(l) > f(r); });
};
template <class U> inline void sortp(vector<U> &a, vector<U> &b) {
vp c;
int n = sz(a);
assert(n == sz(b));
rep(i, n) c.eb(a[i], b[i]);
sort(c);
rep(i, n) {
a[i] = c[i].first;
b[i] = c[i].second;
;
}
};
// F = T<T>
// 例えばreturn p.fi + p.se;
template <class U, class F> inline void sortp(vector<U> &a, vector<U> &b, F f) {
vp c;
int n = sz(a);
assert(n == sz(b));
rep(i, n) c.eb(a[i], b[i]);
sort(c, f);
rep(i, n) {
a[i] = c[i].first;
b[i] = c[i].second;
}
};
template <class U, class F>
inline void sortp(vector<U> &a, vector<U> &b, char type) {
vp c;
int n = sz(a);
assert(n == sz(b));
rep(i, n) c.eb(a[i], b[i]);
sort(c, type);
rep(i, n) {
a[i] = c[i].first;
b[i] = c[i].second;
}
};
template <class U> inline void rsortp(vector<U> &a, vector<U> &b) {
vp c;
int n = sz(a);
assert(n == sz(b));
rep(i, n) c.eb(a[i], b[i]);
rsort(c);
rep(i, n) {
a[i] = c[i].first;
b[i] = c[i].second;
}
};
template <class U, class F>
inline void rsortp(vector<U> &a, vector<U> &b, F f) {
vp c;
int n = sz(a);
assert(n == sz(b));
rep(i, n) c.eb(a[i], b[i]);
rsort(c, f);
rep(i, n) {
a[i] = c[i].first;
b[i] = c[i].second;
}
};
template <class U> inline void sortt(vector<U> &a, vector<U> &b, vector<U> &c) {
vt r;
int n = sz(a);
assert(n == sz(b));
assert(n == sz(c));
rep(i, n) r.eb(a[i], b[i], c[i]);
sort(r);
rep(i, n) {
a[i] = r[i].f;
b[i] = r[i].s;
c[i] = r[i].t;
}
};
template <class U, class F>
inline void sortt(vector<U> &a, vector<U> &b, vector<U> &c, F f) {
vt r;
int n = sz(a);
assert(n == sz(b));
assert(n == sz(c));
rep(i, n) r.eb(a[i], b[i], c[i]);
sort(r, f);
rep(i, n) {
a[i] = r[i].f;
b[i] = r[i].s;
c[i] = r[i].t;
}
};
template <class U, class F>
inline void rsortt(vector<U> &a, vector<U> &b, vector<U> &c, F f) {
vt r;
int n = sz(a);
assert(n == sz(b));
assert(n == sz(c));
rep(i, n) r.eb(a[i], b[i], c[i]);
rsort(r, f);
rep(i, n) {
a[i] = r[i].f;
b[i] = r[i].s;
c[i] = r[i].t;
}
};
template <class T> inline void sort2(vector<vector<T>> &a) {
for (int i = 0, n = a.size(); i < n; i++)
sort(a[i]);
}
template <class T> inline void rsort2(vector<vector<T>> &a) {
for (int i = 0, n = a.size(); i < n; i++)
rsort(a[i]);
}
template <typename A, size_t N, typename T> void fill(A (&a)[N], const T &v) {
rep(i, N) a[i] = v;
}
template <typename A, size_t N, size_t O, typename T>
void fill(A (&a)[N][O], const T &v) {
rep(i, N) rep(j, O) a[i][j] = v;
}
template <typename A, size_t N, size_t O, size_t P, typename T>
void fill(A (&a)[N][O][P], const T &v) {
rep(i, N) rep(j, O) rep(k, P) a[i][j][k] = v;
}
template <typename A, size_t N, size_t O, size_t P, size_t Q, typename T>
void fill(A (&a)[N][O][P][Q], const T &v) {
rep(i, N) rep(j, O) rep(k, P) rep(l, Q) a[i][j][k][l] = v;
}
template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R,
typename T>
void fill(A (&a)[N][O][P][Q][R], const T &v) {
rep(i, N) rep(j, O) rep(k, P) rep(l, Q) rep(m, R) a[i][j][k][l][m] = v;
}
template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R,
size_t S, typename T>
void fill(A (&a)[N][O][P][Q][R][S], const T &v) {
rep(i, N) rep(j, O) rep(k, P) rep(l, Q) rep(m, R) rep(n, S)
a[i][j][k][l][m][n] = v;
}
template <typename V, typename T> void fill(V &xx, const T vall) { xx = vall; }
template <typename V, typename T> void fill(vector<V> &vecc, const T vall) {
for (auto &&vx : vecc)
fill(vx, vall);
}
//@汎用便利関数 入力
template <typename T = int> T _in() {
T x;
cin >> x;
return (x);
}
#define _overloadin(_1, _2, _3, _4, name, ...) name
#define in0() _in()
#define in1(a) cin >> a
#define in2(a, b) cin >> a >> b
#define in3(a, b, c) cin >> a >> b >> c
#define in4(a, b, c, d) cin >> a >> b >> c >> d
#define in(...) _overloadin(__VA_ARGS__, in4, in3, in2, in1, in0)(__VA_ARGS__)
#define _overloaddin(_1, _2, _3, _4, name, ...) name
#define din1(a) \
int a; \
cin >> a
#define din2(a, b) \
int a, b; \
cin >> a >> b
#define din3(a, b, c) \
int a, b, c; \
cin >> a >> b >> c
#define din4(a, b, c, d) \
int a, b, c, d; \
cin >> a >> b >> c >> d
#define din(...) _overloadin(__VA_ARGS__, din4, din3, din2, din1)(__VA_ARGS__)
#define _overloaddind(_1, _2, _3, _4, name, ...) name
#define din1d(a) \
int a; \
cin >> a; \
a--
#define din2d(a, b) \
int a, b; \
cin >> a >> b; \
a--, b--
#define din3d(a, b, c) \
int a, b, c; \
cin >> a >> b >> c; \
a--, b--, c--
#define din4d(a, b, c, d) \
int a, b, c, d; \
cin >> a >> b >> c >> d; \
; \
a--, b--, c--, d--
#define dind(...) \
_overloaddind(__VA_ARGS__, din4d, din3d, din2d, din1d)(__VA_ARGS__)
string sin() { return _in<string>(); }
ll lin() { return _in<ll>(); }
#define na(a, n) \
a.resize(n); \
rep(i, n) cin >> a[i];
#define nao(a, n) \
a.resize(n + 1); \
rep(i, n) cin >> a[i + 1];
#define nad(a, n) \
a.resize(n); \
rep(i, n) { \
cin >> a[i]; \
a[i]--; \
}
#define na2(a, b, n) \
a.resize(n), b.resize(n); \
rep(i, n) cin >> a[i] >> b[i];
#define na2d(a, b, n) \
a.resize(n), b.resize(n); \
rep(i, n) { \
cin >> a[i] >> b[i]; \
a[i]--, b[i]--; \
}
#define na3(a, b, c, n) \
a.resize(n), b.resize(n), c.resize(n); \
rep(i, n) cin >> a[i] >> b[i] >> c[i];
#define na3d(a, b, c, n) \
a.resize(n), b.resize(n), c.resize(n); \
rep(i, n) { \
cin >> a[i] >> b[i] >> c[i]; \
a[i]--, b[i]--, c[i]--; \
}
#define nt(a, h, w) \
resize(a, h, w); \
rep(hi, h) rep(wi, w) cin >> a[hi][wi];
#define ntd(a, h, w) \
rs(a, h, w); \
rep(hi, h) rep(wi, w) cin >> a[hi][wi], a[hi][wi]--;
#define ntp(a, h, w) \
fill(a, '#'); \
rep(hi, 1, h + 1) rep(wi, 1, w + 1) cin >> a[hi][wi];
// デバッグ
#define sp << " " <<
#define debugName(VariableName) #VariableName
#define _deb1(x) cerr << debugName(x) << " = " << x << endl
#define _deb2(x, y) \
cerr << debugName(x) << " = " << x << ", " << debugName(y) << " = " << y \
<< endl
#define _deb3(x, y, z) \
cerr << debugName(x) << " = " << x << ", " << debugName(y) << " = " << y \
<< ", " debugName(z) << " = " << z << endl
#define _deb4(x, y, z, a) \
cerr << debugName(x) << " = " << x << ", " << debugName(y) << " = " << y \
<< ", " << debugName(z) << " = " << z << ", " << debugName(a) << " = " \
<< a << endl
#define _deb5(x, y, z, a, b) \
cerr << debugName(x) << " = " << x << ", " << debugName(y) << " = " << y \
<< ", " << debugName(z) << " = " << z << ", " << debugName(a) << " = " \
<< a << ", " << debugName(b) << " = " << b << endl
#define _overloadebug(_1, _2, _3, _4, _5, name, ...) name
#define debug(...) \
_overloadebug(__VA_ARGS__, _deb5, _deb4, _deb3, _deb2, _deb1)(__VA_ARGS__)
#define deb(...) \
_overloadebug(__VA_ARGS__, _deb5, _deb4, _deb3, _deb2, _deb1)(__VA_ARGS__)
#define debugline(x) \
cerr << x << " " \
<< "(L:" << __LINE__ << ")" << '\n'
void ole() {
#ifdef _DEBUG
debugline("ole");
exit(0);
#endif
string a = "a";
rep(i, 30) a += a;
rep(i, 1 << 17) cout << a << endl;
cout << "OLE 出力長制限超過" << endl;
exit(0);
}
void tle() {
while (inf)
cout << inf << endl;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll gcd(vi b) {
ll res = b[0];
for (auto &&v : b)
res = gcd(v, res);
return res;
}
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
ll rev(ll a) {
ll res = 0;
while (a) {
res *= 10;
res += a % 10;
a /= 10;
}
return res;
}
template <class T> void rev(vector<T> &a) { reverse(all(a)); }
void rev(string &a) { reverse(all(a)); }
ll ceil(ll a, ll b) {
if (b == 0) {
debugline("ceil");
deb(a, b);
ole();
return -1;
} else
return (a + b - 1) / b;
}
ll sqrt(ll a) {
if (a < 0) {
debugline("sqrt");
deb(a);
ole();
}
ll res = (ll)std::sqrt(a);
while (res * res < a)
res++;
return res;
}
double log(double e, double x) { return log(x) / log(e); }
ll sig(ll t) { return (1 + t) * t / 2; }
ll sig(ll s, ll t) { return (s + t) * (t - s + 1) / 2; }
vi divisors(int v) {
vi res;
double lim = std::sqrt(v);
for (int i = 1; i <= lim; ++i) {
if (v % i == 0) {
res.pb(i);
if (i != v / i)
res.pb(v / i);
}
}
return res;
}
vb isPrime;
vi primes;
void setPrime() {
int len = 4010101;
isPrime.resize(4010101);
fill(isPrime, true);
isPrime[0] = isPrime[1] = false;
for (int i = 2; i <= sqrt(len) + 5; ++i) {
if (!isPrime[i])
continue;
for (int j = 2; i * j < len; ++j) {
isPrime[i * j] = false;
}
}
rep(i, len) if (isPrime[i]) primes.pb(i);
}
vi factorization(int v) {
int tv = v;
vi res;
if (isPrime.size() == 0)
setPrime();
for (auto &&p : primes) {
if (v % p == 0)
res.push_back(p);
while (v % p == 0) {
v /= p;
}
if (v == 1 || p * p > tv)
break;
}
if (v > 1)
res.pb(v);
return res;
}
inline bool inside(int h, int w, int H, int W) {
return h >= 0 && w >= 0 && h < H && w < W;
}
inline bool inside(int v, int l, int r) { return l <= v && v < r; }
#define ins inside
ll u(ll a) { return a < 0 ? 0 : a; }
template <class T> vector<T> u(const vector<T> &a) {
vector<T> ret = a;
fora(v, ret) v = u(v);
return ret;
}
#define MIN(a) numeric_limits<a>::min()
#define MAX(a) numeric_limits<a>::max()
int n, m, k, d, H, W, x, y, z, q;
int cou;
vi a, b, c;
vvi(s, 0, 0);
vvc(ba, 0, 0);
vp p;
#define segmin(M) \
[](M a, M b) { return a < b ? a : b; }, [](int len, M x) { return x; }, MAX(M)
#define segmax(M) \
[](M a, M b) { return a > b ? a : b; }, [](int len, M x) { return x; }, MIN(M)
#define segsum(M) \
[](M a, M b) { return a + b; }, [](int len, M x) { return len * x; }, 0
#define segxor(M) \
[](M a, M b) { return a ^ b; }, \
[](int len, M x) { return (len & 1) ? x : 0; }, 0
// 作用素同士の演算は足し算でいいと仮定している
template <class M> struct SegmentLazyAdd {
using func = function<M(M, M)>;
using feval = function<M(
int,
M /*作用素,Mとは違う型を使うのを横着している*/)>; // 区間の値に加算される値
int n;
vector<M> seg;
vi lazy; // 全体に足された数を持つ 3333なら3
const func f; // M同士の演算結果を返す
const feval getAdd; // len,xを受け取り、segに加える数を返す
const M e;
vi arch; // 遅延評価を行うやつ
vi lens;
SegmentLazyAdd(vi a, func f, feval fe, M e) : f(f), getAdd(fe), e(e) {
n = 1;
// rep(i, 100) a.pb(e);
int asz = a.size();
while (n < asz)
n <<= 1;
seg.resize(2 * n - 1, e);
lazy.resize(2 * n - 1, 0);
lens.resize(2 * n - 1);
rep(i, asz) seg[i + n - 1] = a[i];
rer(i, n - 2) seg[i] = f(seg[(i << 1) + 1], seg[(i << 1) + 2]);
int l = 1, v = n;
rep(i, 1, n << 1) {
if ((l << 1) == i) {
l <<= 1;
v >>= 1;
}
lens[i - 1] = v;
}
}
void eval() {
rer(j, sz(arch) - 1) {
int i = arch[j];
// iは1index
// lazyは0index
int v = lazy[i - 1];
if (!v)
continue;
lazy[i * 2 - 1] += v;
lazy[i * 2] += v;
seg[i * 2 - 1] += getAdd(lens[i * 2 - 1], v); //
seg[i * 2] += getAdd(lens[i * 2], v);
lazy[i - 1] = 0;
}
}
void gindex(int l, int r) {
arch.clear();
l += n;
r += n;
int lm = (l / (l & -l)) >> 1;
int rm = (r / (r & -r)) >> 1;
while (l < r) {
if (r <= rm)
arch.pb(r);
if (l <= lm)
arch.pb(l);
l >>= 1;
r >>= 1;
}
while (l) {
arch.pb(l);
l >>= 1;
}
}
void add(int l, int r, int x) {
int L = l + n;
int R = r + n;
while (L < R) {
if (R & 1) {
R--;
lazy[R - 1] += x;
seg[R - 1] += getAdd(lens[R - 1], x);
}
if (L & 1) {
lazy[L - 1] += x;
seg[L - 1] += getAdd(lens[L - 1], x);
L++;
}
L >>= 1;
R >>= 1;
}
gindex(l, r);
fora(i, arch) {
seg[i - 1] = f(seg[i * 2 - 1], seg[i * 2]);
seg[i - 1] += getAdd(lens[i - 1], lazy[i - 1]);
}
}
M get(int l, int r) {
gindex(l, r);
eval();
int L = l + n;
int R = r + n;
M retl = e, retr = e;
while (L < R) {
if (R & 1) {
R--;
retr = f(seg[R - 1], retr);
}
if (L & 1) {
retl = f(retl, seg[L - 1]);
L++;
}
L >>= 1;
R >>= 1;
}
return f(retl, retr);
}
#ifdef _DEBUG
void debu(int len = 10) {
rep(i, min(n, min(len, n))) {
int v = get(i, i + 1);
if (v == e) {
cerr << "e ";
} else {
cerr << v << " ";
}
}
cerr << "" << endl;
}
#else
inline void debu() { ; }
#endif
};
#define seg SegmentLazyAdd
#define raq SegmentLazyAdd
signed main() {
cin >> n >> q;
seg<int> st(vi(n + 1), segsum(int));
rep(i, q) {
int c;
cin >> c;
int s, t;
cin >> s >> t;
if (c == 0) {
int x;
cin >> x;
st.add(s - 1, t, x);
} else {
cout << st.get(s - 1, t) << endl;
}
}
return 0;
}
|
replace
| 820 | 821 | 820 | 821 |
0
| |
p02351
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) for (int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x) cerr << #x << " = " << (x) << endl;
// const int INF = 1e8;
using namespace std;
struct RAQ {
using T = long long;
T operator()(const T &a, const T &b) { return a + b; };
static constexpr T identity() { return 0; }
};
template <class Monoid> class rangeAddQuery {
private:
using T = typename Monoid::T;
Monoid op;
const int n;
vector<T> dat, lazy;
T query(int a, int b, int k, int l, int r) {
if (b <= l || r <= a)
return op.identity();
else if (a <= l && r <= b)
return dat[k] * (r - l) + lazy[k];
else {
T res = (min(b, r) - max(a, l)) * dat[k];
res += query(a, b, k * 2, l, (l + r) / 2);
res += query(a, b, k * 2 + 1, (l + r) / 2, r);
return res;
}
}
void add(int a, int b, int k, int l, int r, T x) {
if (a <= l && r <= b) {
dat[k] += x;
} else if (l < b && a < r) {
lazy[k] += (min(b, r) - max(a, l)) * x;
add(a, b, k * 2, l, (l + r) / 2, x);
add(a, b, k * 2 + 1, (l + r) / 2, r, x);
}
}
int power(int n) {
int res = 1;
while (n >= res)
res *= 2;
return res;
}
public:
rangeAddQuery(int n)
: n(power(n)), dat(4 * n, op.identity()), lazy(4 * n, op.identity()) {}
rangeAddQuery(const vector<T> &v)
: n(v.size()), dat(4 * n), lazy(4 * n, op.identity()) {
copy(begin(v), end(v), begin(dat) + n);
for (int i = n - 1; i > 0; i--)
dat[i] = op(dat[2 * i], dat[2 * i + 1]);
}
int query(int a, int b) { return query(a, b, 1, 0, n); }
void add(int s, int t, T x) { add(s, t, 1, 0, n, x); }
int get(int a) { return query(a, a + 1); };
void out() {
rep(i, n * 2) { cout << dat[i + 1] << ' '; }
cout << endl;
}
};
int main() {
int n, q;
cin >> n >> q;
rangeAddQuery<RAQ> seg(n);
rep(i, q) {
int com;
cin >> com;
if (not com) {
int s, t, x;
cin >> s >> t >> x;
// cout << s << ' ' << t << endl;
seg.add(s - 1, t, x);
} else {
int s, t;
cin >> s >> t;
assert(seg.query(s - 1, t) >= 0);
cout << seg.query(s - 1, t) << endl;
}
// seg.out();
}
}
|
#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) for (int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x) cerr << #x << " = " << (x) << endl;
// const int INF = 1e8;
using namespace std;
struct RAQ {
using T = long long;
T operator()(const T &a, const T &b) { return a + b; };
static constexpr T identity() { return 0; }
};
template <class Monoid> class rangeAddQuery {
private:
using T = typename Monoid::T;
Monoid op;
const int n;
vector<T> dat, lazy;
T query(int a, int b, int k, int l, int r) {
if (b <= l || r <= a)
return op.identity();
else if (a <= l && r <= b)
return dat[k] * (r - l) + lazy[k];
else {
T res = (min(b, r) - max(a, l)) * dat[k];
res += query(a, b, k * 2, l, (l + r) / 2);
res += query(a, b, k * 2 + 1, (l + r) / 2, r);
return res;
}
}
void add(int a, int b, int k, int l, int r, T x) {
if (a <= l && r <= b) {
dat[k] += x;
} else if (l < b && a < r) {
lazy[k] += (min(b, r) - max(a, l)) * x;
add(a, b, k * 2, l, (l + r) / 2, x);
add(a, b, k * 2 + 1, (l + r) / 2, r, x);
}
}
int power(int n) {
int res = 1;
while (n >= res)
res *= 2;
return res;
}
public:
rangeAddQuery(int n)
: n(power(n)), dat(4 * n, op.identity()), lazy(4 * n, op.identity()) {}
rangeAddQuery(const vector<T> &v)
: n(v.size()), dat(4 * n), lazy(4 * n, op.identity()) {
copy(begin(v), end(v), begin(dat) + n);
for (int i = n - 1; i > 0; i--)
dat[i] = op(dat[2 * i], dat[2 * i + 1]);
}
T query(int a, int b) { return query(a, b, 1, 0, n); }
void add(int s, int t, T x) { add(s, t, 1, 0, n, x); }
int get(int a) { return query(a, a + 1); };
void out() {
rep(i, n * 2) { cout << dat[i + 1] << ' '; }
cout << endl;
}
};
int main() {
int n, q;
cin >> n >> q;
rangeAddQuery<RAQ> seg(n);
rep(i, q) {
int com;
cin >> com;
if (not com) {
int s, t, x;
cin >> s >> t >> x;
// cout << s << ' ' << t << endl;
seg.add(s - 1, t, x);
} else {
int s, t;
cin >> s >> t;
assert(seg.query(s - 1, t) >= 0);
cout << seg.query(s - 1, t) << endl;
}
// seg.out();
}
}
|
replace
| 57 | 58 | 57 | 58 |
0
| |
p02351
|
C++
|
Memory Limit Exceeded
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <typename T> class node {
public:
node<T> *l, *r;
T val, val2;
node() : val2(0) {}
node(T val_, node<T> *l_ = nullptr, node<T> *r_ = nullptr)
: l(l_), r(r_), val(val_), val2(0) {}
void init(T val_, node<T> *l_ = nullptr, node<T> *r_ = nullptr) {
val = val_;
l = l_;
r = r_;
}
};
template <typename T> class DynamicLazySegmentTree {
using func_t = function<T(T, T)>;
const ll n;
const T id;
func_t merge;
node<T> *root;
vector<node<T>> pool;
int it;
ll size(ll n) {
ll res = 1;
while (res < n)
res <<= 1;
return res;
}
node<T> *new_node() {
pool[it].init(id);
return &pool[it++];
}
T sub(ll l, ll r, node<T> *n, ll lb, ll ub) {
if (ub <= l || r <= lb)
return id;
if (l <= lb && ub <= r)
return n->val + n->val2 * (ub - lb);
return n->val2 * (min(r, ub) - max(l, lb)) +
merge(n->l != nullptr ? sub(l, r, n->l, lb, (lb + ub) / 2) : id,
n->r != nullptr ? sub(l, r, n->r, (lb + ub) / 2, ub) : id);
}
void suc(ll l, ll r, node<T> *n, ll lb, ll ub, T val) {
if (ub <= l || r <= lb)
return;
if (l <= lb && ub <= r) {
n->val2 += val;
return;
}
n->val += val * (min(r, ub) - max(l, lb));
if (n->l == nullptr) {
n->l = new_node();
}
if (n->r == nullptr) {
n->r = new_node();
}
suc(l, r, n->l, lb, (lb + ub) / 2, val);
suc(l, r, n->r, (lb + ub) / 2, ub, val);
}
public:
DynamicLazySegmentTree(
ll n_, T id_ = 0, func_t merge_ = [](ll a, ll b) { return a + b; },
int max_ = 10000000)
: n(size(n_)), id(id_), merge(merge_), pool(max_) {
it = 0;
root = new_node();
}
void add(ll l, ll r, T val) { suc(l, r + 1, root, 0, n, val); }
T getSum(ll l, ll r) { return sub(l, r + 1, root, 0, n); }
};
int main() {
cin.sync_with_stdio(false);
int n, q;
cin >> n >> q;
DynamicLazySegmentTree<ll> dlst(10000000000000000ll);
for (int i = 0, type, s, t, x; i < q; i++) {
cin >> type;
if (type) {
cin >> s >> t;
printf("%lld\n", dlst.getSum(s, t));
} else {
cin >> s >> t >> x;
dlst.add(s, t, x);
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <typename T> class node {
public:
node<T> *l, *r;
T val, val2;
node() : val2(0) {}
node(T val_, node<T> *l_ = nullptr, node<T> *r_ = nullptr)
: l(l_), r(r_), val(val_), val2(0) {}
void init(T val_, node<T> *l_ = nullptr, node<T> *r_ = nullptr) {
val = val_;
l = l_;
r = r_;
}
};
template <typename T> class DynamicLazySegmentTree {
using func_t = function<T(T, T)>;
const ll n;
const T id;
func_t merge;
node<T> *root;
vector<node<T>> pool;
int it;
ll size(ll n) {
ll res = 1;
while (res < n)
res <<= 1;
return res;
}
node<T> *new_node() {
pool[it].init(id);
return &pool[it++];
}
T sub(ll l, ll r, node<T> *n, ll lb, ll ub) {
if (ub <= l || r <= lb)
return id;
if (l <= lb && ub <= r)
return n->val + n->val2 * (ub - lb);
return n->val2 * (min(r, ub) - max(l, lb)) +
merge(n->l != nullptr ? sub(l, r, n->l, lb, (lb + ub) / 2) : id,
n->r != nullptr ? sub(l, r, n->r, (lb + ub) / 2, ub) : id);
}
void suc(ll l, ll r, node<T> *n, ll lb, ll ub, T val) {
if (ub <= l || r <= lb)
return;
if (l <= lb && ub <= r) {
n->val2 += val;
return;
}
n->val += val * (min(r, ub) - max(l, lb));
if (n->l == nullptr) {
n->l = new_node();
}
if (n->r == nullptr) {
n->r = new_node();
}
suc(l, r, n->l, lb, (lb + ub) / 2, val);
suc(l, r, n->r, (lb + ub) / 2, ub, val);
}
public:
DynamicLazySegmentTree(
ll n_, T id_ = 0, func_t merge_ = [](ll a, ll b) { return a + b; },
int max_ = 1000000)
: n(size(n_)), id(id_), merge(merge_), pool(max_) {
it = 0;
root = new_node();
}
void add(ll l, ll r, T val) { suc(l, r + 1, root, 0, n, val); }
T getSum(ll l, ll r) { return sub(l, r + 1, root, 0, n); }
};
int main() {
cin.sync_with_stdio(false);
int n, q;
cin >> n >> q;
DynamicLazySegmentTree<ll> dlst(10000000000000000ll);
for (int i = 0, type, s, t, x; i < q; i++) {
cin >> type;
if (type) {
cin >> s >> t;
printf("%lld\n", dlst.getSum(s, t));
} else {
cin >> s >> t >> x;
dlst.add(s, t, x);
}
}
return 0;
}
|
replace
| 67 | 68 | 67 | 68 |
MLE
| |
p02351
|
C++
|
Memory Limit Exceeded
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <typename T> class node {
public:
node<T> *l, *r;
T val, add;
node(T val_ = 0, node<T> *l_ = nullptr, node<T> *r_ = nullptr)
: l(l_), r(r_), val(val_), add(0) {}
};
template <typename T> class DynamicLazySegmentTree {
const ll n;
node<T> *root;
ll size(ll n) {
ll res = 1;
while (res < n)
res <<= 1ll;
return res;
}
T sub(ll l, ll r, node<T> *n, ll lb, ll ub) {
if (ub <= l || r <= lb)
return 0;
if (l <= lb && ub <= r)
return n->val + n->add * (ub - lb);
return n->add * (min(r, ub) - max(l, lb)) +
(n->l != nullptr ? sub(l, r, n->l, lb, (lb + ub) / 2) : 0) +
(n->r != nullptr ? sub(l, r, n->r, (lb + ub) / 2, ub) : 0);
}
void suc(ll l, ll r, node<T> *n, ll lb, ll ub, T val) {
if (ub <= l || r <= lb)
return;
if (l <= lb && ub <= r) {
n->add += val;
return;
}
n->val += val * (min(r, ub) - max(l, lb));
if (n->l == nullptr) {
n->l = new node<T>();
}
if (n->r == nullptr) {
n->r = new node<T>();
}
suc(l, r, n->l, lb, (lb + ub) / 2, val);
suc(l, r, n->r, (lb + ub) / 2, ub, val);
}
public:
DynamicLazySegmentTree(ll n_) : n(size(n_)) { root = new node<T>(); }
void add(ll l, ll r, T val) { suc(l, r + 1, root, 0, n, val); }
T getSum(ll l, ll r) { return sub(l, r + 1, root, 0, n); }
};
template <typename T> class node2 {
public:
node2<T> *l, *r;
DynamicLazySegmentTree<T> val, add;
node2(ll w, node2<T> *l_ = nullptr, node2<T> *r_ = nullptr)
: l(l_), r(r_), val(w), add(w) {}
};
template <typename T> class DynamicLazySegmentTree2 {
const ll h, w;
node2<T> *root;
ll size(ll n) {
ll res = 1;
while (res < n)
res <<= 1ll;
return res;
}
T sub(ll li, ll lj, ll ri, ll rj, node2<T> *n, ll lb, ll ub) {
if (ub <= li || ri <= lb)
return 0;
if (li <= lb && ub <= ri)
return n->val.getSum(lj, rj - 1) + n->add.getSum(lj, rj - 1) * (ub - lb);
return n->add.getSum(lj, rj - 1) * (min(ri, ub) - max(li, lb)) +
(n->l == nullptr ? 0
: sub(li, lj, ri, rj, n->l, lb, (lb + ub) / 2)) +
(n->r == nullptr ? 0 : sub(li, lj, ri, rj, n->r, (lb + ub) / 2, ub));
}
void suc(ll li, ll lj, ll ri, ll rj, node2<T> *n, ll lb, ll ub, T val) {
if (ub <= li || ri <= lb)
return;
if (li <= lb && ub <= ri) {
n->add.add(lj, rj - 1, val);
return;
}
n->val.add(lj, rj - 1, val * (min(ri, ub) - max(li, lb)));
if (n->l == nullptr) {
n->l = new node2<T>(w);
}
if (n->r == nullptr) {
n->r = new node2<T>(w);
}
suc(li, lj, ri, rj, n->l, lb, (lb + ub) / 2, val);
suc(li, lj, ri, rj, n->r, (lb + ub) / 2, ub, val);
}
public:
DynamicLazySegmentTree2(ll h_, ll w_) : h(size(h_)), w(size(w_)) {
root = new node2<T>(w);
}
void add(ll li, ll lj, ll ri, ll rj, T val) {
suc(li, lj, ri + 1, rj + 1, root, 0, h, val);
}
T getSum(ll li, ll lj, ll ri, ll rj) {
return sub(li, lj, ri + 1, rj + 1, root, 0, h);
}
};
int main() {
cin.sync_with_stdio(false);
int n, q;
cin >> n >> q;
DynamicLazySegmentTree2<ll> dlst(10000000000ll, 10000000000ll);
for (int i = 0, type, s, t, x; i < q; i++) {
cin >> type >> s >> t;
s--;
t--;
if (type) {
printf("%lld\n", dlst.getSum(0, s, 0, t));
} else {
cin >> x;
dlst.add(0, s, 0, t, x);
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <typename T> class node {
public:
node<T> *l, *r;
T val, add;
node(T val_ = 0, node<T> *l_ = nullptr, node<T> *r_ = nullptr)
: l(l_), r(r_), val(val_), add(0) {}
};
template <typename T> class DynamicLazySegmentTree {
const ll n;
node<T> *root;
ll size(ll n) {
ll res = 1;
while (res < n)
res <<= 1ll;
return res;
}
T sub(ll l, ll r, node<T> *n, ll lb, ll ub) {
if (ub <= l || r <= lb)
return 0;
if (l <= lb && ub <= r)
return n->val + n->add * (ub - lb);
return n->add * (min(r, ub) - max(l, lb)) +
(n->l != nullptr ? sub(l, r, n->l, lb, (lb + ub) / 2) : 0) +
(n->r != nullptr ? sub(l, r, n->r, (lb + ub) / 2, ub) : 0);
}
void suc(ll l, ll r, node<T> *n, ll lb, ll ub, T val) {
if (ub <= l || r <= lb)
return;
if (l <= lb && ub <= r) {
n->add += val;
return;
}
n->val += val * (min(r, ub) - max(l, lb));
if (n->l == nullptr) {
n->l = new node<T>();
}
if (n->r == nullptr) {
n->r = new node<T>();
}
suc(l, r, n->l, lb, (lb + ub) / 2, val);
suc(l, r, n->r, (lb + ub) / 2, ub, val);
}
public:
DynamicLazySegmentTree(ll n_) : n(size(n_)) { root = new node<T>(); }
void add(ll l, ll r, T val) { suc(l, r + 1, root, 0, n, val); }
T getSum(ll l, ll r) { return sub(l, r + 1, root, 0, n); }
};
template <typename T> class node2 {
public:
node2<T> *l, *r;
DynamicLazySegmentTree<T> val, add;
node2(ll w, node2<T> *l_ = nullptr, node2<T> *r_ = nullptr)
: l(l_), r(r_), val(w), add(w) {}
};
template <typename T> class DynamicLazySegmentTree2 {
const ll h, w;
node2<T> *root;
ll size(ll n) {
ll res = 1;
while (res < n)
res <<= 1ll;
return res;
}
T sub(ll li, ll lj, ll ri, ll rj, node2<T> *n, ll lb, ll ub) {
if (ub <= li || ri <= lb)
return 0;
if (li <= lb && ub <= ri)
return n->val.getSum(lj, rj - 1) + n->add.getSum(lj, rj - 1) * (ub - lb);
return n->add.getSum(lj, rj - 1) * (min(ri, ub) - max(li, lb)) +
(n->l == nullptr ? 0
: sub(li, lj, ri, rj, n->l, lb, (lb + ub) / 2)) +
(n->r == nullptr ? 0 : sub(li, lj, ri, rj, n->r, (lb + ub) / 2, ub));
}
void suc(ll li, ll lj, ll ri, ll rj, node2<T> *n, ll lb, ll ub, T val) {
if (ub <= li || ri <= lb)
return;
if (li <= lb && ub <= ri) {
n->add.add(lj, rj - 1, val);
return;
}
n->val.add(lj, rj - 1, val * (min(ri, ub) - max(li, lb)));
if (n->l == nullptr) {
n->l = new node2<T>(w);
}
if (n->r == nullptr) {
n->r = new node2<T>(w);
}
suc(li, lj, ri, rj, n->l, lb, (lb + ub) / 2, val);
suc(li, lj, ri, rj, n->r, (lb + ub) / 2, ub, val);
}
public:
DynamicLazySegmentTree2(ll h_, ll w_) : h(size(h_)), w(size(w_)) {
root = new node2<T>(w);
}
void add(ll li, ll lj, ll ri, ll rj, T val) {
suc(li, lj, ri + 1, rj + 1, root, 0, h, val);
}
T getSum(ll li, ll lj, ll ri, ll rj) {
return sub(li, lj, ri + 1, rj + 1, root, 0, h);
}
};
int main() {
cin.sync_with_stdio(false);
int n, q;
cin >> n >> q;
DynamicLazySegmentTree2<ll> dlst(1000000ll, 1000000ll);
for (int i = 0, type, s, t, x; i < q; i++) {
cin >> type >> s >> t;
s--;
t--;
if (type) {
printf("%lld\n", dlst.getSum(0, s, 0, t));
} else {
cin >> x;
dlst.add(0, s, 0, t, x);
}
}
return 0;
}
|
replace
| 116 | 117 | 116 | 117 |
MLE
| |
p02351
|
C++
|
Runtime Error
|
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <string>
#include <vector>
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define RFOR(i, a, b) for (int i = (a)-1; i >= (b); i--)
#define ll long long
#define ull unsigned long long
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
const int INF = 1e9;
const int MOD = 1e9 + 7;
using namespace std;
const int MAX_N = 1e5 + 3;
struct SegmentTree {
int n;
ll data[MAX_N * 2 - 1], datb[MAX_N * 2 - 1];
;
void init(int n_) {
n = 1;
while (n < n_)
n *= 2;
REP(i, 2 * n - 1) data[i] = 0;
REP(i, 2 * n - 1) datb[i] = 0;
}
//[a,b)???x????????????
void add(int a, int b, int x, int k, int l, int r) {
if (a <= l && r <= b) { //?????????????????????[l,r)????????¨???????????´???
data[k] += x;
} else if (l < b && r > a) { //?????????????????´???
datb[k] += (min(r, b) - max(l, a)) * x;
add(a, b, x, k * 2 + 1, l, (r + l) / 2);
add(a, b, x, k * 2 + 2, (r + l) / 2, r);
}
}
//[a,b)???????°?????????????????????????????????????????????\?????????????????????
//?????????query(a,b,0,0,n)??§?????????????????????????§???????????????£??????????????????
ll sum(int a, int b, int k, int l, int r) {
if (r <= a || b <= l)
return 0; //???????????????
if (a <= l && r <= b)
return datb[k] +
data[k] * (r - l); //?????????????????????[l,r)????????¨?????????
//??¨???????????????
ll res = data[k] * (min(r, b) - max(l, a));
res += sum(a, b, k * 2 + 1, l, l + (r - l) / 2);
res += sum(a, b, k * 2 + 2, l + (r - l) / 2, r);
return res;
}
};
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n, q;
cin >> n >> q;
SegmentTree seg;
seg.init(n);
REP(i, q) {
int o;
cin >> o;
if (!o) {
int a, b, c;
cin >> a >> b >> c;
a--;
seg.add(a, b, c, 0, 0, n);
} else {
int a, b;
cin >> a >> b;
a--;
cout << seg.sum(a, b, 0, 0, n) << endl;
}
}
return 0;
}
|
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <string>
#include <vector>
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define RFOR(i, a, b) for (int i = (a)-1; i >= (b); i--)
#define ll long long
#define ull unsigned long long
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
const int INF = 1e9;
const int MOD = 1e9 + 7;
using namespace std;
const int MAX_N = (1 << 17) + 1;
struct SegmentTree {
int n;
ll data[MAX_N * 2 - 1], datb[MAX_N * 2 - 1];
;
void init(int n_) {
n = 1;
while (n < n_)
n *= 2;
REP(i, 2 * n - 1) data[i] = 0;
REP(i, 2 * n - 1) datb[i] = 0;
}
//[a,b)???x????????????
void add(int a, int b, int x, int k, int l, int r) {
if (a <= l && r <= b) { //?????????????????????[l,r)????????¨???????????´???
data[k] += x;
} else if (l < b && r > a) { //?????????????????´???
datb[k] += (min(r, b) - max(l, a)) * x;
add(a, b, x, k * 2 + 1, l, (r + l) / 2);
add(a, b, x, k * 2 + 2, (r + l) / 2, r);
}
}
//[a,b)???????°?????????????????????????????????????????????\?????????????????????
//?????????query(a,b,0,0,n)??§?????????????????????????§???????????????£??????????????????
ll sum(int a, int b, int k, int l, int r) {
if (r <= a || b <= l)
return 0; //???????????????
if (a <= l && r <= b)
return datb[k] +
data[k] * (r - l); //?????????????????????[l,r)????????¨?????????
//??¨???????????????
ll res = data[k] * (min(r, b) - max(l, a));
res += sum(a, b, k * 2 + 1, l, l + (r - l) / 2);
res += sum(a, b, k * 2 + 2, l + (r - l) / 2, r);
return res;
}
};
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n, q;
cin >> n >> q;
SegmentTree seg;
seg.init(n);
REP(i, q) {
int o;
cin >> o;
if (!o) {
int a, b, c;
cin >> a >> b >> c;
a--;
seg.add(a, b, c, 0, 0, n);
} else {
int a, b;
cin >> a >> b;
a--;
cout << seg.sum(a, b, 0, 0, n) << endl;
}
}
return 0;
}
|
replace
| 26 | 27 | 26 | 27 |
0
| |
p02352
|
C++
|
Runtime Error
|
#include <cassert>
#include <functional>
#include <utility>
#include <vector>
template <typename ValueMonoid, typename OperatorMonoid, class Modify>
class LazySegmentTree {
public:
using value_type = ValueMonoid;
using reference = value_type &;
using const_reference = const value_type &;
using operator_type = OperatorMonoid;
private:
using container_type = std::vector<std::pair<value_type, operator_type>>;
public:
using size_type = typename container_type::size_type;
private:
const Modify m;
const size_type size_, height, capacity;
container_type tree;
static size_type getheight(const size_type &size) noexcept {
size_type ret = 0;
while (static_cast<size_type>(1) << ret < size)
++ret;
return ret;
}
value_type reflect(const size_type &index) {
return m(tree[index].first, tree[index].second);
}
void recalc(const size_type &index) {
tree[index].first = reflect(index << 1) + reflect(index << 1 | 1);
}
void assign(const size_type &index, const operator_type &data) {
tree[index].second = tree[index].second + data;
}
void push(const size_type &index) {
assign(index << 1, tree[index].second);
assign(index << 1 | 1, tree[index].second);
tree[index].second = operator_type();
}
void propagate(const size_type &index) {
for (size_type i = height; i; --i)
push(index >> i);
}
void thrust(const size_type &index) {
tree[index].first = reflect(index);
push(index);
}
void evaluate(const size_type &index) {
for (size_type i = height; i; --i)
thrust(index >> i);
}
void build(size_type index) {
while (index >>= 1)
recalc(index);
}
public:
explicit LazySegmentTree(const size_type &size, const Modify &m = Modify())
: m(m), size_(size), height(getheight(size_)),
capacity(static_cast<size_type>(1) << height), tree(size_ << 1) {}
void update(size_type begin, size_type end, const operator_type &data) {
assert(begin <= end);
assert(begin <= size());
assert(end <= size());
begin += capacity;
end += capacity;
propagate(begin);
propagate(end - 1);
for (size_type left = begin, right = end; left < right;
left >>= 1, right >>= 1) {
if (left & 1)
assign(left++, data);
if (right & 1)
assign(right - 1, data);
}
build(begin);
build(end - 1);
}
void update(size_type index,
const std::function<value_type(const_reference)> &f) {
assert(index < size());
index += capacity;
propagate(index);
tree[index].first = f(reflect(index));
tree[index].second = operator_type();
build(index);
}
void update(const size_type index, const_reference data) {
assert(index < size());
update(index, [&data](const_reference d) { return data; });
}
value_type range(size_type begin, size_type end) {
assert(begin <= end);
assert(begin <= size());
assert(end <= size());
begin += capacity;
end += capacity;
evaluate(begin);
evaluate(end - 1);
value_type retL, retR;
for (; begin < end; begin >>= 1, end >>= 1) {
if (begin & 1)
retL = retL + reflect(begin++);
if (end & 1)
retR = reflect(end - 1) + retR;
}
return retL + retR;
}
size_type search(const std::function<bool(const_reference)> &b) {
if (b(value_type()))
return 0;
if (!b(reflect(1)))
return size() + 1;
value_type acc;
size_type i = 1;
while (i < capacity) {
thrust(i);
if (!b(acc + reflect(i <<= 1)))
acc = acc + reflect(i++);
}
return i - capacity + 1;
}
const_reference operator[](const size_type &index) {
assert(index < size());
index += capacity;
evaluate(index);
tree[index].first = reflect(index);
tree[index].second = operator_type();
return tree[index].first;
}
size_type size() const noexcept { return size_; }
bool empty() const noexcept { return !size_; }
};
template <typename V, typename O, class F>
auto make_Lazy(const typename LazySegmentTree<V, O, F>::size_type &size,
const F &f) {
return LazySegmentTree<V, O, F>(size, f);
}
template <typename T> struct Add {
using value_type = T;
T a;
explicit Add(const T &x = T(0)) : a(x) {}
Add operator+(const Add &o) const { return Add(a + o.a); }
};
#include <algorithm>
#include <limits>
template <typename T> struct Mini {
using value_type = T;
T a;
explicit Mini(const T &x = std::numeric_limits<T>::max()) : a(x) {}
Mini operator+(const Mini &o) const { return Mini(std::min(a, o.a)); }
};
#include <iostream>
int main() {
using M = Mini<int>;
using A = Add<int>;
int n, q;
std::cin >> n >> q;
auto T =
make_Lazy<M, A>(n, [](const M &x, const A &y) { return M(x.a + y.a); });
for (int i = 0; i < T.size(); ++i)
T.update(i, M(0));
while (q--) {
int c, s, t, x;
std::cin >> c >> s >> t;
if (c) {
std::cout << T.range(s, t + 1).a << std::endl;
} else {
std::cin >> x;
T.update(s, t + 1, A(x));
}
}
return 0;
}
|
#include <cassert>
#include <functional>
#include <utility>
#include <vector>
template <typename ValueMonoid, typename OperatorMonoid, class Modify>
class LazySegmentTree {
public:
using value_type = ValueMonoid;
using reference = value_type &;
using const_reference = const value_type &;
using operator_type = OperatorMonoid;
private:
using container_type = std::vector<std::pair<value_type, operator_type>>;
public:
using size_type = typename container_type::size_type;
private:
const Modify m;
const size_type size_, height, capacity;
container_type tree;
static size_type getheight(const size_type &size) noexcept {
size_type ret = 0;
while (static_cast<size_type>(1) << ret < size)
++ret;
return ret;
}
value_type reflect(const size_type &index) {
return m(tree[index].first, tree[index].second);
}
void recalc(const size_type &index) {
tree[index].first = reflect(index << 1) + reflect(index << 1 | 1);
}
void assign(const size_type &index, const operator_type &data) {
tree[index].second = tree[index].second + data;
}
void push(const size_type &index) {
assign(index << 1, tree[index].second);
assign(index << 1 | 1, tree[index].second);
tree[index].second = operator_type();
}
void propagate(const size_type &index) {
for (size_type i = height; i; --i)
push(index >> i);
}
void thrust(const size_type &index) {
tree[index].first = reflect(index);
push(index);
}
void evaluate(const size_type &index) {
for (size_type i = height; i; --i)
thrust(index >> i);
}
void build(size_type index) {
while (index >>= 1)
recalc(index);
}
public:
explicit LazySegmentTree(const size_type &size, const Modify &m = Modify())
: m(m), size_(size), height(getheight(size_)),
capacity(static_cast<size_type>(1) << height), tree(capacity << 1) {}
void update(size_type begin, size_type end, const operator_type &data) {
assert(begin <= end);
assert(begin <= size());
assert(end <= size());
begin += capacity;
end += capacity;
propagate(begin);
propagate(end - 1);
for (size_type left = begin, right = end; left < right;
left >>= 1, right >>= 1) {
if (left & 1)
assign(left++, data);
if (right & 1)
assign(right - 1, data);
}
build(begin);
build(end - 1);
}
void update(size_type index,
const std::function<value_type(const_reference)> &f) {
assert(index < size());
index += capacity;
propagate(index);
tree[index].first = f(reflect(index));
tree[index].second = operator_type();
build(index);
}
void update(const size_type index, const_reference data) {
assert(index < size());
update(index, [&data](const_reference d) { return data; });
}
value_type range(size_type begin, size_type end) {
assert(begin <= end);
assert(begin <= size());
assert(end <= size());
begin += capacity;
end += capacity;
evaluate(begin);
evaluate(end - 1);
value_type retL, retR;
for (; begin < end; begin >>= 1, end >>= 1) {
if (begin & 1)
retL = retL + reflect(begin++);
if (end & 1)
retR = reflect(end - 1) + retR;
}
return retL + retR;
}
size_type search(const std::function<bool(const_reference)> &b) {
if (b(value_type()))
return 0;
if (!b(reflect(1)))
return size() + 1;
value_type acc;
size_type i = 1;
while (i < capacity) {
thrust(i);
if (!b(acc + reflect(i <<= 1)))
acc = acc + reflect(i++);
}
return i - capacity + 1;
}
const_reference operator[](const size_type &index) {
assert(index < size());
index += capacity;
evaluate(index);
tree[index].first = reflect(index);
tree[index].second = operator_type();
return tree[index].first;
}
size_type size() const noexcept { return size_; }
bool empty() const noexcept { return !size_; }
};
template <typename V, typename O, class F>
auto make_Lazy(const typename LazySegmentTree<V, O, F>::size_type &size,
const F &f) {
return LazySegmentTree<V, O, F>(size, f);
}
template <typename T> struct Add {
using value_type = T;
T a;
explicit Add(const T &x = T(0)) : a(x) {}
Add operator+(const Add &o) const { return Add(a + o.a); }
};
#include <algorithm>
#include <limits>
template <typename T> struct Mini {
using value_type = T;
T a;
explicit Mini(const T &x = std::numeric_limits<T>::max()) : a(x) {}
Mini operator+(const Mini &o) const { return Mini(std::min(a, o.a)); }
};
#include <iostream>
int main() {
using M = Mini<int>;
using A = Add<int>;
int n, q;
std::cin >> n >> q;
auto T =
make_Lazy<M, A>(n, [](const M &x, const A &y) { return M(x.a + y.a); });
for (int i = 0; i < T.size(); ++i)
T.update(i, M(0));
while (q--) {
int c, s, t, x;
std::cin >> c >> s >> t;
if (c) {
std::cout << T.range(s, t + 1).a << std::endl;
} else {
std::cin >> x;
T.update(s, t + 1, A(x));
}
}
return 0;
}
|
replace
| 63 | 64 | 63 | 64 |
-6
|
Fatal glibc error: malloc assertion failure in sysmalloc: (old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)
|
p02352
|
C++
|
Time Limit Exceeded
|
#include <bits/stdc++.h>
using namespace std;
using lint = long long int;
template <class T = int> using V = vector<T>;
template <class T = int> using VV = V<V<T>>;
template <class T> void assign(V<T> &v, int n, const T &a = T()) {
v.assign(n, a);
}
template <class T, class... U> void assign(V<T> &v, int n, const U &...u) {
v.resize(n);
for (auto &&i : v)
assign(i, u...);
}
struct M {
using T = struct mms {
lint min, max, sum;
mms(lint a = numeric_limits<lint>::max(),
lint b = numeric_limits<lint>::min(), lint c = 0)
: min(a), max(b), sum(c) {}
};
using U = struct lf {
lint a, b;
lf(lint a = 1, lint b = 0) : a(a), b(b) {}
};
static T op(const T &a, const T &b) {
return T{min(a.min, b.min), max(a.max, b.max), a.sum + b.sum};
}
static void ap(T &a, const U &g, lint k) {
a.min = g.a * a.min + g.b;
a.max = g.a * a.max + g.b;
if (g.a < 0)
swap(a.min, a.max);
a.sum = g.a * a.sum + k * g.b;
}
static void ap(U &f, const U &g) {
f.a *= g.a;
f.b = g.a * f.b + g.b;
}
};
template <class M> struct ST {
using T = typename M::T;
using U = typename M::U;
lint n;
unordered_map<lint, T> t;
unordered_map<lint, U> u;
ST(lint n) : n(1LL << 8 * sizeof(lint) - __builtin_clzll(max(n - 1, 1LL))) {}
void _ap(lint i, const U &f) {
M::ap(t[i], f, 1 << __builtin_clzll(i) - __builtin_clzll(n));
if (i < n)
M::ap(u[i], f);
}
T get(lint l, lint r) {
_push(l, r);
T resl, resr;
for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
if (l & 1)
resl = M::op(resl, t[l++]);
if (r & 1)
resr = M::op(t[--r], resr);
}
return M::op(resl, resr);
}
void _push(lint l, lint r) {
for (lint hl = 8 * sizeof(lint) - __builtin_clzll(l + n) - 1,
hr = 8 * sizeof(lint) - __builtin_clzll(r - 1 + n) - 1;
hr > 0; hl--, hr--) {
lint i = r - 1 + n >> hr;
_ap(2 * i, u[i]);
_ap(2 * i + 1, u[i]);
u.erase(i);
i = l + n >> hl;
if (i == r - 1 + n >> hr or i >= n)
continue;
_ap(2 * i, u[i]);
_ap(2 * i + 1, u[i]);
u.erase(i);
}
}
void set(lint l, lint r, const U &f) {
_push(l, r);
for (lint i = l + n, j = r + n; i < j; i >>= 1, j >>= 1) {
if (i & 1)
_ap(i++, f);
if (j & 1)
_ap(--j, f);
}
for (l += n; !(l & 1);)
l >>= 1;
while (l >>= 1)
t[l] = M::op(t[2 * l], t[2 * l + 1]);
for (r += n; !(r & 1);)
r >>= 1;
while (r >>= 1)
t[r] = M::op(t[2 * r], t[2 * r + 1]);
}
};
int main() {
cin.tie(NULL);
ios::sync_with_stdio(false);
int n, q;
cin >> n >> q;
ST<M> st(1e8);
st.set(0, n, M::U(0, 0));
for (int i = 0; i < q; i++) {
int t;
cin >> t;
if (t) {
int l, r;
cin >> l >> r;
cout << st.get(l, r + 1).min << '\n';
} else {
int l, r, x;
cin >> l >> r >> x;
st.set(l, r + 1, M::U(1, x));
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
using lint = long long int;
template <class T = int> using V = vector<T>;
template <class T = int> using VV = V<V<T>>;
template <class T> void assign(V<T> &v, int n, const T &a = T()) {
v.assign(n, a);
}
template <class T, class... U> void assign(V<T> &v, int n, const U &...u) {
v.resize(n);
for (auto &&i : v)
assign(i, u...);
}
struct M {
using T = struct mms {
lint min, max, sum;
mms(lint a = numeric_limits<lint>::max(),
lint b = numeric_limits<lint>::min(), lint c = 0)
: min(a), max(b), sum(c) {}
};
using U = struct lf {
lint a, b;
lf(lint a = 1, lint b = 0) : a(a), b(b) {}
};
static T op(const T &a, const T &b) {
return T{min(a.min, b.min), max(a.max, b.max), a.sum + b.sum};
}
static void ap(T &a, const U &g, lint k) {
a.min = g.a * a.min + g.b;
a.max = g.a * a.max + g.b;
if (g.a < 0)
swap(a.min, a.max);
a.sum = g.a * a.sum + k * g.b;
}
static void ap(U &f, const U &g) {
f.a *= g.a;
f.b = g.a * f.b + g.b;
}
};
template <class M> struct ST {
using T = typename M::T;
using U = typename M::U;
lint n;
unordered_map<lint, T> t;
unordered_map<lint, U> u;
ST(lint n) : n(1LL << 8 * sizeof(lint) - __builtin_clzll(max(n - 1, 1LL))) {}
void _ap(lint i, const U &f) {
M::ap(t[i], f, 1 << __builtin_clzll(i) - __builtin_clzll(n));
if (i < n)
M::ap(u[i], f);
}
T get(lint l, lint r) {
_push(l, r);
T resl, resr;
for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
if (l & 1)
resl = M::op(resl, t[l++]);
if (r & 1)
resr = M::op(t[--r], resr);
}
return M::op(resl, resr);
}
void _push(lint l, lint r) {
for (lint hl = 8 * sizeof(lint) - __builtin_clzll(l + n) - 1,
hr = 8 * sizeof(lint) - __builtin_clzll(r - 1 + n) - 1;
hr > 0; hl--, hr--) {
lint i = r - 1 + n >> hr;
_ap(2 * i, u[i]);
_ap(2 * i + 1, u[i]);
u.erase(i);
i = l + n >> hl;
if (i == r - 1 + n >> hr or i >= n)
continue;
_ap(2 * i, u[i]);
_ap(2 * i + 1, u[i]);
u.erase(i);
}
}
void set(lint l, lint r, const U &f) {
_push(l, r);
for (lint i = l + n, j = r + n; i < j; i >>= 1, j >>= 1) {
if (i & 1)
_ap(i++, f);
if (j & 1)
_ap(--j, f);
}
for (l += n; !(l & 1);)
l >>= 1;
while (l >>= 1)
t[l] = M::op(t[2 * l], t[2 * l + 1]);
for (r += n; !(r & 1);)
r >>= 1;
while (r >>= 1)
t[r] = M::op(t[2 * r], t[2 * r + 1]);
}
};
int main() {
cin.tie(NULL);
ios::sync_with_stdio(false);
int n, q;
cin >> n >> q;
ST<M> st(1e7);
st.set(0, n, M::U(0, 0));
for (int i = 0; i < q; i++) {
int t;
cin >> t;
if (t) {
int l, r;
cin >> l >> r;
cout << st.get(l, r + 1).min << '\n';
} else {
int l, r, x;
cin >> l >> r >> x;
st.set(l, r + 1, M::U(1, x));
}
}
}
|
replace
| 109 | 110 | 109 | 110 |
TLE
| |
p02352
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 100005;
int n, q;
bool tag[maxn];
ll S[4 * maxn], lazy[4 * maxn];
void upd(int id, int l, int r, int ql, int qr, int v) {
if (ql == l && qr == r) {
tag[id] = 1;
lazy[id] += v;
return;
}
if (tag[id]) {
S[id] += lazy[id];
if (l != r) {
tag[id << 1] = 1;
tag[id << 1 | 1] = 1;
lazy[id << 1] += lazy[id];
lazy[id << 1 | 1] += lazy[id];
}
tag[id] = 0;
lazy[id] = 0;
}
int m = l + r >> 1;
if (qr <= m)
upd(id << 1, l, m, ql, qr, v);
else if (ql > m)
upd(id << 1 | 1, m + 1, r, ql, qr, v);
else
upd(id << 1, l, m, ql, m, v), upd(id << 1 | 1, m + 1, r, m + 1, qr, v);
ll lm = S[id << 1];
if (tag[id << 1])
lm += lazy[id << 1];
ll rm = S[id << 1 | 1];
if (tag[id << 1 | 1])
rm += lazy[id << 1 | 1];
S[id] = min(lm, rm);
}
ll query(int id, int l, int r, int ql, int qr) {
if (tag[id]) {
S[id] += lazy[id];
if (l != r) {
tag[id << 1] = 1;
tag[id << 1 | 1] = 1;
lazy[id << 1] += lazy[id];
lazy[id << 1 | 1] += lazy[id];
}
tag[id] = 0;
lazy[id] = 0;
}
if (ql == l && qr == r)
return S[id];
int m = l + r >> 1;
if (qr <= m)
return query(id << 1, l, m, ql, qr);
else if (ql > m)
return query(id << 1 | 1, m + 1, r, ql, qr);
else
return min(query(id << 1, l, m, ql, m),
query(id << 1 | 1, m + 1, r, m + 1, qr));
}
int main() {
cin >> n >> q;
for (int i = 0; i < q; i++) {
int f, s, t, x;
cin >> f >> s >> t;
if (f)
cout << query(1, 0, n - 1, s, t) << '\n';
else {
cin >> x;
upd(1, 0, n - 1, s, t, x);
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 100005;
int n, q;
bool tag[4 * maxn];
ll S[4 * maxn], lazy[4 * maxn];
void upd(int id, int l, int r, int ql, int qr, int v) {
if (ql == l && qr == r) {
tag[id] = 1;
lazy[id] += v;
return;
}
if (tag[id]) {
S[id] += lazy[id];
if (l != r) {
tag[id << 1] = 1;
tag[id << 1 | 1] = 1;
lazy[id << 1] += lazy[id];
lazy[id << 1 | 1] += lazy[id];
}
tag[id] = 0;
lazy[id] = 0;
}
int m = l + r >> 1;
if (qr <= m)
upd(id << 1, l, m, ql, qr, v);
else if (ql > m)
upd(id << 1 | 1, m + 1, r, ql, qr, v);
else
upd(id << 1, l, m, ql, m, v), upd(id << 1 | 1, m + 1, r, m + 1, qr, v);
ll lm = S[id << 1];
if (tag[id << 1])
lm += lazy[id << 1];
ll rm = S[id << 1 | 1];
if (tag[id << 1 | 1])
rm += lazy[id << 1 | 1];
S[id] = min(lm, rm);
}
ll query(int id, int l, int r, int ql, int qr) {
if (tag[id]) {
S[id] += lazy[id];
if (l != r) {
tag[id << 1] = 1;
tag[id << 1 | 1] = 1;
lazy[id << 1] += lazy[id];
lazy[id << 1 | 1] += lazy[id];
}
tag[id] = 0;
lazy[id] = 0;
}
if (ql == l && qr == r)
return S[id];
int m = l + r >> 1;
if (qr <= m)
return query(id << 1, l, m, ql, qr);
else if (ql > m)
return query(id << 1 | 1, m + 1, r, ql, qr);
else
return min(query(id << 1, l, m, ql, m),
query(id << 1 | 1, m + 1, r, m + 1, qr));
}
int main() {
cin >> n >> q;
for (int i = 0; i < q; i++) {
int f, s, t, x;
cin >> f >> s >> t;
if (f)
cout << query(1, 0, n - 1, s, t) << '\n';
else {
cin >> x;
upd(1, 0, n - 1, s, t, x);
}
}
}
|
replace
| 6 | 7 | 6 | 7 |
0
| |
p02353
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
#define int long long
using namespace std;
/*Starry Sky Tree*/
/*Range Add Min(Max) Query*/
/*区間加算、区間Min,区間Max*/
class RSUQ {
public:
// 遅延用の型
struct T {
int type; // 0 - empty , 1 - update
int value;
T() : type(0), value(0) {}
T(int type, int value) : type(type), value(value) {}
};
// マージ可能な主データ型
struct D {
int value;
D() : value(0) {} /*適切な値にする!!!!!!*/
D(int value) : value(value) {}
bool operator<(D a) const { return value < a.value; } // merge用
};
int n;
vector<D> dat;
vector<T> td;
D returnD = D(0); // 範囲外の時に返す値。
RSUQ() { n = -1; }
RSUQ(int n_) {
n = 1;
while (n < n_)
n *= 2;
td.resize(2 * n - 1, T());
dat.resize(2 * n - 1, D());
}
D merge(D a, D b) { return a.value + b.value; }
void delay(int k, int len) {
if (td[k].type == 0)
return;
int v = td[k].value;
td[k].type = 0;
td[k].value = 0;
len /= 2;
int l = k * 2 + 1, r = k * 2 + 2;
dat[l].value = v * len;
td[l].type = 1;
td[l].value = v;
dat[r].value = v * len;
td[r].type = 1;
td[r].value = v;
}
D write(int k, int x, int len) {
dat[k].value = x * len;
td[k].type = 1;
td[k].value = x;
return dat[k];
}
//[a,b)の値をx変更 query(a,b,x)
D update(int a, int b, int x, bool flg = true, int k = 0, int l = 0,
int r = -1) {
if (r == -1)
r = n, assert(a <= n && b <= n);
if (r <= a || b <= l)
return flg ? dat[k] : returnD;
if (a <= l && r <= b)
return flg ? write(k, x, r - l) : dat[k];
delay(k, r - l);
D vl = update(a, b, x, flg, k * 2 + 1, l, (l + r) / 2);
D vr = update(a, b, x, flg, k * 2 + 2, (l + r) / 2, r);
if (flg)
dat[k] = merge(vl, vr);
return merge(vl, vr);
}
//[a,b)の合計値を得る find(a,b);
int find(int a, int b) {
D res = update(a, b, 0, false);
return res.value;
}
};
signed main() {
int n, q;
cin >> n >> q;
RSUQ rsuq(n);
while (q--) {
int cmd;
cin >> cmd;
if (cmd == 0) {
int s, t, x;
cin >> s >> t >> x;
rsuq.update(s, t + 1, x);
}
if (cmd == 1) {
int s, t;
cin >> s >> t;
assert(s != t);
cout << rsuq.find(s, t + 1) << endl;
}
}
return 0;
}
|
#include <bits/stdc++.h>
#define int long long
using namespace std;
/*Starry Sky Tree*/
/*Range Add Min(Max) Query*/
/*区間加算、区間Min,区間Max*/
class RSUQ {
public:
// 遅延用の型
struct T {
int type; // 0 - empty , 1 - update
int value;
T() : type(0), value(0) {}
T(int type, int value) : type(type), value(value) {}
};
// マージ可能な主データ型
struct D {
int value;
D() : value(0) {} /*適切な値にする!!!!!!*/
D(int value) : value(value) {}
bool operator<(D a) const { return value < a.value; } // merge用
};
int n;
vector<D> dat;
vector<T> td;
D returnD = D(0); // 範囲外の時に返す値。
RSUQ() { n = -1; }
RSUQ(int n_) {
n = 1;
while (n < n_)
n *= 2;
td.resize(2 * n - 1, T());
dat.resize(2 * n - 1, D());
}
D merge(D a, D b) { return a.value + b.value; }
void delay(int k, int len) {
if (td[k].type == 0)
return;
int v = td[k].value;
td[k].type = 0;
td[k].value = 0;
len /= 2;
int l = k * 2 + 1, r = k * 2 + 2;
dat[l].value = v * len;
td[l].type = 1;
td[l].value = v;
dat[r].value = v * len;
td[r].type = 1;
td[r].value = v;
}
D write(int k, int x, int len) {
dat[k].value = x * len;
td[k].type = 1;
td[k].value = x;
return dat[k];
}
//[a,b)の値をx変更 query(a,b,x)
D update(int a, int b, int x, bool flg = true, int k = 0, int l = 0,
int r = -1) {
if (r == -1)
r = n, assert(a <= n && b <= n);
if (r <= a || b <= l)
return flg ? dat[k] : returnD;
if (a <= l && r <= b)
return flg ? write(k, x, r - l) : dat[k];
delay(k, r - l);
D vl = update(a, b, x, flg, k * 2 + 1, l, (l + r) / 2);
D vr = update(a, b, x, flg, k * 2 + 2, (l + r) / 2, r);
if (flg)
dat[k] = merge(vl, vr);
return merge(vl, vr);
}
//[a,b)の合計値を得る find(a,b);
int find(int a, int b) {
D res = update(a, b, 0, false);
return res.value;
}
};
signed main() {
int n, q;
cin >> n >> q;
RSUQ rsuq(n);
while (q--) {
int cmd;
cin >> cmd;
if (cmd == 0) {
int s, t, x;
cin >> s >> t >> x;
rsuq.update(s, t + 1, x);
}
if (cmd == 1) {
int s, t;
cin >> s >> t;
cout << rsuq.find(s, t + 1) << endl;
}
}
return 0;
}
|
delete
| 108 | 109 | 108 | 108 |
0
| |
p02355
|
C++
|
Runtime Error
|
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
int n, k;
cin >> n >> k;
vector<int> counts(k + 1, 0);
vector<int> v(n);
for (int i = 0; i < n; i++) {
cin >> v[i];
}
int total = 0;
int ans = 1000000000;
for (int l = 0, r = 0; r < n; r++) {
if (v[r] > k) {
continue;
}
counts[v[r]]++;
if (counts[v[r]] == 1) {
total++;
}
while (l < r && (v[l] > k || (total == k && counts[v[l]] > 1))) {
counts[v[l]]--;
l++;
}
if (total == k) {
ans = min(ans, r - l + 1);
}
}
cout << (total == k ? ans : 0) << endl;
return 0;
}
|
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
int n, k;
cin >> n >> k;
vector<int> counts(k + 1, 0);
vector<int> v(n);
for (int i = 0; i < n; i++) {
cin >> v[i];
}
int total = 0;
int ans = 1000000000;
for (int l = 0, r = 0; r < n; r++) {
if (v[r] > k) {
continue;
}
counts[v[r]]++;
if (counts[v[r]] == 1) {
total++;
}
while (l < r && (v[l] > k || (total == k && counts[v[l]] > 1))) {
if (v[l] <= k) {
counts[v[l]]--;
}
l++;
}
if (total == k) {
ans = min(ans, r - l + 1);
}
}
cout << (total == k ? ans : 0) << endl;
return 0;
}
|
replace
| 28 | 29 | 28 | 31 |
0
| |
p02356
|
C++
|
Runtime Error
|
#include <cstdio>
#include <iostream>
using namespace std;
long long p[100005], N, Q, A;
int main() {
cin >> N >> Q;
for (int i = 0; i < N; i++)
scanf("%lld", p[i]);
for (int i = 0; i < Q; i++) {
long long sum = 0, R = 0;
cin >> A;
long long cnt = 0;
for (int i = 0; i < N; i++) {
while (R < N) {
if (sum + p[R] > A)
break;
sum += p[R];
R++;
}
if (sum > 0) {
sum -= p[i];
}
cnt += R - i;
if (R < i + 1)
R = i + 1;
}
cout << cnt << endl;
}
return 0;
}
|
#include <cstdio>
#include <iostream>
using namespace std;
long long p[100005], N, Q, A;
int main() {
cin >> N >> Q;
for (int i = 0; i < N; i++)
scanf("%lld", &p[i]);
for (int i = 0; i < Q; i++) {
long long sum = 0, R = 0;
cin >> A;
long long cnt = 0;
for (int i = 0; i < N; i++) {
while (R < N) {
if (sum + p[R] > A)
break;
sum += p[R];
R++;
}
if (sum > 0) {
sum -= p[i];
}
cnt += R - i;
if (R < i + 1)
R = i + 1;
}
cout << cnt << endl;
}
return 0;
}
|
replace
| 7 | 8 | 7 | 8 |
-11
| |
p02357
|
C++
|
Runtime Error
|
// clang-format off
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define main signed main()
#define loop(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) loop(i, 0, n)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define prec(n) fixed << setprecision(n)
constexpr int INF = sizeof(int) == sizeof(long long) ? 1000000000000000000LL : 1000000000;
constexpr int MOD = 1000000007;
constexpr double PI = 3.14159265358979;
template<typename A, typename B> bool cmin(A &a, const B &b) { return a > b ? (a = b, true) : false; }
template<typename A, typename B> bool cmax(A &a, const B &b) { return a < b ? (a = b, true) : false; }
bool odd(const int &n) { return n & 1; }
bool even(const int &n) { return ~n & 1; }
template<typename T> int len(const T &v) { return v.size(); }
template<typename T = int> T in() { T x; cin >> x; return x; }
template<typename T = int> T in(T &&x) { T z(forward<T>(x)); cin >> z; return z; }
template<typename T> istream &operator>>(istream &is, vector<T> &v) { for (T &x : v) is >> x; return is; }
template<typename A, typename B> istream &operator>>(istream &is, pair<A, B> &p) { return is >> p.first >> p.second; }
template<typename T> ostream &operator<<(ostream &os, const vector<vector<T>> &v) { int n = v.size(); rep(i, n) os << v[i] << (i == n - 1 ? "" : "\n"); return os; }
template<typename T> ostream &operator<<(ostream &os, const vector<T> &v) { int n = v.size(); rep(i, n) os << v[i] << (i == n - 1 ? "" : " "); return os; }
template<typename A, typename B> ostream &operator<<(ostream &os, const pair<A, B> &p) { return os << p.first << ' ' << p.second; }
template<typename Head, typename Value> auto vectors(const Head &head, const Value &v) { return vector<Value>(head, v); }
template<typename Head, typename... Tail> auto vectors(Head x, Tail... tail) { auto inner = vectors(tail...); return vector<decltype(inner)>(x, inner); }
// clang-format on
template <typename Monoid> class DisjointSparseTable {
using T = typename Monoid::value_type;
Monoid m;
vector<int> logTable;
vector<vector<T>> table;
public:
template <typename InputIterator>
DisjointSparseTable(InputIterator first, InputIterator last) {
int n = distance(first, last);
int size = 1;
while (size < n)
size *= 2;
logTable.resize(size + 1);
for (int i = 2; i <= size; i++)
logTable[i] = logTable[i >> 1] + 1;
int k = logTable[size];
table.resize(k + 1, vector<T>(size, m.id()));
copy(first, last, table[1].begin());
for (int h = 2, range; (range = 1 << h) <= size; h++) {
int half = range >> 1;
for (int i = half; i < size; i += range) {
table[h][i - 1] = table[1][i - 1];
for (int j = i - 2; j >= i - half; j--)
table[h][j] = m(table[1][j], table[h][j + 1]);
table[h][i] = table[1][i];
for (int j = i + 1; j < i + half; j++)
table[h][j] = m(table[h][j - 1], table[1][j]);
}
}
}
T fold(int l, int r) { // [l, r)
if (l == r)
return m.id();
if (l == --r)
return table[1][l];
int h = logTable[l ^ r] + 1;
return m(table[h][l], table[h][r]);
}
};
template <typename T, T upperInf = numeric_limits<T>::max()> struct minMonoid {
using value_type = T;
value_type id() { return upperInf; }
value_type operator()(const value_type &a, const value_type &b) {
return a < b ? a : b;
}
};
int a[1000000];
main {
int N, L;
scanf("%lld %lld", &N, &L);
rep(i, N) scanf("%lld", &a[i]);
DisjointSparseTable<minMonoid<int>> dst(a, a + N);
rep(i, N - L + 1) printf("%d%c", dst.fold(i, i + L), " \n"[i == N - L]);
}
|
// clang-format off
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define main signed main()
#define loop(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) loop(i, 0, n)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define prec(n) fixed << setprecision(n)
constexpr int INF = sizeof(int) == sizeof(long long) ? 1000000000000000000LL : 1000000000;
constexpr int MOD = 1000000007;
constexpr double PI = 3.14159265358979;
template<typename A, typename B> bool cmin(A &a, const B &b) { return a > b ? (a = b, true) : false; }
template<typename A, typename B> bool cmax(A &a, const B &b) { return a < b ? (a = b, true) : false; }
bool odd(const int &n) { return n & 1; }
bool even(const int &n) { return ~n & 1; }
template<typename T> int len(const T &v) { return v.size(); }
template<typename T = int> T in() { T x; cin >> x; return x; }
template<typename T = int> T in(T &&x) { T z(forward<T>(x)); cin >> z; return z; }
template<typename T> istream &operator>>(istream &is, vector<T> &v) { for (T &x : v) is >> x; return is; }
template<typename A, typename B> istream &operator>>(istream &is, pair<A, B> &p) { return is >> p.first >> p.second; }
template<typename T> ostream &operator<<(ostream &os, const vector<vector<T>> &v) { int n = v.size(); rep(i, n) os << v[i] << (i == n - 1 ? "" : "\n"); return os; }
template<typename T> ostream &operator<<(ostream &os, const vector<T> &v) { int n = v.size(); rep(i, n) os << v[i] << (i == n - 1 ? "" : " "); return os; }
template<typename A, typename B> ostream &operator<<(ostream &os, const pair<A, B> &p) { return os << p.first << ' ' << p.second; }
template<typename Head, typename Value> auto vectors(const Head &head, const Value &v) { return vector<Value>(head, v); }
template<typename Head, typename... Tail> auto vectors(Head x, Tail... tail) { auto inner = vectors(tail...); return vector<decltype(inner)>(x, inner); }
// clang-format on
template <typename Monoid> class DisjointSparseTable {
using T = typename Monoid::value_type;
Monoid m;
vector<int> logTable;
vector<vector<T>> table;
public:
template <typename InputIterator>
DisjointSparseTable(InputIterator first, InputIterator last) {
int n = distance(first, last);
int size = 1;
while (size < n)
size *= 2;
logTable.resize(size + 1);
for (int i = 2; i <= size; i++)
logTable[i] = logTable[i >> 1] + 1;
int k = logTable[size];
table.resize(k + 2, vector<T>(size, m.id()));
copy(first, last, table[1].begin());
for (int h = 2, range; (range = 1 << h) <= size; h++) {
int half = range >> 1;
for (int i = half; i < size; i += range) {
table[h][i - 1] = table[1][i - 1];
for (int j = i - 2; j >= i - half; j--)
table[h][j] = m(table[1][j], table[h][j + 1]);
table[h][i] = table[1][i];
for (int j = i + 1; j < i + half; j++)
table[h][j] = m(table[h][j - 1], table[1][j]);
}
}
}
T fold(int l, int r) { // [l, r)
if (l == r)
return m.id();
if (l == --r)
return table[1][l];
int h = logTable[l ^ r] + 1;
return m(table[h][l], table[h][r]);
}
};
template <typename T, T upperInf = numeric_limits<T>::max()> struct minMonoid {
using value_type = T;
value_type id() { return upperInf; }
value_type operator()(const value_type &a, const value_type &b) {
return a < b ? a : b;
}
};
int a[1000000];
main {
int N, L;
scanf("%lld %lld", &N, &L);
rep(i, N) scanf("%lld", &a[i]);
DisjointSparseTable<minMonoid<int>> dst(a, a + N);
rep(i, N - L + 1) printf("%d%c", dst.fold(i, i + L), " \n"[i == N - L]);
}
|
replace
| 46 | 47 | 46 | 47 |
0
| |
p02357
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
int main() {
int n, l, a[111111];
cin >> n >> l;
for (int i = 0; i < n; i++)
cin >> a[i];
priority_queue<P> q;
for (int i = 0; i < n; i++) {
q.push(P(-a[i], i));
if (i < l - 1)
continue;
while (q.top().second <= i - l)
q.pop();
cout << -q.top().first;
if (i == n - 1)
cout << endl;
else
cout << " ";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
int main() {
int n, l, a[1111111];
cin >> n >> l;
for (int i = 0; i < n; i++)
cin >> a[i];
priority_queue<P> q;
for (int i = 0; i < n; i++) {
q.push(P(-a[i], i));
if (i < l - 1)
continue;
while (q.top().second <= i - l)
q.pop();
cout << -q.top().first;
if (i == n - 1)
cout << endl;
else
cout << " ";
}
return 0;
}
|
replace
| 4 | 5 | 4 | 5 |
0
| |
p02357
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
int a[400000];
int main() {
int n, l;
scanf("%d%d", &n, &l);
rep(i, n) scanf("%d", &a[i]);
multiset<int> se;
rep(i, l) se.insert(a[i]);
printf("%d", *se.begin());
for (int i = l; i < n; i++) {
se.insert(a[i]);
se.erase(se.find(a[i - l]));
printf(" %d", *se.begin());
}
puts("");
}
|
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
int a[1000000];
int main() {
int n, l;
scanf("%d%d", &n, &l);
rep(i, n) scanf("%d", &a[i]);
multiset<int> se;
rep(i, l) se.insert(a[i]);
printf("%d", *se.begin());
for (int i = l; i < n; i++) {
se.insert(a[i]);
se.erase(se.find(a[i - l]));
printf(" %d", *se.begin());
}
puts("");
}
|
replace
| 4 | 5 | 4 | 5 |
0
| |
p02357
|
C++
|
Runtime Error
|
#include <algorithm>
#include <array>
#include <cstdint>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <utility>
#include <vector>
#define INF 1000000000
#define MOD 1000000007
#define rep(i, a, b) for (uint32 i = (a); i < (b); ++i)
#define bitget(a, b) (((a) >> (b)) & 1)
#define ALL(x) (x).begin(), (x).end()
#define C(x) std::cout << #x << " : " << x << std::endl
using int32 = std::int_fast32_t;
using int64 = std::int_fast64_t;
using uint32 = std::uint_fast32_t;
using uint64 = std::uint_fast64_t;
/*
uint32 __builtin_ctz(uint32 c) {
uint32 t = 31;
if (c & 0x0000FFFF) { c &= 0x0000FFFF;t ^= 16; }
if (c & 0x00FF00FF) { c &= 0x00FF00FF;t ^= 8; }
if (c & 0x0F0F0F0F) { c &= 0x0F0F0F0F;t ^= 4; }
if (c & 0x33333333) { c &= 0x33333333;t ^= 2; }
if (c & 0x55555555) { c &= 0x55555555;t ^= 1; }
return t;
}
uint32 __builtin_clz(uint32 c) {
uint32 t = 31;
if (c & 0xFFFF0000) { c &= 0xFFFF0000;t ^= 16; }
if (c & 0xFF00FF00) { c &= 0xFF00FF00;t ^= 8; }
if (c & 0xF0F0F0F0) { c &= 0xF0F0F0F0;t ^= 4; }
if (c & 0xCCCCCCCC) { c &= 0xCCCCCCCC;t ^= 2; }
if (c & 0xAAAAAAAA) { c &= 0xAAAAAAAA;t ^= 1; }
return t;
}
uint32 __builtin_clrsb(uint32 c) {
return c ? __builtin_clz(c) - 1 : 31;
}
//*/
template <typename T> class StaticRMQ {
std::vector<std::vector<uint32>> table;
std::vector<T> a;
uint32 t;
std::vector<uint32> L, R, part;
uint32 bitmaskr[32] = {
0xFFFFFFFF, 0xFFFFFFFE, 0xFFFFFFFC, 0xFFFFFFF8, 0xFFFFFFF0, 0xFFFFFFE0,
0xFFFFFFC0, 0xFFFFFF80, 0xFFFFFF00, 0xFFFFFE00, 0xFFFFFC00, 0xFFFFF800,
0xFFFFF000, 0xFFFFE000, 0xFFFFC000, 0xFFFF8000, 0xFFFF0000, 0xFFFE0000,
0xFFFC0000, 0xFFF80000, 0xFFF00000, 0xFFE00000, 0xFFC00000, 0xFF800000,
0xFF000000, 0xFE000000, 0xFC000000, 0xF8000000, 0xF0000000, 0xE0000000,
0xC0000000, 0x80000000,
};
uint32 bitmaskl[32] = {
0x00000001, 0x00000003, 0x00000007, 0x0000000F, 0x0000001F, 0x0000003F,
0x0000007F, 0x000000FF, 0x000001FF, 0x000003FF, 0x000007FF, 0x00000FFF,
0x00001FFF, 0x00003FFF, 0x00007FFF, 0x0000FFFF, 0x0001FFFF, 0x0003FFFF,
0x0007FFFF, 0x000FFFFF, 0x001FFFFF, 0x003FFFFF, 0x007FFFFF, 0x00FFFFFF,
0x01FFFFFF, 0x03FFFFFF, 0x07FFFFFF, 0x0FFFFFFF, 0x1FFFFFFF, 0x3FFFFFFF,
0x7FFFFFFF, 0xFFFFFFFF};
public:
StaticRMQ(std::vector<T> &array) {
a.resize(array.size());
L.resize((array.size() + 0x1F) >> 5, 0);
R.resize((array.size() + 0x1F) >> 5, 1);
part.resize(array.size(), 0);
t = 0;
if (array.size() >> 5) {
table.resize(31 ^ __builtin_clrsb(array.size() >> 5));
table[0].resize((array.size() >> 5) + 1);
} else {
table.resize(1);
table[0].resize(1);
}
while ((1 << ++t) < (array.size() >> 5)) {
table[t].resize((array.size() >> 5) - (1 << t) + 1);
}
std::stack<uint32> s;
uint32 m;
for (t = 0; t < array.size(); ++t) {
a[t] = array[t];
part[t] |= 1 << (t & 0x1F);
if (!(t & 0x1F)) {
m = array[t];
table[0][t >> 5] = t;
while (!s.empty()) {
s.pop();
}
}
while (!s.empty()) {
if (array[s.top()] > array[t]) {
s.pop();
} else {
part[t] |= part[s.top()];
break;
}
}
s.push(t);
if (m > array[t]) {
m = array[t];
R[t >> 5] |= 1 << (t & 0x1F);
table[0][t >> 5] = t;
}
if (!((~t) & 0x1F)) {
L[t >> 5] = part[t];
}
}
for (t = 0; t + 1 < table.size(); ++t) {
for (m = 0; m < table[t + 1].size(); ++m) {
table[t + 1][m] = a[table[t][m]] < a[table[t][m + (1 << t)]]
? table[t][m]
: table[t][m + (1 << t)];
}
}
}
uint32 operator()(uint32 begin, uint32 end) {
uint32 l = (begin >> 5) + 1;
uint32 r = --end >> 5;
if (l < r) {
t = 31 ^ __builtin_clz(r - l);
if (a[table[t][l]] > a[table[t][r - (1 << t)]]) {
t = table[t][r - (1 << t)];
} else {
t = table[t][l];
}
if (!(a[(begin & ~0x1F) |
__builtin_ctz(L[begin >> 5] & bitmaskr[begin & 0x1F])] > a[t])) {
t = (begin & ~0x1F) |
__builtin_ctz(L[begin >> 5] & bitmaskr[begin & 0x1F]);
}
if (!(a[(end & ~0x1F) |
(31 ^ __builtin_clz(R[end >> 5] & bitmaskl[end & 0x1F]))] >
a[t])) {
t = (end & ~0x1F) |
(31 ^ __builtin_clz(R[end >> 5] & bitmaskl[end & 0x1F]));
}
return t;
}
if (l == r) {
t = (begin & ~0x1F) |
__builtin_ctz(L[begin >> 5] & bitmaskr[begin & 0x1F]);
if (!(a[(end & ~0x1F) |
(31 ^ __builtin_clz(R[end >> 5] & bitmaskl[end & 0x1F]))] >
a[t])) {
t = (end & ~0x1F) |
(31 ^ __builtin_clz(R[end >> 5] & bitmaskl[end & 0x1F]));
}
return t;
}
return (end & ~0x1F) | __builtin_ctz(part[end] & bitmaskr[begin & 0x1F]);
}
};
int main(void) {
uint32 n, l;
scanf("%d ", &n);
std::cin >> l;
std::vector<uint32> a(n);
rep(i, 0, n) { scanf("%d", &a[i]); }
StaticRMQ<uint32> S(a);
printf("%u", a[S(0, l)]);
rep(i, 1, n - l + 1) { printf(" %u", a[S(i, i + l)]); }
printf("\n");
return 0;
}
|
#include <algorithm>
#include <array>
#include <cstdint>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <utility>
#include <vector>
#define INF 1000000000
#define MOD 1000000007
#define rep(i, a, b) for (uint32 i = (a); i < (b); ++i)
#define bitget(a, b) (((a) >> (b)) & 1)
#define ALL(x) (x).begin(), (x).end()
#define C(x) std::cout << #x << " : " << x << std::endl
using int32 = std::int_fast32_t;
using int64 = std::int_fast64_t;
using uint32 = std::uint_fast32_t;
using uint64 = std::uint_fast64_t;
/*
uint32 __builtin_ctz(uint32 c) {
uint32 t = 31;
if (c & 0x0000FFFF) { c &= 0x0000FFFF;t ^= 16; }
if (c & 0x00FF00FF) { c &= 0x00FF00FF;t ^= 8; }
if (c & 0x0F0F0F0F) { c &= 0x0F0F0F0F;t ^= 4; }
if (c & 0x33333333) { c &= 0x33333333;t ^= 2; }
if (c & 0x55555555) { c &= 0x55555555;t ^= 1; }
return t;
}
uint32 __builtin_clz(uint32 c) {
uint32 t = 31;
if (c & 0xFFFF0000) { c &= 0xFFFF0000;t ^= 16; }
if (c & 0xFF00FF00) { c &= 0xFF00FF00;t ^= 8; }
if (c & 0xF0F0F0F0) { c &= 0xF0F0F0F0;t ^= 4; }
if (c & 0xCCCCCCCC) { c &= 0xCCCCCCCC;t ^= 2; }
if (c & 0xAAAAAAAA) { c &= 0xAAAAAAAA;t ^= 1; }
return t;
}
uint32 __builtin_clrsb(uint32 c) {
return c ? __builtin_clz(c) - 1 : 31;
}
//*/
template <typename T> class StaticRMQ {
std::vector<std::vector<uint32>> table;
std::vector<T> a;
uint32 t;
std::vector<uint32> L, R, part;
uint32 bitmaskr[32] = {
0xFFFFFFFF, 0xFFFFFFFE, 0xFFFFFFFC, 0xFFFFFFF8, 0xFFFFFFF0, 0xFFFFFFE0,
0xFFFFFFC0, 0xFFFFFF80, 0xFFFFFF00, 0xFFFFFE00, 0xFFFFFC00, 0xFFFFF800,
0xFFFFF000, 0xFFFFE000, 0xFFFFC000, 0xFFFF8000, 0xFFFF0000, 0xFFFE0000,
0xFFFC0000, 0xFFF80000, 0xFFF00000, 0xFFE00000, 0xFFC00000, 0xFF800000,
0xFF000000, 0xFE000000, 0xFC000000, 0xF8000000, 0xF0000000, 0xE0000000,
0xC0000000, 0x80000000,
};
uint32 bitmaskl[32] = {
0x00000001, 0x00000003, 0x00000007, 0x0000000F, 0x0000001F, 0x0000003F,
0x0000007F, 0x000000FF, 0x000001FF, 0x000003FF, 0x000007FF, 0x00000FFF,
0x00001FFF, 0x00003FFF, 0x00007FFF, 0x0000FFFF, 0x0001FFFF, 0x0003FFFF,
0x0007FFFF, 0x000FFFFF, 0x001FFFFF, 0x003FFFFF, 0x007FFFFF, 0x00FFFFFF,
0x01FFFFFF, 0x03FFFFFF, 0x07FFFFFF, 0x0FFFFFFF, 0x1FFFFFFF, 0x3FFFFFFF,
0x7FFFFFFF, 0xFFFFFFFF};
public:
StaticRMQ(std::vector<T> &array) {
a.resize(array.size());
L.resize((array.size() + 0x1F) >> 5, 0);
R.resize((array.size() + 0x1F) >> 5, 1);
part.resize(array.size(), 0);
t = 0;
if (array.size() >> 5) {
table.resize(31 ^ __builtin_clrsb(array.size() >> 5));
table[0].resize((array.size() >> 5) + 1);
} else {
table.resize(1);
table[0].resize(1);
}
while ((1 << ++t) < (array.size() >> 5)) {
table[t].resize((array.size() >> 5) - (1 << t) + 1);
}
std::stack<uint32> s;
uint32 m;
for (t = 0; t < array.size(); ++t) {
a[t] = array[t];
part[t] |= 1 << (t & 0x1F);
if (!(t & 0x1F)) {
m = array[t];
table[0][t >> 5] = t;
while (!s.empty()) {
s.pop();
}
}
while (!s.empty()) {
if (array[s.top()] > array[t]) {
s.pop();
} else {
part[t] |= part[s.top()];
break;
}
}
s.push(t);
if (m > array[t]) {
m = array[t];
R[t >> 5] |= 1 << (t & 0x1F);
table[0][t >> 5] = t;
}
if (!((~t) & 0x1F)) {
L[t >> 5] = part[t];
}
}
for (t = 0; t + 1 < table.size(); ++t) {
for (m = 0; m < table[t + 1].size(); ++m) {
table[t + 1][m] = a[table[t][m]] < a[table[t][m + (1 << t)]]
? table[t][m]
: table[t][m + (1 << t)];
}
}
}
uint32 operator()(uint32 begin, uint32 end) {
uint32 l = (begin >> 5) + 1;
uint32 r = --end >> 5;
if (l < r) {
t = 31 ^ __builtin_clz(r - l);
if (a[table[t][l]] > a[table[t][r - (1 << t)]]) {
t = table[t][r - (1 << t)];
} else {
t = table[t][l];
}
if (!(a[(begin & ~0x1F) |
__builtin_ctz(L[begin >> 5] & bitmaskr[begin & 0x1F])] > a[t])) {
t = (begin & ~0x1F) |
__builtin_ctz(L[begin >> 5] & bitmaskr[begin & 0x1F]);
}
if (!(a[(end & ~0x1F) |
(31 ^ __builtin_clz(R[end >> 5] & bitmaskl[end & 0x1F]))] >
a[t])) {
t = (end & ~0x1F) |
(31 ^ __builtin_clz(R[end >> 5] & bitmaskl[end & 0x1F]));
}
return t;
}
if (l == r) {
t = (begin & ~0x1F) |
__builtin_ctz(L[begin >> 5] & bitmaskr[begin & 0x1F]);
if (!(a[(end & ~0x1F) |
(31 ^ __builtin_clz(R[end >> 5] & bitmaskl[end & 0x1F]))] >
a[t])) {
t = (end & ~0x1F) |
(31 ^ __builtin_clz(R[end >> 5] & bitmaskl[end & 0x1F]));
}
return t;
}
return (end & ~0x1F) | __builtin_ctz(part[end] & bitmaskr[begin & 0x1F]);
}
};
int main(void) {
int n, l;
scanf("%d ", &n);
std::cin >> l;
std::vector<uint32> a(n);
rep(i, 0, n) { scanf("%d", &a[i]); }
StaticRMQ<uint32> S(a);
printf("%u", a[S(0, l)]);
rep(i, 1, n - l + 1) { printf(" %u", a[S(i, i + l)]); }
printf("\n");
return 0;
}
|
replace
| 164 | 165 | 164 | 165 |
-6
|
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
|
p02357
|
C++
|
Time Limit Exceeded
|
#include <iostream>
#include <set>
using namespace std;
int main() {
int N, L, a[1000000];
cin >> N >> L;
for (int i = 0; i < N; i++) {
cin >> a[i];
}
// solve
multiset<int> ms;
for (int i = 0; i < L; i++) {
ms.insert(a[i]);
}
cout << *ms.begin();
for (int i = L; i < N; i++) {
int c = ms.erase(a[i - L]);
for (int j = 0; j < c - 1; j++) {
ms.insert(a[i - L]);
}
ms.insert(a[i]);
cout << ' ' << *ms.begin();
}
cout << endl;
}
|
#include <iostream>
#include <set>
using namespace std;
int main() {
int N, L, a[1000000];
cin >> N >> L;
for (int i = 0; i < N; i++) {
cin >> a[i];
}
// solve
multiset<int> ms;
for (int i = 0; i < L; i++) {
ms.insert(a[i]);
}
cout << *ms.begin();
for (int i = L; i < N; i++) {
ms.erase(ms.find(a[i - L]));
ms.insert(a[i]);
cout << ' ' << *ms.begin();
}
cout << endl;
}
|
replace
| 25 | 29 | 25 | 26 |
TLE
| |
p02357
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
#define MAX 1000010
#define inf 2147483647
#define linf (1e16)
#define eps (1e-8)
#define Eps (1e-12)
#define mod 1000000007
#define pi acos(-1.0)
#define phi (1.0 + sqrt(5.0)) / 2.0
#define f first
#define s second
#define mp make_pair
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define pd(a) printf("%.10f\n", (double)(a))
#define pld(a) printf("%.10Lf\n", (ld)(a))
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (a)-1; (b) <= i; i--)
#define Unique(v) v.erase(unique(all(v)), v.end())
#define equals(a, b) (fabs((a) - (b)) < eps)
using namespace std;
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<int, double> pid;
typedef pair<double, int> pdi;
typedef pair<double, double> pdd;
typedef vector<int> vi;
typedef vector<pii> vpi;
int n, n_, L;
int st[MAX * 2];
void init() {
n_ = 1;
while (n_ < n)
n_ *= 2;
FOR(i, 0, 2 * n_ - 1) st[i] = inf;
}
void update(int a, int b) {
a += n_ - 1;
st[a] = b;
while (a > 0) {
a = (a - 1) / 2;
st[a] = min(st[a * 2 + 1], st[a * 2 + 2]);
}
}
int find(int a, int b, int c, int l, int r) {
if (r <= a || b <= l)
return inf;
if (a <= l && r <= b)
return st[c];
int L = find(a, b, c * 2 + 1, l, (l + r) / 2);
int R = find(a, b, c * 2 + 2, (l + r) / 2, r);
return min(L, R);
}
void solve() {
FOR(i, 0, n - L + 1) {
cout << find(i, i + L, 0, 0, n_);
if (i != n - L)
cout << " ";
}
cout << endl;
}
int main() {
cin >> n >> L;
init();
FOR(i, 0, n) {
int a;
cin >> a;
update(i, a);
}
solve();
return 0;
}
|
#include <bits/stdc++.h>
#define MAX 2000010
#define inf 2147483647
#define linf (1e16)
#define eps (1e-8)
#define Eps (1e-12)
#define mod 1000000007
#define pi acos(-1.0)
#define phi (1.0 + sqrt(5.0)) / 2.0
#define f first
#define s second
#define mp make_pair
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define pd(a) printf("%.10f\n", (double)(a))
#define pld(a) printf("%.10Lf\n", (ld)(a))
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (a)-1; (b) <= i; i--)
#define Unique(v) v.erase(unique(all(v)), v.end())
#define equals(a, b) (fabs((a) - (b)) < eps)
using namespace std;
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<int, double> pid;
typedef pair<double, int> pdi;
typedef pair<double, double> pdd;
typedef vector<int> vi;
typedef vector<pii> vpi;
int n, n_, L;
int st[MAX * 2];
void init() {
n_ = 1;
while (n_ < n)
n_ *= 2;
FOR(i, 0, 2 * n_ - 1) st[i] = inf;
}
void update(int a, int b) {
a += n_ - 1;
st[a] = b;
while (a > 0) {
a = (a - 1) / 2;
st[a] = min(st[a * 2 + 1], st[a * 2 + 2]);
}
}
int find(int a, int b, int c, int l, int r) {
if (r <= a || b <= l)
return inf;
if (a <= l && r <= b)
return st[c];
int L = find(a, b, c * 2 + 1, l, (l + r) / 2);
int R = find(a, b, c * 2 + 2, (l + r) / 2, r);
return min(L, R);
}
void solve() {
FOR(i, 0, n - L + 1) {
cout << find(i, i + L, 0, 0, n_);
if (i != n - L)
cout << " ";
}
cout << endl;
}
int main() {
cin >> n >> L;
init();
FOR(i, 0, n) {
int a;
cin >> a;
update(i, a);
}
solve();
return 0;
}
|
replace
| 1 | 2 | 1 | 2 |
0
| |
p02357
|
C++
|
Runtime Error
|
#define _USE_MATH_DEFINES
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;
typedef pair<long long int, long long int> P;
long long int INF = 1e18;
long long int a[110000], deq[110000];
int main() {
int n, L;
cin >> n >> L;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int s = 0, t = 0; // 先頭と末尾
for (int i = 0; i < n; i++) {
while (s < t && a[deq[t - 1]] >= a[i]) {
t--;
}
deq[t] = i;
t++;
if (i - L + 1 >= 0) {
// cout << deq[s] << " " << i - L + 1 << endl;
printf("%d%c", a[deq[s]], i == n - 1 ? '\n' : ' ');
if (deq[s] == i - L + 1) {
s++;
}
} else {
continue;
}
}
return 0;
}
|
#define _USE_MATH_DEFINES
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;
typedef pair<long long int, long long int> P;
long long int INF = 1e18;
long long int a[1100000], deq[1100000];
int main() {
int n, L;
cin >> n >> L;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int s = 0, t = 0; // 先頭と末尾
for (int i = 0; i < n; i++) {
while (s < t && a[deq[t - 1]] >= a[i]) {
t--;
}
deq[t] = i;
t++;
if (i - L + 1 >= 0) {
// cout << deq[s] << " " << i - L + 1 << endl;
printf("%d%c", a[deq[s]], i == n - 1 ? '\n' : ' ');
if (deq[s] == i - L + 1) {
s++;
}
} else {
continue;
}
}
return 0;
}
|
replace
| 16 | 17 | 16 | 17 |
0
| |
p02358
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
#define ll long long
#define INF 999999999
#define MOD 1000000007
#define rep(i, n) for (int i = 0; i < n; ++i)
using namespace std;
typedef pair<int, int> P;
const int MAX_N = 2005;
vector<int> X, Y, X1, Y1, X2, Y2;
bool flag[MAX_N][MAX_N];
int n;
int main() {
int p, q, r, s;
scanf("%d", &n);
rep(i, n) {
scanf("%d%d%d%d", &p, &q, &r, &s);
X.push_back(p), X.push_back(r);
Y.push_back(q), Y.push_back(s);
X1.push_back(p), X2.push_back(r);
Y1.push_back(q), Y2.push_back(s);
}
sort(X.begin(), X.end());
sort(Y.begin(), Y.end());
X.erase(unique(X.begin(), X.end()), X.end());
Y.erase(unique(Y.begin(), Y.end()), Y.end());
rep(i, n) {
X1[i] = lower_bound(X.begin(), X.end(), X1[i]) - X.begin();
Y1[i] = lower_bound(Y.begin(), Y.end(), Y1[i]) - Y.begin();
X2[i] = lower_bound(X.begin(), X.end(), X2[i]) - X.begin();
Y2[i] = lower_bound(Y.begin(), Y.end(), Y2[i]) - Y.begin();
}
rep(i, n) {
for (int j = Y1[i]; j < Y2[i]; j++) {
for (int k = X1[i]; k < X2[i]; k++) {
flag[j][k] = true;
}
}
}
ll ans = 0;
rep(i, Y.size() - 1) {
rep(j, X.size() - 1) {
if (flag[i][j]) {
ans += (ll)(X[j + 1] - X[j]) * (Y[i + 1] - Y[i]);
}
}
}
printf("%lld\n", ans);
return 0;
}
|
#include <bits/stdc++.h>
#define ll long long
#define INF 999999999
#define MOD 1000000007
#define rep(i, n) for (int i = 0; i < n; ++i)
using namespace std;
typedef pair<int, int> P;
const int MAX_N = 2005;
vector<int> X, Y, X1, Y1, X2, Y2;
bool flag[MAX_N * 2][MAX_N * 2];
int n;
int main() {
int p, q, r, s;
scanf("%d", &n);
rep(i, n) {
scanf("%d%d%d%d", &p, &q, &r, &s);
X.push_back(p), X.push_back(r);
Y.push_back(q), Y.push_back(s);
X1.push_back(p), X2.push_back(r);
Y1.push_back(q), Y2.push_back(s);
}
sort(X.begin(), X.end());
sort(Y.begin(), Y.end());
X.erase(unique(X.begin(), X.end()), X.end());
Y.erase(unique(Y.begin(), Y.end()), Y.end());
rep(i, n) {
X1[i] = lower_bound(X.begin(), X.end(), X1[i]) - X.begin();
Y1[i] = lower_bound(Y.begin(), Y.end(), Y1[i]) - Y.begin();
X2[i] = lower_bound(X.begin(), X.end(), X2[i]) - X.begin();
Y2[i] = lower_bound(Y.begin(), Y.end(), Y2[i]) - Y.begin();
}
rep(i, n) {
for (int j = Y1[i]; j < Y2[i]; j++) {
for (int k = X1[i]; k < X2[i]; k++) {
flag[j][k] = true;
}
}
}
ll ans = 0;
rep(i, Y.size() - 1) {
rep(j, X.size() - 1) {
if (flag[i][j]) {
ans += (ll)(X[j + 1] - X[j]) * (Y[i + 1] - Y[i]);
}
}
}
printf("%lld\n", ans);
return 0;
}
|
replace
| 13 | 14 | 13 | 14 |
0
| |
p02358
|
C++
|
Runtime Error
|
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
ll N;
vector<ll> x, y;
ll x1[2000], y1[2000], x2[2000], y2[2000];
ll c[2020][2020];
int main() {
cin >> N;
rep(i, N) {
cin >> x1[i] >> y1[i] >> x2[i] >> y2[i]; //[x1,x2) [y1,y2)
x.push_back(x1[i]);
x.push_back(x2[i]);
y.push_back(y1[i]);
y.push_back(y2[i]);
}
sort(x.begin(), x.end());
sort(y.begin(), y.end());
x.erase(unique(x.begin(), x.end()), x.end());
y.erase(unique(y.begin(), y.end()), y.end());
rep(i, N) {
ll ix1 = lower_bound(x.begin(), x.end(), x1[i]) - x.begin();
ll ix2 = lower_bound(x.begin(), x.end(), x2[i]) - x.begin();
ll iy1 = lower_bound(y.begin(), y.end(), y1[i]) - y.begin();
ll iy2 = lower_bound(y.begin(), y.end(), y2[i]) - y.begin();
c[iy1][ix1]++;
c[iy1][ix2]--;
c[iy2][ix1]--;
c[iy2][ix2]++;
}
rep(i, y.size()) {
rep(j, x.size() - 1) { c[i][j + 1] += c[i][j]; }
}
rep(j, x.size()) {
rep(i, y.size() - 1) { c[i + 1][j] += c[i][j]; }
}
ll ans = 0;
rep(i, y.size() - 1) {
rep(j, x.size() - 1) {
if (c[i][j] > 0) {
ans += (x[j + 1] - x[j]) * (y[i + 1] - y[i]);
}
}
}
cout << ans << endl;
return 0;
}
|
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
ll N;
vector<ll> x, y;
ll x1[2000], y1[2000], x2[2000], y2[2000];
ll c[4020][4020]; // x1,x2があるのでNの倍必要
int main() {
cin >> N;
rep(i, N) {
cin >> x1[i] >> y1[i] >> x2[i] >> y2[i]; //[x1,x2) [y1,y2)
x.push_back(x1[i]);
x.push_back(x2[i]);
y.push_back(y1[i]);
y.push_back(y2[i]);
}
sort(x.begin(), x.end());
sort(y.begin(), y.end());
x.erase(unique(x.begin(), x.end()), x.end());
y.erase(unique(y.begin(), y.end()), y.end());
rep(i, N) {
ll ix1 = lower_bound(x.begin(), x.end(), x1[i]) - x.begin();
ll ix2 = lower_bound(x.begin(), x.end(), x2[i]) - x.begin();
ll iy1 = lower_bound(y.begin(), y.end(), y1[i]) - y.begin();
ll iy2 = lower_bound(y.begin(), y.end(), y2[i]) - y.begin();
c[iy1][ix1]++;
c[iy1][ix2]--;
c[iy2][ix1]--;
c[iy2][ix2]++;
}
rep(i, y.size()) {
rep(j, x.size() - 1) { c[i][j + 1] += c[i][j]; }
}
rep(j, x.size()) {
rep(i, y.size() - 1) { c[i + 1][j] += c[i][j]; }
}
ll ans = 0;
rep(i, y.size() - 1) {
rep(j, x.size() - 1) {
if (c[i][j] > 0) {
ans += (x[j + 1] - x[j]) * (y[i + 1] - y[i]);
}
}
}
cout << ans << endl;
return 0;
}
|
replace
| 14 | 15 | 14 | 15 |
0
| |
p02358
|
C++
|
Runtime Error
|
#include <algorithm>
#include <iostream>
#include <vector>
#define ll long long
using namespace std;
//
int n;
ll px[2000], py[2000], qx[2000], qy[2000];
int dp[8000][8000];
vector<ll> compress(ll *x1, ll *x2) {
vector<ll> xs;
for (int i = 0; i < n; i++) {
for (int j = -1; j <= 1; j++) {
xs.push_back(x1[i] + j);
xs.push_back(x2[i] + j);
}
}
xs.push_back(1e18 + 7);
sort(xs.begin(), xs.end());
xs.erase(unique(xs.begin(), xs.end()), xs.end());
for (int i = 0; i < n; i++) {
x1[i] = lower_bound(xs.begin(), xs.end(), x1[i]) - xs.begin();
x2[i] = lower_bound(xs.begin(), xs.end(), x2[i]) - xs.begin();
};
return xs;
}
//
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> px[i] >> py[i] >> qx[i] >> qy[i];
}
vector<ll> X = compress(px, qx);
vector<ll> Y = compress(py, qy);
for (int i = 0; i < n; i++) {
dp[px[i]][py[i]]++;
dp[qx[i]][qy[i]]++;
dp[px[i]][qy[i]]--;
dp[qx[i]][py[i]]--;
}
for (int i = 0; i < X.size() - 1; i++) {
for (int j = 1; j < Y.size() - 1; j++) {
dp[i][j] += dp[i][j - 1];
}
}
for (int i = 0; i < Y.size() - 1; i++) {
for (int j = 1; j < X.size() - 1; j++) {
dp[j][i] += dp[j - 1][i];
}
}
ll ans = 0;
for (int i = 0; i < X.size() - 1; i++) {
for (int j = 0; j < Y.size() - 1; j++) {
if (dp[i][j] > 0) {
ans += (X[i + 1] - X[i]) * (Y[j + 1] - Y[j]);
}
}
}
cout << ans << endl;
}
|
#include <algorithm>
#include <iostream>
#include <vector>
#define ll long long
using namespace std;
//
int n;
ll px[2000], py[2000], qx[2000], qy[2000];
int dp[8000][8000];
vector<ll> compress(ll *x1, ll *x2) {
vector<ll> xs;
for (int i = 0; i < n; i++) {
for (int j = 0; j <= 1; j++) {
xs.push_back(x1[i] + j);
xs.push_back(x2[i] + j);
}
}
xs.push_back(1e18 + 7);
sort(xs.begin(), xs.end());
xs.erase(unique(xs.begin(), xs.end()), xs.end());
for (int i = 0; i < n; i++) {
x1[i] = lower_bound(xs.begin(), xs.end(), x1[i]) - xs.begin();
x2[i] = lower_bound(xs.begin(), xs.end(), x2[i]) - xs.begin();
};
return xs;
}
//
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> px[i] >> py[i] >> qx[i] >> qy[i];
}
vector<ll> X = compress(px, qx);
vector<ll> Y = compress(py, qy);
for (int i = 0; i < n; i++) {
dp[px[i]][py[i]]++;
dp[qx[i]][qy[i]]++;
dp[px[i]][qy[i]]--;
dp[qx[i]][py[i]]--;
}
for (int i = 0; i < X.size() - 1; i++) {
for (int j = 1; j < Y.size() - 1; j++) {
dp[i][j] += dp[i][j - 1];
}
}
for (int i = 0; i < Y.size() - 1; i++) {
for (int j = 1; j < X.size() - 1; j++) {
dp[j][i] += dp[j - 1][i];
}
}
ll ans = 0;
for (int i = 0; i < X.size() - 1; i++) {
for (int j = 0; j < Y.size() - 1; j++) {
if (dp[i][j] > 0) {
ans += (X[i + 1] - X[i]) * (Y[j + 1] - Y[j]);
}
}
}
cout << ans << endl;
}
|
replace
| 12 | 13 | 12 | 13 |
-11
| |
p02358
|
C++
|
Runtime Error
|
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
//
int n;
vector<int> px, py, qx, qy;
int dp[8100][8100];
struct vevev {
vector<int> a;
vector<int> b;
vector<int> ab;
};
vevev compress(vector<int> x1, vector<int> x2) {
vector<int> xs;
for (int i = 0; i < n; i++) {
for (int j = -1; j <= 1; j++) {
xs.push_back(x1[i] + j);
xs.push_back(x2[i] + j);
}
}
xs.push_back(1e9 + 7);
sort(xs.begin(), xs.end());
xs.erase(unique(xs.begin(), xs.end()), xs.end());
for (int i = 0; i < n; i++) {
x1[i] = lower_bound(xs.begin(), xs.end(), x1[i]) - xs.begin();
x2[i] = lower_bound(xs.begin(), xs.end(), x2[i]) - xs.begin();
}
vevev v;
v.a = x1;
v.b = x2;
v.ab = xs;
return v;
}
//
int main() {
cin >> n;
px.resize(n);
py.resize(n);
qx.resize(n);
qy.resize(n);
for (int i = 0; i < n; i++) {
cin >> px[i] >> py[i] >> qx[i] >> qy[i];
}
vevev x = compress(px, qx);
vevev y = compress(py, qy);
px = x.a;
qx = x.b;
vector<int> X = x.ab;
py = y.a;
qy = y.b;
vector<int> Y = y.ab;
for (int i = 0; i < n; i++) {
dp[px[i]][py[i]]++;
dp[qx[i]][qy[i]]++;
dp[px[i]][qy[i]]--;
dp[qx[i]][py[i]]--;
}
for (int i = 0; i < X.size() - 1; i++) {
for (int j = 1; j < Y.size() - 1; j++) {
dp[i][j] += dp[i][j - 1];
}
}
for (int i = 0; i < Y.size() - 1; i++) {
for (int j = 1; j < X.size() - 1; j++) {
dp[j][i] += dp[j - 1][i];
}
}
long long ans = 0;
for (int i = 0; i < X.size() - 1; i++) {
for (int j = 0; j < Y.size() - 1; j++) {
if (dp[i][j]) {
ans += (long long)(X[i + 1] - X[i]) * (Y[j + 1] - Y[j]);
}
}
}
cout << ans << endl;
}
|
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
//
int n;
vector<int> px, py, qx, qy;
int dp[8100][8100];
struct vevev {
vector<int> a;
vector<int> b;
vector<int> ab;
};
vevev compress(vector<int> x1, vector<int> x2) {
vector<int> xs;
for (int i = 0; i < n; i++) {
for (int j = 0; j <= 1; j++) {
xs.push_back(x1[i] + j);
xs.push_back(x2[i] + j);
}
}
xs.push_back(1e9 + 7);
sort(xs.begin(), xs.end());
xs.erase(unique(xs.begin(), xs.end()), xs.end());
for (int i = 0; i < n; i++) {
x1[i] = lower_bound(xs.begin(), xs.end(), x1[i]) - xs.begin();
x2[i] = lower_bound(xs.begin(), xs.end(), x2[i]) - xs.begin();
}
vevev v;
v.a = x1;
v.b = x2;
v.ab = xs;
return v;
}
//
int main() {
cin >> n;
px.resize(n);
py.resize(n);
qx.resize(n);
qy.resize(n);
for (int i = 0; i < n; i++) {
cin >> px[i] >> py[i] >> qx[i] >> qy[i];
}
vevev x = compress(px, qx);
vevev y = compress(py, qy);
px = x.a;
qx = x.b;
vector<int> X = x.ab;
py = y.a;
qy = y.b;
vector<int> Y = y.ab;
for (int i = 0; i < n; i++) {
dp[px[i]][py[i]]++;
dp[qx[i]][qy[i]]++;
dp[px[i]][qy[i]]--;
dp[qx[i]][py[i]]--;
}
for (int i = 0; i < X.size() - 1; i++) {
for (int j = 1; j < Y.size() - 1; j++) {
dp[i][j] += dp[i][j - 1];
}
}
for (int i = 0; i < Y.size() - 1; i++) {
for (int j = 1; j < X.size() - 1; j++) {
dp[j][i] += dp[j - 1][i];
}
}
long long ans = 0;
for (int i = 0; i < X.size() - 1; i++) {
for (int j = 0; j < Y.size() - 1; j++) {
if (dp[i][j]) {
ans += (long long)(X[i + 1] - X[i]) * (Y[j + 1] - Y[j]);
}
}
}
cout << ans << endl;
}
|
replace
| 16 | 17 | 16 | 17 |
-11
| |
p02358
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
int n;
int X1[2000], Y1[2000], X2[2000], Y2[2000];
int fld[2000][2000];
int compress(int *X1, int *X2, vector<int> &X) {
rep(i, n) {
X.push_back(X1[i]);
X.push_back(X2[i]);
}
sort(X.begin(), X.end());
X.erase(unique(X.begin(), X.end()), X.end());
rep(i, n) {
X1[i] = lower_bound(X.begin(), X.end(), X1[i]) - X.begin();
X2[i] = lower_bound(X.begin(), X.end(), X2[i]) - X.begin();
}
return X.size();
}
int main() {
scanf("%d", &n);
rep(i, n) scanf("%d%d%d%d", &X1[i], &Y1[i], &X2[i], &Y2[i]);
vector<int> a, b;
int w = compress(X1, X2, a), h = compress(Y1, Y2, b);
rep(i, n) {
for (int j = X1[i]; j < X2[i]; j++)
for (int k = Y1[i]; k < Y2[i]; k++)
fld[j][k] = true;
}
ll ans = 0;
rep(i, w - 1) rep(j, h - 1) {
if (fld[i][j])
ans += ((ll)a[i + 1] - a[i]) * (b[j + 1] - b[j]);
}
printf("%lld\n", ans);
}
|
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
int n;
int X1[2000], Y1[2000], X2[2000], Y2[2000];
int fld[4000][4000];
int compress(int *X1, int *X2, vector<int> &X) {
rep(i, n) {
X.push_back(X1[i]);
X.push_back(X2[i]);
}
sort(X.begin(), X.end());
X.erase(unique(X.begin(), X.end()), X.end());
rep(i, n) {
X1[i] = lower_bound(X.begin(), X.end(), X1[i]) - X.begin();
X2[i] = lower_bound(X.begin(), X.end(), X2[i]) - X.begin();
}
return X.size();
}
int main() {
scanf("%d", &n);
rep(i, n) scanf("%d%d%d%d", &X1[i], &Y1[i], &X2[i], &Y2[i]);
vector<int> a, b;
int w = compress(X1, X2, a), h = compress(Y1, Y2, b);
rep(i, n) {
for (int j = X1[i]; j < X2[i]; j++)
for (int k = Y1[i]; k < Y2[i]; k++)
fld[j][k] = true;
}
ll ans = 0;
rep(i, w - 1) rep(j, h - 1) {
if (fld[i][j])
ans += ((ll)a[i + 1] - a[i]) * (b[j + 1] - b[j]);
}
printf("%lld\n", ans);
}
|
replace
| 7 | 8 | 7 | 8 |
0
| |
p02358
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) for (int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x) cerr << #x << " = " << (x) << endl;
#define debug(x) \
cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" \
<< " " << __FILE__ << endl;
const long long INF = 2000000000;
using namespace std;
const long long MAX_N = 2005;
long long g[MAX_N][MAX_N] = {0};
long long imos[MAX_N][MAX_N] = {0};
void compress(vector<long long> &v) {
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
}
long long lb(vector<long long> v, long long num) {
return lower_bound(all(v), num) - v.begin();
}
int main() {
long long n;
cin >> n;
pair<pair<long long, long long>, pair<long long, long long>> inp[MAX_N];
vector<long long> x, y;
rep(i, n) {
cin >> inp[i].first.first >> inp[i].first.second >> inp[i].second.first >>
inp[i].second.second;
x.emplace_back(inp[i].first.first);
x.emplace_back(inp[i].second.first);
y.emplace_back(inp[i].first.second);
y.emplace_back(inp[i].second.second);
}
compress(x);
compress(y);
rep(i, n) {
long long lx = lb(x, inp[i].first.first);
long long ly = lb(y, inp[i].first.second);
long long rx = lb(x, inp[i].second.first);
long long ry = lb(y, inp[i].second.second);
for (long long j = ly; j < ry; j++)
g[j][lx]++;
for (long long j = ly; j < ry; j++)
g[j][rx]--;
}
rep(i, y.size()) {
long long c = 0;
rep(j, x.size()) {
c += g[i][j];
imos[i][j] = c;
}
}
// rep(i,5){ rep(j,5){ cout << imos[i][j] << ' '; } cout << endl; }
long long sum = 0;
range(i, 0, y.size()) {
range(j, 0, x.size()) {
if (imos[i][j] != 0) {
// if(imos[i][j] != 0 && imos[i][j + 1] != 0 && imos[i + 1][j] != 0 &&
// imos[i + 1][j + 1] != 0){ cout << (y[i + 1] - y[i]) << ' ' << (x[j
// + 1] - x[j]) << endl;; sum += (y[i + 1] - y[i]) * (x[j + 1] - x[j]);
sum += abs(y[i + 1] - y[i]) * abs(x[j + 1] - x[j]);
}
}
}
cout << sum << endl;
}
|
#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) for (int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x) cerr << #x << " = " << (x) << endl;
#define debug(x) \
cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" \
<< " " << __FILE__ << endl;
const long long INF = 2000000000;
using namespace std;
const long long MAX_N = 4005;
long long g[MAX_N][MAX_N] = {0};
long long imos[MAX_N][MAX_N] = {0};
void compress(vector<long long> &v) {
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
}
long long lb(vector<long long> v, long long num) {
return lower_bound(all(v), num) - v.begin();
}
int main() {
long long n;
cin >> n;
pair<pair<long long, long long>, pair<long long, long long>> inp[MAX_N];
vector<long long> x, y;
rep(i, n) {
cin >> inp[i].first.first >> inp[i].first.second >> inp[i].second.first >>
inp[i].second.second;
x.emplace_back(inp[i].first.first);
x.emplace_back(inp[i].second.first);
y.emplace_back(inp[i].first.second);
y.emplace_back(inp[i].second.second);
}
compress(x);
compress(y);
rep(i, n) {
long long lx = lb(x, inp[i].first.first);
long long ly = lb(y, inp[i].first.second);
long long rx = lb(x, inp[i].second.first);
long long ry = lb(y, inp[i].second.second);
for (long long j = ly; j < ry; j++)
g[j][lx]++;
for (long long j = ly; j < ry; j++)
g[j][rx]--;
}
rep(i, y.size()) {
long long c = 0;
rep(j, x.size()) {
c += g[i][j];
imos[i][j] = c;
}
}
// rep(i,5){ rep(j,5){ cout << imos[i][j] << ' '; } cout << endl; }
long long sum = 0;
range(i, 0, y.size()) {
range(j, 0, x.size()) {
if (imos[i][j] != 0) {
// if(imos[i][j] != 0 && imos[i][j + 1] != 0 && imos[i + 1][j] != 0 &&
// imos[i + 1][j + 1] != 0){ cout << (y[i + 1] - y[i]) << ' ' << (x[j
// + 1] - x[j]) << endl;; sum += (y[i + 1] - y[i]) * (x[j + 1] - x[j]);
sum += abs(y[i + 1] - y[i]) * abs(x[j + 1] - x[j]);
}
}
}
cout << sum << endl;
}
|
replace
| 11 | 12 | 11 | 12 |
0
| |
p02358
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
#define x first
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<int, pii> pip;
struct node {
int num, l, r, ls, rs;
node(int _l, int _r) {
num = 0;
l = _l;
r = _r;
}
};
vector<node> T;
vector<ll> d;
int make(int l, int r) {
int id = T.size();
T.push_back(node(l, r));
T[id].ls = (l + 1 == r ? -1 : make(l, (l + r) / 2));
T[id].rs = (l + 1 == r ? -1 : make((l + r) / 2, r));
return id;
}
void update(int id, int l, int r, int t) {
if (l == r || T[id].l >= r || T[id].r <= l)
return;
if (T[id].l >= l && T[id].r <= r) {
T[id].num += t;
return;
}
int mid = (T[id].l + T[id].r) >> 1;
if (l < mid)
update(T[id].ls, l, r, t);
if (r > mid)
update(T[id].rs, l, r, t);
int mi = min(T[T[id].ls].num, T[T[id].rs].num);
T[id].num += mi;
T[T[id].ls].num -= mi;
T[T[id].rs].num -= mi;
}
ll gettot(int id) {
if (id == -1)
return 0;
if (T[id].num > 0)
return d[T[id].r] - d[T[id].l];
return gettot(T[id].ls) + gettot(T[id].rs);
}
int main() {
T.reserve(1 << 30 - 1);
int n;
scanf("%d", &n);
set<int> s;
priority_queue<pip, vector<pip>, greater<pip>> pq;
for (int i = 0; i < n; i++) {
int x1, y1, x2, y2;
scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
s.insert(y1);
s.insert(y2);
pq.push(make_pair(x1, make_pair(y1, y2)));
pq.push(make_pair(x2, make_pair(y2, y1)));
}
map<int, int> y;
d.clear();
for (set<int>::iterator it = s.begin(); it != s.end(); it++) {
y[*it] = d.size();
d.push_back(*it);
}
make(0, d.size() - 1);
ll ans = 0;
while (true) {
pip t = pq.top();
pq.pop();
if (pq.empty())
break;
if (t.second.first < t.second.second)
update(0, y[t.second.first], y[t.second.second], 1);
else
update(0, y[t.second.second], y[t.second.first], -1);
ans += gettot(0) * (pq.top().x - t.x);
}
printf("%lld\n", ans);
return 0;
}
|
#include <bits/stdc++.h>
#define x first
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<int, pii> pip;
struct node {
int num, l, r, ls, rs;
node(int _l, int _r) {
num = 0;
l = _l;
r = _r;
}
};
vector<node> T;
vector<ll> d;
int make(int l, int r) {
int id = T.size();
T.push_back(node(l, r));
T[id].ls = (l + 1 == r ? -1 : make(l, (l + r) / 2));
T[id].rs = (l + 1 == r ? -1 : make((l + r) / 2, r));
return id;
}
void update(int id, int l, int r, int t) {
if (l == r || T[id].l >= r || T[id].r <= l)
return;
if (T[id].l >= l && T[id].r <= r) {
T[id].num += t;
return;
}
int mid = (T[id].l + T[id].r) >> 1;
if (l < mid)
update(T[id].ls, l, r, t);
if (r > mid)
update(T[id].rs, l, r, t);
int mi = min(T[T[id].ls].num, T[T[id].rs].num);
T[id].num += mi;
T[T[id].ls].num -= mi;
T[T[id].rs].num -= mi;
}
ll gettot(int id) {
if (id == -1)
return 0;
if (T[id].num > 0)
return d[T[id].r] - d[T[id].l];
return gettot(T[id].ls) + gettot(T[id].rs);
}
int main() {
T.reserve(50000);
int n;
scanf("%d", &n);
set<int> s;
priority_queue<pip, vector<pip>, greater<pip>> pq;
for (int i = 0; i < n; i++) {
int x1, y1, x2, y2;
scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
s.insert(y1);
s.insert(y2);
pq.push(make_pair(x1, make_pair(y1, y2)));
pq.push(make_pair(x2, make_pair(y2, y1)));
}
map<int, int> y;
d.clear();
for (set<int>::iterator it = s.begin(); it != s.end(); it++) {
y[*it] = d.size();
d.push_back(*it);
}
make(0, d.size() - 1);
ll ans = 0;
while (true) {
pip t = pq.top();
pq.pop();
if (pq.empty())
break;
if (t.second.first < t.second.second)
update(0, y[t.second.first], y[t.second.second], 1);
else
update(0, y[t.second.second], y[t.second.first], -1);
ans += gettot(0) * (pq.top().x - t.x);
}
printf("%lld\n", ans);
return 0;
}
|
replace
| 56 | 57 | 56 | 57 |
-6
|
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
|
p02358
|
C++
|
Memory Limit Exceeded
|
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef pair<int, pii> pip;
typedef vector<pip> vip;
const double PI = acos(-1);
const double EPS = 1e-8;
const int inf = 1e8;
int main() {
int n;
cin >> n;
vvi in(n, vi(4));
rep(i, n) rep(j, 4) cin >> in[i][j];
vi x, y;
rep(i, n) rep(j, 4) loop(k, -1, 2) if (j % 2) y.pb(in[i][j] + k);
else x.pb(in[i][j] + k);
sort(all(x));
sort(all(y));
x.erase(unique(all(x)), x.end());
y.erase(unique(all(y)), y.end());
vvi tmp = in;
rep(i, n) rep(j, 4) if (j % 2) tmp[i][j] =
find(all(y), tmp[i][j]) - y.begin();
else tmp[i][j] = find(all(x), tmp[i][j]) - x.begin();
vvi field(x.size(), vi(y.size()));
rep(i, n) loop(j, tmp[i][0], tmp[i][2]) loop(k, tmp[i][1], tmp[i][3])
field[j][k] = true;
ll out = 0;
rep(i, x.size()) rep(j, y.size()) if (field[i][j]) out +=
(ll)(x[i + 1] - x[i]) * (y[j + 1] - y[j]);
cout << out << endl;
}
|
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef pair<int, pii> pip;
typedef vector<pip> vip;
const double PI = acos(-1);
const double EPS = 1e-8;
const int inf = 1e8;
int main() {
int n;
cin >> n;
vvi in(n, vi(4));
rep(i, n) rep(j, 4) cin >> in[i][j];
vi x, y;
rep(i, n) rep(j, 4) if (j % 2) y.pb(in[i][j]);
else x.pb(in[i][j]);
sort(all(x));
sort(all(y));
x.erase(unique(all(x)), x.end());
y.erase(unique(all(y)), y.end());
vvi tmp = in;
rep(i, n) rep(j, 4) if (j % 2) tmp[i][j] =
find(all(y), tmp[i][j]) - y.begin();
else tmp[i][j] = find(all(x), tmp[i][j]) - x.begin();
vvi field(x.size(), vi(y.size()));
rep(i, n) loop(j, tmp[i][0], tmp[i][2]) loop(k, tmp[i][1], tmp[i][3])
field[j][k] = true;
ll out = 0;
rep(i, x.size()) rep(j, y.size()) if (field[i][j]) out +=
(ll)(x[i + 1] - x[i]) * (y[j + 1] - y[j]);
cout << out << endl;
}
|
replace
| 38 | 40 | 38 | 40 |
MLE
| |
p02360
|
C++
|
Runtime Error
|
#include <iostream>
#include <vector>
struct Square {
int x1, y1, x2, y2;
};
std::istream &operator>>(std::istream &is, Square &s) {
return is >> s.x1 >> s.y1 >> s.x2 >> s.y2;
}
int main() {
int n;
std::cin >> n;
constexpr int max_coord = 4;
std::vector<std::vector<int>> map(max_coord + 1,
std::vector<int>(max_coord + 1, 0));
Square s;
for (auto i = 0; i < n; ++i) {
std::cin >> s;
map[s.x1][s.y1]++;
map[s.x2][s.y1]--;
map[s.x1][s.y2]--;
map[s.x2][s.y2]++;
}
for (auto i = 1; i < map.size(); ++i) {
for (auto j = 0; j < map[i].size(); ++j) {
map[i][j] += map[i - 1][j];
}
}
int max = 0;
for (auto i = 0; i < map.size(); ++i) {
if (max < map[i][0])
max = map[i][0];
for (auto j = 1; j < map[i].size(); ++j) {
map[i][j] += map[i][j - 1];
if (max < map[i][j])
max = map[i][j];
}
}
std::cout << max << std::endl;
}
|
#include <iostream>
#include <vector>
struct Square {
int x1, y1, x2, y2;
};
std::istream &operator>>(std::istream &is, Square &s) {
return is >> s.x1 >> s.y1 >> s.x2 >> s.y2;
}
int main() {
int n;
std::cin >> n;
constexpr int max_coord = 1000;
std::vector<std::vector<int>> map(max_coord + 1,
std::vector<int>(max_coord + 1, 0));
Square s;
for (auto i = 0; i < n; ++i) {
std::cin >> s;
map[s.x1][s.y1]++;
map[s.x2][s.y1]--;
map[s.x1][s.y2]--;
map[s.x2][s.y2]++;
}
for (auto i = 1; i < map.size(); ++i) {
for (auto j = 0; j < map[i].size(); ++j) {
map[i][j] += map[i - 1][j];
}
}
int max = 0;
for (auto i = 0; i < map.size(); ++i) {
if (max < map[i][0])
max = map[i][0];
for (auto j = 1; j < map[i].size(); ++j) {
map[i][j] += map[i][j - 1];
if (max < map[i][j])
max = map[i][j];
}
}
std::cout << max << std::endl;
}
|
replace
| 12 | 13 | 12 | 13 |
0
| |
p02361
|
C++
|
Runtime Error
|
#include <algorithm>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define REP(i, j) for (int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for (int i = (int)(j); i < (int)(k); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
// typedef complex<double> P;
typedef pair<int, int> P;
const int MAX_V = 100010;
const int INF = 1e9 + 7;
int dijkstra(const vector<vector<P>> &cost, int s, int t, int V) {
priority_queue<P, vector<P>, greater<P>> open;
open.push(P(s, 0));
int closed[MAX_V];
REP(i, V) closed[i] = INF;
while (!open.empty()) {
P tmp = open.top();
open.pop();
int now = tmp.first, c = tmp.second;
if (closed[now] < c)
continue;
closed[now] = c;
REP(i, cost[now].size()) {
int next = cost[now][i].first, nc = cost[now][i].second;
if (nc == INF || c + nc >= closed[next])
continue;
closed[next] = c + nc;
open.push(P(next, closed[next]));
}
}
REP(i, V) {
if (closed[i] == INF)
cout << "INF" << endl;
else
cout << closed[i] << endl;
}
return closed[t];
}
int main() {
int V, E, r;
cin >> V >> E >> r;
vector<vector<P>> cost(V);
REP(i, E) {
int f, t, c;
cin >> f >> t >> c;
cost[f].push_back(P(t, c));
}
REP(i, E) {
if ((int)cost[i].size() <= 0)
continue;
SORT(cost[i]);
vector<P> tmp;
int now = cost[i][0].first;
tmp.push_back(cost[i][0]);
FOR(j, 1, cost[i].size()) {
if (now == cost[i][j].first)
continue;
now = cost[i][j].first;
tmp.push_back(cost[i][j]);
}
cost[i] = tmp;
}
dijkstra(cost, r, -1, V);
return 0;
}
|
#include <algorithm>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define REP(i, j) for (int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for (int i = (int)(j); i < (int)(k); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
// typedef complex<double> P;
typedef pair<int, int> P;
const int MAX_V = 100010;
const int INF = 1e9 + 7;
int dijkstra(const vector<vector<P>> &cost, int s, int t, int V) {
priority_queue<P, vector<P>, greater<P>> open;
open.push(P(s, 0));
int closed[MAX_V];
REP(i, V) closed[i] = INF;
while (!open.empty()) {
P tmp = open.top();
open.pop();
int now = tmp.first, c = tmp.second;
if (closed[now] < c)
continue;
closed[now] = c;
REP(i, cost[now].size()) {
int next = cost[now][i].first, nc = cost[now][i].second;
if (nc == INF || c + nc >= closed[next])
continue;
closed[next] = c + nc;
open.push(P(next, closed[next]));
}
}
REP(i, V) {
if (closed[i] == INF)
cout << "INF" << endl;
else
cout << closed[i] << endl;
}
return closed[t];
}
int main() {
int V, E, r;
cin >> V >> E >> r;
vector<vector<P>> cost(V);
REP(i, E) {
int f, t, c;
cin >> f >> t >> c;
cost[f].push_back(P(t, c));
}
// REP(i, E){
// if((int)cost[i].size() <= 0) continue;
// SORT(cost[i]);
// vector<P> tmp;
// int now = cost[i][0].first;
// tmp.push_back(cost[i][0]);
// FOR(j, 1, cost[i].size()){
// if(now == cost[i][j].first) continue;
// now = cost[i][j].first;
// tmp.push_back(cost[i][j]);
// }
// cost[i] = tmp;
// }
dijkstra(cost, r, -1, V);
return 0;
}
|
replace
| 67 | 82 | 67 | 80 |
-11
| |
p02361
|
C++
|
Time Limit Exceeded
|
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define pb push_back
#define INF 1010001000
#define loop(i, n) for (int i = 0; i < n; i++)
typedef pair<int, int> i_i;
typedef struct edge {
int st, en, cst;
} Edge;
int main() {
int v, e, r;
cin >> v >> e >> r;
vector<vector<i_i>> G(v);
vector<int> ans(v, INF);
loop(i, e) {
int st, en, d;
cin >> st >> en >> d;
G[st].pb(i_i(d, en));
}
priority_queue<i_i, vector<i_i>, greater<i_i>> pq;
pq.push(i_i(0, r));
ans[r] = 0;
while (!pq.empty()) {
i_i p = pq.top();
pq.pop();
int u = p.second;
if (ans[u] < p.first) {
continue;
}
for (i_i i : G[u]) {
if (ans[i.second] > ans[u] + i.first) {
ans[i.second] = ans[u] + i.first;
pq.push(i_i(ans[i.second], i.second));
}
}
}
loop(i, v) {
if (ans[i] != INF) {
cout << ans[i] << endl;
} else {
cout << "INF" << endl;
}
}
return 0;
}
|
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define pb push_back
#define INF 1010001000
#define loop(i, n) for (int i = 0; i < n; i++)
typedef pair<int, int> i_i;
typedef struct edge {
int st, en, cst;
} Edge;
int main() {
int v, e, r;
cin >> v >> e >> r;
vector<vector<i_i>> G(v);
vector<int> ans(v, INF);
loop(i, e) {
int st, en, d;
cin >> st >> en >> d;
G[st].pb(i_i(d, en));
}
priority_queue<i_i, vector<i_i>, greater<i_i>> pq;
pq.push(i_i(0, r));
ans[r] = 0;
while (!pq.empty()) {
i_i p = pq.top();
pq.pop();
int u = p.second;
if (ans[u] < p.first) {
continue;
}
for (int j = 0; j < G[u].size(); j++) {
i_i i = G[u][j];
if (ans[i.second] > ans[u] + i.first) {
ans[i.second] = ans[u] + i.first;
pq.push(i_i(ans[i.second], i.second));
}
}
}
loop(i, v) {
if (ans[i] != INF) {
cout << ans[i] << endl;
} else {
cout << "INF" << endl;
}
}
return 0;
}
|
replace
| 37 | 38 | 37 | 39 |
TLE
| |
p02361
|
C++
|
Runtime Error
|
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <vector>
using namespace std;
#define int long long
inline int in() {
int32_t x;
scanf("%d", &x);
return x;
}
inline string getStr() {
char ch[200000];
scanf("%s", ch);
return ch;
}
inline char getCh() {
char ch;
scanf(" %c", &ch);
return ch;
}
template <class P, class Q> inline P smin(P &a, Q b) {
if (b < a)
a = b;
return a;
}
template <class P, class Q> inline P smax(P &a, Q b) {
if (a < b)
a = b;
return a;
}
const int MOD = 1e9 + 7;
const int MAX_N = 5e3 + 10;
const int MA_LG = 21;
const int base = 29;
vector<pair<int, int>> g[MAX_N];
int res[MAX_N];
bool vis[MAX_N];
int32_t main() {
int n = in(), m = in(), source = in();
for (int i = 0; i < m; i++) {
int v = in(), u = in(), w = in();
g[v].push_back({u, w});
}
for (int v = 0; v < n; v++)
res[v] = 1e18;
res[source] = 0;
priority_queue<pair<int, int>> pq;
pq.push({0, source});
res[source] = 0;
while (!pq.empty()) {
int v = pq.top().second;
int di = -pq.top().first;
pq.pop();
if (vis[v] && res[v] <= di)
continue;
vis[v] = true;
res[v] = di;
for (int pt = 0; pt < g[v].size(); pt++) {
int u = g[v][pt].first, w = g[v][pt].second;
if (res[u] > w + di) {
if (vis[u] && w + di < 0) {
cout << "NEGATIVE CYCLE\n";
exit(0);
}
// res[u] = w + di;
pq.push({-w - di, u});
}
}
}
for (int i = 0; i < n; i++) {
if (res[i] >= 1e18) {
cout << "INF\n";
} else
cout << res[i] << "\n";
}
}
|
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <vector>
using namespace std;
#define int long long
inline int in() {
int32_t x;
scanf("%d", &x);
return x;
}
inline string getStr() {
char ch[200000];
scanf("%s", ch);
return ch;
}
inline char getCh() {
char ch;
scanf(" %c", &ch);
return ch;
}
template <class P, class Q> inline P smin(P &a, Q b) {
if (b < a)
a = b;
return a;
}
template <class P, class Q> inline P smax(P &a, Q b) {
if (a < b)
a = b;
return a;
}
const int MOD = 1e9 + 7;
const int MAX_N = 1e5 + 10;
const int MA_LG = 21;
const int base = 29;
vector<pair<int, int>> g[MAX_N];
int res[MAX_N];
bool vis[MAX_N];
int32_t main() {
int n = in(), m = in(), source = in();
for (int i = 0; i < m; i++) {
int v = in(), u = in(), w = in();
g[v].push_back({u, w});
}
for (int v = 0; v < n; v++)
res[v] = 1e18;
res[source] = 0;
priority_queue<pair<int, int>> pq;
pq.push({0, source});
res[source] = 0;
while (!pq.empty()) {
int v = pq.top().second;
int di = -pq.top().first;
pq.pop();
if (vis[v] && res[v] <= di)
continue;
vis[v] = true;
res[v] = di;
for (int pt = 0; pt < g[v].size(); pt++) {
int u = g[v][pt].first, w = g[v][pt].second;
if (res[u] > w + di) {
if (vis[u] && w + di < 0) {
cout << "NEGATIVE CYCLE\n";
exit(0);
}
// res[u] = w + di;
pq.push({-w - di, u});
}
}
}
for (int i = 0; i < n; i++) {
if (res[i] >= 1e18) {
cout << "INF\n";
} else
cout << res[i] << "\n";
}
}
|
replace
| 35 | 36 | 35 | 36 |
0
| |
p02361
|
C++
|
Runtime Error
|
#include "bits/stdc++.h"
using namespace std;
typedef long long lint;
#define MAXN 100010
// d,edge,g,P,q
lint d[MAXN];
struct edge {
int to;
lint cost;
};
vector<edge> g[MAXN];
typedef pair<lint, int> P;
priority_queue<P, vector<P>, greater<P>> q;
void pfs(int s) {
d[s] = 0;
q.push({0, s});
lint cd, v;
while (q.size()) {
cd = q.top().first;
v = q.top().second;
q.pop();
if (cd > d[v])
continue;
for (edge &e : g[v])
if (d[e.to] > cd + e.cost) {
d[e.to] = cd + e.cost;
q.push({d[e.to], e.to});
}
}
}
#define rep(i, n) for (int i = 0; i < (n); ++i)
int main() {
lint v, e, r;
cin >> v >> e >> r;
rep(i, v) d[i] = INT_MAX;
int a, b;
lint c;
rep(i, e) {
cin >> a >> b >> c;
g[a].push_back({b, c});
}
pfs(r);
rep(i, v) {
if (d[i] == INT_MAX)
cout << "INF¥n";
else
cout << d[i] << endl;
}
}
|
#include "bits/stdc++.h"
using namespace std;
typedef long long lint;
#define MAXN 100010
// d,edge,g,P,q
lint d[MAXN];
struct edge {
int to;
lint cost;
};
vector<edge> g[MAXN];
typedef pair<lint, int> P;
priority_queue<P, vector<P>, greater<P>> q;
void pfs(int s) {
d[s] = 0;
q.push({0, s});
lint cd, v;
while (q.size()) {
cd = q.top().first;
v = q.top().second;
q.pop();
if (cd > d[v])
continue;
for (edge &e : g[v])
if (d[e.to] > cd + e.cost) {
d[e.to] = cd + e.cost;
q.push({d[e.to], e.to});
}
}
}
#define rep(i, n) for (int i = 0; i < (n); ++i)
int main() {
lint v, e, r;
cin >> v >> e >> r;
rep(i, v) d[i] = INT_MAX;
int a, b;
lint c;
rep(i, e) {
cin >> a >> b >> c;
g[a].push_back({b, c});
}
pfs(r);
rep(i, v) {
if (d[i] == INT_MAX)
cout << "INF" << endl;
else
cout << d[i] << endl;
}
}
|
replace
| 48 | 49 | 48 | 49 |
0
| |
p02361
|
C++
|
Time Limit Exceeded
|
#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define srep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)
#define each(a, b) for (auto(a) : (b))
#define all(v) (v).begin(), (v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)), v.erase(unique(all(v)), v.end())
#define cmx(x, y) x = max(x, y)
#define cmn(x, y) x = min(x, y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout << #x << " = " << (x) << endl
#define spair(p) cout << #p << ": " << p.fi << " " << p.se << endl
#define svec(v) \
cout << #v << ":"; \
rep(kbrni, v.size()) cout << " " << v[kbrni]; \
cout << endl
#define sset(s) \
cout << #s << ":"; \
each(kbrni, s) cout << " " << kbrni; \
cout << endl
#define smap(m) \
cout << #m << ":"; \
each(kbrni, m) cout << " {" << kbrni.first << ":" << kbrni.second << "}"; \
cout << endl
using namespace std;
typedef pair<int, int> P;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;
const int MAX_N = 100005;
template <typename T> class Heap {
public:
struct node {
node *l;
node *r;
T val;
node(T t) : l(nullptr), r(nullptr), val(t) {}
};
node *root;
Heap() : root(nullptr) {}
node *meld(node *a, node *b) {
if (!a)
return b;
if (!b)
return a;
if (a->val < b->val)
swap(a, b);
a->r = meld(a->r, b);
swap(a->l, a->r);
return a;
}
void push(T val) {
node *p = new node(val);
root = meld(root, p);
}
T top() { return root->val; }
void pop() {
node *p = root;
root = meld(root->r, root->l);
delete p;
}
};
struct edge {
int to, cost;
};
int d[MAX_N];
vector<edge> G[MAX_N];
void dijkstra(int s) {
Heap<P> que;
d[s] = 0;
que.push(P(0, s));
while (que.root) {
P p = que.top();
que.pop();
int v = p.second;
if (d[v] < p.first)
continue;
for (auto w : G[v]) {
if (d[w.to] > d[v] + w.cost) {
d[w.to] = d[v] + w.cost;
que.push(P(d[w.to], w.to));
}
}
}
}
int main() {
int n, m, s;
scanf("%d%d%d", &n, &m, &s);
rep(i, m) {
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
G[a].pb((edge){b, c});
}
fill(d, d + n, INF);
dijkstra(s);
rep(i, n) {
if (d[i] == INF) {
cout << "INF\n";
} else {
cout << d[i] << "\n";
}
}
return 0;
}
|
#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define srep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)
#define each(a, b) for (auto(a) : (b))
#define all(v) (v).begin(), (v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)), v.erase(unique(all(v)), v.end())
#define cmx(x, y) x = max(x, y)
#define cmn(x, y) x = min(x, y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout << #x << " = " << (x) << endl
#define spair(p) cout << #p << ": " << p.fi << " " << p.se << endl
#define svec(v) \
cout << #v << ":"; \
rep(kbrni, v.size()) cout << " " << v[kbrni]; \
cout << endl
#define sset(s) \
cout << #s << ":"; \
each(kbrni, s) cout << " " << kbrni; \
cout << endl
#define smap(m) \
cout << #m << ":"; \
each(kbrni, m) cout << " {" << kbrni.first << ":" << kbrni.second << "}"; \
cout << endl
using namespace std;
typedef pair<int, int> P;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;
const int MAX_N = 100005;
template <typename T> class Heap {
public:
struct node {
node *l;
node *r;
T val;
node(T t) : l(nullptr), r(nullptr), val(t) {}
};
node *root;
Heap() : root(nullptr) {}
node *meld(node *a, node *b) {
if (!a)
return b;
if (!b)
return a;
if (a->val > b->val)
swap(a, b);
a->r = meld(a->r, b);
swap(a->l, a->r);
return a;
}
void push(T val) {
node *p = new node(val);
root = meld(root, p);
}
T top() { return root->val; }
void pop() {
node *p = root;
root = meld(root->r, root->l);
delete p;
}
};
struct edge {
int to, cost;
};
int d[MAX_N];
vector<edge> G[MAX_N];
void dijkstra(int s) {
Heap<P> que;
d[s] = 0;
que.push(P(0, s));
while (que.root) {
P p = que.top();
que.pop();
int v = p.second;
if (d[v] < p.first)
continue;
for (auto w : G[v]) {
if (d[w.to] > d[v] + w.cost) {
d[w.to] = d[v] + w.cost;
que.push(P(d[w.to], w.to));
}
}
}
}
int main() {
int n, m, s;
scanf("%d%d%d", &n, &m, &s);
rep(i, m) {
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
G[a].pb((edge){b, c});
}
fill(d, d + n, INF);
dijkstra(s);
rep(i, n) {
if (d[i] == INF) {
cout << "INF\n";
} else {
cout << d[i] << "\n";
}
}
return 0;
}
|
replace
| 60 | 61 | 60 | 61 |
TLE
| |
p02361
|
C++
|
Runtime Error
|
#include <climits>
#include <iostream>
#include <queue>
#include <vector>
#define INF 1e+18
using namespace std;
struct Edge {
int trg;
long long w;
};
struct vertex {
int v;
long long w;
bool operator<(const vertex &x) const { return w < x.w; }
bool operator>(const vertex &x) const { return w > x.w; }
};
const int MAX_V = 10000;
vector<vector<Edge>> graph(MAX_V);
vector<long long> min_cost(MAX_V, INF);
vector<bool> used(MAX_V, false);
void dijkstra(int s, int V) {
min_cost[s] = 0;
priority_queue<vertex, vector<vertex>, greater<vertex>> pq;
pq.push(vertex{s, 0});
while (!pq.empty()) {
vertex ver = pq.top();
pq.pop();
int v = ver.v;
if (used[v])
continue;
used[v] = true;
min_cost[v] = ver.w;
for (int i = 0; i < graph[v].size(); i++) {
if (!used[graph[v][i].trg]) {
pq.push(vertex{graph[v][i].trg, min_cost[v] + graph[v][i].w});
}
}
}
}
void print_array(vector<long long> &array, int node) {
for (int i = 0; i < node; i++) {
if (array[i] == INF) {
cout << "INF" << endl;
} else {
cout << array[i] << endl;
}
}
}
int main() {
int V, E, r;
cin >> V >> E >> r;
int s, t, d;
for (int i = 0; i < E; i++) {
cin >> s >> t >> d;
graph[s].push_back(Edge{t, d});
}
dijkstra(r, V);
print_array(min_cost, V);
return 0;
}
|
#include <climits>
#include <iostream>
#include <queue>
#include <vector>
#define INF 1e+18
using namespace std;
struct Edge {
int trg;
long long w;
};
struct vertex {
int v;
long long w;
bool operator<(const vertex &x) const { return w < x.w; }
bool operator>(const vertex &x) const { return w > x.w; }
};
const int MAX_V = 100000;
vector<vector<Edge>> graph(MAX_V);
vector<long long> min_cost(MAX_V, INF);
vector<bool> used(MAX_V, false);
void dijkstra(int s, int V) {
min_cost[s] = 0;
priority_queue<vertex, vector<vertex>, greater<vertex>> pq;
pq.push(vertex{s, 0});
while (!pq.empty()) {
vertex ver = pq.top();
pq.pop();
int v = ver.v;
if (used[v])
continue;
used[v] = true;
min_cost[v] = ver.w;
for (int i = 0; i < graph[v].size(); i++) {
if (!used[graph[v][i].trg]) {
pq.push(vertex{graph[v][i].trg, min_cost[v] + graph[v][i].w});
}
}
}
}
void print_array(vector<long long> &array, int node) {
for (int i = 0; i < node; i++) {
if (array[i] == INF) {
cout << "INF" << endl;
} else {
cout << array[i] << endl;
}
}
}
int main() {
int V, E, r;
cin >> V >> E >> r;
int s, t, d;
for (int i = 0; i < E; i++) {
cin >> s >> t >> d;
graph[s].push_back(Edge{t, d});
}
dijkstra(r, V);
print_array(min_cost, V);
return 0;
}
|
replace
| 19 | 20 | 19 | 20 |
0
| |
p02361
|
C++
|
Memory Limit Exceeded
|
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int(i) = 0; (i) < (n); ++(i))
const int INF_32 = -1 + (1 << 30);
template <class Abel> struct D_Graph {
int V, E;
bool **exist;
vector<vector<pair<int, Abel>>> adj;
vector<vector<pair<int, Abel>>> adj_inv;
D_Graph(int V_size = 0) { init(V_size); }
void init(int V_size) {
V = V_size, E = 0;
/*exist = (bool**)malloc(sizeof(bool*) * (V + 1));
for(int i = 0; i <= V; ++i){
exist[i] = (bool*)malloc(sizeof(bool) * (V + 1));
fill(exist[i],exist[i] + V + 1,0);
}*/
adj.clear();
adj_inv.clear();
adj.resize(V + 1);
adj_inv.resize(V + 1);
adj[0].resize(V);
for (int i = 1; i <= V; ++i) {
adj[0][i - 1] = {i, 0};
// exist[0][i] = 1;
adj[i].reserve(min(V, 1000));
adj_inv[i].reserve(min(V, 1000));
}
}
int deg_out(int v) { return adj[v].size(); }
int deg_in(int v) { return adj_inv[v].size(); }
bool is_leaf(int v) { return deg_out(v) == 0; }
bool is_root(int v) { return deg_in(v) == 0; }
void add_edge(int from, int to, Abel cost = 1) {
adj[from].push_back({to, cost});
adj_inv[to].push_back({from, cost});
// exist[from][to] = 1;
++E;
}
void sort_edge() {
for (int i = 0; i <= V; ++i)
sort_edge(i);
}
void sort_edge(int v) {
sort(begin(adj[v]), end(adj[v]));
sort(begin(adj_inv[v]), end(adj_inv[v]));
}
void dijkstra(vector<Abel> &dist, int src, Abel dist_max = INF_32) {
dist.clear();
dist.resize(V + 1);
fill(begin(dist), end(dist), dist_max);
priority_queue<pair<Abel, int>, vector<pair<Abel, int>>,
greater<pair<Abel, int>>>
que;
que.push({0, src});
while (!que.empty()) {
int d = que.top().first;
int v = que.top().second;
que.pop();
if (dist[v] <= d)
continue;
dist[v] = d;
for (auto e : adj[v])
que.push({e.second + d, e.first});
}
}
/*bool is_cyclic(){
vector<int> s;
return !tsort(s);
}
bool tsort(vector<int> &seq, bool idx_ord = 0) {
seq.clear();
seq.reserve(V + 1);
vector<int> in(V + 1);
for(int i = 0; i <= V; ++i) in[i] = deg_in(i);
stack<int> rt;
rt.push(0);
while(!rt.empty()){
int r = rt.top();
rt.pop();
for(auto e : adj[r]){
int v = e.first;
if(r) --in[v];
if(!in[v]){
rt.push(v);
if(idx_ord){
auto pos = begin(seq);
bool det = 0;
for(auto i = begin(seq); i < end(seq);
++i){ if(!det && v > *i) ++pos; else det = 1; if(exist[*i][v]){ pos = i + 1;
det = 0;
}
}
seq.insert(pos,v);
}else{
seq.push_back(v);
}
}
}
}
if(seq.size() == V) return 1;
return 0;
}*/
};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int V, E, r;
cin >> V >> E >> r;
D_Graph<int> G(V);
int s, t, d;
rep(i, E) {
cin >> s >> t >> d;
G.add_edge(++s, ++t, d);
}
vector<int> dist;
G.dijkstra(dist, ++r);
rep(i, V) {
if (dist[i + 1] < INF_32)
cout << dist[i + 1] << '\n';
else
cout << "INF\n";
}
}
|
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int(i) = 0; (i) < (n); ++(i))
const int INF_32 = -1 + (1 << 30);
template <class Abel> struct D_Graph {
int V, E;
bool **exist;
vector<vector<pair<int, Abel>>> adj;
vector<vector<pair<int, Abel>>> adj_inv;
D_Graph(int V_size = 0) { init(V_size); }
void init(int V_size) {
V = V_size, E = 0;
/*exist = (bool**)malloc(sizeof(bool*) * (V + 1));
for(int i = 0; i <= V; ++i){
exist[i] = (bool*)malloc(sizeof(bool) * (V + 1));
fill(exist[i],exist[i] + V + 1,0);
}*/
adj.clear();
adj_inv.clear();
adj.resize(V + 1);
adj_inv.resize(V + 1);
adj[0].resize(V);
for (int i = 1; i <= V; ++i) {
adj[0][i - 1] = {i, 0};
// exist[0][i] = 1;
// adj[i].reserve(min(V,1000));
// adj_inv[i].reserve(min(V,1000));
}
}
int deg_out(int v) { return adj[v].size(); }
int deg_in(int v) { return adj_inv[v].size(); }
bool is_leaf(int v) { return deg_out(v) == 0; }
bool is_root(int v) { return deg_in(v) == 0; }
void add_edge(int from, int to, Abel cost = 1) {
adj[from].push_back({to, cost});
adj_inv[to].push_back({from, cost});
// exist[from][to] = 1;
++E;
}
void sort_edge() {
for (int i = 0; i <= V; ++i)
sort_edge(i);
}
void sort_edge(int v) {
sort(begin(adj[v]), end(adj[v]));
sort(begin(adj_inv[v]), end(adj_inv[v]));
}
void dijkstra(vector<Abel> &dist, int src, Abel dist_max = INF_32) {
dist.clear();
dist.resize(V + 1);
fill(begin(dist), end(dist), dist_max);
priority_queue<pair<Abel, int>, vector<pair<Abel, int>>,
greater<pair<Abel, int>>>
que;
que.push({0, src});
while (!que.empty()) {
int d = que.top().first;
int v = que.top().second;
que.pop();
if (dist[v] <= d)
continue;
dist[v] = d;
for (auto e : adj[v])
que.push({e.second + d, e.first});
}
}
/*bool is_cyclic(){
vector<int> s;
return !tsort(s);
}
bool tsort(vector<int> &seq, bool idx_ord = 0) {
seq.clear();
seq.reserve(V + 1);
vector<int> in(V + 1);
for(int i = 0; i <= V; ++i) in[i] = deg_in(i);
stack<int> rt;
rt.push(0);
while(!rt.empty()){
int r = rt.top();
rt.pop();
for(auto e : adj[r]){
int v = e.first;
if(r) --in[v];
if(!in[v]){
rt.push(v);
if(idx_ord){
auto pos = begin(seq);
bool det = 0;
for(auto i = begin(seq); i < end(seq);
++i){ if(!det && v > *i) ++pos; else det = 1; if(exist[*i][v]){ pos = i + 1;
det = 0;
}
}
seq.insert(pos,v);
}else{
seq.push_back(v);
}
}
}
}
if(seq.size() == V) return 1;
return 0;
}*/
};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int V, E, r;
cin >> V >> E >> r;
D_Graph<int> G(V);
int s, t, d;
rep(i, E) {
cin >> s >> t >> d;
G.add_edge(++s, ++t, d);
}
vector<int> dist;
G.dijkstra(dist, ++r);
rep(i, V) {
if (dist[i + 1] < INF_32)
cout << dist[i + 1] << '\n';
else
cout << "INF\n";
}
}
|
replace
| 33 | 35 | 33 | 35 |
MLE
| |
p02361
|
C++
|
Runtime Error
|
#include <iostream>
#define MAX_V 1000
#define MAX_E 2000
#define INF 1e9
using namespace std;
struct edge {
int from, to, cost;
};
edge es[MAX_E];
int d[MAX_V];
int V;
int E;
bool find_negative_loop(int s) {
d[s] = 0;
int count = 0;
while (true) {
bool update = false;
count++;
for (int i = 0; i < MAX_E; i++) {
if (d[es[i].from] != INF && d[es[i].to] > d[es[i].from] + es[i].cost) {
if (count == V) {
return true;
}
d[es[i].to] = d[es[i].from] + es[i].cost;
update = true;
}
}
if (update == false) {
break;
}
}
return false;
}
int main() {
int r;
cin >> V >> E >> r;
for (int i = 0; i < MAX_V; i++) {
d[i] = INF;
}
for (int i = 0; i < E; i++) {
cin >> es[i].from >> es[i].to >> es[i].cost;
}
if (find_negative_loop(r)) {
cout << "NEGATIVE CYCLE" << endl;
} else {
for (int i = 0; i < V; i++) {
if (d[i] == INF) {
cout << "INF" << endl;
} else {
cout << d[i] << endl;
}
}
}
return 0;
}
|
#include <iostream>
#define MAX_V 100000
#define MAX_E 500000
#define INF 1e9
using namespace std;
struct edge {
int from, to, cost;
};
edge es[MAX_E];
int d[MAX_V];
int V;
int E;
bool find_negative_loop(int s) {
d[s] = 0;
int count = 0;
while (true) {
bool update = false;
count++;
for (int i = 0; i < MAX_E; i++) {
if (d[es[i].from] != INF && d[es[i].to] > d[es[i].from] + es[i].cost) {
if (count == V) {
return true;
}
d[es[i].to] = d[es[i].from] + es[i].cost;
update = true;
}
}
if (update == false) {
break;
}
}
return false;
}
int main() {
int r;
cin >> V >> E >> r;
for (int i = 0; i < MAX_V; i++) {
d[i] = INF;
}
for (int i = 0; i < E; i++) {
cin >> es[i].from >> es[i].to >> es[i].cost;
}
if (find_negative_loop(r)) {
cout << "NEGATIVE CYCLE" << endl;
} else {
for (int i = 0; i < V; i++) {
if (d[i] == INF) {
cout << "INF" << endl;
} else {
cout << d[i] << endl;
}
}
}
return 0;
}
|
replace
| 1 | 3 | 1 | 3 |
0
| |
p02361
|
C++
|
Runtime Error
|
#include <iostream>
#include <queue>
#include <set>
#include <vector>
using namespace std;
const int MAXN = 20020;
int NODOS = 0;
typedef int Costo;
const Costo INF = 1 << 30;
typedef pair<Costo, int> CostoNodo;
vector<CostoNodo> grafoCosto[MAXN];
const bool bi = false;
// Grafos con ponderacion.
// Nodos indexados de 0 a n - 1.
// bi = true -> Bidireccional.
// bi = false -> Dirigido.
struct Ponderada {
int x, y;
Costo c;
Ponderada() {}
Ponderada(int x_, int y_, int c_) : x(x_), y(y_), c(c_) {}
bool operator<(const Ponderada &q) const { return c < q.c ? true : false; }
};
void AgregarArista(int u, int v, Costo c) {
if (bi)
grafoCosto[v].push_back(CostoNodo(c, u));
grafoCosto[u].push_back(CostoNodo(c, v));
}
void limpia() {
for (int i = 0; i < NODOS; i++)
grafoCosto[i].clear();
}
// Algoritmo de dijkstra desde el nodo s.
// Devuelve el vector de distancias a todos
// los nodos desde s. Un valor INF indica que
// no es posible ir de s al respectivo nodo.
Costo dist[MAXN];
void Dijkstra(int s) {
for (int i = 0; i < NODOS; i++)
dist[i] = INF;
priority_queue<CostoNodo> pq;
pq.push(CostoNodo(0, s)), dist[s] = 0;
while (!pq.empty()) {
Costo p = -pq.top().first;
int u = pq.top().second;
pq.pop();
if (dist[u] < p)
continue;
for (CostoNodo arista : grafoCosto[u]) {
int v = arista.second;
p = dist[u] + arista.first;
if (p < dist[v])
dist[v] = p, pq.push(CostoNodo(-p, v));
}
}
}
int main() {
cin.tie(0);
ios_base::sync_with_stdio(0);
int casos, N, M, S, T, x, y, c;
cin >> N >> M >> S;
NODOS = N;
limpia();
for (int i = 0; i < M; i++) {
cin >> x >> y >> c;
AgregarArista(x, y, c);
}
Dijkstra(S);
for (int i = 0; i < NODOS; i++) {
if (dist[i] == INF) {
cout << "INF\n";
} else
cout << dist[i] << '\n';
}
return 0;
}
|
#include <iostream>
#include <queue>
#include <set>
#include <vector>
using namespace std;
const int MAXN = 200020;
int NODOS = 0;
typedef int Costo;
const Costo INF = 1 << 30;
typedef pair<Costo, int> CostoNodo;
vector<CostoNodo> grafoCosto[MAXN];
const bool bi = false;
// Grafos con ponderacion.
// Nodos indexados de 0 a n - 1.
// bi = true -> Bidireccional.
// bi = false -> Dirigido.
struct Ponderada {
int x, y;
Costo c;
Ponderada() {}
Ponderada(int x_, int y_, int c_) : x(x_), y(y_), c(c_) {}
bool operator<(const Ponderada &q) const { return c < q.c ? true : false; }
};
void AgregarArista(int u, int v, Costo c) {
if (bi)
grafoCosto[v].push_back(CostoNodo(c, u));
grafoCosto[u].push_back(CostoNodo(c, v));
}
void limpia() {
for (int i = 0; i < NODOS; i++)
grafoCosto[i].clear();
}
// Algoritmo de dijkstra desde el nodo s.
// Devuelve el vector de distancias a todos
// los nodos desde s. Un valor INF indica que
// no es posible ir de s al respectivo nodo.
Costo dist[MAXN];
void Dijkstra(int s) {
for (int i = 0; i < NODOS; i++)
dist[i] = INF;
priority_queue<CostoNodo> pq;
pq.push(CostoNodo(0, s)), dist[s] = 0;
while (!pq.empty()) {
Costo p = -pq.top().first;
int u = pq.top().second;
pq.pop();
if (dist[u] < p)
continue;
for (CostoNodo arista : grafoCosto[u]) {
int v = arista.second;
p = dist[u] + arista.first;
if (p < dist[v])
dist[v] = p, pq.push(CostoNodo(-p, v));
}
}
}
int main() {
cin.tie(0);
ios_base::sync_with_stdio(0);
int casos, N, M, S, T, x, y, c;
cin >> N >> M >> S;
NODOS = N;
limpia();
for (int i = 0; i < M; i++) {
cin >> x >> y >> c;
AgregarArista(x, y, c);
}
Dijkstra(S);
for (int i = 0; i < NODOS; i++) {
if (dist[i] == INF) {
cout << "INF\n";
} else
cout << dist[i] << '\n';
}
return 0;
}
|
replace
| 6 | 7 | 6 | 7 |
0
| |
p02361
|
C++
|
Runtime Error
|
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
typedef pair<long long, long long> P;
int v, e, r;
struct edge {
int to;
long long cost;
edge(int _to, long long _cost) : to(_to), cost(_cost) {}
};
vector<edge> G[100010];
long long d[10010];
void dijkstra(int start) {
priority_queue<P, vector<P>, greater<P>> que;
for (int i = 0; i < v; i++)
d[i] = 1e12;
d[start] = 0;
que.push(P(d[r], r));
while (!que.empty()) {
P p = que.top();
que.pop();
int v = p.second;
if (d[v] < p.first)
continue;
for (int i = 0; i < G[v].size(); i++) {
edge e = G[v][i];
if (d[e.to] > d[v] + e.cost) {
d[e.to] = d[v] + e.cost;
que.push(P(d[e.to], e.to));
}
}
}
}
int main() {
cin >> v >> e >> r;
for (int i = 0; i < e; i++) {
int s, t, d;
cin >> s >> t >> d;
G[s].push_back(edge(t, d));
}
dijkstra(r);
for (int i = 0; i < v; i++) {
if (d[i] != 1e12)
cout << d[i] << endl;
else
cout << "INF" << endl;
}
}
|
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
typedef pair<long long, long long> P;
int v, e, r;
struct edge {
int to;
long long cost;
edge(int _to, long long _cost) : to(_to), cost(_cost) {}
};
vector<edge> G[100010];
long long d[100100];
void dijkstra(int start) {
priority_queue<P, vector<P>, greater<P>> que;
for (int i = 0; i < v; i++)
d[i] = 1e12;
d[start] = 0;
que.push(P(d[r], r));
while (!que.empty()) {
P p = que.top();
que.pop();
int v = p.second;
if (d[v] < p.first)
continue;
for (int i = 0; i < G[v].size(); i++) {
edge e = G[v][i];
if (d[e.to] > d[v] + e.cost) {
d[e.to] = d[v] + e.cost;
que.push(P(d[e.to], e.to));
}
}
}
}
int main() {
cin >> v >> e >> r;
for (int i = 0; i < e; i++) {
int s, t, d;
cin >> s >> t >> d;
G[s].push_back(edge(t, d));
}
dijkstra(r);
for (int i = 0; i < v; i++) {
if (d[i] != 1e12)
cout << d[i] << endl;
else
cout << "INF" << endl;
}
}
|
replace
| 12 | 13 | 12 | 13 |
0
| |
p02361
|
C++
|
Time Limit Exceeded
|
#include <algorithm>
#include <queue>
#include <stdio.h>
#include <vector>
using namespace std;
#define MAX_V 100001
#define INF (1e9 + 1)
int V, E, r, a, b, c;
vector<int> G_t[MAX_V];
vector<int> G_c[MAX_V];
int d[MAX_V];
void dijkstra() {
int i;
for (i = 0; i < MAX_V; i++)
d[i] = INF;
d[r] = 0;
priority_queue<pair<int, int>> Q;
Q.push(make_pair(-d[r], r));
while (!Q.empty()) {
pair<int, int> p = Q.top();
Q.top();
int pos = p.second, cost = -p.first;
if (cost > d[pos])
continue;
for (i = 0; i < G_t[pos].size(); i++) {
int to = G_t[pos][i];
int newcost = cost + G_c[pos][i];
if (newcost < d[to]) {
d[to] = newcost;
Q.push(make_pair(-d[to], to));
}
}
}
for (i = 0; i < V; i++) {
if (d[i] == INF)
printf("INF\n");
else
printf("%d\n", d[i]);
}
}
int main() {
int i;
scanf("%d%d%d", &V, &E, &r);
for (i = 0; i < E; i++) {
scanf("%d%d%d", &a, &b, &c);
G_t[a].push_back(b);
G_c[a].push_back(c);
}
dijkstra();
return 0;
}
|
#include <algorithm>
#include <queue>
#include <stdio.h>
#include <vector>
using namespace std;
#define MAX_V 100001
#define INF (1e9 + 1)
int V, E, r, a, b, c;
vector<int> G_t[MAX_V];
vector<int> G_c[MAX_V];
int d[MAX_V];
void dijkstra() {
int i;
for (i = 0; i < MAX_V; i++)
d[i] = INF;
d[r] = 0;
priority_queue<pair<int, int>> Q;
Q.push(make_pair(-d[r], r));
while (!Q.empty()) {
pair<int, int> p = Q.top();
Q.pop();
int pos = p.second, cost = -p.first;
if (cost > d[pos])
continue;
for (i = 0; i < G_t[pos].size(); i++) {
int to = G_t[pos][i];
int newcost = cost + G_c[pos][i];
if (newcost < d[to]) {
d[to] = newcost;
Q.push(make_pair(-d[to], to));
}
}
}
for (i = 0; i < V; i++) {
if (d[i] == INF)
printf("INF\n");
else
printf("%d\n", d[i]);
}
}
int main() {
int i;
scanf("%d%d%d", &V, &E, &r);
for (i = 0; i < E; i++) {
scanf("%d%d%d", &a, &b, &c);
G_t[a].push_back(b);
G_c[a].push_back(c);
}
dijkstra();
return 0;
}
|
replace
| 22 | 23 | 22 | 23 |
TLE
| |
p02361
|
C++
|
Runtime Error
|
#include "bits/stdc++.h"
using namespace std;
#include <chrono>
//?????????
#pragma region MACRO
#define putans(x) \
std::cerr << "[ answer ]: "; \
cout << (x) << endl
#define dputans(x) \
std::cerr << "[ answer ]: "; \
cout << setprecision(27) << (double)(x) << endl
#define REP(i, a, n) for (int i = (a); i < (int)(n); i++)
#define RREP(i, a, n) for (int i = (int)(n - 1); i >= a; i--)
#define rep(i, n) REP(i, 0, n)
#define rrep(i, n) RREP(i, 0, n)
#define all(a) begin((a)), end((a))
#define mp make_pair
#define exist(container, n) ((container).find((n)) != (container).end())
#define equals(a, b) (fabs((a) - (b)) < EPS)
#ifdef _DEBUG //???????????????????????????????????????????????????
std::ifstream ifs("data.txt");
#define put ifs >>
#else //?????£????????????????????§?????????????????????
#define put cin >>
#endif
#pragma endregion
//???????????°??????????????´
#pragma region CODING_SUPPORT
#ifdef _DEBUG
#define dbg(var0) \
{ std::cerr << (#var0) << "=" << (var0) << endl; }
#define dbg2(var0, var1) \
{ \
std::cerr << (#var0) << "=" << (var0) << ", "; \
dbg(var1); \
}
#define dbg3(var0, var1, var2) \
{ \
std::cerr << (#var0) << "=" << (var0) << ", "; \
dbg2(var1, var2); \
}
#define dbgArray(a, n) \
{ \
std::cerr << (#a) << "="; \
rep(i, n) { std::cerr << (a[i]) << ","; } \
cerr << endl; \
}
#else
#define dbg(var0) \
{}
#define dbg2(var0, var1) \
{}
#define dbg3(var0, var1, var2) \
{}
#define dbgArray(a, n) \
{}
#endif
#pragma endregion
// typedef????????????????????????????¶????????????§?????????
#pragma region TYPE_DEF
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<string, string> pss;
typedef pair<int, string> pis;
typedef vector<string> vs;
typedef vector<int> vi;
#pragma endregion
//??????????????°(???????????????????????§??????)
#pragma region CONST_VAL
#define PI (2 * acos(0.0))
#define EPS (1e-10)
#define MOD (ll)(1e9 + 7)
#define INF (ll)(1e9)
#pragma endregion
// static const int MAX = 100;
// static const int WHITE = 0;
// static const int GRAY = 1;
// static const int BLACK = 2;
//
// static const int n = 100;
// int d[n][n];
/*
void floyd() {
rep(k, n) {
rep(i, n) {
if (d[i][k] == INF) continue;
rep(j, n) {
d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
}
}
}
}
void topologicalSort() {
auto bfs = [&](int s )->void{
queue<int> q;
q.push(s);
V[s] = true;
while (!q.empty()) {
int u = q.front(); q.pop();
rep(i, G[u].size()) {
int v = G[u][i];
indeg[v]--;
if (indeg[v] == 0 && !V[v]) {
q.push();
}
}
}
};
rep(i, n) {
indeg[i] = 0;
}
rep(u, n) {
rep(i, G[u].size()) {
int v = G[u][i];
indeg[v]++;
}
rep(i, n) {
if (indeg[i] == !V[i]) bfs(u);
}//????????§???????????????
}
}*/
//??????????£?????????°???
// https://www23.atwiki.jp/akitaicpc/pages/65.html
struct edge {
int to, cost;
};
typedef pair<int, int> P;
// dikstra
int V;
static const int MAX_V = 10000;
vector<edge> G[MAX_V];
int d[MAX_V];
void dijkstra(int s) {
priority_queue<P, vector<P>, greater<P>> que;
fill(d, d + V, INF);
d[s] = 0;
que.push(P(0, s));
while (!que.empty()) {
P p = que.top();
que.pop();
int v = p.second;
if (d[v] < p.first)
continue;
for (int i = 0; i < G[v].size(); i++) {
edge e = G[v][i];
if (d[e.to] > d[v] + e.cost) {
d[e.to] = d[v] + e.cost;
que.push(P(d[e.to], e.to));
}
}
}
}
void AddEdge(int from, int to, int cost) {
edge e = {to, cost};
G[from].push_back(e);
}
void calc() {
int v, e, r;
put v >> e >> r;
V = v;
rep(i, e) {
int from, to, cost;
put from >> to >> cost;
AddEdge(from, to, cost);
}
dijkstra(r);
rep(i, v) {
string ans = ((d[i] != INF ? to_string(d[i]) : "INF"));
cout << ans << endl;
}
}
int main() {
calc();
END:
return 0;
}
|
#include "bits/stdc++.h"
using namespace std;
#include <chrono>
//?????????
#pragma region MACRO
#define putans(x) \
std::cerr << "[ answer ]: "; \
cout << (x) << endl
#define dputans(x) \
std::cerr << "[ answer ]: "; \
cout << setprecision(27) << (double)(x) << endl
#define REP(i, a, n) for (int i = (a); i < (int)(n); i++)
#define RREP(i, a, n) for (int i = (int)(n - 1); i >= a; i--)
#define rep(i, n) REP(i, 0, n)
#define rrep(i, n) RREP(i, 0, n)
#define all(a) begin((a)), end((a))
#define mp make_pair
#define exist(container, n) ((container).find((n)) != (container).end())
#define equals(a, b) (fabs((a) - (b)) < EPS)
#ifdef _DEBUG //???????????????????????????????????????????????????
std::ifstream ifs("data.txt");
#define put ifs >>
#else //?????£????????????????????§?????????????????????
#define put cin >>
#endif
#pragma endregion
//???????????°??????????????´
#pragma region CODING_SUPPORT
#ifdef _DEBUG
#define dbg(var0) \
{ std::cerr << (#var0) << "=" << (var0) << endl; }
#define dbg2(var0, var1) \
{ \
std::cerr << (#var0) << "=" << (var0) << ", "; \
dbg(var1); \
}
#define dbg3(var0, var1, var2) \
{ \
std::cerr << (#var0) << "=" << (var0) << ", "; \
dbg2(var1, var2); \
}
#define dbgArray(a, n) \
{ \
std::cerr << (#a) << "="; \
rep(i, n) { std::cerr << (a[i]) << ","; } \
cerr << endl; \
}
#else
#define dbg(var0) \
{}
#define dbg2(var0, var1) \
{}
#define dbg3(var0, var1, var2) \
{}
#define dbgArray(a, n) \
{}
#endif
#pragma endregion
// typedef????????????????????????????¶????????????§?????????
#pragma region TYPE_DEF
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<string, string> pss;
typedef pair<int, string> pis;
typedef vector<string> vs;
typedef vector<int> vi;
#pragma endregion
//??????????????°(???????????????????????§??????)
#pragma region CONST_VAL
#define PI (2 * acos(0.0))
#define EPS (1e-10)
#define MOD (ll)(1e9 + 7)
#define INF (ll)(1e9)
#pragma endregion
// static const int MAX = 100;
// static const int WHITE = 0;
// static const int GRAY = 1;
// static const int BLACK = 2;
//
// static const int n = 100;
// int d[n][n];
/*
void floyd() {
rep(k, n) {
rep(i, n) {
if (d[i][k] == INF) continue;
rep(j, n) {
d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
}
}
}
}
void topologicalSort() {
auto bfs = [&](int s )->void{
queue<int> q;
q.push(s);
V[s] = true;
while (!q.empty()) {
int u = q.front(); q.pop();
rep(i, G[u].size()) {
int v = G[u][i];
indeg[v]--;
if (indeg[v] == 0 && !V[v]) {
q.push();
}
}
}
};
rep(i, n) {
indeg[i] = 0;
}
rep(u, n) {
rep(i, G[u].size()) {
int v = G[u][i];
indeg[v]++;
}
rep(i, n) {
if (indeg[i] == !V[i]) bfs(u);
}//????????§???????????????
}
}*/
//??????????£?????????°???
// https://www23.atwiki.jp/akitaicpc/pages/65.html
struct edge {
int to, cost;
};
typedef pair<int, int> P;
// dikstra
int V;
static const int MAX_V = 100001;
vector<edge> G[MAX_V];
int d[MAX_V];
void dijkstra(int s) {
priority_queue<P, vector<P>, greater<P>> que;
fill(d, d + V, INF);
d[s] = 0;
que.push(P(0, s));
while (!que.empty()) {
P p = que.top();
que.pop();
int v = p.second;
if (d[v] < p.first)
continue;
for (int i = 0; i < G[v].size(); i++) {
edge e = G[v][i];
if (d[e.to] > d[v] + e.cost) {
d[e.to] = d[v] + e.cost;
que.push(P(d[e.to], e.to));
}
}
}
}
void AddEdge(int from, int to, int cost) {
edge e = {to, cost};
G[from].push_back(e);
}
void calc() {
int v, e, r;
put v >> e >> r;
V = v;
rep(i, e) {
int from, to, cost;
put from >> to >> cost;
AddEdge(from, to, cost);
}
dijkstra(r);
rep(i, v) {
string ans = ((d[i] != INF ? to_string(d[i]) : "INF"));
cout << ans << endl;
}
}
int main() {
calc();
END:
return 0;
}
|
replace
| 139 | 140 | 139 | 140 |
0
| |
p02361
|
C++
|
Runtime Error
|
#include <algorithm>
#include <functional>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
vector<pair<int, int>> x[500];
int n, m, s, a, b, c, dist[500], dist2[500][500];
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>>
Q;
void dijkstra(int V, int E, int s) {
for (int i = 0; i < V; i++)
dist[i] = 999999999;
dist[s] = 0;
Q.push(make_pair(0, s));
while (!Q.empty()) {
int a1 = Q.top().first, a2 = Q.top().second;
Q.pop();
for (int i = 0; i < x[a2].size(); i++) {
if (dist[x[a2][i].first] > a1 + x[a2][i].second) {
dist[x[a2][i].first] = a1 + x[a2][i].second;
Q.push(make_pair(dist[x[a2][i].first], x[a2][i].first));
}
}
}
}
int main() {
int s;
cin >> n >> m;
cin >> s;
for (int i = 0; i < m; i++) {
cin >> a >> b >> c;
x[a].push_back(make_pair(b, c));
}
/*for(int i=0;i<n;i++){
dijkstra(n,m,i);
for(int j=0;j<n;j++)dist2[i][j]=dist[j];
}
int q;cin>>q;
for(int i=0;i<q;i++){
cin>>a>>b;a--;b--;
if(dist2[a][b]>=999999999)cout<<"-1"<<endl;
else cout<<dist2[a][b]<<endl;
}*/
dijkstra(n, m, s);
for (int i = 0; i < n; i++) {
if (dist[i] == 999999999)
cout << "INF" << endl;
else
cout << dist[i] << endl;
}
return 0;
}
|
#include <algorithm>
#include <functional>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
vector<pair<int, int>> x[300000];
int n, m, s, a, b, c, dist[300000], dist2[500][500];
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>>
Q;
void dijkstra(int V, int E, int s) {
for (int i = 0; i < V; i++)
dist[i] = 999999999;
dist[s] = 0;
Q.push(make_pair(0, s));
while (!Q.empty()) {
int a1 = Q.top().first, a2 = Q.top().second;
Q.pop();
for (int i = 0; i < x[a2].size(); i++) {
if (dist[x[a2][i].first] > a1 + x[a2][i].second) {
dist[x[a2][i].first] = a1 + x[a2][i].second;
Q.push(make_pair(dist[x[a2][i].first], x[a2][i].first));
}
}
}
}
int main() {
int s;
cin >> n >> m;
cin >> s;
for (int i = 0; i < m; i++) {
cin >> a >> b >> c;
x[a].push_back(make_pair(b, c));
}
/*for(int i=0;i<n;i++){
dijkstra(n,m,i);
for(int j=0;j<n;j++)dist2[i][j]=dist[j];
}
int q;cin>>q;
for(int i=0;i<q;i++){
cin>>a>>b;a--;b--;
if(dist2[a][b]>=999999999)cout<<"-1"<<endl;
else cout<<dist2[a][b]<<endl;
}*/
dijkstra(n, m, s);
for (int i = 0; i < n; i++) {
if (dist[i] == 999999999)
cout << "INF" << endl;
else
cout << dist[i] << endl;
}
return 0;
}
|
replace
| 6 | 8 | 6 | 8 |
0
| |
p02361
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<int, pii> pip;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<pii> vpii;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, n) for (int i = (n); i >= 0; i--)
#define rrep2(i, a, b) for (int i = (a); i > b; i--)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(a) (a).begin(), (a).end()
const int mod = 1e9 + 7;
const ll INF = 1 << 30;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
int V, E, r;
struct edge {
int to, cost;
};
vector<edge> g[100000 + 5];
int d[10000 + 5];
void dijkstra(int s) {
priority_queue<pii, vector<pii>, greater<pii>> que;
fill(d, d + V, INF);
d[s] = 0;
que.push(pii(0, s));
while (!que.empty()) {
pii p = que.top();
que.pop();
int v = p.second;
if (d[v] < p.first)
continue;
for (int i = 0; i < g[v].size(); i++) {
edge e = g[v][i];
if (d[e.to] > d[v] + e.cost) {
d[e.to] = d[v] + e.cost;
que.push(pii(d[e.to], e.to));
}
}
}
}
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
cin >> V >> E >> r;
rep(i, E) {
int s, t, d;
cin >> s >> t >> d;
g[s].pb(edge{t, d});
}
dijkstra(r);
rep(i, V) {
if (d[i] == INF)
cout << "INF" << endl;
else
cout << d[i] << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<int, pii> pip;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<pii> vpii;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, n) for (int i = (n); i >= 0; i--)
#define rrep2(i, a, b) for (int i = (a); i > b; i--)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(a) (a).begin(), (a).end()
const int mod = 1e9 + 7;
const ll INF = 1 << 30;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
int V, E, r;
struct edge {
int to, cost;
};
vector<edge> g[100000 + 5];
int d[100000 + 5];
void dijkstra(int s) {
priority_queue<pii, vector<pii>, greater<pii>> que;
fill(d, d + V, INF);
d[s] = 0;
que.push(pii(0, s));
while (!que.empty()) {
pii p = que.top();
que.pop();
int v = p.second;
if (d[v] < p.first)
continue;
for (int i = 0; i < g[v].size(); i++) {
edge e = g[v][i];
if (d[e.to] > d[v] + e.cost) {
d[e.to] = d[v] + e.cost;
que.push(pii(d[e.to], e.to));
}
}
}
}
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
cin >> V >> E >> r;
rep(i, E) {
int s, t, d;
cin >> s >> t >> d;
g[s].pb(edge{t, d});
}
dijkstra(r);
rep(i, V) {
if (d[i] == INF)
cout << "INF" << endl;
else
cout << d[i] << endl;
}
}
|
replace
| 31 | 32 | 31 | 32 |
0
| |
p02361
|
Python
|
Runtime Error
|
import sys
import queue
class Dijkstra:
class Edge:
def __init__(self, end, cost):
self.to = end
self.cost = cost
def __init__(self, node_size, inf):
self._node = node_size
self._graph = [[] for _ in range(self._node)]
self.inf = inf
self.dist = [self.inf for _ in range(self._node)]
def add_edge(self, st, ed, cs):
self._graph[st].append(self.Edge(ed, cs))
def solve(self, start):
que = queue.PriorityQueue()
self.dist[start] = 0
que.put((0, start))
while que:
cur_cost, cur_vertex = que.get()
if self.dist[cur_vertex] < cur_cost:
continue
for e in self._graph[cur_vertex]:
if self.dist[e.to] > cur_cost + e.cost:
self.dist[e.to] = cur_cost + e.cost
que.put((self.dist[e.to], e.to))
if __name__ == "__main__":
V, E, r = map(int, sys.stdin.readline().split())
dk = Dijkstra(V, 10**10)
for i in range(E):
s, t, d = map(int, sys.stdin.readline().split())
dk.add_edge(s, t, d)
dk.solve(r)
for value in dk.dist:
if value == dk.inf:
print("INF")
else:
print(value)
|
import sys
import queue
class Dijkstra:
class Edge:
def __init__(self, end, cost):
self.to = end
self.cost = cost
def __init__(self, node_size, inf):
self._node = node_size
self._graph = [[] for _ in range(self._node)]
self.inf = inf
self.dist = [self.inf for _ in range(self._node)]
def add_edge(self, st, ed, cs):
self._graph[st].append(self.Edge(ed, cs))
def solve(self, start):
que = queue.PriorityQueue()
self.dist[start] = 0
que.put((0, start))
while not que.empty():
cur_cost, cur_vertex = que.get()
if self.dist[cur_vertex] < cur_cost:
continue
for e in self._graph[cur_vertex]:
if self.dist[e.to] > cur_cost + e.cost:
self.dist[e.to] = cur_cost + e.cost
que.put((self.dist[e.to], e.to))
if __name__ == "__main__":
V, E, r = map(int, sys.stdin.readline().split())
dk = Dijkstra(V, 10**10)
for i in range(E):
s, t, d = map(int, sys.stdin.readline().split())
dk.add_edge(s, t, d)
dk.solve(r)
for value in dk.dist:
if value == dk.inf:
print("INF")
else:
print(value)
|
replace
| 23 | 24 | 23 | 24 |
TLE
| |
p02361
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
const int kInfinity = 0x3F3F3F3F;
const int kNil = -1;
const int kMaxV = 10010;
template <class T> struct Edge {
using w_type = T;
Edge(int u, int v, w_type w) : u(u), v(v), w(w) {}
int u, v;
w_type w;
bool operator<(const Edge<T> &rhs) const {
if (w != rhs.w) {
return w < rhs.w;
}
if (u != rhs.u) {
return u < rhs.u;
}
return v < rhs.v;
}
};
template <class Edge>
decltype(auto) Dijkstra(const vector<Edge> &edges,
typename vector<Edge>::size_type node_num,
typename vector<Edge>::size_type source) {
using size_type = typename vector<Edge>::size_type;
using w_type = typename Edge::w_type;
bitset<kMaxV> bs;
vector<vector<Edge>> adjacency(node_num, vector<Edge>());
for (const auto &e : edges) {
adjacency[e.u].push_back(e);
}
vector<w_type> dist(node_num, kInfinity);
vector<int> parent(node_num, kNil);
dist[source] = 0;
using Pair = pair<size_type, w_type>;
auto compare = [](const Pair &x, const Pair &y) {
return y.second < x.second || (!(x.second < y.second) && y.first < x.first);
};
priority_queue<Pair, vector<Pair>, decltype(compare)> que(compare);
que.emplace(source, 0);
while (!que.empty()) {
auto top_no = que.top().first;
auto top_dist = que.top().second;
que.pop();
bs.set(top_no);
for (const auto &e : adjacency[top_no]) {
const auto cost = dist[e.u] + e.w;
if (dist[e.v] > cost && !bs.test(e.v)) {
dist[e.v] = cost;
parent[e.v] = e.u;
que.emplace(e.v, cost);
}
}
}
return dist;
}
int main(int argc, char const *argv[]) {
vector<Edge<int>> edges;
unsigned V, E, r;
cin >> V >> E >> r;
for (unsigned i = 0; i < E; ++i) {
unsigned u, v;
unsigned w;
cin >> u >> v >> w;
edges.emplace_back(u, v, w);
}
auto dist = Dijkstra(edges, V, r);
for (unsigned i = 0; i < V; ++i) {
if (dist[i] == kInfinity) {
cout << "INF\n";
} else {
cout << dist[i] << endl;
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int kInfinity = 0x3F3F3F3F;
const int kNil = -1;
const int kMaxV = 100010;
template <class T> struct Edge {
using w_type = T;
Edge(int u, int v, w_type w) : u(u), v(v), w(w) {}
int u, v;
w_type w;
bool operator<(const Edge<T> &rhs) const {
if (w != rhs.w) {
return w < rhs.w;
}
if (u != rhs.u) {
return u < rhs.u;
}
return v < rhs.v;
}
};
template <class Edge>
decltype(auto) Dijkstra(const vector<Edge> &edges,
typename vector<Edge>::size_type node_num,
typename vector<Edge>::size_type source) {
using size_type = typename vector<Edge>::size_type;
using w_type = typename Edge::w_type;
bitset<kMaxV> bs;
vector<vector<Edge>> adjacency(node_num, vector<Edge>());
for (const auto &e : edges) {
adjacency[e.u].push_back(e);
}
vector<w_type> dist(node_num, kInfinity);
vector<int> parent(node_num, kNil);
dist[source] = 0;
using Pair = pair<size_type, w_type>;
auto compare = [](const Pair &x, const Pair &y) {
return y.second < x.second || (!(x.second < y.second) && y.first < x.first);
};
priority_queue<Pair, vector<Pair>, decltype(compare)> que(compare);
que.emplace(source, 0);
while (!que.empty()) {
auto top_no = que.top().first;
auto top_dist = que.top().second;
que.pop();
bs.set(top_no);
for (const auto &e : adjacency[top_no]) {
const auto cost = dist[e.u] + e.w;
if (dist[e.v] > cost && !bs.test(e.v)) {
dist[e.v] = cost;
parent[e.v] = e.u;
que.emplace(e.v, cost);
}
}
}
return dist;
}
int main(int argc, char const *argv[]) {
vector<Edge<int>> edges;
unsigned V, E, r;
cin >> V >> E >> r;
for (unsigned i = 0; i < E; ++i) {
unsigned u, v;
unsigned w;
cin >> u >> v >> w;
edges.emplace_back(u, v, w);
}
auto dist = Dijkstra(edges, V, r);
for (unsigned i = 0; i < V; ++i) {
if (dist[i] == kInfinity) {
cout << "INF\n";
} else {
cout << dist[i] << endl;
}
}
return 0;
}
|
replace
| 6 | 7 | 6 | 7 |
0
| |
p02361
|
C++
|
Time Limit Exceeded
|
#include <algorithm>
#include <iostream>
#include <queue>
#include <utility>
using namespace std;
const int Inf = 10000 * 100000 + 100;
int a, b, V, E, R, S, Start[500000 + 10], To[500000 + 10], Dis[500000 + 10],
N[500000 + 10];
int Ans[100000 + 10];
priority_queue<pair<int, int>> Q;
// first is the distance from R, second is the number of V
// redefine the operator '<' to let the priority_queue making the smallest in
// the first place
bool operator<(pair<int, int> a, pair<int, int> b) {
return (a.first == b.first) ? a.second > b.second : a.first > b.first;
}
int main() {
cin >> V >> E >> R;
for (int i = 1; i <= E; i++) {
cin >> S >> To[i] >> Dis[i];
N[i] = Start[S];
Start[S] = i;
}
fill(Ans, Ans + 100010, Inf);
Ans[R] = 0;
Q.push(make_pair(0, R));
while (!Q.empty()) {
a = Q.top().first;
b = Q.top().second;
Q.pop();
if (Ans[b] == a) {
for (int i = Start[b]; i; i = N[i]) {
int t = To[i];
if (a + Dis[i] < Ans[t]) {
Ans[t] = a + Dis[i];
Q.push(make_pair(Ans[t], t));
}
}
}
}
for (int i = 0; i < V; i++) {
if (Ans[i] < Inf) {
cout << Ans[i] << endl;
} else {
cout << "INF" << endl;
}
}
}
|
#include <algorithm>
#include <iostream>
#include <queue>
#include <utility>
using namespace std;
const int Inf = 10000 * 100000 + 100;
int a, b, V, E, R, S, Start[500000 + 10], To[500000 + 10], Dis[500000 + 10],
N[500000 + 10];
int Ans[100000 + 10];
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>>
Q;
int main() {
cin >> V >> E >> R;
for (int i = 1; i <= E; i++) {
cin >> S >> To[i] >> Dis[i];
N[i] = Start[S];
Start[S] = i;
}
fill(Ans, Ans + 100010, Inf);
Ans[R] = 0;
Q.push(make_pair(0, R));
while (!Q.empty()) {
a = Q.top().first;
b = Q.top().second;
Q.pop();
if (Ans[b] == a) {
for (int i = Start[b]; i; i = N[i]) {
int t = To[i];
if (a + Dis[i] < Ans[t]) {
Ans[t] = a + Dis[i];
Q.push(make_pair(Ans[t], t));
}
}
}
}
for (int i = 0; i < V; i++) {
if (Ans[i] < Inf) {
cout << Ans[i] << endl;
} else {
cout << "INF" << endl;
}
}
}
|
replace
| 12 | 20 | 12 | 14 |
TLE
| |
p02361
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct node {
int id;
ll dp;
};
struct edge {
int des;
ll w;
};
int n, m;
vector<edge> adj[10007];
ll Min[100007];
bool mark[100007];
bool operator<(node a, node b) { return a.dp >= b.dp; }
void prim(int u) {
fill(Min, Min + 100007, 1e11);
Min[u] = 0;
node nd;
nd.id = u;
nd.dp = 0;
priority_queue<node> q;
q.push(nd);
while (q.size() > 0) {
nd = q.top();
q.pop();
if (mark[nd.id])
continue;
mark[nd.id] = 1;
int u = nd.id;
for (int i = 0; i < adj[u].size(); i++) {
int v = adj[u][i].des;
int val = adj[u][i].w;
if (Min[v] <= val + nd.dp)
continue;
Min[v] = val + nd.dp;
node nn;
nn.id = v;
nn.dp = val + nd.dp;
q.push(nn);
}
}
}
int main() {
int u;
cin >> n >> m >> u;
for (int i = 0; i < m; i++) {
int x, y;
edge e;
cin >> x >> y >> e.w;
e.des = y;
adj[x].push_back(e);
}
prim(u);
for (int i = 0; i < n; i++) {
if (Min[i] < 1e11)
cout << Min[i] << endl;
else
cout << "INF" << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct node {
int id;
ll dp;
};
struct edge {
int des;
ll w;
};
int n, m;
vector<edge> adj[100007];
ll Min[100007];
bool mark[100007];
bool operator<(node a, node b) { return a.dp >= b.dp; }
void prim(int u) {
fill(Min, Min + 100007, 1e11);
Min[u] = 0;
node nd;
nd.id = u;
nd.dp = 0;
priority_queue<node> q;
q.push(nd);
while (q.size() > 0) {
nd = q.top();
q.pop();
if (mark[nd.id])
continue;
mark[nd.id] = 1;
int u = nd.id;
for (int i = 0; i < adj[u].size(); i++) {
int v = adj[u][i].des;
int val = adj[u][i].w;
if (Min[v] <= val + nd.dp)
continue;
Min[v] = val + nd.dp;
node nn;
nn.id = v;
nn.dp = val + nd.dp;
q.push(nn);
}
}
}
int main() {
int u;
cin >> n >> m >> u;
for (int i = 0; i < m; i++) {
int x, y;
edge e;
cin >> x >> y >> e.w;
e.des = y;
adj[x].push_back(e);
}
prim(u);
for (int i = 0; i < n; i++) {
if (Min[i] < 1e11)
cout << Min[i] << endl;
else
cout << "INF" << endl;
}
}
|
replace
| 16 | 17 | 16 | 17 |
0
| |
p02361
|
C++
|
Time Limit Exceeded
|
/****Author: Barish Namazov****/
#include <bits/stdc++.h>
using namespace std;
/***TEMPLATE***/
#define intt long long
#define pii pair<intt, intt>
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define F first
#define S second
#define pb push_back
#define IO \
ios_base::sync_with_stdio(false); \
cin.tie();
#define endl '\n'
const intt max4 = 10004;
const intt maxx = 100005;
const intt max6 = 1000006;
const intt max7 = 10000007;
const intt lg4 = 13;
const intt lg5 = 17;
const intt lg6 = 20;
const intt INF = 2LL * 1000000000;
/***************/
intt powmod(intt a, intt b, intt mod) {
intt res = 1;
a %= mod;
for (; b; b >>= 1) {
if (b & 1)
res = (res * a) % mod;
a = (a * a) % mod;
}
return res;
}
intt gcd(intt a, intt b) {
while (b > 0) {
intt t = a % b;
a = b, b = t;
}
return a;
}
intt lcm(intt a, intt b) { return (a / gcd(a, b)) * b; }
intt is_prime(intt n) {
if (n <= 1 || n > 3 && (n % 2 == 0 || n % 3 == 0))
return 0;
for (intt i = 5, t = 2; i * i <= n; i += t, t = 6 - t)
if (n % i == 0)
return 0;
return 1;
}
/**************************/
intt n, m, source;
intt a, b, c;
vector<pii> g[maxx];
intt used[maxx], dis[maxx];
priority_queue<pii, vector<pii>, greater<pii>> pq;
int main() {
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
IO;
cin >> n >> m >> source;
source++;
for (intt i = 1; i <= m; i++) {
cin >> a >> b >> c;
a++, b++;
g[a].pb({b, c}); // g[b].pb ({a, c}); delete "//" if graph is undirected
}
for (intt i = 1; i <= n; i++)
dis[i] = INF;
dis[source] = 0;
pq.push({0, source});
while (!pq.empty()) {
a = pq.top().S;
pq.pop();
if (used[a])
continue;
used[a] = 1;
for (pii u : g[a]) {
b = u.F, c = u.S;
if (dis[a] + c < dis[b]) {
dis[b] = dis[a] + c;
pq.push({dis[b], b});
}
}
}
for (intt i = 1; i <= n; i++) {
if (dis[i] == INF)
cout << "INF" << endl;
else
cout << dis[i] << endl;
}
return 0;
}
|
/****Author: Barish Namazov****/
#include <bits/stdc++.h>
using namespace std;
/***TEMPLATE***/
#define intt long long
#define pii pair<intt, intt>
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define F first
#define S second
#define pb push_back
#define IO \
ios_base::sync_with_stdio(false); \
cin.tie();
#define endl '\n'
const intt max4 = 10004;
const intt maxx = 100005;
const intt max6 = 1000006;
const intt max7 = 10000007;
const intt lg4 = 13;
const intt lg5 = 17;
const intt lg6 = 20;
const intt INF = 2LL * 1000000000;
/***************/
intt powmod(intt a, intt b, intt mod) {
intt res = 1;
a %= mod;
for (; b; b >>= 1) {
if (b & 1)
res = (res * a) % mod;
a = (a * a) % mod;
}
return res;
}
intt gcd(intt a, intt b) {
while (b > 0) {
intt t = a % b;
a = b, b = t;
}
return a;
}
intt lcm(intt a, intt b) { return (a / gcd(a, b)) * b; }
intt is_prime(intt n) {
if (n <= 1 || n > 3 && (n % 2 == 0 || n % 3 == 0))
return 0;
for (intt i = 5, t = 2; i * i <= n; i += t, t = 6 - t)
if (n % i == 0)
return 0;
return 1;
}
/**************************/
intt n, m, source;
intt a, b, c;
vector<pii> g[maxx];
intt used[maxx], dis[maxx];
priority_queue<pii, vector<pii>, greater<pii>> pq;
int main() {
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
IO;
cin >> n >> m >> source;
source++;
for (intt i = 1; i <= m; i++) {
cin >> a >> b >> c;
a++, b++;
g[a].pb({b, c}); // g[b].pb ({a, c}); delete "//" if graph is undirected
}
for (intt i = 1; i <= n; i++)
dis[i] = INF;
dis[source] = 0;
pq.push({0, source});
while (!pq.empty()) {
a = pq.top().S;
pq.pop();
if (used[a])
continue;
used[a] = 1;
for (auto u : g[a]) {
b = u.F, c = u.S;
if (dis[a] + c < dis[b]) {
dis[b] = dis[a] + c;
pq.push({dis[b], b});
}
}
}
for (intt i = 1; i <= n; i++) {
if (dis[i] == INF)
cout << "INF" << endl;
else
cout << dis[i] << endl;
}
return 0;
}
|
replace
| 92 | 93 | 92 | 93 |
TLE
| |
p02361
|
C++
|
Runtime Error
|
#include <iostream>
#include <queue>
#include <vector>
#define INF 50000000000
struct vertex {
std::vector<int> edge_to;
std::vector<int> edge_cost;
bool done;
long long int mincost;
};
std::vector<vertex> G(10000);
void shortest_path(int v);
int V, E;
int r;
int main(void) {
std::cin >> V >> E >> r;
for (int i = 0; i < E; i++) {
int from, to, cost;
std::cin >> from >> to >> cost;
G[from].edge_to.push_back(to);
G[from].edge_cost.push_back(cost);
// G[to].edge_to.push_back(from);
// G[to].edge_cost.push_back(cost);
}
shortest_path(r);
for (int i = 0; i < V; i++) {
if (G[i].mincost != INF) {
std::cout << G[i].mincost << std::endl;
} else {
std::cout << "INF" << std::endl;
}
}
return 0;
}
void shortest_path(int v) {
for (int i = 0; i < V; i++) {
G[i].mincost = INF;
}
G[v].mincost = 0;
while (1) {
bool update = false;
for (int i = 0; i < V; i++) {
for (int j = 0; j < G[i].edge_to.size(); j++) {
if (G[G[i].edge_to[j]].mincost > G[i].mincost + G[i].edge_cost[j]) {
G[G[i].edge_to[j]].mincost = G[i].mincost + G[i].edge_cost[j];
update = true;
}
}
}
if (!update) {
break;
}
}
}
|
#include <iostream>
#include <queue>
#include <vector>
#define INF 50000000000
struct vertex {
std::vector<int> edge_to;
std::vector<int> edge_cost;
bool done;
long long int mincost;
};
std::vector<vertex> G(100000);
void shortest_path(int v);
int V, E;
int r;
int main(void) {
std::cin >> V >> E >> r;
for (int i = 0; i < E; i++) {
int from, to, cost;
std::cin >> from >> to >> cost;
G[from].edge_to.push_back(to);
G[from].edge_cost.push_back(cost);
// G[to].edge_to.push_back(from);
// G[to].edge_cost.push_back(cost);
}
shortest_path(r);
for (int i = 0; i < V; i++) {
if (G[i].mincost != INF) {
std::cout << G[i].mincost << std::endl;
} else {
std::cout << "INF" << std::endl;
}
}
return 0;
}
void shortest_path(int v) {
for (int i = 0; i < V; i++) {
G[i].mincost = INF;
}
G[v].mincost = 0;
while (1) {
bool update = false;
for (int i = 0; i < V; i++) {
for (int j = 0; j < G[i].edge_to.size(); j++) {
if (G[G[i].edge_to[j]].mincost > G[i].mincost + G[i].edge_cost[j]) {
G[G[i].edge_to[j]].mincost = G[i].mincost + G[i].edge_cost[j];
update = true;
}
}
}
if (!update) {
break;
}
}
}
|
replace
| 10 | 11 | 10 | 11 |
0
| |
p02361
|
C++
|
Time Limit Exceeded
|
#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define FOR(i, k, n) for (int(i) = (k); (i) < (n); ++(i))
#define rep(i, n) FOR(i, 0, n)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fst first
#define snd second
#define all(v) begin(v), end(v)
#define debug(x) cerr << #x << ": " << x << endl
#define debug2(x, y) cerr << #x << ": " << x << ", " << #y << ": " << y << endl
#define INF INT_MAX / 2
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
template <class T> using vv = vector<vector<T>>;
struct Edge {
int cost;
int to;
};
int n;
vv<Edge> g;
vector<int> d;
void dijkstra(int s) {
d.assign(n, INF);
priority_queue<vi, vector<vi>> q;
d[s] = 0;
q.push({0, s}); // candidate of min-cost, edge number
while (!q.empty()) {
auto p = q.top(); // delete from queue
q.pop();
int v = p[1];
if (d[v] < p[0]) {
continue;
}
for (auto e : g[v]) {
if (d[e.to] > d[v] + e.cost) {
d[e.to] = d[v] + e.cost;
q.push({d[e.to], e.to});
}
}
}
}
int main() {
int ne, s;
scanf("%d %d %d", &n, &ne, &s);
g.resize(n);
rep(i, ne) {
int a, b, d;
scanf("%d %d %d", &a, &b, &d);
Edge e;
e.cost = d;
e.to = b;
g[a].pb(e);
}
dijkstra(s);
rep(i, n) {
if (d[i] == INF) {
printf("INF\n");
} else {
printf("%d\n", d[i]);
}
}
return 0;
}
|
#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define FOR(i, k, n) for (int(i) = (k); (i) < (n); ++(i))
#define rep(i, n) FOR(i, 0, n)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fst first
#define snd second
#define all(v) begin(v), end(v)
#define debug(x) cerr << #x << ": " << x << endl
#define debug2(x, y) cerr << #x << ": " << x << ", " << #y << ": " << y << endl
#define INF INT_MAX / 2
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
template <class T> using vv = vector<vector<T>>;
struct Edge {
int cost;
int to;
};
int n;
vv<Edge> g;
vector<int> d;
void dijkstra(int s) {
d.assign(n, INF);
priority_queue<vi, vector<vi>, greater<vi>> q;
d[s] = 0;
q.push({0, s}); // candidate of min-cost, edge number
while (!q.empty()) {
auto p = q.top(); // delete from queue
q.pop();
int v = p[1];
if (d[v] < p[0]) {
continue;
}
for (auto e : g[v]) {
if (d[e.to] > d[v] + e.cost) {
d[e.to] = d[v] + e.cost;
q.push({d[e.to], e.to});
}
}
}
}
int main() {
int ne, s;
scanf("%d %d %d", &n, &ne, &s);
g.resize(n);
rep(i, ne) {
int a, b, d;
scanf("%d %d %d", &a, &b, &d);
Edge e;
e.cost = d;
e.to = b;
g[a].pb(e);
}
dijkstra(s);
rep(i, n) {
if (d[i] == INF) {
printf("INF\n");
} else {
printf("%d\n", d[i]);
}
}
return 0;
}
|
replace
| 52 | 53 | 52 | 53 |
TLE
| |
p02361
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
#define inf 999999999
typedef pair<int, int> P;
int V, E, r;
int d[100000];
vector<P> edges[10000];
priority_queue<P, vector<P>, greater<P>> q;
void solve() {
P start = P(0, r);
d[r] = 0;
q.push(start);
while (!q.empty()) {
P p = q.top();
q.pop();
if (d[p.second] < p.first)
continue;
for (int i = 0; i < edges[p.second].size(); i++) {
P edge = edges[p.second][i];
P next = P(p.first + edge.second, edge.first);
if (d[next.second] < next.first)
continue;
d[next.second] = next.first;
q.push(next);
}
}
return;
}
int main() {
fill_n(d, 100000, inf);
cin >> V >> E >> r;
for (int i = 0; i < E; i++) {
int A, B, cost;
cin >> A >> B >> cost;
edges[A].push_back(P(B, cost));
}
solve();
for (int i = 0; i < V; i++) {
if (d[i] == inf) {
cout << "INF" << endl;
} else {
cout << d[i] << endl;
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
#define inf 999999999
typedef pair<int, int> P;
int V, E, r;
int d[100000];
vector<P> edges[100000];
priority_queue<P, vector<P>, greater<P>> q;
void solve() {
P start = P(0, r);
d[r] = 0;
q.push(start);
while (!q.empty()) {
P p = q.top();
q.pop();
if (d[p.second] < p.first)
continue;
for (int i = 0; i < edges[p.second].size(); i++) {
P edge = edges[p.second][i];
P next = P(p.first + edge.second, edge.first);
if (d[next.second] < next.first)
continue;
d[next.second] = next.first;
q.push(next);
}
}
return;
}
int main() {
fill_n(d, 100000, inf);
cin >> V >> E >> r;
for (int i = 0; i < E; i++) {
int A, B, cost;
cin >> A >> B >> cost;
edges[A].push_back(P(B, cost));
}
solve();
for (int i = 0; i < V; i++) {
if (d[i] == inf) {
cout << "INF" << endl;
} else {
cout << d[i] << endl;
}
}
}
|
replace
| 6 | 7 | 6 | 7 |
0
| |
p02361
|
C++
|
Time Limit Exceeded
|
#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<long long, int> ll_i;
typedef pair<double, int> d_i;
typedef pair<long long, long long> ll_ll;
typedef pair<double, double> d_d;
typedef vector<int> vint;
typedef vector<char> vchar;
#define PI 3.141592653589793238462643383279
#define intinf 200000014
#define longinf 200000000000000014LL
#define mod 1000000007LL
#define rep(i, n) for (i = 0; i < n; ++i)
#define rep1(i, n) for (i = 1; i < n; ++i)
#define rep2d(i, j, n) \
for (i = 0; i < n; ++i) \
for (j = i + 1; j < n; ++j)
#define per(i, n) for (i = n - 1; i > -1; --i)
#define int(x) \
int x; \
scanf("%d", &x)
#define int2(x, y) \
int x, y; \
scanf("%d%d", &x, &y)
#define int3(x, y, z) \
int x, y, z; \
scanf("%d%d%d", &x, &y, &z)
#define sc(x) cin >> x
#define sc2(x, y) cin >> x >> y
#define sc3(x, y, z) cin >> x >> y >> z
#define scn(n, a) rep(i, n) cin >> a[i]
#define sc2n(n, a, b) rep(i, n) cin >> a[i] >> b[i]
#define pri(x) cout << x << "\n"
#define pri2(x, y) cout << x << " " << y << "\n"
#define pri3(x, y, z) cout << x << " " << y << " " << z << "\n"
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define kabe puts("---------------------------")
#define kara puts("")
#define debug(x) cout << " --- " << x << "\n"
#define debug2(x, y) cout << " --- " << x << " " << y << "\n"
#define debug3(x, y, z) cout << " --- " << x << " " << y << " " << z << "\n"
#define debugn(i, n, a) rep(i, n) cout << " --- " << a[i] << "\n";
#define debugin(i, n, a) rep(i, n) printf(" --- %10d\n", a[i])
#define debugi2n(i, n, a, b) rep(i, n) printf(" --- %10d %10d\n", a[i], b[i])
#define debugiin(i, n, a) \
rep(i, n) printf(" --- %10d %10d\n", a[i].first, a[i].second)
#define X first
#define Y second
#define eps 0.0001
#define prid(x) printf("%.15lf\n", x)
struct edge {
int to, cost;
};
vector<edge> G[100000];
ll dijkstra(int v, int e, int r) {
ll res;
int i, j;
ll d[v];
rep(i, v) d[i] = longinf;
d[r] = 0LL;
priority_queue<ll_i> Q;
Q.push(mp(0LL, r));
for (; !Q.empty();) {
ll_i now = Q.top();
Q.pop();
if (d[now.Y] < now.X)
continue;
rep(i, G[now.Y].size()) {
edge tmp = G[now.Y][i];
if (d[tmp.to] > d[now.Y] + tmp.cost) {
d[tmp.to] = d[now.Y] + tmp.cost;
Q.push(mp(d[tmp.to], tmp.to));
}
}
}
rep(i, v) {
if (d[i] >= longinf)
pri("INF");
else
pri(d[i]);
}
return 0;
}
signed main(void) {
int i, j, tmp, tmp2;
for (int testcase = 0;; testcase++) {
int3(v, e, r);
rep(i, e) {
int3(s, t, d);
G[s].pb((edge){t, d});
}
dijkstra(v, e, r);
/*/
//*/
break;
}
return 0;
}
|
#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<long long, int> ll_i;
typedef pair<double, int> d_i;
typedef pair<long long, long long> ll_ll;
typedef pair<double, double> d_d;
typedef vector<int> vint;
typedef vector<char> vchar;
#define PI 3.141592653589793238462643383279
#define intinf 200000014
#define longinf 200000000000000014LL
#define mod 1000000007LL
#define rep(i, n) for (i = 0; i < n; ++i)
#define rep1(i, n) for (i = 1; i < n; ++i)
#define rep2d(i, j, n) \
for (i = 0; i < n; ++i) \
for (j = i + 1; j < n; ++j)
#define per(i, n) for (i = n - 1; i > -1; --i)
#define int(x) \
int x; \
scanf("%d", &x)
#define int2(x, y) \
int x, y; \
scanf("%d%d", &x, &y)
#define int3(x, y, z) \
int x, y, z; \
scanf("%d%d%d", &x, &y, &z)
#define sc(x) cin >> x
#define sc2(x, y) cin >> x >> y
#define sc3(x, y, z) cin >> x >> y >> z
#define scn(n, a) rep(i, n) cin >> a[i]
#define sc2n(n, a, b) rep(i, n) cin >> a[i] >> b[i]
#define pri(x) cout << x << "\n"
#define pri2(x, y) cout << x << " " << y << "\n"
#define pri3(x, y, z) cout << x << " " << y << " " << z << "\n"
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define kabe puts("---------------------------")
#define kara puts("")
#define debug(x) cout << " --- " << x << "\n"
#define debug2(x, y) cout << " --- " << x << " " << y << "\n"
#define debug3(x, y, z) cout << " --- " << x << " " << y << " " << z << "\n"
#define debugn(i, n, a) rep(i, n) cout << " --- " << a[i] << "\n";
#define debugin(i, n, a) rep(i, n) printf(" --- %10d\n", a[i])
#define debugi2n(i, n, a, b) rep(i, n) printf(" --- %10d %10d\n", a[i], b[i])
#define debugiin(i, n, a) \
rep(i, n) printf(" --- %10d %10d\n", a[i].first, a[i].second)
#define X first
#define Y second
#define eps 0.0001
#define prid(x) printf("%.15lf\n", x)
struct edge {
int to, cost;
};
vector<edge> G[100000];
ll dijkstra(int v, int e, int r) {
ll res;
int i, j;
ll d[v];
rep(i, v) d[i] = longinf;
d[r] = 0LL;
priority_queue<ll_i, vector<ll_i>, greater<ll_i>> Q;
Q.push(mp(0LL, r));
for (; !Q.empty();) {
ll_i now = Q.top();
Q.pop();
if (d[now.Y] < now.X)
continue;
rep(i, G[now.Y].size()) {
edge tmp = G[now.Y][i];
if (d[tmp.to] > d[now.Y] + tmp.cost) {
d[tmp.to] = d[now.Y] + tmp.cost;
Q.push(mp(d[tmp.to], tmp.to));
}
}
}
rep(i, v) {
if (d[i] >= longinf)
pri("INF");
else
pri(d[i]);
}
return 0;
}
signed main(void) {
int i, j, tmp, tmp2;
for (int testcase = 0;; testcase++) {
int3(v, e, r);
rep(i, e) {
int3(s, t, d);
G[s].pb((edge){t, d});
}
dijkstra(v, e, r);
/*/
//*/
break;
}
return 0;
}
|
replace
| 92 | 93 | 92 | 93 |
TLE
| |
p02361
|
C++
|
Time Limit Exceeded
|
#include <algorithm>
#include <iostream>
#include <queue>
#define ll long long
#define INF 100000000000000
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
ll V, E, r;
cin >> V >> E >> r;
vector<vector<pair<ll, ll>>> edges(V);
vector<ll> dist(V, INF);
for (ll i = 0; i < E; i++) {
ll s, t, d;
cin >> s >> t >> d;
edges[s].push_back(make_pair(t, d));
}
dist[r] = 0;
priority_queue<pair<ll, ll>> pq;
pq.push(make_pair(0, r));
while (!pq.empty()) {
ll source = pq.top().second;
pq.pop();
for (auto i : edges[source]) {
if (dist[i.first] == INF || dist[source] + i.second < dist[i.first]) {
dist[i.first] = dist[source] + i.second;
pq.push(make_pair(dist[i.first], i.first));
}
}
}
for (ll i = 0; i < V; i++) {
if (dist[i] == INF)
cout << "INF" << endl;
else
cout << dist[i] << endl;
}
}
|
#include <algorithm>
#include <iostream>
#include <queue>
#define ll long long
#define INF 100000000000000
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
ll V, E, r;
cin >> V >> E >> r;
vector<vector<pair<ll, ll>>> edges(V);
vector<ll> dist(V, INF);
for (ll i = 0; i < E; i++) {
ll s, t, d;
cin >> s >> t >> d;
edges[s].push_back(make_pair(t, d));
}
dist[r] = 0;
priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> pq;
pq.push(make_pair(0, r));
while (!pq.empty()) {
ll source = pq.top().second;
pq.pop();
for (auto i : edges[source]) {
if (dist[i.first] == INF || dist[source] + i.second < dist[i.first]) {
dist[i.first] = dist[source] + i.second;
pq.push(make_pair(dist[i.first], i.first));
}
}
}
for (ll i = 0; i < V; i++) {
if (dist[i] == INF)
cout << "INF" << endl;
else
cout << dist[i] << endl;
}
}
|
replace
| 18 | 19 | 18 | 19 |
TLE
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.