4378: [POI2015]Logistyka
Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 419 Solved: 223
[ Submit][ Status][ Discuss]
Description
维护一个长度为n的序列,一开始都是0,支持以下两种操作:
1.U k a 将序列中第k个数修改为a。
2.Z c s 在这个序列上,每次选出c个正数,并将它们都减去1,询问能否进行s次操作。
每次询问独立,即每次询问不会对序列进行修改。
Input
第一行包含两个正整数n,m(1<=n,m<=1000000),分别表示序列长度和操作次数。
接下来m行为m个操作,其中1<=k,c<=n,0<=a<=10^9,1<=s<=10^9。
Output
包含若干行,对于每个Z询问,若可行,输出TAK,否则输出NIE。
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
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
TAK
NIE
TAK
HINT
鸣谢Claris
Source
对于询问c s,直接考虑每个数字对答案的贡献。
首先需要合法地选出数值为c*s的数字,这样才够减
对于当前数集,统计大于等于s的数字有多少个,每次-1都让这些数字参与
假设一共有t个大于等于s的数字,那么它们的贡献就是t*s(只需要做s次就够了)
对于每个小于s的数字,只要和大于等于s的数字一起-1就行了
因此只要安排的顺序得当,这些数的和有多少,就能贡献多少
只要用树状数组维护和查询就行了
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn = 1E6 + 10;
typedef long long LL;
struct Query{
int typ,x,y; Query(){}
Query(int typ,int x,int y): typ(typ),x(x),y(y){}
}Q[maxn];
LL sum[maxn];
int n,m,cur = 1,A[maxn],c[maxn],B[maxn];
void Modify(int num,LL typ)
{
int pos = lower_bound(A + 1,A + cur + 1,num) - A;
for (int i = pos; i <= cur; i += i&-i)
c[i] += typ,sum[i] += typ*num;
}
LL Get_sum(int pos)
{
LL ret = 0;
for (int i = pos; i > 0; i -= i&-i) ret += sum[i];
return ret;
}
int Get_num(int pos)
{
int ret = 0;
for (int i = cur; i > 0; i -= i&-i) ret += c[i];
for (int i = pos; i > 0; i -= i&-i) ret -= c[i];
return ret;
}
int gettyp()
{
char ch = getchar();
while (ch != 'U' && ch != 'Z') ch = getchar();
return ch == 'U'?1:2;
}
int getint()
{
char ch = getchar(); int ret = 0;
while (ch < '0' || '9' < ch) ch = getchar();
while ('0' <= ch && ch <= '9')
ret = ret*10 + ch - '0',ch = getchar();
return ret;
}
int main()
{
#ifdef DMC
freopen("DMC.txt","r",stdin);
#endif
n = getint(); m = getint();
for (int i = 1; i <= m; i++)
{
int typ = gettyp(),x,y;
x = getint(); y = getint();
Q[i] = Query(typ,x,y); A[i] = y;
}
sort(A + 1,A + m + 1);
for (int i = 2; i <= m; i++)
if (A[i] != A[i-1]) A[++cur] = A[i];
for (int i = 1; i <= m; i++)
{
Query k = Q[i];
if (k.typ == 1)
{
if (B[k.x]) Modify(B[k.x],-1);
B[k.x] = k.y; Modify(B[k.x],1);
}
else
{
LL tot = 1LL*k.x*k.y;
int pos = lower_bound(A + 1,A + cur + 1,k.y) - A - 1;
LL now = Get_sum(pos) + 1LL*k.y*Get_num(pos);
if (now >= tot) puts("TAK"); else puts("NIE");
}
}
return 0;
}