# Bzoj4378 [poi2015]logistyka

## 离散化后用树状数组维护

Posted by yjjr's blog on April 11, 2018

# 题目

## Description

1. U k a 将序列中第k个数修改为a。
2. Z c s 在这个序列上，每次选出c个正数，并将它们都减去1，询问能否进行s次操作。

## Sample Input

3 8
U 1 5
U 2 7
Z 2 6
U 3 1
Z 2 6
U 2 2
Z 2 6
Z 2 1


## Sample Output

NIE
TAK
NIE
TAK


# code

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define dep(i,a,b) for(int i=a;i>=b;i--)
#define ll long long
#define mem(x,num) memset(x,num,sizeof x)
#define reg(x) for(int i=last[x];i;i=e[i].next)
using namespace std;
ll f=1,x=0;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
#define mid ((l+r)>>1)
#define lowbit(x) (x&(-x))
const int maxn=1e6+6;
int n,m,cnt,j,a[maxn],x[maxn],y[maxn],z[maxn],num[maxn],hash[maxn];
char ch[10];
struct bit_node{
ll c[maxn];
void modify(int x,int t){for(;x<=cnt;x+=lowbit(x))c[x]+=t;}
ll query(int x){ll re=0;for(;x;x-=lowbit(x))re+=c[x];return re;}
}bit1,bit2;
inline int find(int x){
int l=1,r=cnt;
while(l<r)if(hash[mid]<x)l=mid+1;else r=mid;
return l;
}
int main(){
rep(i,1,m){
scanf("%s%d%d",ch,&x[i],&y[i]);num[i]=y[i];
if(ch[0]=='U')z[i]=1;else z[i]=0;
}
sort(num+1,num+1+m);hash[cnt=1]=num[1];
rep(i,2,m)
if(num[i]!=num[i-1])hash[++cnt]=num[i];
rep(i,1,m)y[i]=find(y[i]);
rep(i,1,m)
if(z[i]){
if(j=a[x[i]]){bit1.modify(j,-1);bit2.modify(j,-hash[j]);}
a[x[i]]=y[i];
bit1.modify(y[i],1);bit2.modify(y[i],hash[y[i]]);
}else puts(bit2.query(y[i]-1)>=(x[i]-bit1.query(cnt)+bit1.query(y[i]-1))*hash[y[i]]?"TAK":"NIE");
return 0;
}