Submission #391101

# Submission time Handle Problem Language Result Execution time Memory
391101 2021-04-17T20:48:26 Z nafis_shifat Street Lamps (APIO19_street_lamps) C++14
100 / 100
1172 ms 24872 KB
#include<bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define mp make_pair
#define f first
#define s second
using namespace std;
const int mxn=3e5+5;
const int inf=1e9;
int a[mxn];

int ans[mxn] = {};
int n;
struct segtree {
	int tree[4 * mxn + 1];

	void Set(int node,int b,int e,int p,int v) {
		if(e < p || b > p)return;
		if(b == e) {
			tree[node] = v;
			return;
		}
		int mid=b+e>>1;
		int left=node<<1;
		int right=left|1;

		Set(left,b,mid,p,v);
		Set(right,mid+1,e,p,v);
		tree[node] = tree[left] + tree[right];
	}

	void update(int node,int b,int e,int p,int v) {
		if(e < p || b > p)return;
		if(b == e) {
			tree[node]+=v;
			return;
		}
		int mid=b+e>>1;
		int left=node<<1;
		int right=left|1;

		update(left,b,mid,p,v);
		update(right,mid+1,e,p,v);
		tree[node] = tree[left] + tree[right];
	}

	int query(int node,int b,int e,int l,int r) {
		if(e < l || b > r) return 0;
		if(b >= l && e <= r) {
			return tree[node];
		}
		int mid=b+e>>1;
		int left=node<<1;
		int right=left|1;
		return query(left,b,mid,l,r) + query(right,mid+1,e,l,r);
	}
};


struct BIT
{
	int bit[mxn] = {};
	void update(int p,int v) {
		if(p==0) return;
		for(;p< mxn; p+= p&-p) bit[p] += v;
	}
    int query(int p) {
    	int r=0;
    	for(;p>0;p-=p&-p) r += bit[p];
        return r;
    }
    int query(int a,int b) {
    	return query(b) - query(a - 1);
    }
} bt1,bt2;

struct QUERY {
	int l,r,ind,i;
	int val;
	QUERY(int a = 0,int b = 0,int c = 0,int d = -1 ,int v = 0) : l(a),r(b),ind(c),i(d),val(v) {}	
};
vector<QUERY> Qs;




void process(vector<int> left,vector<int> right) {
	vector<pair<pii,int>> v;

	for(int i : left) {
		v.push_back(mp(mp(Qs[i].l,-1),i));
		v.push_back(mp(mp(Qs[i].i,2),i));
	}

	for(int i : right) {
		v.push_back(mp(mp(Qs[i].l,0),i));
		//v.emplace_back(right[i].r,1,i);
	}

	sort(v.begin(),v.end());

	int cursum = 0;

	for(auto i : v) {
		if(i.f.s == -1) {
			cursum += Qs[i.s].val;
			bt1.update(Qs[i.s].r,Qs[i.s].val);
			bt2.update(Qs[i.s].i,Qs[i.s].val);
		}
		if(i.f.s == 2) {
			cursum -= Qs[i.s].val;
			bt1.update(Qs[i.s].r,-Qs[i.s].val);
			bt2.update(Qs[i.s].i,-Qs[i.s].val);
		}

		if(i.f.s == 0) {
			int x = bt1.query(Qs[i.s].r - 1);
			int y = bt2.query(mxn - 1) - bt2.query(Qs[i.s].r);
			ans[Qs[i.s].ind] += cursum - x - y;
		}
	}



}



void solve(int l,int r) {

	if(l == r) return;
	int mid = l + r >> 1;

	vector<int> left,right;

	for(int i = l; i <= mid; i++) {
		if(Qs[i].i != -1) {
			left.push_back(i);
		}
	}

	for(int i = mid + 1; i <= r; i++) {
		if(Qs[i].i == -1) {
			right.push_back(i);
		}
	}

	process(left,right);

	solve(l,mid);
	solve(mid + 1,r);



}



