#include #include #include #include #include #include #include #include #include #include #include #include #include #pragma warning(disable:4996) typedef long long ll; #define MIN(a, b) ((a)>(b)? (b): (a)) #define MAX(a, b) ((a)<(b)? (b): (a)) #define LINF 9223300000000000000 #define INF 2140000000 const long long MOD = 1000000007; using namespace std; const long long INF2 = (((ll)1<<31)-1); template class SegTree // 0-indexed { private: int n; // 葉の数 vector data; // ノードの値を持つ配列 T def; // 初期値かつ単位元 T operation(T a, T b) // 区間クエリで使う処理 { //return min(a, b); // 区間minクエリ return make_pair(a.first+b.first, a.second+b.second); } T update(T a, ll b) // 点更新で使う処理 { ll tmp=a.second+b; return make_pair(tmp==0?0:1, tmp); // 加算の場合 //return b; // 更新の場合 } // 区間[a,b)の総和。ノードk=[l,r)に着目している。 T _query(int a, int b, int k, int l, int r) { if (r <= a || b <= l) return def; // 交差しない if (a <= l && r <= b) return data[k]; // a,l,r,bの順で完全に含まれる else { T c1 = _query(a, b, 2 * k + 1, l, (l + r) / 2); // 左の子 T c2 = _query(a, b, 2 * k + 2, (l + r) / 2, r); // 右の子 return operation(c1, c2); } } public: SegTree(int _n) { // _n:必要サイズ def = make_pair(0,0); // 初期値かつ単位元 n = 1; while (n < _n) n *= 2; data = vector(2 * n - 1, def); } // 場所i(0-indexed)の値をxで更新 void change(int i, ll x) { i += n - 1; data[i] = update(data[i],x); while (i > 0) { i = (i - 1) / 2; data[i] = operation(data[i * 2 + 1], data[i * 2 + 2]); } } // [a, b)の区間クエリを実行 T query(int a, int b) { return _query(a, b, 0, 0, n); } // 添字でアクセス T operator[](int i) { return data[i + n - 1]; } }; int main(int argc, char* argv[]) { int n,q; scanf("%d%d", &n, &q); SegTree > S(n); vector a(n); int i; for(i=0; i