QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#141427 | #6533. Traveling in Cells | cy1999 | WA | 1ms | 26472kb | C++20 | 6.0kb | 2023-08-17 12:00:35 | 2023-08-17 12:00:37 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std ;
typedef long long ll ;
const int N = 100010 ;
int n , q , T , k;
int c[N] , v[N] , a[N] ;
bool tag , vis[N] ;
struct SGTt {
#define ls p*2
#define rs p*2+1
struct tree {
map<int , int> cnt ;
ll sum ;
} t[N * 4] ;
set<int> merge(set<int> x , set<int> y ) {
if(x.size() > y.size()) {
auto it = y.begin() ; for( ; it != y.end() ; it ++ ) {
x.insert(*it) ;
}
return x ;
} else {
auto it = x.begin() ;
for( ; it != x.end() ; it ++) {
y.insert(*it) ;
}
return y ;
}
}
void pushup(tree &p , tree x , tree y) {
if(x.cnt.size() > y.cnt.size()) {
p = x ;
auto it = y.cnt.begin() ; for( ; it != y.cnt.end() ; it ++ ) {
p.cnt[it->first] += it->second ;
}
} else {
p = y ;
auto it = x.cnt.begin() ;
for( ; it != x.cnt.end() ; it ++) {
p.cnt[it->first] += it->second ;
}
}
p.sum = x.sum + y.sum ;
}
void build(int p , int l , int r) {
t[p].cnt.clear() ;
if(l == r) {
t[p].cnt[c[l]] = 1 ;
t[p].sum = v[l] ;
return ;
}
int mid = (l + r) >> 1 ;
build(ls , l , mid) ; build(rs , mid + 1 , r) ;
pushup(t[p] , t[ls] , t[rs]) ;
}
tree query(int p , int l , int r , int ql , int qr){
tree res ;
if(tag) return res ;
if(ql <= l && r <= qr) {
return t[p] ;
}
int mid = (l + r) >> 1 ;
if(ql > mid) {
tree res1 = query(rs , mid + 1 , r , ql , qr) ;
if(res1.cnt.size() > k) return tag = 1 , res ;
return res1 ;
}
if(qr <= mid) {
tree res1 = query(ls , l , mid , ql , qr) ;
if(res1.cnt.size() > k) return tag = 1 , res ;
return res1 ;
}
tree res1 = query(ls , l , mid , ql , qr) , res2 = query(rs , mid + 1 , r , ql , qr) ;
pushup(res , res1 , res2) ;
if(res.cnt.size() > k) {
tag = 1 ;
}
return res ;
}
void ERASE(tree &p , int cx) {
p.cnt[cx] -- ;
if(p.cnt[cx] <= 0) {
p.cnt.erase(cx) ;
}
}
void del(int p , int l , int r , int x) {
ERASE(t[p] , c[x]) ;
if(l == r) {
return ;
}
int mid = (l + r) >> 1 ;
if(x <= mid) {
del(ls , l , mid , x) ;
} else {
del(rs , mid + 1 , r , x) ;
}
}
void ADD(tree &p , int cx) {
p.cnt[cx] ++ ;
}
void add(int p , int l , int r , int x) {
ADD(t[p] , c[x]) ;
if(l == r) return ;
int mid = (l + r) >> 1 ;
if(x <= mid) add(ls , l , mid , x) ;
else add(rs , mid + 1 , r , x) ;
}
void change(int p , int l , int r , int x) {
if(l == r) {
t[p].sum = v[x] ;
return ;
}
int mid = (l + r) >> 1;
if(x <= mid) change(ls , l , mid , x) ;
else change(rs , mid + 1 , r , x ) ;
t[p].sum = t[ls].sum + t[rs].sum ;
}
ll ask(int p , int l , int r , int ql , int qr) {
if(ql <= l && r <= qr) {
return t[p].sum ;
}
ll res = 0 ; int mid = (l + r) >> 1 ;
if(ql <= mid) res += ask(ls , l , mid , ql , qr);
if(qr > mid) res += ask(rs , mid + 1 , r , ql , qr) ;
return res ;
}
tree res ;
} seg ;
bool check(map<int , int> x) {
auto it = x.begin() ;
for( ; it != x.end() ; it ++) {
if(it->second <= 0) continue ;
if(!vis[it->first]) return 0 ;
}
return 1 ;
}
int main() {
// freopen("data.in" , "r" , stdin) ;
// freopen("mine.out" , "w" , stdout) ;
// clock_t be = clock() ;
cin >> T ;
while(T --) {
scanf("%d%d" , &n , &q) ;
for(int i = 1 ; i <= n ; i ++) scanf("%d" , &c[i]) ;
for(int i = 1 ; i <= n ; i ++) scanf("%d" , &v[i]) ;
seg.build(1 , 1 , n) ;
// printf("%.5lf\n" , (double)(clock() - be) / CLOCKS_PER_SEC) ;
while(q --) {
int opt ; scanf("%d" , &opt) ;
if(opt == 1) {
int p , x ; scanf("%d%d" , &p , &x) ;
seg.del(1 , 1 , n , p) ;
c[p] = x ; seg.add(1 , 1 , n , p) ;
} else if(opt == 2) {
int p , x ; scanf("%d%d" , &p , &x) ;
v[p] = x ; seg.change(1 , 1 , n , p) ;
} else {
// clock_t oo = clock() ;
int x ; scanf("%d%d" , &x , &k) ;
for(int i = 1 ; i <= k ; i ++) {
scanf("%d" , &a[i]) ;
vis[a[i]] = 1 ;
}
int posl , posr ;
int l = x , r = n ;
while(l < r) {
int mid = (l + r + 1) >> 1 ;
tag = 0 ; seg.res = seg.query(1 , 1 , n , x , mid) ;
if(tag == 0 && check(seg.res.cnt)) l = mid ;
else r = mid - 1 ;
}
posr = l ;
r = x , l = 1 ;
while(l < r) {
int mid = (l + r) >> 1;
tag = 0 ; seg.res = seg.query(1 , 1 , n , mid , x) ;
if(tag == 0 && check(seg.res.cnt)) r = mid ;
else l = mid + 1 ;
}
posl = l ;
ll ans = seg.ask(1 , 1 , n , posl , posr) ;
printf("%lld\n" , ans) ;
// printf("time : %.5lf\n" , (double)(clock() - oo) / CLOCKS_PER_SEC) ;
}
}
}
}
/*
1
10 3
6 3 6 1 1 6 1 1 9 5
13110 1530 2229 4123 2326 30416 20749 4039 1078 2630
1 6 1
1 10 5
3 9 4 9 3 1 4
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 1ms
memory: 26472kb
input:
2 5 10 1 2 3 1 2 1 10 100 1000 10000 3 3 1 3 3 3 2 2 3 2 5 20000 2 3 200 3 3 2 1 3 3 3 3 1 2 3 1 3 4 2 1 100000 1 2 2 3 1 2 1 2 4 1 1 2 3 4 1000000 1000000 1000000 1000000 3 4 4 1 2 3 4
output:
100 110 1210 21211 100010 4000000
result:
wrong answer 3rd numbers differ - expected: '1200', found: '1210'