int main() {
	int k;
	cin >> n >> k;

	string s;
	cin >> s;

	for(int i = 1; i <= n; i++) {
		a[i] = s[i - 1] - '0';
	}


	BIT cur;
	for(int i = 1; i <= n; i++) {
		if(a[i] == 1) cur.update(i,1);
	}

	char p[7];

	for(int i = 1; i <= k; i++) {
		scanf("%s",p);
		if(p[0] == 't') {
			int x;
			scanf("%d",&x);
			QUERY q;
			if(a[x] == 0) 
				cur.update(x,1);
			int lo = 1;
			int hi = x;

			while(lo <= hi) {
				int mid = lo + hi >> 1;
				if(cur.query(mid,x) == x - mid + 1) {
					q.l = mid;
					hi = mid - 1;
				} else {
					lo = mid + 1;
				}
			}




			lo = x;
			hi = n;

			while(lo <= hi) {
				int mid = lo + hi >> 1;
				if(cur.query(x,mid) == mid - x + 1) {
					q.r = mid;
					lo = mid + 1;
				} else {
					hi = mid - 1;
				}

			}
			q.ind = i;
			q.i = x;

			if(a[x] == 0) q.val = -i;
			else q.val = i;

			if(a[x] == 1) {
				cur.update(x,-1);
			}
			a[x] ^= 1;
			Qs.push_back(q);			
		} else {
			int a,b;
			scanf("%d%d",&a,&b);
			b--;
			Qs.push_back(QUERY(a,b,i));
			if(cur.query(a,b) == b - a + 1) {
				ans[i] = i;
			}
		}
	}

	solve(0,Qs.size() - 1);

	for(int i = 0; i < Qs.size(); i++) {
		if(Qs[i].i == -1) printf("%d\n",ans[Qs[i].ind]);
	}



	
}

Compilation message

street_lamps.cpp: In member function 'void segtree::Set(int, int, int, int, int)':
street_lamps.cpp:23:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   23 |   int mid=b+e>>1;
      |           ~^~
street_lamps.cpp: In member function 'void segtree::update(int, int, int, int, int)':
street_lamps.cpp:38:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   38 |   int mid=b+e>>1;
      |           ~^~
street_lamps.cpp: In member function 'int segtree::query(int, int, int, int, int)':
street_lamps.cpp:52:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   52 |   int mid=b+e>>1;
      |           ~^~
street_lamps.cpp: In function 'void solve(int, int)':
street_lamps.cpp:132:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  132 |  int mid = l + r >> 1;
      |            ~~^~~
street_lamps.cpp: In function 'int main()':
street_lamps.cpp:190:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  190 |     int mid = lo + hi >> 1;
      |               ~~~^~~~
street_lamps.cpp:206:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  206 |     int mid = lo + hi >> 1;
      |               ~~~^~~~
