QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#483706 | #6509. Not Another Range Query Problem | BalintR | WA | 70ms | 348944kb | C++20 | 3.4kb | 2024-07-19 07:47:42 | 2024-07-19 07:47:42 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef unsigned uint;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef complex<double> cpx;
template <typename T> using minPq = priority_queue<T, vector<T>, greater<T>>;
#define ms(a, x) memset(a, x, sizeof(a))
#define pb push_back
#define fs first
#define sn second
#define ALL(v) begin(v), end(v)
#define SZ(v) ((int) (v).size())
#define lbv(v, x) (lower_bound(ALL(v), x) - (v).begin())
#define ubv(v, x) (upper_bound(ALL(v), x) - (v).begin())
template <typename T> inline void UNIQUE(vector<T> &v){sort(ALL(v)); v.resize(unique(ALL(v)) - v.begin());}
const int INF = 0x3f3f3f3f;
const ll LLINF = 0x3f3f3f3f3f3f3f3f;
const double PI = acos(-1);
#define FR(i, n) for(int i = 0; i < (n); i++)
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define FORR(i, a, b) for(int i = (a); i >= (b); i--)
#define dbg(x) {cerr << #x << ' ' << x << endl;}
#define dbgArr(arr, n) {cerr << #arr; FR(_i, n) cerr << ' ' << (arr)[_i]; cerr << endl;}
template <typename T, typename U>
ostream& operator<<(ostream &os, pair<T, U> p){return os << "(" << p.fs << ", " << p.sn << ")";}
struct Blk {
bool col;
deque<int> dq;
};
template <typename T>
void merge(deque<T> &a, deque<T> &b){
if(SZ(a) >= SZ(b)){
a.insert(a.end(), ALL(b));
b.clear();
}
else {
b.insert(b.begin(), ALL(a));
a = move(b);
}
}
const int TSZ = 1 << 19;
const int MN = 5e5 + 5;
namespace Tree {
int tree[TSZ*2];
void upd(int l, int r){
for(l += TSZ, r += TSZ; l < r; l >>= 1, r >>= 1){
if(l & 1) tree[l++]++;
if(r & 1) tree[--r]++;
}
}
int query(int i){
int res = 0;
for(i += TSZ; i; i >>= 1) res += tree[i];
return res;
}
}
int n, q;
string str;
Blk blks[MN];
int nb, pref[MN];
int ans[MN];
vector<pair<pii, int>> qrys[MN];
void proc(int t){
FR(i, nb) pref[i+1] = pref[i] + SZ(blks[i].dq);
for(auto [pr, ind] : qrys[t]){
auto [l, r] = pr;
int li = lower_bound(blks, blks+nb, l, [](const Blk &a, const int &b){
return a.dq.back() < b;}) - blks;
int ri = lower_bound(blks, blks+nb, r, [](const Blk &a, const int &b){
return a.dq.back() < b;}) - blks;
int lp = lbv(blks[li].dq, l) + Tree::query(l);
int rp = lbv(blks[ri].dq, r);
if(li == ri) ans[ind] = max(0, rp - lp);
else ans[ind] = max(0, SZ(blks[li].dq) - lp) + pref[ri] - pref[li+1] + rp;
}
int oldNb = nb;
nb = 0;
FR(i, oldNb){
int l = blks[i].dq.front();
int r = blks[i].dq.back();
blks[i].dq.pop_front();
if(blks[i].dq.empty()) continue;
Tree::upd(l+1, r+1);
if(nb && blks[i].col == blks[nb-1].col) merge(blks[nb-1].dq, blks[i].dq);
else blks[nb++] = move(blks[i]);
}
}
int main(){
cin.sync_with_stdio(0); cin.tie(0);
cin >> n >> q >> str;
FR(i, q){
int l, r, t;
cin >> l >> r >> t;
l--;
qrys[t].pb({{l, r}, i});
}
FR(i, n){
if(i && str[i] == str[i-1]) blks[nb-1].dq.pb(i);
else blks[nb].col = str[i]-'0', blks[nb++].dq.pb(i);
}
FR(t, n+1) proc(t);
FR(i, q) cout << ans[i] << '\n';
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 50ms
memory: 347516kb
input:
9 7 100110001 2 5 1 3 6 1 4 8 2 2 7 1 1 9 1 1 9 0 1 9 8
output:
2 1 1 3 4 9 0
result:
ok 7 numbers
Test #2:
score: -100
Wrong Answer
time: 70ms
memory: 348944kb
input:
100 100000 0000011010101000111011110110000111110101101010111111101011011010111001111010111000001000011000001010 76 99 3 25 84 7 45 83 11 10 12 10 69 86 4 27 28 1 22 42 42 4 86 25 26 91 22 20 81 17 50 78 0 77 93 50 31 50 34 7 46 13 78 89 0 79 98 0 2 84 33 58 93 11 56 75 2 55 77 68 7 9 41 44 46 11 47 ...
output:
0 0 0 0 0 0 0 0 0 0 29 0 0 0 12 20 0 0 1 0 0 0 0 0 0 0 0 0 18 0 0 57 0 0 11 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 10 0 0 0 0 0 0 8 0 0 16 0 19 29 37 19 12 26 0 21 6 0 0 0 0 0 0 0 0 0 0 0 0 0 0 51 0 0 0 0 11 0 0 0 9 0 0 16 22 0 0 0 0 0 0 0 0 0 0 0 46 59 0 0 43 10 0 0 0 0 15 ...
result:
wrong answer 1st numbers differ - expected: '8', found: '0'