[ABC426C]Upgrade Required题解
Time Limit: 2 sec / Memory Limit: 1024 MiB
Score : 300 points
Problem Statement
There are N versions of a certain OS, numbered 1,2,…,N in chronological order.
There are N PCs, and initially the OS version of the i-th PC is i.
Perform Q operations in order. The i-th operation is as follows:
- Upgrade all PCs whose current OS version is Xi or earlier to version Yi(>Xi). Then, print the number of PCs upgraded in this operation.
Note that for i<Q, the upgrades from the i-th operation are performed before proceeding to the (i+1)-th operation.
Constraints
- All input values are integers.
- 2≤N≤106
- 1≤Q≤2×105
- 1≤Xi<Yi≤N
有道 翻译
# #约束
—所有输入值均为整数。
—— 2≤N≤106
—— 1≤Q≤2×105
—— 1≤Xi<Yi≤N
Input
The input is given from Standard Input in the following format:
N Q X1 Y1 X2 Y2 ⋮ XQ YQ
有道 翻译
# #输入
输入来自标准输入,格式如下:
N Q
X1 Y1
X2 Y2
⋮
XQ YQ
Output
Output Q lines.
The i-th line should contain the number of PCs upgraded in the i-th operation.
有道 翻译
# #输出
输出 Q 行。
i \第一行应该包含 i \第一次操作中升级的pc的数量。
Sample Input 1
Copy
8 5 2 6 3 5 1 7 5 7 7 8
Sample Output 1
Copy
2 1 0 3 7
This input contains five operations.
- Initially, the versions of the eight PCs are 1,2,3,4,5,6,7,8.
- In the first operation, PCs with version 2 or earlier are upgraded to version 6.
- This operation upgrades two PCs, and the versions of the PCs become 6,6,3,4,5,6,7,8.
- In the second operation, PCs with version 3 or earlier are upgraded to version 5.
- This operation upgrades one PC, and the versions of the PCs become 6,6,5,4,5,6,7,8.
- In the third operation, PCs with version 1 or earlier are upgraded to version 7.
- This operation upgrades zero PCs, and the versions of the PCs become 6,6,5,4,5,6,7,8.
- In the fourth operation, PCs with version 5 or earlier are upgraded to version 7.
- This operation upgrades three PCs, and the versions of the PCs become 6,6,7,7,7,6,7,8.
- In the fifth operation, PCs with version 7 or earlier are upgraded to version 8.
- This operation upgrades seven PCs, and the versions of the PCs become 8,8,8,8,8,8,8,8.
有道 翻译
###输出示例
2
1
0
3
7
这个输入包含五个操作。
—最初,8台pc的版本号为 1,2,3,4,5,6,7,8 。
—第一次操作中,将版本为 2 及之前版本的pc升级到版本为 6 。
—该操作升级两台pc,两台pc的版本号变为 6,6,3,4,5,6,7,8 。
—第二次操作中,将版本为 3 及之前版本的pc升级为版本为 5 的pc。
—该操作升级了一台PC, PC的版本号变为 6,6,5,4,5,6,7,8 。
—第三次操作中,将版本为 1 及之前的pc升级到版本为 7 。
—此操作升级0台pc, pc的版本号变为 6,6,5,4,5,6,7,8 。
—第四次操作将版本为 5 及之前版本的pc升级到版本为 7 。
—升级三台pc,升级后的pc版本号为 6,6,7,7,7,6,7,8 。
—第五次操作中,将版本为 7 及之前版本的pc升级到版本为 8 。
—本次操作升级7台pc, pc的版本号为 8,8,8,8,8,8,8,8 。
思路
用线段树记录即可。
代码见下
#include<bits/stdc++.h>
using namespace std;
long long n,q,x[1000006],y[1000006],op[1000006],dd=0;
long long te[4000006],lz[4000006],xx,siz[4000006];
void alz(int a1,long long v){
te[a1]=siz[a1]*v;
lz[a1]=v;
}//a1打标记值v
void dow(int a1){
if(lz[a1]!=-1){
alz(a1*2,lz[a1]);
alz(a1*2+1,lz[a1]);
}
lz[a1]=-1;
}//a1下发标记至儿子
void bu(int a1,int l,int r){
siz[a1]=r-l+1;
lz[a1]=-1;
if(l==r){
te[a1]=1;
return ;
}
int mid=(l+r)/2;
bu(a1*2,l,mid);
bu(a1*2+1,mid+1,r);
te[a1]=te[a1*2]+te[a1*2+1];
return ;
}//l~r建树节点a1
void ci(int a1,int l,int r,int x,int y,long long v){
if(l>=x&&r<=y){
alz(a1,v);
return ;
}
int mid=(l+r)/2;
dow(a1);
if(x<=mid){
ci(a1*2,l,mid,x,y,v);
}
if(mid+1<=y){
ci(a1*2+1,mid+1,r,x,y,v);
}
te[a1]=te[a1*2]+te[a1*2+1];
return ;
}//x~y区间加v至l~r区间a1点
long long co(int a1,int l,int r,int x,int y){
if(x<=l&&r<=y){
return te[a1];
}
int mid=(l+r)/2;
long long dbdb=0;
dow(a1);
if(mid>=x){
dbdb+=co(a1*2,l,mid,x,y);
}
if(mid+1<=y){
dbdb+=co(a1*2+1,mid+1,r,x,y);
}
return dbdb;
}//x~y区间问至l~r区间a1点
int main(){
cin>>n>>q;
bu(1,1,n);
for(int i=1;i<=q;i++){
cin>>x[i]>>y[i];
cout<<co(1,1,n,1,x[i])<<endl;
ci(1,1,n,y[i],y[i],co(1,1,n,1,x[i])+co(1,1,n,y[i],y[i]));
ci(1,1,n,1,x[i],0);
}
return 0;
}

浙公网安备 33010602011771号