QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#653878 | #7436. Optimal Ordered Problem Solver | lichenghan | 0 | 0ms | 0kb | C++14 | 6.3kb | 2024-10-18 20:47:47 | 2024-10-18 20:47:49 |
answer
#include <bits/stdc++.h>
using namespace std;
const int N=1e6+10;
inline constexpr int diff(const int& x,const int& y){ return x>y?x-y:y-x; }
#define USE_FREAD_FWRITE
// #define READ_NEGATIVE
// #define WRITE_NEGATIVE
namespace IO{
#ifdef USE_FREAD_FWRITE
#define SIZE (1<<20)
char in[SIZE],out[SIZE],*p1=in,*p2=in,*p3=out;
char getchar(){ return p1==p2&&(p2=(p1=in)+fread(in,1,SIZE,stdin),p1==p2)?EOF:*p1++; }
void flush(){ int len=p3-out; fwrite(p3=out,1,len,stdout); }
void putchar(char ch){ if(p3==out+SIZE)flush(); *p3++=(ch); }
struct Flush{~Flush(){flush();}}_;
#else
char getchar(){ return ::getchar(); }
void putchar(char ch){ ::putchar(ch); }
void flush(){}
#endif
template<typename type=int> inline type read(){
#ifdef READ_NEGATIVE
type x(0);bool flag(0);char ch=getchar();
for(;ch<'0'||ch>'9';ch=getchar())flag^=ch=='-';
for(;ch>='0'&&ch<='9';ch=getchar())x=(x<<1)+(x<<3)+(ch^48);
return flag?-x:x;
#else
type x(0);char ch=getchar();
for(;ch<'0'||ch>'9';ch=getchar());
for(;ch>='0'&&ch<='9';ch=getchar())x=(x<<1)+(x<<3)+(ch^48);
return x;
#endif
}
template<typename type>
inline void write(type x){
#ifdef WRITE_NEGATIVE
if(x<0)x=-x,putchar('-');
#endif
static int Stack[50],top(0);
do Stack[++top]=x%10,x/=10;while(x);
while(top) putchar(Stack[top--]|48);
}
}
using IO::read;
using IO::write;
int n,m;
array<int,2> a[N];
int tim[N];
int stk[N],top;
int opt[N],cx[N],cy[N],qx[N],qy[N];
int ans[N];
struct segt{
// untouched
// obt: y min
int t[N<<2];
inline void psu(int p){ t[p]=min(t[p<<1],t[p<<1|1]); }
void build(int l,int r,int p){
if(l==r){
t[p]=a[l][1];
return;
}
int mid=(l+r)>>1;
build(l,mid,p<<1);
build(mid+1,r,p<<1|1);
psu(p);
}
void q(int l,int r,int p,int x,int y){
if(t[p]>y) return;
if(l==r){
t[p]=N;
stk[++top]=l;
return;
}
int mid=(l+r)>>1;
if(x<=mid) q(l,mid,p<<1,x,y);
else q(l,mid,p<<1,mid,y),q(mid+1,r,p<<1|1,x,y);
psu(p);
}
void init(){
build(1,n,1);
}
void q(int x,int y){
top=0;
x=upper_bound(a+1,a+n+1,(array<int,2>){x,N})-a-1;
q(1,n,1,x,y);
}
}Tw;
struct AVL{
struct node{
int ls,rs,ht,siz;
int x,y,cx,cy;
}; node t[N]; int nc,rt;
void psu(int p){
if(!p) return;
t[p].ht=max(t[t[p].ls].ht,t[t[p].rs].ht)+1;
t[p].siz=t[t[p].ls].siz+t[t[p].rs].siz+1;
}
void adtx(int p,int x){
if(p) t[p].x=t[p].cx=x;
}
void adty(int p,int y){
if(p) t[p].y=t[p].cy=y;
}
void psd(int p){
if(t[p].cx){
adtx(t[p].ls,t[p].cx);
adtx(t[p].rs,t[p].cx);
t[p].cx=0;
}
if(t[p].cy){
adty(t[p].ls,t[p].cy);
adty(t[p].rs,t[p].cy);
t[p].cy=0;
}
}
void print(int p){
if(!p) return;
psd(p);
print(t[p].ls);
printf("#%d: ls=%d rs=%d ht=%d siz=%d x=%d y=%d\n",
p,t[p].ls,t[p].rs,t[p].ht,t[p].siz,t[p].x,t[p].y);
print(t[p].rs);
}
void right(int& p){
int q=t[p].ls;
t[p].ls=t[q].rs;
t[q].rs=p;
psu(q); psu(p);
p=q;
}
void left(int& p){
int q=t[p].rs;
t[p].rs=t[q].ls;
t[q].ls=p;
psu(q); psu(p);
p=q;
}
void insert(int x,int y,int& p){
if(!p){
p=++nc;
t[p].x=x; t[p].y=y; t[p].siz=1;
return;
}
psd(p);
if(x<=t[p].x&&y>=t[p].y){
insert(x,y,t[p].ls);
if(t[t[p].ls].ht==t[t[p].rs].ht+2){
if(t[t[t[p].ls].ls].ht>=t[t[t[p].ls].rs].ht){
right(p);
}else{
left(t[p].ls);
right(p);
}
}
}else{
insert(x,y,t[p].rs);
if(t[t[p].rs].ht==t[t[p].rs].ht+2){
if(t[t[t[p].rs].rs].ht>=t[t[t[p].rs].ls].ht){
left(p);
}else{
right(t[p].rs);
left(p);
}
}
}
psu(p);
assert(diff(t[t[p].ls].ht,t[t[p].rs].ht)<=1);
// assert(t[0].siz==0);
}
void insert(int x,int y){
// printf("insert %d %d!\n",x,y);
insert(x,y,rt);
// print(rt);
}
int cov(int x,int y,int op){
// printf("cov %d %d %d\n",x,y,op);
int p=rt;
while(p){
if(t[p].x>x){
p=t[p].ls;
}else if(t[p].y>y){
p=t[p].rs;
}else{
break;
}
}
if(!p) return 0;
// printf("p = %d\n",p);
if(op==1) t[p].x=x;
if(op==2) t[p].y=y;
int ans=1;
int u=t[p].ls;
while(u){
if(t[u].y<=y){
// printf("ans = %d\n",ans);
if(op==1) t[u].x=x;
if(op==2) t[u].y=y;
if(op==0) ans+=1+t[t[u].rs].siz;
else if(op==1) adtx(t[u].rs,x);
else adty(t[u].rs,x);
// printf("count %d\n",t[u].rs);
// printf("ans = %d\n",ans);
u=t[u].ls;
}else{
u=t[u].rs;
}
}
u=t[p].rs;
while(u){
if(t[u].x<=x){
// printf("ans = %d\n",ans);
if(op==1) t[u].x=x;
if(op==2) t[u].y=y;
if(op==0) ans+=1+t[t[u].ls].siz;
else if(op==1) adtx(t[u].ls,x);
else adty(t[u].ls,x);
// printf("count %d\n",t[u].ls);
// printf("ans = %d\n",ans);
u=t[u].rs;
}else{
u=t[u].ls;
}
}
// printf("tot=%d under=%d\n",t[rt].siz,ans);
// assert(t[0].siz==0);
// print(rt);
return t[rt].siz-ans;
}
}Ts;
struct BIT_suf{
int t[N];
void c(int x){ for(;x;x&=x-1) t[x]++; }
int q(int x){ int r=0; for(;x<N;x+=x&-x) r+=t[x]; return r; }
}T1,T2,T3;
vector<int> rtc[N],rxc[N],rxq[N],rxqc[N];
int main(){
n=read(),m=read();
for(int i=1;i<=n;i++) {
for(int &j:a[i]) j=read();
rxc[a[i][0]].push_back(i);
}
sort(a+1,a+n+1);
Tw.init();
for(int i=1;i<=m;i++){
opt[i]=read(); cx[i]=read(); cy[i]=read(); qx[i]=read(); qy[i]=read();
// printf("op: %d %d %d %d %d\n",opt[i],cx[i],cy[i],qx[i],qy[i]);
rxq[qx[i]].push_back(i);
rxqc[cx[i]].push_back(i);
}
for(int i=1;i<=m;i++){
if(opt[i]==1) Ts.cov(cx[i],cy[i],2);
else Ts.cov(cx[i],cy[i],1);
Tw.q(cx[i],cy[i]);
rtc[i]=vector<int>(stk+1,stk+top+1);
for(int j=1;j<=top;j++){
tim[stk[j]]=i;
int x=a[stk[j]][0],y=a[stk[j]][1];
if(opt[i]==1) y=cy[i];
else x=cx[i];
Ts.insert(x,y);
}
ans[i]=Ts.cov(qx[i],qy[i],0);
// printf("ans[%d] = %d\n",i,ans[i]);
}
for(int t=m;t>=1;t--){
ans[t]+=T1.q(qx[t]+1)+T2.q(qy[t]+1);
for(int i:rtc[t]){
T1.c(a[i][0]);
T2.c(a[i][1]);
}
}
for(int x=n;x>=1;x--){
for(int i:rxq[x]){
ans[i]-=T3.q(qy[i]+1);
}
for(int i:rxc[x]){
T3.c(a[i][1]);
}
}
int maxn=-1;
for(int x=n;x>=1;x--){
for(int i:rxq[x]){
if(maxn>qy[i]) ans[i]=n;
}
for(int i:rxqc[x]){
maxn=max(maxn,cy[i]);
}
}
for(int i=1;i<=m;i++){
write(n-ans[i]);
IO::putchar('\n');
}
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Runtime Error
Test #1:
score: 0
Runtime Error
input:
995 996 950 481 376 18 141 776 711 966 130 727 468 529 47 39 857 563 832 821 776 472 154 914 825 279 332 415 470 361 968 440 45 560 299 755 703 785 744 387 547 382 3 549 344 573 778 424 784 765 280 115 251 434 695 98 463 506 379 38 610 486 305 623 703 244 856 365 117 360 772 847 331 723 663 991 900 ...
output:
result:
Subtask #2:
score: 0
Runtime Error
Test #9:
score: 0
Runtime Error
input:
999996 999996 921339 140126 955508 363759 958698 342406 280137 955674 907511 75721 189876 946586 152058 168837 541857 557702 84498 865987 185574 809224 704828 701026 815379 548000 989515 518951 335439 336866 745570 790616 766723 212893 926629 859003 51261 40866 592510 556235 324926 700261 320946 798...
output:
result:
Subtask #3:
score: 0
Runtime Error
Test #17:
score: 0
Runtime Error
input:
999995 999997 261379 334440 985281 986034 549380 718157 670003 253925 533027 437989 326806 983213 965935 756259 229069 686789 331338 684961 957559 390618 937820 959719 338153 779814 582581 965418 634156 421264 308778 938878 913059 390904 481477 431492 739774 793015 901442 934600 256704 991485 366691...
output:
result:
Subtask #4:
score: 0
Skipped
Dependency #1:
0%
Subtask #5:
score: 0
Skipped
Dependency #1:
0%