street_lamps.cpp:239:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<QUERY>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  239 |  for(int i = 0; i < Qs.size(); i++) {
      |                 ~~^~~~~~~~~~~
street_lamps.cpp:179:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  179 |   scanf("%s",p);
      |   ~~~~~^~~~~~~~
street_lamps.cpp:182:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  182 |    scanf("%d",&x);
      |    ~~~~~^~~~~~~~~
street_lamps.cpp:228:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  228 |    scanf("%d%d",&a,&b);
      |    ~~~~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1484 KB Output is correct
2 Correct 1 ms 1484 KB Output is correct
3 Correct 1 ms 1484 KB Output is correct
4 Correct 1 ms 1484 KB Output is correct
5 Correct 1 ms 1488 KB Output is correct
6 Correct 2 ms 1484 KB Output is correct
7 Correct 1 ms 1484 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 888 ms 16468 KB Output is correct
2 Correct 878 ms 16432 KB Output is correct
3 Correct 943 ms 16528 KB Output is correct
4 Correct 946 ms 19960 KB Output is correct
5 Correct 878 ms 20092 KB Output is correct
6 Correct 1010 ms 24872 KB Output is correct
7 Correct 582 ms 16592 KB Output is correct
8 Correct 565 ms 18044 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 1612 KB Output is correct
2 Correct 3 ms 1612 KB Output is correct
3 Correct 3 ms 1612 KB Output is correct
4 Correct 2 ms 1484 KB Output is correct
5 Correct 1143 ms 22624 KB Output is correct
6 Correct 1061 ms 24744 KB Output is correct
7 Correct 891 ms 24708 KB Output is correct
8 Correct 608 ms 24036 KB Output is correct
9 Correct 290 ms 14776 KB Output is correct
10 Correct 328 ms 17560 KB Output is correct
11 Correct 328 ms 17720 KB Output is correct
12 Correct 530 ms 22512 KB Output is correct
13 Correct 557 ms 23952 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1612 KB Output is correct
2 Correct 3 ms 1612 KB Output is correct
3 Correct 3 ms 1612 KB Output is correct
4 Correct 3 ms 1612 KB Output is correct
5 Correct 595 ms 18988 KB Output is correct
6 Correct 817 ms 23992 KB Output is correct
7 Correct 1031 ms 24848 KB Output is correct
8 Correct 1172 ms 22056 KB Output is correct
9 Correct 991 ms 20264 KB Output is correct
10 Correct 1095 ms 18584 KB Output is correct
11 Correct 1008 ms 20296 KB Output is correct
12 Correct 1089 ms 18584 KB Output is correct
13 Correct 1006 ms 20560 KB Output is correct
14 Correct 1092 ms 18532 KB Output is correct
15 Correct 540 ms 16596 KB Output is correct
16 Correct 544 ms 17996 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1484 KB Output is correct
2 Correct 1 ms 1484 KB Output is correct
3 Correct 1 ms 1484 KB Output is correct
4 Correct 1 ms 1484 KB Output is correct
5 Correct 1 ms 1488 KB Output is correct
6 Correct 2 ms 1484 KB Output is correct
7 Correct 1 ms 1484 KB Output is correct
8 Correct 888 ms 16468 KB Output is correct
9 Correct 878 ms 16432 KB Output is correct
10 Correct 943 ms 16528 KB Output is correct
11 Correct 946 ms 19960 KB Output is correct
12 Correct 878 ms 20092 KB Output is correct
13 Correct 1010 ms 24872 KB Output is correct
14 Correct 582 ms 16592 KB Output is correct
15 Correct 565 ms 18044 KB Output is correct
16 Correct 3 ms 1612 KB Output is correct
17 Correct 3 ms 1612 KB Output is correct
18 Correct 3 ms 1612 KB Output is correct
19 Correct 2 ms 1484 KB Output is correct
20 Correct 1143 ms 22624 KB Output is correct
21 Correct 1061 ms 24744 KB Output is correct
22 Correct 891 ms 24708 KB Output is correct
23 Correct 608 ms 24036 KB Output is correct
24 Correct 290 ms 14776 KB Output is correct
25 Correct 328 ms 17560 KB Output is correct
26 Correct 328 ms 17720 KB Output is correct
27 Correct 530 ms 22512 KB Output is correct
28 Correct 557 ms 23952 KB Output is correct
29 Correct 2 ms 1612 KB Output is correct
30 Correct 3 ms 1612 KB Output is correct
31 Correct 3 ms 1612 KB Output is correct
32 Correct 3 ms 1612 KB Output is correct
33 Correct 595 ms 18988 KB Output is correct
34 Correct 817 ms 23992 KB Output is correct
35 Correct 1031 ms 24848 KB Output is correct
36 Correct 1172 ms 22056 KB Output is correct
37 Correct 991 ms 20264 KB Output is correct
38 Correct 1095 ms 18584 KB Output is correct
39 Correct 1008 ms 20296 KB Output is correct
40 Correct 1089 ms 18584 KB Output is correct
41 Correct 1006 ms 20560 KB Output is correct
42 Correct 1092 ms 18532 KB Output is correct
43 Correct 540 ms 16596 KB Output is correct
44 Correct 544 ms 17996 KB Output is correct
45 Correct 561 ms 13732 KB Output is correct
46 Correct 568 ms 13988 KB Output is correct
47 Correct 580 ms 15900 KB Output is correct
48 Correct 959 ms 24660 KB Output is correct
49 Correct 370 ms 18688 KB Output is correct
50 Correct 367 ms 18712 KB Output is correct
51 Correct 375 ms 18932 KB Output is correct
52 Correct 366 ms 19144 KB Output is correct
53 Correct 363 ms 19220 KB Output is correct
54 Correct 366 ms 19148 KB Output is correct