解题思路:用树状数组实时维护第i节车厢的左右两边的符合条件的对数(反正这句话不好理解,我搞了好久还是晕晕晕)
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<vector>
#include<cmath>
#include<string>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;
int n;
int col[500010],l[500010],r[500010];
int num_R[500010],num_L[500010];
int ans[500010];//树状数组
int lowbit(int x){
return x&(-x);
}
void add(int pos,int z){
//if(pos==0)return ;
while(pos<=500000){
ans[pos]+=z;
pos+=lowbit(pos);
}
return ;
}
ll sum(int pos){
ll s=0;
while(pos>0){
s+=ans[pos];
pos-=lowbit(pos);
}
return s;
}
int main(){
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%d %d %d",&col[i],&l[i],&r[i]);
num_R[col[i]]++;
}
for(int i=0;i<n;i++){
num_R[col[i]]--;//减去本车厢一个col
add(col[i],-num_L[col[i]]);
printf("%lld ",sum(r[i])-sum(l[i]-1));
num_L[col[i]]++;
add(col[i],num_R[col[i]]);
}
printf("\n");
return 0;
}
// /\ | / |**、
// / \ | / | \
// / \ |/ | / _____ ____ | /
// /------\ |\ |__/ / \ \ /\ / / \ | /
// / \ | \ | / \ \ / \ / /______\ |/
// / \ | \ | \ / \ / \ / \ |
// / \ | \ | \_____/ \/ \/ \_____ |
/**
* ┏┓ ┏┓
* ┏┛┗━━━━━━━┛┗━━━┓
* ┃ ┃
* ┃ ━ ┃
* ┃ > < ┃
* ┃ ┃
* ┃... ⌒ ... ┃
* ┃ ┃
* ┗━┓ ┏━┛
* ┃ ┃ Code is far away from bug with the animal protecting
* ┃ ┃ 神兽保佑,代码无bug
* ┃ ┃
* ┃ ┃
* ┃ ┃
* ┃ ┃
* ┃ ┗━━━┓
* ┃ ┣┓
* ┃ ┏┛
* ┗┓┓┏━┳┓┏┛
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛
*/
// warm heart, wagging tail,and a smile just for you!
//
// _ooOoo_
// o8888888o
// 88" . "88
// (| -_- |)
// O\ = /O
// ____/`---'\____
// .' \| |// `.
// / \||| : |||// \
// / _||||| -:- |||||- \
// | | \\ - /// | |
// | \_| ''\---/'' | |
// \ .-\__ `-` ___/-. /
// ___`. .' /--.--\ `. . __
// ."" '< `.___\_<|>_/___.' >'"".
// | | : `- \`.;`\ _ /`;.`/ - ` : | |
// \ \ `-. \_ __\ /__ _/ .-` / /
// ======`-.____`-.___\_____/___.-`____.-'======
// `=---='
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//