# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
90898 | YottaByte | Trading (IZhO13_trading) | C++14 | 7 ms | 520 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <algorithm>
#include <stdlib.h>
#include <stdio.h>
#include <vector>
using namespace std;
#define pb push_back
#define mk make_pair
#define fr first
#define endl '\n'
#define sc second
#define pii pair < int, int >
#define piii pair < int, pair < int, int > >
const int N = 3e5 + 1;
int n, m, c;
int t[N * 4], h[N * 4];
vector < pair < pii, pii > > v;
void push(int v, int l, int r)
{
if(h[v])
{
t[v] = (r - l + 1) * h[v];
h[v + v] = h[v];
h[v + v + 1] = h[v];
h[v] = 0;
}
}
void upd(int ql, int qr, int x, int v = 1, int l = 1, int r = n)
{
push(v, l, r);
if(l > qr || r < ql) return;
//cout << l << " " << r << endl;
if(ql <= l && r <= qr)
{
h[v] = x;
push(v, l, r);
return;
}
else
{
int mid = l + r >> 1;
upd(ql, qr, x, v + v, l, mid);
upd(ql, qr, x, v + v + 1, mid + 1, r);
}
t[v] = t[v + v] + t[v + v + 1];
}
long long getsum(int ql, int qr, int v = 1, int l = 1, int r = n)
{
push(v, l, r);
if(l > qr || r < ql) return 0;
if(ql <= l && r <= qr)
return t[v];
int mid = l + r >> 1;
return getsum(ql, qr, v + v, l, mid) +
getsum(ql, qr, v + v + 1, mid + 1, r);
}
void solve(int l, int r, int x)
{
//printf("%d %d\n", l, r);
//printf("idr = %d, idl = %d, curr = %d, curl = %d\n", idr + 1, idl + 1, curr, curl);
if(getsum(l, r) == 0)
{
upd(l, r, x);
//puts("");
return;
}
if(l == r)
{
return;
}
int mid = l + r >> 1;
solve(l, mid, x);
solve(mid + 1, r, x);
}
int get(int x, int pos)
{
if(x == 0) return 0;
int res = v[x - 1].fr.sc;
res += (pos - v[x - 1].sc.fr);
return res;
}
main()
{
scanf("%d%d", &n, &m);
c = m;
for(int i = 1; i <= m; i++)
{
int l, r, x;
scanf("%d%d%d", &l, &r, &x);
v.pb( mk( mk ( x - l + r, x ), mk( l, r ) ) );
}
sort(v.rbegin(), v.rend());
for(int i = 1; i <= m; i++)
{
int l = v[i - 1].sc.fr;
int r = v[i - 1].sc.sc;
solve(l, r, i);
}
for(int i = 1; i <= n; i++)
{
printf("%d ", get(getsum(i, i), i));
}
}
/**
5 2
1 3 2
2 4 6
2 6 7 8 0
----------------------
6 4
1 2 5
4 4 3
6 6 1
5 6 1
5 6 0 3 1 2
----------------------
10 9
7 8 9
10 10 8
9 10 8
10 10 4
6 8 4
3 9 4
4 9 3
1 3 3
4 8 2
3 4 5 5 6 7 9 10 10 9
----------------------
3 4
2 2 4
1 1 4
3 3 1
1 3 0
4 4 2
**/
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |