Bzoj_4378_[poi2015]logistyka_ Tree-like array
Description
Maintaining a sequence of length n, beginning with 0, supports the following two operations:
1.U k A modifies the number of k in the sequence to a.
2.Z c s On this sequence, each time a C positive number is selected, and they are subtracted 1, asking whether the s operation can be done.
Each time you ask for independence, the sequence is not modified for each query.
Input
The first line contains two positive integer n,m (1<=n,m<=1000000) that represent the sequence length and the number of operations.
Next m behaves m operations, where 1<=k,c<=n,0<=a<=10^9,1<=s<=10^9.
Output
Contains several lines, for each z query, if feasible, output tak, otherwise output nie.
Sample Input3 8
U 1 5
U 2 7
Z 2 6
U 3 1
Z 2 6
U 2 2
Z 2 6
Z 2 1Sample OutputNIE
TAK
NIE
TAK It is important to note that the query is over the entire sequence rather than a given interval. Assuming that the number of greater than S is K, then the remaining weights and must be greater than or equal to (c-k) *s. We then discretized the weights and maintained them with two tree arrays. Code:
#include <stdio.h> #include <string.h> #include <algorithm>using namespace std; #define N 1000050# Define RR registertypedef Long long ll;int n,m,t[n],maxn=1000000000,h[n],p[n];ll c[n][2];char opt[10];inline Int Rd () { RR int x=0,f=1; RR Char S=getchar (); while (s< ' 0 ' | | S> ' 9 ') {if (s== '-') F=-1;s=getchar ();} while (s>= ' 0 ' &&s<= ' 9 ') {x= (x<<3) + (x<<1) +s-' 0 '; S=getchar ();} return x*f;} struct A {int num,v,id,opt,pos;} a[n];inline bool Cmp1 (const a &x,const a &y) {return x.num<y.num;} inline bool Cmp2 (const a &x,const a &y) {return x.id<y.id;} void fix (int x,int v,int FLG) {for (;x<=m;x+=x& (x)) C[x][flg]+=v;} ll inq (int x,int flg) {ll re=0; For (;x;x-=x& (x)) RE+=C[X][FLG]; return re;} int main () {n=rd (); M=rd (); int i,j; for (i=1;i<=m;i++) {scanf ("%s", opt); if (opt[0]== ' U ') {a[i].opt=1; a[i].id=i; A[i].pos=rd (); A[i].num=rd (); }else {a[i].opt=2; A[i].id=i; A[i].pos=rd (); A[i].num=rd (); }} sort (A+1,A+M+1,CMP1); a[0].num=134234; for (j=0,i=1;i<=m;i++) {if (A[i].num!=a[i-1].num) j + +; A[i].v=j; H[j]=a[i].num; } sort (A+1,A+M+1,CMP2); for (i=1;i<=m;i++) {if (a[i].opt==1) {int t=a[i].pos; if (P[t]) {fix (p[t],-1,1); Fix (p[t],-h[p[t]],2); } p[t]=a[i].v; Fix (p[t],1,1); Fix (p[t],h[p[t]],2); }else {int k=inq (m,1)-inq (a[i].v-1,1); if (K>=a[i].pos) {puts ("TAK"); continue; } ll Sum=inq (a[i].v-1,2); Puts (sum>=1ll*a[i].num* (a[i].pos-k)? " TAK ":" NIE "); } }}
Bzoj_4378_[poi2015]logistyka_ Tree-like array