QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#880313 | #9694. Light Drinking and Low Singing | hos_lyric | TL | 98ms | 3928kb | C++14 | 4.2kb | 2025-02-03 05:02:01 | 2025-02-03 05:02:02 |
Judging History
answer
#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using Int = long long;
template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")
struct Node {
// assume power of 2
int sz;
// (00)*, (01)*, (10)*, (11)*
int same;
int sum, head, tail, lz;
Node() : sz(0), same(0), sum(0), head(0), tail(0), lz(0) {}
void push(Node &l, Node &r) {
if (lz) {
l.flip();
r.flip();
lz = 0;
}
}
void pull(const Node &l, const Node &r) {
if (sz == 2) {
same = l.head | r.tail << 1;
} else {
same = (l.same == r.same) ? l.same : -1;
}
sum = l.sum + r.sum;
head = l.head;
tail = r.tail;
}
void flip() {
same ^= 3;
sum = sz - sum;
head ^= 1;
tail ^= 1;
lz ^= 1;
}
};
int segN;
vector<Node> seg;
void push(int u) { seg[u].push(seg[u << 1], seg[u << 1 | 1]); }
void pull(int u) { seg[u].pull(seg[u << 1], seg[u << 1 | 1]); }
void flip(int u, int l, int r, int a, int b) {
if (b <= l || r <= a) return;
if (a <= l && r <= b) {
seg[u].flip();
return;
}
const int uL = u << 1, uR = u << 1 | 1;
const int m = (l + r) / 2;
push(u);
flip(uL, l, m, a, b);
flip(uR, m, r, a, b);
pull(u);
}
void oper(int u, int l, int r, int a, int b, int o) {
if (b <= l || r <= a) return;
if (a <= l && r <= b) {
if (seg[u].same == 0) return;
if (seg[u].same == 3) return;
if (seg[u].same == (o | (o ^ 1) << 1)) {
flip(u, l, r, l, r);
return;
}
if (seg[u].same == ((o ^ 1) | o << 1)) {
if (l + 1 < r - 1) flip(u, l, r, l + 1, r - 1);
return;
}
}
const int uL = u << 1, uR = u << 1 | 1;
const int m = (l + r) / 2;
push(u);
const bool f = (a < m && m < b && seg[uL].tail == o && seg[uR].head == (o ^ 1));
oper(uL, l, m, a, b, o);
oper(uR, m, r, a, b, o);
if (f) {
flip(uL, l, m, m - 1, m);
flip(uR, m, r, m, m + 1);
}
pull(u);
}
int get(int u, int l, int r, int a, int b) {
if (b <= l || r <= a) return 0;
if (a <= l && r <= b) return seg[u].sum;
const int uL = u << 1, uR = u << 1 | 1;
const int m = (l + r) / 2;
push(u);
const int resL = get(uL, l, m, a, b);
const int resR = get(uR, m, r, a, b);
pull(u);
return resL + resR;
}
int N, Q;
char S[2'000'010];
int main() {
for (; ~scanf("%d%d", &N, &Q); ) {
scanf("%s", S);
for (segN = 1; segN < N; segN <<= 1) {}
seg.assign(segN << 1, Node());
for (int u = segN; u < segN << 1; ++u) {
seg[u].sz = 1;
if (u - segN < N && S[u - segN] == '1') {
seg[u].same = 3;
seg[u].sum = seg[u].head = seg[u].tail = 1;
}
}
for (int u = segN; --u; ) {
seg[u].sz = seg[u << 1].sz << 1;
pull(u);
}
for (; Q--; ) {
int O, L, R;
scanf("%d%d%d", &O, &L, &R);
--O;
--L;
if (O == 0 || O == 1) {
oper(1, 0, segN, L, R, O);
} else if (O == 2) {
const int ans = get(1, 0, segN, L, R);
printf("%d\n", ans);
} else {
assert(false);
}
}
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3840kb
input:
10 10 0011101100 2 5 9 3 2 5 1 1 10 1 1 5 3 4 6 1 2 5 2 4 9 3 5 10 3 2 7 3 1 8
output:
2 1 3 3 4
result:
ok 5 number(s): "2 1 3 3 4"
Test #2:
score: 0
Accepted
time: 68ms
memory: 3924kb
input:
5000 5000 11110100011000001111101011001001000111001111100101011101110100010011000011100110001001111101001100111111001000000100001001101101001001110010101000001101110001000000001101101110101100101010010011010111011110100011001111100101101100000100111100000100001001000100100110111010010110010101101110...
output:
2466 2321 2400 2359 2430 2377 2302 2430 2426 977 2408 2356 2389 2475 2332 2466 2466 2385 2362 2284 2357 2377 2403 2432 2397 2296 2327 2353 2409 2416 2344 2385 2388 237 215 2467 2277 351 2393 2358 2398 2396 2446 2375 2439 2314 2335 2461 2432 2136 2462 2365 2305 1468 2364 2397 2391 1407 2280 2312 986 ...
result:
ok 2535 numbers
Test #3:
score: 0
Accepted
time: 73ms
memory: 3928kb
input:
5000 5000 01101001101111100000101011101110001000101100000110001111010010110001011001111101000001100110010100000100111010111001111110000101000100110100011001010101011100011100001011000101100000101010000101111101111111101011001101001011001101101001001011010100111001101010000110011001001001110011100101...
output:
1468 496 1548 277 1632 162 651 1336 73 24 315 1032 1258 1056 395 142 113 950 554 145 155 290 1160 1001 2009 879 1169 1160 142 261 560 1715 281 1688 1061 1858 228 869 707 1509 1293 1338 58 695 451 1271 736 374 856 320 2118 741 1111 1821 838 67 459 273 1888 1606 2076 2248 325 352 60 765 452 677 705 99...
result:
ok 1618 numbers
Test #4:
score: 0
Accepted
time: 98ms
memory: 3928kb
input:
5000 5000 01010101110000011010001101100111011100111111101011010011111111111111111111110001110100111010111110110100110101101111101010100110100000001011011011111000111011101011000111011110100010100000110011111100111110011100111000010100100111110010011111011110111011110110010011011000101011101001110110...
output:
271 441 2171 715 507 462 1218 2090 1977 1289 966 2184 541 1574 155 240 531 874 132 621 2034 2045 1177 602 1987 1221 1161 951 1863 643 654 348 362 706 228 376 1221 436 861 962 93 205 49 223 20 1148 1682 765 401 492 1018 1547 451 1351 1554 94 132 269 1448 355 41 110 851 580 582 531 642 675 1079 374 19...
result:
ok 469 numbers
Test #5:
score: 0
Accepted
time: 67ms
memory: 3928kb
input:
5000 4999 00000111110101110011101000000011101111110000101111011000011111111011011111011110010111000010000111010011000110000010110101011100101001000011001110110111111011000110010010001111110011011111000011101110110011001010111000000000100100001001011011000110100010111110001010000000110100110100110101...
output:
2265 2381 2376 2356 325 1636 1908 2321 2279 2320 2265 2388 2315 2232 2393 2341 2349 2347 427 131 2416 2295 2324 2262 2250 2258 2319 2408 2432 2319 2247 2278 2311 2370 2416 2303 2266 2307 2411 453 2293 2397 2335 2406 2346 2442 2350 2299 2386 2260 875 2307 2368 2292 2271 2400 2319 2220 2287 1377 2454 ...
result:
ok 962 numbers
Test #6:
score: 0
Accepted
time: 68ms
memory: 3928kb
input:
5000 5000 11010110011001001000101000010011011000100000110010010000101111101001011111111000110100101001000010110110100101011101011100001011001001100110110110111111101011011111000011010100011000110011000100011101110100010100101011000110001110010011101100101110011110111001000111000011000010110110100000...
output:
1116 131 1373 1062 66 299 1568 1261 160 1301 550 467 449 1704 354 823 192 320 1134 203 1240 782 1853 888 1789 1605 285 1505 835 1282 1289 205 594 534 329 566 625 2093 1221 665 1898 741 389 1298 587 1638 1350 1312 1259 763 1166 423 239 1164 978 171 186 1346 122 1945 1427 27 889 659 1189 228 1266 601 ...
result:
ok 1714 numbers
Test #7:
score: -100
Time Limit Exceeded
input:
100000 100000 1010111101101111001100000010000100110010010011000110000000010011010110001101000100001110110101100001100111101101111110110000011011100010111110000100110000010110101111001100100010001110000000101100110010101010111111001101101011010111001110100000111000111000010010011000101100000010001011...
output:
47842 46846 48368 47330 44883 30907 45125 46978 48287 45607 47272 48264 46522 46124 7713 46192 18370 47011 45153 47233 47274 47423 46410 46134 45765 12207 48753 46849 46265 47659 45767 44721 47589 46886 48378 47080 47247 47348 45381 46247 47667 47183 36147 46243 47095 45124 47416 47759 48585 46426 4...