QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#360801 | #8229. 栈 | Strywr | 0 | 130ms | 22016kb | C++20 | 4.3kb | 2024-03-22 09:46:30 | 2024-03-22 09:46:31 |
Judging History
answer
#include<bits/stdc++.h>
#define lson (nw << 1)
#define rson (nw << 1 | 1)
using namespace std;
typedef long long ll;
inline ll read() {
ll x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = (x << 1) + (x << 3) + (ch ^ '0');
ch = getchar();
}
return x * f;
}
const int N = 2e5 + 10;
struct Op {
int x, y, t; ll a, b; //x个栈,y的时间轴
Op(){}
Op(int xx, int yy, int tt, ll aa, ll bb) {
x = xx; y = yy; t = tt; a = aa; b = bb;
}
//t是type, a, b是参数
}a[N]; int tot, n, m, all; ll ans[N];
//!!!!!!注意longlong 线段树1~m
bool cmp(Op a, Op b) {
if (a.x != b.x) return a.x < b.x;
return a.t < b.t;
}
struct Seg {
int l, r; ll a, b, sum; //右括号, 左括号数, 以及所有数的和
}tr[N*4];
ll merge(ll k, int nw) { //在x里面选择k个左括号
if (!k) return 0;
if (tr[nw].l == tr[nw].r) {
if (!tr[nw].b) return 0;
return min(tr[nw].b, k) * (tr[nw].sum / tr[nw].b);
} ll lcnt = max(0ll, tr[lson].b - tr[rson].a);
if (lcnt >= k) return merge(k, lson);
else return tr[nw].sum - tr[rson].sum + merge(k - lcnt, rson);
//tr[nw].sum - tr[rson].sum 就是合并以后左边的sum,直接左边的sum是没有合并右边的
//如果不可减, 可以考虑再维护一个比如这题的:右边合并以后的左边sum, 或者楼房重建里的算上左边的max的时候右子树的sum
}
void pushup(int nw) {
if (tr[lson].b < tr[rson].a) {
tr[nw].a = tr[lson].a + tr[rson].a - tr[lson].b; tr[nw].b = tr[rson].b;
tr[nw].sum = tr[rson].sum;
} else {
tr[nw].a = tr[lson].a; tr[nw].b = tr[rson].b + tr[lson].b - tr[rson].a;
tr[nw].sum = tr[rson].sum + merge(tr[lson].b - tr[rson].a, lson);
}
}
void build(int nw, int l, int r) {
tr[nw].l = l; tr[nw].r = r;
if (l == r) return;
int mid = (l + r) >> 1;
build(lson, l, mid); build(rson, mid + 1, r);
}
void update(int nw, int x, ll a, ll b) { //a是左边的,也就是右括号, b是右边的,是左括号!!!!!!!!
if (tr[nw].l == tr[nw].r) {
if (!b) {
tr[nw].a = a; tr[nw].b = 0; tr[nw].sum = 0;
} else {
tr[nw].a = 0; tr[nw].b = b; tr[nw].sum = a * b;
}
return;
} int mid = (tr[nw].l + tr[nw].r) >> 1;
if (x <= mid) update(lson, x, a, b);
else update(rson, x, a, b);
pushup(nw);
}
pair<ll, ll> geta(int nw, int x, int y) {
if (x <= tr[nw].l && tr[nw].r <= y) {
return make_pair(tr[nw].a, tr[nw].b);
} int mid = (tr[nw].l + tr[nw].r) >> 1;
pair<ll, ll> l = {}, r = {};
if (x <= mid) l = geta(lson, x, y);
if (y > mid) r = geta(rson, x, y);
if (l.second < r.first) return make_pair(l.first + r.first - l.second, r.second);
else return make_pair(l.first, r.second + l.second - r.first);
}
ll ask(int nw, int x, ll k) { //return的值是nw里面实际上删除了多少个
//!!!!!!!!!!!不能直接左边取k个,因为可能右半部分会抵消
if (!k) return 0;
if (tr[nw].l == tr[nw].r) {
if (!tr[nw].b) return 0;
return min(tr[nw].b, k) * (tr[nw].sum / tr[nw].b);
} int mid = (tr[nw].l + tr[nw].r) >> 1;
if (x <= mid) return ask(lson, x, k);
else {
ll a = geta(rson, mid + 1, x).first;
ll lcnt = max(tr[lson].b - a, 0ll);
if (k <= lcnt) return ask(lson, x, k);
else {
return merge(lcnt, lson) + ask(rson, x, k - lcnt);
}
}
}
int main() {
n = read(), m = read();
for (int i = 1; i <= m; i++) {
int op = read(); ll l = read(), r = read(), x = read();
if (op == 1) {
ll y = read(); a[++tot] = Op(l, i, -1, x, y);
if (r != n) a[++tot] = Op(r + 1, i, -3, x, y); //先修改,再查询
} else if (op == 2) {
a[++tot] = Op(l, i, -2, x, 0);
if (r != n) a[++tot] = Op(r + 1, i, -3, x, 0);
} else {
++all; a[++tot] = Op(l, i, all, r, x);
}
}
sort(a + 1, a + tot + 1, cmp); build(1, 1, m);
for (int i = 1; i <= m; i++) {
int x = a[i].x;
while (i <= m && a[i].x == x) {
if (a[i].t == -3) update(1, a[i].y, 0, 0);
else if (a[i].t == -2) update(1, a[i].y, a[i].a, 0);
else if (a[i].t == -1) update(1, a[i].y, a[i].b, a[i].a);
else {
ans[a[i].t] = ask(1, a[i].y, a[i].b) - ask(1, a[i].y, a[i].a - 1); // 1~a[i].y次修改中, 前a[i].b个元素的和
} i++;
} i--;
}
for (int i = 1; i <= all; i++) printf("%lld\n", ans[i]);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 0
Wrong Answer
time: 2ms
memory: 8480kb
input:
4907 4910 2 763 3330 1 3 307 1 1 1 2262 3430 22699 89397 1 1915 4000 51541 67587 2 212 2990 9763 2 1086 2162 1 2 1813 4496 16760 1 51 2796 68005 99390 1 1267 1519 74236 66178 3 1768 23808 54314 2 900 4122 27758 3 3287 17350 28989 2 3277 4024 3633 2 444 4866 1 2 353 4219 1061 1 987 3141 99906 17320 2...
output:
0 3032090730 0 0 0 0 647114751 0 50793012 0 0 0 0 0 0 871619914 536311874 0 0 1792521566 0 2415375780 0 225987900 0 1145132507 140071334 0 0 0 5646099271 84280112 1232466642 4992966775 7968862819 3943711826 0 0 4254918170 642842550 1027888122 113773506 5682165530 0 0 0 274738278 212323788 2573356630...
result:
wrong answer 3rd numbers differ - expected: '903396180', found: '0'
Subtask #2:
score: 0
Wrong Answer
Test #6:
score: 0
Wrong Answer
time: 130ms
memory: 21012kb
input:
99999 99998 1 5026 18575 27178 90423 3 30623 1 1 3 76936 1 1 1 77021 95683 84664 24734 1 46085 74886 40512 11266 3 5048 8594 22468 1 53318 77721 97151 70784 1 70645 91192 37556 13013 1 56752 56940 91812 62887 1 7928 34576 87339 69404 3 74875 32807 100970 3 22338 17221 25771 3 21421 20602 57957 3 717...
output:
0 0 1254619125 0 593473604 2592655824 0 5652513833 110091352 1226646296 1989326852 763582808 8205318671 0 3012598941 20085582585 0 0 24555397019 4722824224 20308857160 899316516 38935050954 988382364 13341823621 11397759491 2449683584 0 8057235510 0 456207732 13193219860 16395004737 16985376388 2887...
result:
wrong answer 4th numbers differ - expected: '4366274868', found: '0'
Subtask #3:
score: 0
Wrong Answer
Test #12:
score: 0
Wrong Answer
time: 75ms
memory: 21584kb
input:
100000 99993 1 47773 70467 16065 1 2 52349 78446 2304 3 40821 1 1 1 40216 93069 78144 1 1 41089 43671 76025 1 2 35263 68629 31066 3 79881 13534 57327 3 5556 1 1 2 21962 38192 1 1 664 58116 9417 1 3 28089 6039 7989 2 88500 90302 9946 3 63215 49410 60770 2 11069 89527 57581 2 70303 97603 12363 1 3420 ...
output:
0 0 0 1951 0 0 0 29245 7969 1947 0 16405 59952 123666 0 68209 0 0 32225 0 0 0 0 96178 14285 300941 0 1602 129470 61935 4068 114182 17609 152949 26099 0 250368 0 183349 125791 0 61871 0 0 0 183297 23029 0 0 26259 204595 0 0 0 0 0 0 0 138046 35783 0 32269 118370 136766 0 36659 34954 130481 24044 0 0 4...
result:
wrong answer 2nd numbers differ - expected: '43794', found: '0'
Subtask #4:
score: 0
Wrong Answer
Test #17:
score: 0
Wrong Answer
time: 63ms
memory: 22016kb
input:
99999 99996 3 77889 1 10000000000 1 6316 86327 89644 386 3 9260 1 10000000000 2 2603 47234 69717 2 20260 73011 19290 2 62477 81233 26127 1 50140 68508 37004 98794 2 14449 22788 16063 1 43860 84932 50375 21777 1 67345 94584 28202 66610 2 661 68654 1 1 14411 94422 82738 61196 1 16563 94416 4920 38408 ...
output:
0 34602584 0 0 27739639583 0 0 1902514434 1902514434 2147553884 1902514434 15794375094 0 4192446657 15797478185 13141921145 0 0 5775183021 363222594 1995572111 2193350882 0 0 5508935691 250667843 0 0 0 21958753162 0 4496343819 0 0 0 5150682518 0 9331298212 3263861048 717360204 0 0 18115621795 0 7279...
result:
wrong answer 6th numbers differ - expected: '1363823412', found: '0'
Subtask #5:
score: 0
Skipped
Dependency #1:
0%